Hacker News new | past | comments | ask | show | jobs | submit login
A Neural Network in 11 Lines of Python (iamtrask.github.io)
341 points by adamnemecek on July 14, 2015 | hide | past | favorite | 41 comments



At the risk of being pedantic...if you're not familiar with data/math work in Python, the `np` word refers to "numpy", which is an extension to Python that includes array and matrix math...so the OP needs 12 lines, the first being:

       import numpy as np


That isn't pedantic at all. Python tutorials should always include imports.


No, definitely not pedantic. In fact, I have only just now realized that "numpy" means "NumPy," instead of being a nonsense word that rhymes with "lumpy." Sigh.


I will still always call it nump-pee, and never num-pie

Lumpy Numpy FTW


Me too! It drives professors nuts, but its just so much more fun to say.


Revelation. Thanks.


Thank you! Non-obvious for sure.


I knew that np stood for numpy but had to Google to realize that the .T suffix meant transpose.


Famous antigravity in one line in Python: https://xkcd.com/353/


... Isn't grammatically correct. Pedantic, I know.


Just for comparison this is almost the same using a library [1]:

  import numpy as np
  X = np.array([ [0,0,1],[0,1,1],[1,0,1],[1,1,1]])
  y = np.array([[0,0,1,1]]).T

  from keras.models import Sequential
  from keras.layers.core import Dense

  model = Sequential([Dense(3, 1, init='uniform', activation='sigmoid')])
  model.compile(loss='mean_absolute_error', optimizer='sgd')
  model.fit(X, y, nb_epoch=10000)
  model.predict(X)
[1] http://keras.io


But this is not nearly as clear for a beginner, or someone who doesn't know what Sequential, compile, fit and predict are doing.


But this is the tested, maintained solution by experts in the topic. Hopefully.


"Do Not Reinvent The Wheel Unless You Plan On Learning More About Wheels"


Thanks for this. It is really well explained. However, it did feel like it went downhill when you explained how the updating happens. At least it was less of a walkthrough than the previous sections.

Also, lines 22 and 23 in the three layer network are not intuitive for a beginner. Before, syn0 was (3,1) and now its (3,4), and the new syn1 is (4,1). Your previous explanation for this initialization was much better.

But again, awesome work, just being nitpicky. Really appreciate it!


I recently spent some time learning how backpropagation works and wound up putting together this step by step tutorial that you might find helpful: http://mattmazur.com/2015/03/17/a-step-by-step-backpropagati...


This is brilliant. I've been looking for an explanation like this, which truly understands the perspective of a beginner. Thanks!


Great! I will definitely check it out!


Hey avyfain, I'll look out for that in my next post. Thanks for the note.


Also recommended, 7 part video series (link is video 1):

https://www.youtube.com/watch?v=bxe2T-V8XRs


Can you suggest any good video explanation about NN but with some code with Python but using an actual NN library (like theano)?


Honestly I'm a beginner myself. Other then google search that you can do yourself, here's what might be helpful:

https://triangleinequality.wordpress.com/2014/08/12/theano-a...


Great work! I have been reading a practical guide to neural networks [1], and I am amazed that it is so comprehensible. I had assumed ML to be a lot more complicated. Although, I won't classify it as an easy craft but (maybe) it isn't so hard as it looks.

[1]: http://neuralnetworksanddeeplearning.com/chap1.html


Some of you may like seeing the two level neural network code translated to kona (or k3)!

    nonlin:{:[y;x*1-x;1%1+_exp -x]}
    x:(0 0 1;0 1 1;1 0 1;1 1 1)
    y:+,0 0 1 1
    syn0:-0.5+3 1 _draw 0
    step:{L1:nonlin[x _mul syn0;0]
          L1err:y-L1
          L1delta:L1err*nonlin[L1;1]
          syn0+:(+x)_mul L1delta;L1}
    do[10000;L1:step[x;y]]
    L1
  (,0.009668795
   ,0.007862982
   ,0.9935916
   ,0.9921171)
k3 does this in 140ms, Kona in 2030ms on the same machine.


This is a really fantastic article, I've been looking for a code-based "Neural network implementation for idiots" thing like this.


I wonder if it'd work better with a leaky ReLU. Somebody wanna try? It's a _very_ simple mechanism. [1] Basically, instead of a sigmoid, let f(z·w) be the activation due to inputs z and weight w, then a(z) = z·w if z·w > 0 else 0.01 * z·w. Sigmoid for activation have several problems, most notably they suffer from vanishing gradient problems with bigger networks.

[1] https://en.wikipedia.org/wiki/Rectifier_(neural_networks)#Le...


Shouldn't the derivative of sigmod(x) be sigmoid(x)(1-sigmoid(x)) and not x(1-x). The code seems to implement the second.


Can HN recommend any good neural net tutorials? (Preferably in Python)


This is an excellent course no matter which language you prefer:

https://class.coursera.org/neuralnets-2012-001/lecture


Why do you say it is an excellent course? I started it and so far I found the professor to be uninspiring and the lectures lacking in preparation and detail.

I much rather read a book.


I done something similar some months ago. Maybe this helps someone though it should have same content: http://nbviewer.ipython.org/github/bodokaiser/data-science/b...


Interesting fact about the sigmoid function: If you scale it so its slope at the origin is the same as the cumulative error function, it differs from that function by at most 0.017.


I don't get the point of these "x in n lines" posts.

Fewer lines of code doesn't always equate to simple, comprehensible code.


Well, I personally presumed that neural networks would require two or three orders of magnitude more of code. When its revealed that the code is actually much more manageable, individuals (such as myself) take the dive into the subject, knowing that it is something we can at least get through.


The final "Line 36" in your explanation should be "Line 39."


YES! Thank you!


This is amazing stuff. Just want more!


Anyone fancy translating this to C#?


Pretty trivial if you have a math library, I don't see anything here that is special. Why not give it a shot yourself?



very nice, thank you!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: