Skip to content

Instantly share code, notes, and snippets.

@starhopp3r
Last active September 21, 2017 10:41
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/f2a73410919d797f947ad66c4b46b4b2 to your computer and use it in GitHub Desktop.
Save starhopp3r/f2a73410919d797f947ad66c4b46b4b2 to your computer and use it in GitHub Desktop.
import numpy as np
import os
# Features and labels
training_set_inputs = np.array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
training_set_outputs = np.transpose(np.array([[0, 1, 1, 1]]))
# Predict
predict = np.array([[0, 0, 1]])
# Check if model has been saved
if not os.path.exists('model.npy'):
# No saved model
print("Couldn't find saved model...")
# Seed
np.random.seed(1)
# Random initial weights
synaptic_weights = 2 * np.random.random((3, 1)) - 1
# Training loop
for i in range(50000):
# Layer
output = 1 / (1 + np.exp(-(np.dot(training_set_inputs, synaptic_weights))))
# Update weights
synaptic_weights += np.dot(np.transpose(training_set_inputs), (training_set_outputs - output) * output * (1 - output))
# Save model
np.save('model.npy', synaptic_weights)
# Weights
weights = synaptic_weights
else:
# Load saved model
print('Loading saved model...')
weights = np.load('model.npy')
# Predict
prediction = 1 / (1 + np.exp(-(np.dot(predict, weights))))
print(prediction)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment