Last active
August 29, 2015 14:25
-
-
Save n6g7/15391881f3d1eae700b8 to your computer and use it in GitHub Desktop.
Neural network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
# Initialize not-so-randomness | |
numpy.random.seed(1) | |
# Sigmoid function : R -> [0-1] | |
def sigmoid(x, deriv=False): | |
return x*(1-x) if deriv else 1/(1+numpy.exp(-x)) | |
# Inputs | |
X = numpy.array([ | |
[0,0,1], | |
[0,1,1], | |
[1,0,1], | |
[1,1,1] | |
]) | |
# Outputs | |
Y = numpy.array([[0,1,1,0]]).T | |
# Synapses | |
syn0 = 2*numpy.random.random((3,4)) - 1 | |
syn1 = 2*numpy.random.random((4,1)) - 1 | |
for j in xrange(60000): | |
l0 = X | |
l1 = sigmoid(numpy.dot(l0, syn0)) | |
l2 = sigmoid(numpy.dot(l1, syn1)) | |
# l2 correction | |
l2_error = Y - l2 | |
l2_delta = l2_error * sigmoid(l2, True) | |
syn1 += l1.T.dot(l2_delta) | |
# l1 correction | |
l1_error = l2_delta.dot(syn1.T) | |
l1_delta = l1_error * sigmoid(l1, True) | |
syn0 += l0.T.dot(l1_delta) | |
if j % 10000 == 0: | |
print 'Error : %s' % numpy.mean(numpy.abs(l2_error)) | |
print l2, syn0, syn1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from http://iamtrask.github.io/2015/07/12/basic-python-network/?