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.

Python Neural Network Perceptron Visualization

Python Neural Network Perceptron Visualization Python Neural Network Perceptron Visualization

10 Comments

  1. how about course itself ?
    my last achievement ML from Andrew NG
    and I am stuck what course to learn next

    Reply
    • Hello,

      I like it, as it pushes you to read more about this topic from other resources.
      If you are not afraid of Math – try it.

      I made it a little bit harder for myself, decided to rewrite the code into Python.

      Playing with GPU instead of CPU for week5 code right now.. difference is huge 🙂

      Reply
  2. Giantiger03/20/2018 at 3:40 pm

    I am reading your code. (I’m new to both python and NN etc.).

    How did you get the dataset?.mat from dataset?_ancient_octave.mat?

    Thanks

    Reply
  3. Erika Miller02/10/2020 at 7:18 pm

    Is it okay if we feature your site in our next email newsletter? It’s a perfect fit for a piece we’re doing and I think our audience would find some of the content on your site super useful.

    I know you’re probably busy, so just a simple yes or no would suffice.

    Many Thanks,

    Reply
  4. Erika Miller02/25/2020 at 8:03 pm

    I just wanted to followup on the request I submitted through your contact form a couple weeks ago as I haven’t heard anything back. I pasted it below for your reference.

    Is it okay if we feature your site in our next email newsletter? It’s a perfect fit for a piece we’re doing and I think our audience would find some of the content on your site super useful.

    I know you’re probably busy, so just a simple yes or no would suffice.

    Many Thanks,

    Reply

Your email address will not be published.