From https://iamtrask.github.io/2015/07/12/basic-python-network/
import numpy as np
sigmoid function
def nonlin(x,deriv=False):
if(deriv==True):
return x*(1-x)
return 1/(1+np.exp(-x))
input dataset
X = np.array([ [0,0,1],
[0,1,1],
[1,0,1],
[1,1,1] ])
output dataset
y = np.array([[0,0,1,1]]).T
seed random numbers to make calculation deterministic (just a good practice)
np.random.seed(1)
initialize weights randomly with mean 0
syn0 = 2*np.random.random((3,1)) - 1
print(syn0)
## [[-0.16595599]
## [ 0.44064899]
## [-0.99977125]]
- variables
- l0 is input layer
- l1 is hidden layer
- l1_error is the loss function
- l1_delta is the gradient descent function for calculating the back-propagation
- syn0 are synapses, weights between l0 and l1, and also how the weights are updated are shown.
for iter in range(100000):
# forward propagation
l0 = X
l1 = nonlin(np.dot(l0,syn0))
# how much did we miss
l1_error = y - l1
# multiply how much we missed by the
# slope of the sigmoid at the values in l1
l1_delta = l1_error * nonlin(l1,True)
# update weights
syn0 += np.dot(l0.T,l1_delta)
check l1, which is output layer:
print("Output After Training:")
## Output After Training:
print(l1)
## [[0.00301758]
## [0.00246109]
## [0.99799161]
## [0.99753723]]