Skip to content

Instantly share code, notes, and snippets.

@starhopp3r
Last active September 23, 2017 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starhopp3r/f0fc73283097fa11406f1d29f81e2a15 to your computer and use it in GitHub Desktop.
Save starhopp3r/f0fc73283097fa11406f1d29f81e2a15 to your computer and use it in GitHub Desktop.
import numpy as np
import os
class NeuralNetwork:
def __init__(self):
# Our training data
self.X = np.array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
self.y = np.transpose(np.array([[0, 1, 1, 1]]))
# Seed random number generator to produce the same
# random numbers evertime the program is executed.
np.random.seed(1)
# Random initial weights. We model a single neuron
# with 3 inputs and 1 output. The weight is a 3x1
# matrix with random values in the range -1 to 1
# with a mean of 0.
self.weight_1 = 2 * np.random.random((3, 4)) - 1
self.weight_2 = 2 * np.random.random((4, 1)) - 1
def __sigmoid(self, x):
# Normalizes the weighted sum to a value between
# 0 and 1.
return 1 / (1 + np.exp(-x))
def __sigmoid_derivative(self, x):
# Returns the gradient of the sigmoid curve at the
# current point. Indicates how confident we are
# about the existing weight.
return x * (1 - x)
def mind(self, inputs, weight=None):
# Check is any custom weights have been supplied.
# If custom weights have been supplied, weights
# loaded from saved model.
if weight is not None:
layer_1 = self.__sigmoid(np.dot(inputs, weight[0]))
layer_2 = self.__sigmoid(np.dot(layer_1, weight[1]))
return layer_2
lr_1 = self.__sigmoid(np.dot(inputs, self.weight_1))
lr_2 = self.__sigmoid(np.dot(lr_1, self.weight_2))
return lr_1, lr_2
def train_network(self, inputs, outputs, epochs):
# Training loop
for epoch in range(epochs):
# Train
layer_1, layer_2 = self.mind(inputs)
# Error
error_layer_2 = outputs - layer_2
delta_layer_2 = error_layer_2 * self.__sigmoid_derivative(layer_2)
error_layer_1 = np.dot(delta_layer_2, np.transpose(self.weight_2))
delta_layer_1 = error_layer_1 * self.__sigmoid_derivative(layer_1)
# Adjust weights
self.weight_1 += np.dot(np.transpose(inputs), delta_layer_1)
self.weight_2 += np.dot(np.transpose(layer_1), delta_layer_2)
def save_model(self):
# Save weights in model.npy file
np.save('model.npy', np.array([self.weight_1, self.weight_2]))
def run_saved_model(self, inpt):
if not os.path.exists('model.npy'):
# No saved model
print("Couldn't find saved model...")
else:
print("Running saved model...")
# Load saved model
weights = np.load('model.npy')
# Predict
return self.mind(inpt, weights)
if __name__ == '__main__':
# Initialize neural network
nn = NeuralNetwork()
# Training data
X = nn.X
y = nn.y
# Train network
nn.train_network(X, y, 50000)
# Save model
nn.save_model()
# Run saved model
result = nn.run_saved_model(X)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment