Skip to content

Instantly share code, notes, and snippets.

@AndyNovo
Created April 6, 2016 20:33
Show Gist options
  • Save AndyNovo/91e80d111fa131d91617726e6e843ffc to your computer and use it in GitHub Desktop.
Save AndyNovo/91e80d111fa131d91617726e6e843ffc to your computer and use it in GitHub Desktop.
import random,copy
W1 = [[random.random()*.2, random.random()*.2, random.random()*.2] for i in range(3)]
W2 = [[random.random()*.2, random.random()*.2, random.random()*.2] for i in range(3)]
#an empty 3x3 matrix
def activation(a_float):
return round(a_float)
def apply_input(input_in, W):
input = copy.copy(input_in)
input.append(-1)
output = [0.0 for r in range(len(W))]
for c in range(len(input)):
for r in range(len(W)):
output[r] += input[c]*W[r][c]
return map(activation, output)
def mlp_apply(input_in, layers):
inputs = [input_in]
for layer in layers:
inputs.append(apply_input(inputs[-1], layer))
return inputs
training = [[[1,0],[1,0,0]], [[1,1], [0,1,0]], [[0,1], [0,0,1]], [[0,0],[0,0,1]]]
eta = .1
def iterate():
test = random.sample(training, 1)
output = apply_input(test[0][0], W1)
deltas = [-1*output[j]+test[0][1][j] for j in range(len(output))]
if (deltas == [0 for j in range(len(output))]):
return True
input = copy.copy(test[0][0])
input.append(-1)
for c in range(len(input)):
for r in range(len(test[0][1])):
W1[r][c] += eta*deltas[r]*input[c]
return False
good_enough = False
ohshit = 0
correct_in_a_row = 0
while not good_enough and ohshit < 1000:
ohshit += 1
print ohshit
if iterate():
correct_in_a_row += 1
if correct_in_a_row > 12:
good_enough = True
else:
correct_in_a_row = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment