Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save randcode-generator/ae7aca911cbac236d2c7a3fa51125b2c to your computer and use it in GitHub Desktop.
Save randcode-generator/ae7aca911cbac236d2c7a3fa51125b2c to your computer and use it in GitHub Desktop.
import numpy as np
inputLayerSize, hiddenLayerSize, outputLayerSize = 3, 3, 1
L = 0.25
X = np.array([[0, 0, 1], [0, 1, 1], [1, 0, 1], [0, 1, 0], [1, 0, 0], [0, 0, 0], [1, 1, 0]])
Y = np.array([[0], [0], [0], [0], [0], [0], [1]])
iterations = 50000
#sigmoid function
def f(x): return 1/(1 + np.exp(-x))
#derivative sigmoid
def f_(x): return x * (1 - x)
np.random.seed(1)
H1 = np.random.uniform(size=(inputLayerSize, hiddenLayerSize))
H2 = np.random.uniform(size=(hiddenLayerSize, outputLayerSize))
for i in range(iterations):
R0 = np.dot(X, H1)
R = f(R0)
Oc = np.dot(R, H2)
E = Y - Oc
dOc = E * L
dR = dOc.dot(H2.T) * f_(R)
H2 += R.T.dot(dOc)
H1 += X.T.dot(dR)
print "Input:\n[1, 1, 1]"
X = np.array([1, 1, 1])
print "\nOutput:"
print f(np.dot(X, H1)).dot(H2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment