Site icon EndurantDevs

Python instead of Octave: Week 3, Neural Networks, Coursera

Neural Networks for Machine Learning

Coursera.org is one of the greatest online courses. It allows you to train your brain with not much time spent.
At the end of the previous week, I decided to spend some time on “Neural Network for Machine Learning ,” the course by Geoffrey Hinton, Professor, University of Toronto. This is Engineering course but has a lot of the general topics. It recalls the knowledge from my learning during university years.

On the second Quiz, there is a practice with learning perceptron by given datasets, input vectors, etc. The code is for Octave (like OpenSource Matlab). The course proposes to use 3.8 version of Octave, but that build doesn’t work on my Mac. So, I installed the last version from Brew (4.2) and thought I am OK. But, unfortunately, starting example programs just pushes Octave to hang up.

To train your brains process should be harder, right? So, I got Python to help me with programs, as we are coming by Engineering way. NumPy and SciPy will help us with Dataset format of input data and processing vectors and multiplying matrixes. Additionally, PyLab(a part of MatPlotLib) will be used to plot the graphics.

To do the same not in Octave, but in Python, you can use the shared code from Github: Python version for Week 3, Neural Networks, Coursera. I didn’t share the solution for Quiz(so, I don’t break Coursera rules), and the code looks pretty same as in the task. I saved the styling of the code, so mostly this is Matlab/Octave code-style Python version. 🙂
This is for easier reading/understanding of those who is not familiar with Python and comparing with original code by the author of the course.

Will add few notes about the code.
I was using Python3, but it should work on the second version of Python as well.

If you do not need to draw graphics just comment out(lines: ~4 and ~95) and you will not need to install MatPlotLib either:

from plot_perceptron import plot_perceptron

and

plot_perceptron(neg_examples, pos_examples, mistakes0, mistakes1, num_err_history, w, w_dist_history);

PNG files with state after each iteration are getting saved into “img” directory relatively to your current dir. Filenames are equal to number of iteration respectively. The code does not clean this directory, you can add this to your version or do it manually when it is needed.

To change current dataset you need to change the path to it on line ~175:

mat_contents = sio.loadmat('Datasets/dataset1.mat')

To visualize you can make animations from PNGs as state after each iteration(this used Lecture3, but not Lecture2 required to accomplish the Quiz ).

UPD(03/20/2017): 

Updated the code in GitHub. Now it is Python 2 and Python 3 compatible. (Tested on Python 2.7 and Python 3.6)

Also, Matplotlib is not required by default. Graphs will be plotting only if you pass –with-graphs=1

Made this because of people requests about problems with running Python in framework mode. By default, the code does counting only for now.

Exit mobile version