-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Add simple neural network #6452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cclauss
merged 7 commits into
TheAlgorithms:master
from
CaioCordeiro:add_simple_neural_network
Oct 30, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ae8bb0
feat: add simple foward propagation implementation
2da3951
fix: add PR requested changes
f633886
feat: add code example
5d3d778
fix: solve pre-commit failure
83de3f5
feat: add doctest inside code execution
a4a1364
fix: PR requested changes
9d2ca82
fix: pr requested changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Forward propagation explanation: | ||
https://towardsdatascience.com/forward-propagation-in-neural-networks-simplified-math-and-code-version-bbcfef6f9250 | ||
""" | ||
|
||
import math | ||
import random | ||
|
||
|
||
# Sigmoid | ||
def sigmoid_function(value: float, deriv: bool = False) -> float: | ||
"""Return the sigmoid function of a float. | ||
|
||
>>> sigmoid_function(3.5) | ||
0.9706877692486436 | ||
>>> sigmoid_function(3.5, True) | ||
-8.75 | ||
""" | ||
if deriv: | ||
return value * (1 - value) | ||
return 1 / (1 + math.exp(-value)) | ||
|
||
|
||
# Initial Value | ||
INITIAL_VALUE = 0.02 | ||
|
||
|
||
def forward_propagation(expected: int, number_propagations: int) -> float: | ||
"""Return the value found after the forward propagation training. | ||
|
||
>>> res = forward_propagation(32, 10000000) | ||
>>> res > 31 and res < 33 | ||
True | ||
|
||
>>> res = forward_propagation(32, 1000) | ||
>>> res > 31 and res < 33 | ||
False | ||
""" | ||
|
||
# Random weight | ||
weight = float(2 * (random.randint(1, 100)) - 1) | ||
|
||
for _ in range(number_propagations): | ||
# Forward propagation | ||
layer_1 = sigmoid_function(INITIAL_VALUE * weight) | ||
# How much did we miss? | ||
layer_1_error = (expected / 100) - layer_1 | ||
# Error delta | ||
layer_1_delta = layer_1_error * sigmoid_function(layer_1, True) | ||
# Update weight | ||
weight += INITIAL_VALUE * layer_1_delta | ||
|
||
return layer_1 * 100 | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
expected = int(input("Expected value: ")) | ||
number_propagations = int(input("Number of propagations: ")) | ||
print(forward_propagation(expected, number_propagations)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.