5 return 1 / (1 + math.exp(-x))
8 return sigmoid(x) * (1 - sigmoid(x))
11 end_node['inputs'] = inputs[:]
13 for i in range(len(inputs)):
14 s += inputs[i] * end_node['weights'][i]
15 end_node['weighted_biased_input'] = s + end_node['bias']
16 end_node['sigmoid_output'] = sigmoid(end_node['weighted_biased_input'])
17 return end_node['sigmoid_output']
19 def backprop(end_result, target, cost):
20 d_cost_over_sigmoid_output = 2*(end_result - target)
21 for i in range(len(end_node['weights'])):
22 d_weighted_biased_input_over_weight = end_node['inputs'][i]
23 d_sigmoid_output_over_weighted_biased_input = d_sigmoid(end_node['weighted_biased_input'])
24 d_cost_over_weight = d_cost_over_sigmoid_output * d_sigmoid_output_over_weighted_biased_input * d_weighted_biased_input_over_weight
25 end_node['weights'][i] -= d_cost_over_weight
26 d_cost_over_bias = d_cost_over_sigmoid_output
27 end_node['bias'] -= d_cost_over_bias
30 training_set = [((0,), 0),
34 #training_set = [((0,), 1),
38 #training_set = [((0,0), 0),
44 #training_set = [((0,0), 0),
49 # NOT (with one irrelevant column)
50 #training_set = [((0,0), 1),
56 #training_set = [((0,0), 0),
61 # 1 if above f(x)=x line, else 0
62 #training_set = [((0,1), 1),
67 # 1 if above f(x)=x**2, else 0 (will fail: no linear separability)
68 #training_set = [((2,4), 0),
73 end_node = {'weights': [random.random() for i in range(len(training_set[0][0]))],
74 'bias': random.random()}
76 for i in range(n_training_runs):
78 for element in training_set:
81 result_ = result(inputs)
82 cost = (result_ - target)**2
83 formatted_weights = []
84 for w in end_node['weights']:
85 formatted_weights += ['%1.3f' % w]
86 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']))
87 backprop(result_, target, cost)