Skip to content

Instantly share code, notes, and snippets.

@hamzamuric
Last active April 8, 2018 18:03
Show Gist options
  • Save hamzamuric/979941a197ee3e2e0e7c3f92d47ec74b to your computer and use it in GitHub Desktop.
Save hamzamuric/979941a197ee3e2e0e7c3f92d47ec74b to your computer and use it in GitHub Desktop.
from numpy import exp, array, random, dot
class NeuralNetwork:
def __init__(self):
random.seed(1)
self.weights = 2 * random.random((3, 1)) - 1
def __sigmoid(self, x):
return 1 / (1 + exp(-x))
def train(self, inputs, outputs, num):
for iteration in range(num):
output = self.think(inputs)
error = outputs - output
adjustment = dot(inputs.T, error * output * (1 - output))
self.weights += adjustment
def think(self, inputs):
result = self.__sigmoid(dot(inputs, self.weights))
return result
network = NeuralNetwork()
inputs = array([[1, 1, 1], [1, 0, 1], [0, 1, 1]])
outputs = array([[1, 1, 0]]).T
network.train(inputs, outputs, 10000)
result = network.think(array([1, 0, 0]))
if result >= 0.5:
print(1)
else:
print(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment