Last active
September 21, 2017 10:41
-
-
Save starhopp3r/f2a73410919d797f947ad66c4b46b4b2 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 | |
# 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