Skip to content

Instantly share code, notes, and snippets.

@exelban
Created February 22, 2017 19:48
Show Gist options
  • Save exelban/a2a844621cdbbf49cbf23cd353932fce to your computer and use it in GitHub Desktop.
Save exelban/a2a844621cdbbf49cbf23cd353932fce to your computer and use it in GitHub Desktop.
Simple logical function in neural network.
import numpy as np
X_and = np.array([[0,0], [0,1], [1,0], [1,1]])
Y_and = np.array([ [0], [0], [0], [1]])
X_or = np.array([[0, 0], [0,1], [1,0], [1,1]])
Y_or = np.array([ [0], [1], [1], [1]])
X_nor = np.array([[0,0], [0,1], [1,0], [1,1]])
Y_nor = np.array([ [1], [0], [0], [0]])
X_xor = np.array([[0,0], [0,1], [1,0], [1,1]])
Y_xor = np.array([ [0], [1], [1], [0]])
iteration = 10000
inputLayerSize, hiddenLayerSize, outputLayerSize = 2, 3, 1
w_l1 = np.random.uniform(size=(inputLayerSize, hiddenLayerSize))
w_l2 = np.random.uniform(size=(hiddenLayerSize,outputLayerSize))
hiddenLayer_val = np.random.uniform(size=(inputLayerSize, hiddenLayerSize))
outputLayer_val = np.random.uniform(size=(hiddenLayerSize,outputLayerSize))
predict_and = np.random.uniform(size=(hiddenLayerSize,outputLayerSize))
predict_or = np.random.uniform(size=(hiddenLayerSize,outputLayerSize))
predict_nor = np.random.uniform(size=(hiddenLayerSize,outputLayerSize))
predict_xor = np.random.uniform(size=(hiddenLayerSize,outputLayerSize))
def sigmoid (x): return 1/(1 + np.exp(-x))
def sigmoid_(x): return x * (1 - x)
for a in range(4):
X = X_and if a == 0 else X_or if a == 1 else X_nor if a == 2 else X_xor
Y = Y_and if a == 0 else Y_or if a == 1 else Y_nor if a == 2 else Y_xor
for i in range(iteration):
hiddenLayer_val = sigmoid(np.dot(X, w_l1))
outputLayer_val = sigmoid(np.dot(hiddenLayer_val, w_l2))
error = Y - outputLayer_val
dF2 = error * sigmoid_(outputLayer_val)
dF1 = dF2.dot(w_l2.T) * sigmoid_(hiddenLayer_val)
w_l2 += hiddenLayer_val.T.dot(dF2)
w_l1 += X.T.dot(dF1)
if a == 0:
predict_and = Y.T
elif a == 1:
predict_or = Y.T
elif a == 2:
predict_nor = Y.T
elif a == 3:
predict_xor = Y.T
print("Predict AND: " + str(predict_and))
print("Predict OR: " + str(predict_or))
print("Predict NOR: " + str(predict_nor))
print("Predict XOR: " + str(predict_xor))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment