Machine Learning Cookbook

Checkout Machine Learning In Hindsight

NOTE: the hindsight series are based on old HowTos and are being ported to the more recent versions. Some of this info may be presently out of date until this porting effort is finished.

1. Install Python and Virtualenv
see: https://www.tensorflow.org/install/
Use virtualenv to created walled off python envrionment
MACOSX

    $ brew install python python3
    # next use the python package manager 'pip' to install virtualenv
    $ sudo easy_install pip
    $ sudo easy_install pip3
    $ pip3 install --upgrade pip
    $ pip3 install --user virtualenv
    $ pip3 install --user virtualenvwrapper
    $ pip3 install numpy scipy pillow
    $ pip3 install matplotlib
    $ pip3 install future
    $ mkdir -p ~/virtualenvs

Ubuntu

    $ sudo apt-get install python-pip python3-pip
    $ sudo apt-get install build-essential libssl-dev libffi-dev python3-dev
    $ sudo apt-get install libfreetype6-dev libxft-dev
    $ pip3 install --upgrade pip
    # next use the python package manager 'pip' to install virtualenv
    $ pip3 install --user virtualenv
    $ pip3 install --user virtualenvwrapper
    # add virtualenv to your path
    $ pip3 install numpy
    $ pip3 install pillow
    $ pip3 install future
    $ sudo pip3 install matplotlib
    $ sudo apt-get install virtualenv
    $ mkdir -p ~/virtualenvs

2. Setup your paths
MAC OSX

    # WARNING: you may have to set PYTHONPATH to include ~/library/Python/2.7/lib/python/site-packages/
    # Place the following in your .profile settings
    VIRTUALENVWRAPPER_PYTHON=$(which python3)
    export VIRTUALENVWRAPPER_PYTHON
    export WORKON_HOME=~/virtualenvs
    if [ -f "/usr/local/bin/virtualenvwrapper.sh" ]; then
        . /usr/local/bin/virtualenvwrapper.sh
    fi
    PATH="$PATH:$HOME/Library/Python/2.7/bin"
    export PATH
    # end .profile changes

Ubuntu

    a. In your $HOME/.profile-extra,
    PATH="$HOME/.local/bin:$PATH"
    export PATH
    VIRTUALENVWRAPPER_PYTHON=$(which python3)
    export VIRTUALENVWRAPPER_PYTHON
    export WORKON_HOME=~/virtualenvs
    if [ -f "$HOME/.local/bin/virtualenvwrapper.sh" ]; then
        . "$HOME/.local/bin/virtualenvwrapper.sh"
    elif [ -f "/usr/local/bin/virtualenvwrapper.sh" ]; then
        . /usr/local/bin/virtualenvwrapper.sh
    fi
    # end .profile changes
    b. In your $HOME/.bashrc-extra,
    . "$HOME/.local/bin/virtualenvwrapper.sh"

    # end .profile changes

3. Create working directory

MACOSX

    $ mkdir -p ~/work/Foo/project1
    $ cd ~/work/Foo
    $ virtualenv -p /usr/local/bin/python3 project1

Ubuntu

    $ mkdir -p ~/work/Foo/project1
    $ cd ~/work/Foo
    $ virtualenv -p /usr/bin/python3.5 project1

4. Install TensorFlow:
NOTE: Use python3 instead of python 2

MACOSX & Ubuntu

WARNING: GPUs are no longer supported on Mac OS X.
 
    $ mkdir -p ~/tensorflow3/src
    # remove the  "-p python3" from the command line if you want a python2 package installation
    $ virtualenv --system-site-packages -p python3 ~/tensorflow3
    $ . ~/tensorflow3/bin/activate
    (tensorflow3) $ cd ~/tensorflow3
    (tensorflow3) $ easy_install -U pip
    # for Ubuntu, upgrade tensorflow-gpu or tensorflow. For Mac, the only option is tensorflow (the CPU version).
    (tensorflow3) $ pip3 install --upgrade tensorflow-gpu
    # type
    # $ deactivate
    # at the shell prompt to leave the virtual environment

5. Install Jupyter Notebook
MACOSX & Ubuntu

    $ . ~/tensorflow3/bin/activate
    (tensorflow3) $ pip3 install jupyter
    # we need to configure jupyter to be remotely accessible
    (tensorflow3) $ cd ~/tensorflow3/src

See Jupyter.org for more info on what jupyter notebook is and how to use it.

6. Optional: Make the jupyter notebook accessible remotely
By default, the jupyter notebook is accessible only via localhost. Do the
following to make it available remotely.

MACOSX & Ubuntu

    $ . ~/tensorflow3/bin/activate
    (tensorflow3) $ jupyter notebook --generate-config
    # this creates a file ~/.jupyter/jupyter_notebook_config.py 
    #
    # set the password using:
    (tensorflow3) $ jupyter notebook password
    # generate a certificate for using https
    (tensorflow3) $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout jupyter.key -out jupyter.pem
    (tensorflow3) $ mv jupyter.key jupyter.pem  ~/.jupyter/
    # Edit the ~/.jupyter/jupyter_notebook_config.json file to set the additional
    # settings:
Begin File contents of: ~/.jupyter/jupyter_notebook_config.json
    {
  "NotebookApp": {
    "certfile" : "/home/{my-user-name}/.jupyter/jupyter.pem",
    "keyfile" : "/home/{my-user-name}/.jupyter/jupyter.key",
    "port": 9111,
    "open_browser": false,
    "ip": "my-hostname-or-ip-address",
    "password": "generated-sha-do-not-change"
  }
}
End File contents of: ~/.jupyter/jupyter_notebook_config.json
WARNING: you need to make certain that your port you selected, e.g. 9111, as well as the ports 49152-65535 are open in your firewall. Opening these up is dependent upon your environment.

7. Run the Jupyter Notebook
MACOSX & Ubuntu

Notebooks

Here are some basic jupyter notebook worksheets illustrating common algorithms. Save this to your work area, ~/tensorflow3/src, and play around with them.

Starting jupyter

    $ . ~/tensorflow3/bin/activate
    (tensorflow3) $ cd ~/tensorflow3/src
    (tensorflow3) $ jupyter notebook
    # open the link in your browser and you should see the various worksheets that you've saved. Click on one
    # to run.