Skip to content

Instantly share code, notes, and snippets.

@peko
Created September 4, 2016 17:10
Show Gist options
  • Select an option

  • Save peko/6d898bbbb2b3f320d15d234a8f014fe7 to your computer and use it in GitHub Desktop.

Select an option

Save peko/6d898bbbb2b3f320d15d234a8f014fe7 to your computer and use it in GitHub Desktop.
Minimal neural network
import numpy as np
# sigmoid
def nonlin(x, deriv=False):
if deriv is True:
return x*(1-x)
return 1/(1+np.exp(-x))
# train data
X = np.array([
[0,0,1],
[0,1,1],
[1,0,1],
[1,1,1]
])
# train result
y = np.array([
[0,
0,
1,
1]]).T
np.random.seed(1)
# hidden layer filled with random
h = 2*np.random.random((3,1))-1
# training
for i in range(10000):
# forward propаgation
l0 = X
l1 = nonlin(np.dot(l0, h))
# error calc
l1_err = y - l1
l1_dlt = l1_err * nonlin(l1, True)
# correcting hidden layer
h += np.dot(l0.T, l1_dlt)
print("result of training\n", l1)
print("must be\n", y)
test = np.array([[0,0,0],[1,1,0]])
print("test\n", nonlin(np.dot(test,h)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment