X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=neural%2Fsingle_neuron_with_backprop.py;fp=neural%2Fsingle_neuron_with_backprop.py;h=f438c7ef383d71c393b235129a4a7ae7ff8370db;hb=1b80c5b27b73e55b25251e002c4e545ecc5ae298;hp=0000000000000000000000000000000000000000;hpb=6e960c317711557fdd5e52e5e11333df69a99624;p=plomrogue2-experiments diff --git a/neural/single_neuron_with_backprop.py b/neural/single_neuron_with_backprop.py new file mode 100755 index 0000000..f438c7e --- /dev/null +++ b/neural/single_neuron_with_backprop.py @@ -0,0 +1,87 @@ +import random + +def sigmoid(x): + import math + return 1 / (1 + math.exp(-x)) + +def d_sigmoid(x): + return sigmoid(x) * (1 - sigmoid(x)) + +def result(inputs): + end_node['inputs'] = inputs[:] + s = 0 + for i in range(len(inputs)): + s += inputs[i] * end_node['weights'][i] + end_node['weighted_biased_input'] = s + end_node['bias'] + end_node['sigmoid_output'] = sigmoid(end_node['weighted_biased_input']) + return end_node['sigmoid_output'] + +def backprop(end_result, target, cost): + d_cost_over_sigmoid_output = 2*(end_result - target) + for i in range(len(end_node['weights'])): + d_weighted_biased_input_over_weight = end_node['inputs'][i] + d_sigmoid_output_over_weighted_biased_input = d_sigmoid(end_node['weighted_biased_input']) + d_cost_over_weight = d_cost_over_sigmoid_output * d_sigmoid_output_over_weighted_biased_input * d_weighted_biased_input_over_weight + end_node['weights'][i] -= d_cost_over_weight + d_cost_over_bias = d_cost_over_sigmoid_output + end_node['bias'] -= d_cost_over_bias + +# identity +training_set = [((0,), 0), + ((1,), 1)] + +# NOT +#training_set = [((0,), 1), +# ((1,), 0)] + +# AND +#training_set = [((0,0), 0), +# ((1,0), 0), +# ((0,1), 0), +# ((1,1), 1)] + +# OR +#training_set = [((0,0), 0), +# ((1,0), 1), +# ((0,1), 1), +# ((1,1), 1)] + +# NOT (with one irrelevant column) +#training_set = [((0,0), 1), +# ((1,0), 0), +# ((0,1), 1), +# ((1,1), 0)] + +# XOR (will fail) +#training_set = [((0,0), 0), +# ((1,0), 1), +# ((0,1), 1), +# ((1,1), 0)] + +# 1 if above f(x)=x line, else 0 +#training_set = [((0,1), 1), +# ((2,3), 1), +# ((1,1), 0), +# ((2,2), 0)] + +# 1 if above f(x)=x**2, else 0 (will fail: no linear separability) +#training_set = [((2,4), 0), +# ((2,5), 1), +# ((3,9), 0), +# ((3,10), 1)] + +end_node = {'weights': [random.random() for i in range(len(training_set[0][0]))], + 'bias': random.random()} +n_training_runs = 100 +for i in range(n_training_runs): + print() + for element in training_set: + inputs = element[0] + target = element[1] + result_ = result(inputs) + cost = (result_ - target)**2 + formatted_weights = [] + for w in end_node['weights']: + formatted_weights += ['%1.3f' % w] + print("inputs %s target %s result %0.9f cost %0.9f weights [%s] bias %1.3f" % (inputs, target, result_, cost, ','.join(formatted_weights), end_node['bias'])) + backprop(result_, target, cost)