Skip to content

Instantly share code, notes, and snippets.

@Gromeer
Created June 19, 2018 10:20
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 Gromeer/24b5de96e1639812605e42a16812e75c to your computer and use it in GitHub Desktop.
Save Gromeer/24b5de96e1639812605e42a16812e75c to your computer and use it in GitHub Desktop.
Something wrong with the code
import math, random
#Def XOR
def XOR(x1,x2):
if(x1 != x2):
return 1;
else:
return 0;
#Sigmoid old school
def sigmoid(x):
return 1/ (1+math.exp(-x) );
#The perceptron class
class perceptron(object):
def __init__(self):
self.weight = [random.uniform(-1,1), random.uniform(-1,1), random.uniform(-1,1)];
self.value = 0;
#Creating the perceptrons
p = [];
for i in range(0,2):
p.append(perceptron());
o = perceptron();
#Forwarding in the neural network
def forward():
#Zeroing the values so we dont add old values with the new ones
o.value = 0;
for i in range(0,2):
p[i].value = 0;
#Forwarding
for y in range(0,2):
for x in range(0,3):
p[y].value += inputs[x] * p[y].weight[x];
for i in range(0,2):
p[i].value = sigmoid(p[i].value);
for i in range(0,2):
o.value += p[i].value * o.weight[i];
o.value += o.weight[2] * 1;
o.value = sigmoid(o.value);
#As the name of the function states this is the backpropagation
def backPropagation():
error = XOR(inputs[0], inputs[1]) - o.value;
errorh = [None,None];
errorh[0] = (o.weight[0] / (o.weight[0] + o.weight[1])) * error;
errorh[1] = (o.weight[1] / (o.weight[0] + o.weight[1])) * error;
for i in range(0,2):
o.weight[i] += error * p[i].value * 0.1;
o.weight[2] += error * 0.1;
for y in range(0,2):
for x in range(0,2):
p[y].weight[x] += errorh[y] * inputs[x] * 0.1;
for i in range(0,2):
p[i].weight[2] += errorh[i] * 0.1;
inputs = [None,None,None];
#Supposed to be the training
for i in range(0,10000):
inputs = [random.randint(0,1), random.randint(0,1), 1];
forward();
backPropagation();
print("done");
#This is the manual testing
while True:
inputs = [float(input()),float(input()),1]
forward();
backPropagation();
print(o.value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment