Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import math
- import random
- import numpy as np
- def layer(inputs, weights, bias):
- """Calculates the values of the next layer"""
- #adds one as bias to the inputs
- inputs = np.vstack([inputs, [1]]) if bias else inputs
- return np.tanh(np.dot(weights, inputs))
- def network(inputs, all_weights, bias):
- """same as network(network(inputs, weights[0]), weights[1])..."""
- outputs = inputs
- for weights in all_weights:
- outputs = layer(outputs, weights, bias)
- return outputs
- def derivative(x):
- """Derivative of the tan function"""
- return 1/(math.cosh(x)**2)
- def calculate_errors(ideal_outputs, outputs, all_weights):
- """calculates the error for the last layer"""
- error_signals = [np.subtract(ideal_outputs, outputs)[0]]
- for weights in reversed(all_weights[1:]):
- error_signals.append(np.dot(error_signals[-1], weights))
- return reversed(error_signals)
- def backpropagate(speed, all_weights, all_errors, inputs, bias):
- new_all_weights = []
- outputs = inputs
- for weights, errors in zip(all_weights, all_errors):
- outputs = layer(outputs, weights, bias)
- new_weights = np.array([[]])
- for index,error in enumerate(errors):
- new_row = np.array([])
- row = weights[index:index+1]
- for weight in row:
- new_row = np.append(new_row,
- weight+speed*error*inputs[index]*outputs[index])
- new_weights = np.append(new_weights,new_row) if np.array_equal(new_weights,np.array([[]])) else np.vstack((new_weights,new_row))
- new_all_weights.append(new_weights)
- return new_all_weights
- def main():
- weights = [np.random.rand(2,2),np.random.rand(1,2)]
- isenough = False
- #while not isenough:
- inputs = np.array([[np.random.randint(0,2)],
- [np.random.randint(0,2)]])
- ideal_outputs = np.array([inputs[0]^inputs[1]])
- outputs = network(inputs,weights,False)
- print weights
- errors = calculate_errors(ideal_outputs, outputs, weights)
- weights = backpropagate(0.1,weights, errors, inputs, False)
- isenough = abs(outputs[0]-(inputs[0]^inputs[1]))<0.1
- inputs = np.array([[np.random.randint(0,2)],
- [np.random.randint(0,2)]])
- ideal_outputs = np.array([inputs[0]^inputs[1]])
- outputs = network(inputs,weights,False)
- print weights
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement