|
| 1 | +""" |
| 2 | +Forward propagation explanation: |
| 3 | +https://towardsdatascience.com/forward-propagation-in-neural-networks-simplified-math-and-code-version-bbcfef6f9250 |
| 4 | +""" |
| 5 | + |
| 6 | +import math |
| 7 | +import random |
| 8 | + |
| 9 | + |
| 10 | +# Sigmoid |
| 11 | +def sigmoid_function(value: float, deriv: bool = False) -> float: |
| 12 | + """Return the sigmoid function of a float. |
| 13 | +
|
| 14 | + >>> sigmoid_function(3.5) |
| 15 | + 0.9706877692486436 |
| 16 | + >>> sigmoid_function(3.5, True) |
| 17 | + -8.75 |
| 18 | + """ |
| 19 | + if deriv: |
| 20 | + return value * (1 - value) |
| 21 | + return 1 / (1 + math.exp(-value)) |
| 22 | + |
| 23 | + |
| 24 | +# Initial Value |
| 25 | +INITIAL_VALUE = 0.02 |
| 26 | + |
| 27 | + |
| 28 | +def forward_propagation(expected: int, number_propagations: int) -> float: |
| 29 | + """Return the value found after the forward propagation training. |
| 30 | +
|
| 31 | + >>> res = forward_propagation(32, 10000000) |
| 32 | + >>> res > 31 and res < 33 |
| 33 | + True |
| 34 | +
|
| 35 | + >>> res = forward_propagation(32, 1000) |
| 36 | + >>> res > 31 and res < 33 |
| 37 | + False |
| 38 | + """ |
| 39 | + |
| 40 | + # Random weight |
| 41 | + weight = float(2 * (random.randint(1, 100)) - 1) |
| 42 | + |
| 43 | + for _ in range(number_propagations): |
| 44 | + # Forward propagation |
| 45 | + layer_1 = sigmoid_function(INITIAL_VALUE * weight) |
| 46 | + # How much did we miss? |
| 47 | + layer_1_error = (expected / 100) - layer_1 |
| 48 | + # Error delta |
| 49 | + layer_1_delta = layer_1_error * sigmoid_function(layer_1, True) |
| 50 | + # Update weight |
| 51 | + weight += INITIAL_VALUE * layer_1_delta |
| 52 | + |
| 53 | + return layer_1 * 100 |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + import doctest |
| 58 | + |
| 59 | + doctest.testmod() |
| 60 | + |
| 61 | + expected = int(input("Expected value: ")) |
| 62 | + number_propagations = int(input("Number of propagations: ")) |
| 63 | + print(forward_propagation(expected, number_propagations)) |
0 commit comments