Skip to content

Added Algorithm: Artificial Neural Networks#7652

Closed
DragonDare wants to merge 0 commit into
TheAlgorithms:masterfrom
DragonDare:master
Closed

Added Algorithm: Artificial Neural Networks#7652
DragonDare wants to merge 0 commit into
TheAlgorithms:masterfrom
DragonDare:master

Conversation

@DragonDare
Copy link
Copy Markdown

@DragonDare DragonDare commented Oct 25, 2022

Describe your change:

Added python implementation of ANN (Artificial Neural Networks) algorithm

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@algorithms-keeper algorithms-keeper Bot added the require type hints https://docs.python.org/3/library/typing.html label Oct 25, 2022
Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated
grad_add: Gradient computation through the add operation
"""

def __add__(self, other):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __add__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: other

Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 25, 2022
Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated
Comment thread machine_learning/artificial_neural_networks.py Outdated

pass

def backward(self, gradients=None):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: backward. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: gradients

mulgrad4 = np.array([[8.0, 8.0], [13.0, 13.0]])


def test_case():
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: test_case. If the function does not return a value, please provide the type hint as: def function() -> None:

Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

(bool)(otherwise gradient dont apply to the tensor)
"""

def __init__(self, arr, requires_grad=True) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: arr

Please provide type hint for the parameter: requires_grad

self.zero_grad()
self.shape = self.arr.shape

def zero_grad(self):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: zero_grad. If the function does not return a value, please provide the type hint as: def function() -> None:

"""
self.grad = np.zeros_like(self.arr)

def set_history(self, op, operand1, operand2):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: set_history. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: op

Please provide type hint for the parameter: operand1

Please provide type hint for the parameter: operand2

grad_add: Gradient computation through the add operation
"""

def __add__(self, other):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __add__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: other

grad_matmul: Gradient computation through the matrix multiplication operation
"""

def __matmul__(self, other):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __matmul__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: other


return out_tensor

def grad_add(self, gradients=None):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: grad_add. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: gradients


pass

def grad_matmul(self, gradients=None):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: grad_matmul. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: gradients


pass

def backward(self, gradients=None):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: backward. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: gradients

mulgrad4 = np.array([[8.0, 8.0], [13.0, 13.0]])


def test_case():
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: test_case. If the function does not return a value, please provide the type hint as: def function() -> None:

@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Oct 25, 2022
Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

(bool)(otherwise gradient dont apply to the tensor)
"""

def __init__(self, arr, requires_grad=True) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: arr

Please provide type hint for the parameter: requires_grad

"""
self.grad = np.zeros_like(self.arr)

def set_history(self, op, operand1, operand2) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: op

Please provide type hint for the parameter: operand1

Please provide type hint for the parameter: operand2

grad_add: Gradient computation through the add operation
"""

def __add__(self, other) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: other

grad_matmul: Gradient computation through the matrix multiplication operation
"""

def __matmul__(self, other) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: other


return out_tensor

def grad_add(self, gradients=None) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: gradients


pass

def grad_matmul(self, gradients=None) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: gradients


pass

def backward(self, gradients=None) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: gradients

@algorithms-keeper algorithms-keeper Bot added require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required labels Oct 28, 2022
Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread machine_learning/adaboost.py Outdated

y1 = np.array([1, 0, 2, 1, 2, 0, 0, 0, 0, 0, 1, 0, 2, 1, 2, 1, 0, 1, 1, 1])

X1_test = np.array(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: X1_test

Comment thread machine_learning/adaboost.py Outdated
pred1 = np.array([2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 1, 2, 1, 2])


def assert_close(x, y):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: assert_close. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file machine_learning/adaboost.py, please provide doctest for the function assert_close

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: y

Please provide descriptive name for the parameter: y

Comment thread machine_learning/adaboost.py Outdated
n_stumps: Number of stumps (int.)
"""

def __init__(self, n_stumps=20):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: n_stumps

Comment thread machine_learning/adaboost.py Outdated
self.n_stumps = n_stumps
self.stumps = []

def fit(self, x, y):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: fit. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file machine_learning/adaboost.py, please provide doctest for the function fit

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: y

Please provide descriptive name for the parameter: y

Comment thread machine_learning/adaboost.py Outdated

return self

def stump_error(self, y, y_pred, sample_weights):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: stump_error. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file machine_learning/adaboost.py, please provide doctest for the function stump_error

Please provide type hint for the parameter: y

Please provide descriptive name for the parameter: y

Please provide type hint for the parameter: y_pred

Please provide type hint for the parameter: sample_weights

grad_add: Gradient computation through the add operation
"""

def __add__(self, other) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: other

grad_matmul: Gradient computation through the matrix multiplication operation
"""

def __matmul__(self, other) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: other


return out_tensor

def grad_add(self, gradients=None) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: gradients


pass

def grad_matmul(self, gradients=None) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: gradients


pass

def backward(self, gradients=None) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: gradients

Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread machine_learning/adaboost.py Outdated

y1 = np.array([1, 0, 2, 1, 2, 0, 0, 0, 0, 0, 1, 0, 2, 1, 2, 1, 0, 1, 1, 1])

X1_test = np.array(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: X1_test

Comment thread machine_learning/adaboost.py Outdated
pred1 = np.array([2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 1, 2, 1, 2])


def assert_close(x, y):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: assert_close. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file machine_learning/adaboost.py, please provide doctest for the function assert_close

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: y

Please provide descriptive name for the parameter: y

Comment thread machine_learning/adaboost.py Outdated
n_stumps: Number of stumps (int.)
"""

def __init__(self, n_stumps=20):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: n_stumps

Comment thread machine_learning/adaboost.py Outdated
self.n_stumps = n_stumps
self.stumps = []

def fit(self, x, y):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: fit. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file machine_learning/adaboost.py, please provide doctest for the function fit

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: y

Please provide descriptive name for the parameter: y

Comment thread machine_learning/adaboost.py Outdated

return self

def stump_error(self, y, y_pred, sample_weights):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: stump_error. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file machine_learning/adaboost.py, please provide doctest for the function stump_error

Please provide type hint for the parameter: y

Please provide descriptive name for the parameter: y

Please provide type hint for the parameter: y_pred

Please provide type hint for the parameter: sample_weights

grad_add: Gradient computation through the add operation
"""

def __add__(self, other) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: other

grad_matmul: Gradient computation through the matrix multiplication operation
"""

def __matmul__(self, other) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: other


return out_tensor

def grad_add(self, gradients=None) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: gradients


pass

def grad_matmul(self, gradients=None) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: gradients


pass

def backward(self, gradients=None) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide type hint for the parameter: gradients

@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Oct 28, 2022
@DragonDare DragonDare closed this Oct 28, 2022
@algorithms-keeper algorithms-keeper Bot removed require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Oct 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant