Last active
September 24, 2017 00:47
-
-
Save starhopp3r/63c2cf83d918f5fa065342c13dae8837 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], [0, 0, 0], [0, 1, 0]]) | |
self.y = np.transpose(np.array([[0, 1, 1, 1, 1, 0]])) | |
# 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, 4)) - 1 | |
self.weight_3 = 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])) | |
layer_3 = self.__sigmoid(np.dot(layer_2, weight[2])) | |
return layer_3 | |
lr_1 = self.__sigmoid(np.dot(inputs, self.weight_1)) | |
lr_2 = self.__sigmoid(np.dot(lr_1, self.weight_2)) | |
lr_3 = self.__sigmoid(np.dot(lr_2, self.weight_3)) | |
return lr_1, lr_2, lr_3 | |
def train_network(self, inputs, outputs, epochs): | |
# Training loop | |
for epoch in range(epochs): | |
# Train | |
layer_1, layer_2, layer_3 = self.mind(inputs) | |
# Error | |
error_layer_3 = outputs - layer_3 | |
delta_layer_3 = error_layer_3 * self.__sigmoid_derivative(layer_3) | |
error_layer_2 = np.dot(delta_layer_3, np.transpose(self.weight_3)) | |
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) | |
self.weight_3 += np.dot(np.transpose(layer_2), delta_layer_3) | |
def save_model(self): | |
# Save weights in model.npy file | |
np.save('model.npy', np.array([self.weight_1, self.weight_2, self.weight_3])) | |
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