Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Deep Neural Network with 2 Hidden Layers (using NumPy)
import numpy as np
def nonlin(x,deriv=False):
if deriv == True:
return x * (1 - x)
return 1 / (1 + np.exp(-x))
X = np.array([[0,0,1],
[0,1,1],
[1,0,1],
[1,1,1]])
y = np.array([[0],
[1],
[1],
[0]])
np.random.seed(1)
# Randomly initialize our weights with mean 0
syn_01 = 2*np.random.random((3, 5)) - 1 # syn weights between layer 0 and layer 1
syn_12 = 2*np.random.random((5, 7)) - 1 # syn weights between layer 1 and layer 2
syn_23 = 2*np.random.random((7, 1)) - 1 # syn weights between layer 2 and layer 3
for j in range(1000):
# Feed forward through layers 0, 1, 2, 3
l0 = X # input layer
l1 = nonlin(np.dot(l0, syn_01)) # first hidden layer
l2 = nonlin(np.dot(l1, syn_12)) # second hidden layer
l3 = nonlin(np.dot(l2, syn_23)) # output layer
# Error: How much did we miss the target value?
# Delta: In what direction is the target value? Were we really sure? If so, don't change too much.
l3_error = y - l3
l3_delta = l3_error * nonlin(l3, deriv=True) # "deriv=True" coz we're interested in the gradient
l2_error = np.dot(l3_delta, syn_23.T) # l3_delta.dot(syn_23.T)
l2_delta = l2_error * nonlin(l2, deriv=True)
l1_error = np.dot(l2_delta, syn_12.T)
l1_delta = l1_error * nonlin(l1, deriv=True)
# Update syn weights
syn_23 += l2.T.dot(l3_delta)
syn_12 += l1.T.dot(l2_delta)
syn_01 += l0.T.dot(l1_delta)
if (j% 100) == 0:
print("Error: {0:.4f}".format((np.mean(np.abs(l3_error)))))
# End of for loop.
#=======================#
# Print the syn weights #
#=======================#
np.set_printoptions(precision=4)
print('\n\nsyn_01 (shape:%s): \n%s' % (syn_01.shape, syn_01))
print('\nsyn_12 (shape:%s): \n%s' % (syn_12.shape, syn_12))
print('\nsyn_23 (shape:%s): \n%s' % (syn_23.shape, syn_23))
#=========#
# Predict #
#=========#
def pred(data):
for syn in [syn_01, syn_12, syn_23]:
data = nonlin(np.dot(data, syn))
return data
X_test = np.array([[0,1,1], [1,0,1]])
print('\n\nPredictions for: %s' % X_test.tolist())
print(pred(X_test))
X_test = np.array([[0,0,1], [1,1,1]])
print('\nPredictions for: %s' % X_test.tolist())
print(pred(X_test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment