python ANN forward Propagate

Calculate neuron activation for an input

1
2
3
4
5
def activate(weights, inputs):
activation = weights[-1]
for i in range(len(weights)-1):
activation += weights[i] * inputs[i]
return activation

Transfer neuron activation

1
2
3
from math import exp
def transfer(activation):
return 1.0 / (1.0 + exp(-activation))

Forward propagate input to a network output

1
2
3
4
5
6
7
8
9
10
def forward_propagate(network, row):
inputs = row
for layer in network:
new_inputs = []
for neuron in layer:
activation = activate(neuron['weights'], inputs)
neuron['output'] = transfer(activation)
new_inputs.append(neuron['output'])
inputs = new_inputs
return inputs

test forward propagation

1
2
3
4
5
network = [[{'weights': [0.13436424411240122, 0.8474337369372327, 0.763774618976614]}],
[{'weights': [0.2550690257394217, 0.49543508709194095]}, {'weights': [0.4494910647887381, 0.651592972722763]}]]
row = [1, 0, None]
output = forward_propagate(network, row)
print(output)

output

1
[0.6629970129852887, 0.7253160725279748]