Materials for Neural Networks Workshop
- January 21 2017
- NYC Women in Machine Learning & Data Science Meetup
The instructions below will help you set up a virtual environment, get all of the scripts and data, and install all of the libraries required for this tutorial. There are two sets of instructions. If you have the Anaconda distribution of Python then follow the instructions "1. Using Anaconda". Otherwise follow the instructions "2. Using virtualenv."
What is a virtual environment and why use one?
A virtual environment is a named, isolated, working copy of Python that maintains its own files, directories, and paths so that you can work with specific versions of libraries or Python itself without affecting other Python projects. Virtual environments make it easy to cleanly separate different projects and avoid problems with different dependencies and version requirements across components. Source
There are two ways to create virtual environments in Python.
- Using the Anaconda distribution of Python.
- Using virtualenv.
If you have the anaconda distribution of Python (see here for more info), then follow the instructions below. Note, there is an issue with matplotlib and python 3.6, so please specify the python version to be <=3.5 to make the matplotlib install work.
Linux/Mac
# In the terminal create a directory that you want to contain this repo and navigate to it
mkdir <directory_name>
# eg. mkdir NN_tutorial
cd <directory_name>
# Then, create a virtual environment
conda create -n <your_environment_name> python=<yourPythonVersion>
# for example conda create -n NN_tutorial python=3.5
# When conda asks you proceed ([y]/n)? type 'y'
# Switch into your new environment
source activate <your_environment_name>
# Clone the repo, install the relevant libraries using setup.py and test the install worked
git clone https://github.com/lgraesser/Neural-Networks-Workshop-Materials-WiMLDS.git
cd Neural-Networks-Workshop-Materials-WiMLDS
python setup.py install
python test/test_install.py
# Now you are finished.
# When you need to exit the environment (at the end of the tutorial for example), type.
source deactivate
If you get an error message when running python test/test_install.py
that ends with "Import Error: No module named tensorflow" this means that your version of Keras is using Tensorflow instead of Theano as the backend. To change the backend follow the instructions in the "Troubleshooting - Import Error: no module named tensorflow" section of this document.
Windows
# In the terminal create a directory that you want to contain this repo and navigate to it
mkdir <directory_name>
# eg. mkdir NN_tutorial
cd <directory_name>
# Clone the repo and navigate to its main directory
git clone https://github.com/lgraesser/Neural-Networks-Workshop-Materials-WiMLDS.git
cd Neural-Networks-Workshop-Materials-WiMLDS
# Then, create a virtual environment
conda env create -f environment.yml
# Note: the environment will be named NN_tutorial and will be set up with python 3.5
# Switch into your new environment
activate NN_tutorial
# Test the install worked
python test/test_install.py
# Now you are finished.
# When you need to exit the environment (at the end of the tutorial for example), type.
deactivate
If you get an error message when running python test/test_install.py
that ends with "Import Error: No module named tensorflow" this means that your version of Keras is using Tensorflow instead of Theano as the backend. To change the backend follow the instructions in the "Troubleshooting - Import Error: no module named tensorflow" section of this document.
To use virtualenv (see here for more info) follow the instructions below.
# In the terminal, install virtualenv
pip install virtualenv
# Create a directory that you want to contain the repo and navigate to it
mkdir <directory_name>
# eg. mkdir NN_tutorial
cd <directory_name>
# Then, create a virtual environment
virtualenv <your_environment_name>
# Switch into your new environment
source <your_environment_name>/bin/activate
# Clone the repo, install the relevant libraries using setup.py and test the install worked
git clone https://github.com/lgraesser/Neural-Networks-Workshop-Materials-WiMLDS.git
cd Neural-Networks-Workshop-Materials-WiMLDS
python setup.py install
python test/test_install.py
# Now you are finished.
# When you need to exit the environment (at the end of the tutorial for example), type.
deactivate
If you get an error message when running python test/test_install.py
that ends with "Import Error: No module named tensorflow" this means that your version of Keras is using Tensorflow instead of Theano as the backend. To change the backend follow the instructions in the "Troubleshooting - Import Error: no module named tensorflow" section of this document.
Keras is installed with Theano as the backend. This is all that is required for the tutorial. However, if you would like to install Tensorflow then choose the correct binary to install from Tensorflow.
# for example, TF for Python3, MacOS, CPU only
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0-py3-none-any.whl
# or, TF for Python2, MacOS, CPU only
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0rc1-py2-none-any.whl
# or Linux CPU-only, Python3.5
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp35-cp35m-linux_x86_64.whl
# Then install Tensorflow
# Python 2
$ sudo pip install --upgrade $TF_BINARY_URL
# Python 3
$ sudo pip3 install --upgrade $TF_BINARY_UR
Then you may need to change Keras' backend to Tensorflow. Check the current backend by typing into the terminal.
python -c "from keras import backend"
Either "Using Theano backend" or "Using tensorflow backend" will be printed to the screen. If the backend is "theano", then follow the instructions below in "Troubleshooting - Import Error: No module named tensorflow" to change it to tensorflow or see Keras' documentation.
If you get an error message when running python test/test_install.py
that ends with "Import Error: No module named tensorflow" this means that your version of Keras is using Tensorflow instead of Theano as the backend.
To change the backend to Theano you need to change the settings in the Keras config file.
First check if you have one by navigating to the directory listed below then typing ls
to list the files contained in that directory. The commands are below.
cd ~/.keras/
ls
If there is a file named keras.json then open it by typing open keras.json
The default configuration looks like this.
{
"image_dim_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
Change the value of "backend" to "theano" and "image_dim_ordering" to "th". Save and close the file.
Alternatively, if there is no keras.json file then create one and open it by typing
touch keras.json
open keras.json
Then copy the information below into the file and save it.
{
"image_dim_ordering": "th",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "theano"
}
Navigate back to the github directory (type cd ~
in the terminal to navigate to your home directory, cd ..
to navigate to the directory that contains the directory you are currently in, and ls
at any point to list the files in the directory you are in). Then check that the installation tests works by running python test/test_install.py
The first line printed to the screen when you run it should be "Using Theano backend"
If Keras is working in Python but not IPython, this is because the sys.paths of the two are different. Lucy Park has the answer. Follow her tutorial (only a few steps) and this should fix it
Try installing with no virtual environments by following the instructions below
# In the terminal create a directory that you want to contain the repo and navigate to it
mkdir <directory_name>
# eg. mkdir NN_tutorial
cd <directory_name>
# Clone the repo, install the relevant libraries using setup.py and test the install worked
git clone https://github.com/lgraesser/Neural-Networks-Workshop-Materials-WiMLDS.git
cd Neural-Networks-Workshop-Materials-WiMLDS
python setup.py install
python test/test_install.py
There are three scripts which acommpany this tutorial. They can be run through either the jupyter notebook or the command line
- Part1
- KerasIntro_Example1
- KerasIntro_OtherExamples
- Part2
- IntrotoNeuralNets2_CIFAR_2classes
# In the terminal, to make sure you have the most up to date version of the repo , type
git pull origin master
#To load a jupyter notebook, type
jupyter notebook
# If you get an error, you may need to install ipython first. Try
pip install ipython
# Then
jupyter notebook
# Otherwise to run a .py script from command line
# navigate to the directory it is in and type python <filename>, for example
cd Part1
python KerasIntro_Example1.py
- Read Michael Nielsen's excellent book Neural Networks and Deep Learning
- Watch over 20 hours of videos of deep learning tutorials thanks to the fantastic Bay Area Deep Learning School. Day 2 is here and the schedule is here
- Implement your own neural network. See my implementation in Python for an example.
- Check out my blog for links to more resources on neural networks. I update it on a regular basis.
Thank you to Nidhin Pattaniyil for his detailed feedback on the setup and installation instructions.