For those of you following at home, to get this working you need to add an import and the tabs to make it valid python. Technically a neural network in 12 lines of python, but still quite impressive.
import math, random
X = [ [0,0,1],[0,1,1],[1,0,1],[1,1,1] ]
y = zip(*[[0,1,1,0]]) # transpose
syn0 = [[2*random.random()-1 for x in range(4)] for _ in range(3)] # 3x4
syn1 = [[2*random.random()-1 for x in range(1)] for _ in range(4)] # 4x1
for j in xrange(60000):
l1 = [[1/(1+math.exp(-sum(a*b for a,b in zip(r, c)))) for c in zip(*syn0)] for r in X]
l2 = [[1/(1+math.exp(-sum(a*b for a,b in zip(r, c)))) for c in zip(*syn1)] for r in l1]
l2_delta = [[(ry[0]-a)*(a*(1-a)) for a in r] for r, ry in zip(l2, y)]
l1_delta = [[n*a*(1-a) for a,n in zip(r,m)] for r, m in zip(l1, [[sum(a2*b2 for a2,b2 in zip(r2,c2)) for c2 in syn1] for r2 in l2_delta])]
syn1 = [[n+sum(a*b for a,b in zip(r,c)) for c, n in zip(zip(*l2_delta), m)] for r, m in zip(zip(*l1), syn1)]
syn0 = [[n+sum(a*b for a,b in zip(r,c)) for c, n in zip(zip(*l1_delta), m)] for r, m in zip(zip(*X), syn0)]
7 comments
2 u/Stackholder 16 Jul 2015 00:09
Very short article, didn't understand a thing. (I haven't started to learn about neural networks yet, but still..)
1 u/cronenburger 17 Jul 2015 09:22
This is a much better explanation.
1 u/TisTheJMan 16 Jul 2015 10:08
For those of you following at home, to get this working you need to add an import and the tabs to make it valid python. Technically a neural network in 12 lines of python, but still quite impressive.
0 u/cronenburger 17 Jul 2015 09:01
Here it is in pure python