import numpy as np iterations = 750 # input patterns. Only 2 inputs 'on' yeilds a True result, everything else would be False X = np.array([ [0,0,1],[0,1,1],[1,0,1],[1,1,1],[0,0,0],[1,1,0]]) # answers to training examples '1' means True, '0' means false y = np.array([[0,1,1,0,0,1]]).T #network structure # n = number of examples # i = shape of example, in this case, 3 n,i=X.shape syn0 = (2*np.random.random((i,n)) - 1) syn1 = (2*np.random.random((n,1)) - 1) for j in xrange(iterations): # perform the forward pass through the network l1 = 1/(1+np.exp(-(np.dot(X,syn0)))) l2 = 1/(1+np.exp(-(np.dot(l1,syn1)))) # take the answer given by the net and calculate how wrong its guess is l2_delta = (y - l2)*(l2*(1-l2)) l1_delta = l2_delta.dot(syn1.T) * (l1 * (1-l1)) # penalize the nuerons that got it 'wrong' and reward the nueron that got it # right. in this example we have 3 nuerons that are trying to learn the problem # once the network is well trained, the nuerons will fire 'more correctly' and # have less error syn1 += l1.T.dot(l2_delta) syn0 += X.T.dot(l1_delta) #uncomment next line to show net examples it has not seen. it will likely get them wrong # the correct answers would be '0',. You can also make up your own sequence for testing. # Bonus: modify the traing set above with this two missing examples (be sure to # correct answers to Y as well ) and the run again to 'learn' from the two missing values as well #X=np.array([[1,0,0],[0,1,0]]) # Query the network by showing it the examples. it will output its answer. the raw results # show how close it is to the actual value. for example, .95 is confidently close to 1 # and .05 is confidently close to 0. This residual can be thought of as the error. the # higher the iteration, the lower the error l1 = 1/(1+np.exp(-(np.dot(X,syn0)))) l2 = 1/(1+np.exp(-(np.dot(l1,syn1)))) print 'raw results:' print l2.T print 'Neural net prediction:' print np.round(l2.T)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question