In [13]:
import numpy as np
#sigmoid
def nonlin(x, deriv=False):
if (deriv==True):
return x*(1-x)
return 1/(1+np.exp(-x))
#input data
x=np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]])
#output data
y=np.array([0,1,1,0]).T
#seed random numbers to make calculation
np.random.seed(1)
#initialize weights with mean=0
syn0=2*np.random.random((3,4))-1
for iter in range(10000):
#forward propogation
l0=x
l1=nonlin(np.dot(l0,syn0))
l1_error=y-l1
#multiply error by slope of sigmoid at values of l1
l1_delta=l1_error*nonlin(l1,True)
#update weights
syn0+=np.dot(l0.T, l1_delta)
print("Output after training: ",l1 )
In [ ]: