Adding LSTM algorithm from scratch in neural network algorithm sections - #12082
Adding LSTM algorithm from scratch in neural network algorithm sections#12082LEVIII007 wants to merge 28 commits into
Conversation
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
| ##### Testing ##### | ||
| # lstm.test() | ||
|
|
||
| # testing can be done by uncommenting the above lines of code. No newline at end of file |
There was a problem hiding this comment.
An error occurred while parsing the file: neural_network/lstm.py
Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 317:1.
parser error: error at 317:62: expected INDENT
# testing can be done by uncommenting the above lines of code.
^for more information, see https://pre-commit.ci
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
| self.char_to_idx = {c: i for i, c in enumerate(self.chars)} | ||
| self.idx_to_char = {i: c for i, c in enumerate(self.chars)} | ||
|
|
||
| self.train_X, self.train_y = self.data[:-1], self.data[1:] |
There was a problem hiding this comment.
Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: train_X
| self.initialize_weights() | ||
|
|
||
| ##### Helper Functions ##### | ||
| def one_hot_encode(self, char: str) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function one_hot_encode
| vector[self.char_to_idx[char]] = 1 | ||
| return vector | ||
|
|
||
| def initialize_weights(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function initialize_weights
| self.wy = self.init_weights(self.hidden_dim, self.char_size) | ||
| self.by = np.zeros((self.char_size, 1)) | ||
|
|
||
| def init_weights(self, input_dim: int, output_dim: int) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function init_weights
| np.sqrt(6 / (input_dim + output_dim)) | ||
|
|
||
| ##### Activation Functions ##### | ||
| def sigmoid(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function sigmoid
Please provide descriptive name for the parameter: x
| self.input_gates = {} | ||
| self.outputs = {} | ||
|
|
||
| def forward(self, inputs: list) -> list: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function forward
|
|
||
| return outputs | ||
|
|
||
| def backward(self, errors: list, inputs: list) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function backward
| [d_bf, d_bi, d_bc, d_bo, d_by]): | ||
| param -= self.lr * grad | ||
|
|
||
| def train(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function train
| # Backward pass and weight updates | ||
| self.backward(errors, inputs) | ||
|
|
||
| def predict(self, inputs: list) -> str: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function predict
| output = self.forward(inputs)[-1] | ||
| return self.idx_to_char[np.argmax(self.softmax(output))] | ||
|
|
||
| def test(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function test
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
| # lstm.train() | ||
|
|
||
| # # Test the LSTM network and compute accuracy | ||
| # lstm.test() |
There was a problem hiding this comment.
An error occurred while parsing the file: neural_network/lstm.py
Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 358:1.
parser error: error at 359:0: expected INDENT
# lstm.test()
^There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
| self.char_to_idx = {c: i for i, c in enumerate(self.chars)} | ||
| self.idx_to_char = {i: c for i, c in enumerate(self.chars)} | ||
|
|
||
| self.train_X, self.train_y = self.data[:-1], self.data[1:] |
There was a problem hiding this comment.
Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: train_X
| self.initialize_weights() | ||
|
|
||
| ##### Helper Functions ##### | ||
| def one_hot_encode(self, char: str) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function one_hot_encode
| vector[self.char_to_idx[char]] = 1 | ||
| return vector | ||
|
|
||
| def initialize_weights(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function initialize_weights
| self.wy = self.init_weights(self.hidden_dim, self.char_size) | ||
| self.by = np.zeros((self.char_size, 1)) | ||
|
|
||
| def init_weights(self, input_dim: int, output_dim: int) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function init_weights
| ) | ||
|
|
||
| ##### Activation Functions ##### | ||
| def sigmoid(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function sigmoid
Please provide descriptive name for the parameter: x
| self.input_gates = {} | ||
| self.outputs = {} | ||
|
|
||
| def forward(self, inputs: list) -> list: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function forward
|
|
||
| return outputs | ||
|
|
||
| def backward(self, errors: list, inputs: list) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function backward
| ): | ||
| param -= self.lr * grad | ||
|
|
||
| def train(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function train
| # Backward pass and weight updates | ||
| self.backward(errors, inputs) | ||
|
|
||
| def predict(self, inputs: list) -> str: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function predict
| output = self.forward(inputs)[-1] | ||
| return self.idx_to_char[np.argmax(self.softmax(output))] | ||
|
|
||
| def test(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function test
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
| self.char_to_idx = {c: i for i, c in enumerate(self.chars)} | ||
| self.idx_to_char = dict(enumerate(self.chars)) | ||
|
|
||
| self.train_X, self.train_y = self.data[:-1], self.data[1:] |
There was a problem hiding this comment.
Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: train_X
| self.initialize_weights() | ||
|
|
||
| ##### Helper Functions ##### | ||
| def one_hot_encode(self, char: str) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function one_hot_encode
| vector[self.char_to_idx[char]] = 1 | ||
| return vector | ||
|
|
||
| def initialize_weights(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function initialize_weights
| self.wy = self.init_weights(self.hidden_dim, self.char_size, rng) | ||
| self.by = np.zeros((self.char_size, 1)) | ||
|
|
||
| def init_weights( |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function init_weights
| ) | ||
|
|
||
| ##### Activation Functions ##### | ||
| def sigmoid(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function sigmoid
Please provide descriptive name for the parameter: x
| return exp_x / exp_x.sum(axis=0) | ||
|
|
||
| ##### LSTM Network Methods ##### | ||
| def reset(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function reset
| self.input_gates = {} | ||
| self.outputs = {} | ||
|
|
||
| def forward(self, inputs: list) -> list: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function forward
|
|
||
| return outputs | ||
|
|
||
| def backward(self, errors: list, inputs: list) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function backward
| self.wy += d_wy * self.lr | ||
| self.by += d_by * self.lr | ||
|
|
||
| def train(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function train
|
|
||
| self.backward(errors, self.concat_inputs) | ||
|
|
||
| def test(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function test
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
| self.char_to_idx = {c: i for i, c in enumerate(self.chars)} | ||
| self.idx_to_char = dict(enumerate(self.chars)) | ||
|
|
||
| self.train_X, self.train_y = self.data[:-1], self.data[1:] |
There was a problem hiding this comment.
Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: train_X
| self.initialize_weights() | ||
|
|
||
| ##### Helper Functions ##### | ||
| def one_hot_encode(self, char: str) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function one_hot_encode
| vector[self.char_to_idx[char]] = 1 | ||
| return vector | ||
|
|
||
| def initialize_weights(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function initialize_weights
| self.wy = self.init_weights(self.hidden_dim, self.char_size) | ||
| self.by = np.zeros((self.char_size, 1)) | ||
|
|
||
| def init_weights( |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function init_weights
| ) | ||
|
|
||
| ##### Activation Functions ##### | ||
| def sigmoid(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function sigmoid
Please provide descriptive name for the parameter: x
| return exp_x / exp_x.sum(axis=0) | ||
|
|
||
| ##### LSTM Network Methods ##### | ||
| def reset(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function reset
| self.input_gates = {} | ||
| self.outputs = {} | ||
|
|
||
| def forward(self, inputs: list) -> list: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function forward
|
|
||
| return outputs | ||
|
|
||
| def backward(self, errors: list, inputs: list) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function backward
| self.wy += d_wy * self.lr | ||
| self.by += d_by * self.lr | ||
|
|
||
| def train(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function train
|
|
||
| self.backward(errors, self.concat_inputs) | ||
|
|
||
| def test(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function test
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
|
|
||
| self.initialize_weights() | ||
|
|
||
| def one_hot_encode(self, char: str) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function one_hot_encode
| vector[self.char_to_idx[char]] = 1 | ||
| return vector | ||
|
|
||
| def initialize_weights(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function initialize_weights
| self.wy: np.ndarray = self.init_weights(self.hidden_dim, self.char_size) | ||
| self.by: np.ndarray = np.zeros((self.char_size, 1)) | ||
|
|
||
| def init_weights(self, input_dim: int, output_dim: int) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function init_weights
| 6 / (input_dim + output_dim) | ||
| ) | ||
|
|
||
| def sigmoid(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function sigmoid
Please provide descriptive name for the parameter: x
| return x * (1 - x) | ||
| return 1 / (1 + np.exp(-x)) | ||
|
|
||
| def tanh(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function tanh
Please provide descriptive name for the parameter: x
| exp_x = np.exp(x - np.max(x)) | ||
| return exp_x / exp_x.sum(axis=0) | ||
|
|
||
| def reset(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function reset
| self.input_gates = {} | ||
| self.outputs = {} | ||
|
|
||
| def forward(self, inputs: list[np.ndarray]) -> list[np.ndarray]: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function forward
|
|
||
| return outputs | ||
|
|
||
| def backward(self, errors: list[np.ndarray], inputs: list[np.ndarray]) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function backward
| self.wy += d_wy * self.lr | ||
| self.by += d_by * self.lr | ||
|
|
||
| def train(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function train
|
|
||
| self.backward(errors, inputs) | ||
|
|
||
| def test(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function test
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
|
|
||
| self.initialize_weights() | ||
|
|
||
| def one_hot_encode(self, char: str) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function one_hot_encode
| vector[self.char_to_idx[char]] = 1 | ||
| return vector | ||
|
|
||
| def initialize_weights(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function initialize_weights
| self.wy: np.ndarray = self.init_weights(self.hidden_dim, self.char_size) | ||
| self.by: np.ndarray = np.zeros((self.char_size, 1)) | ||
|
|
||
| def init_weights(self, input_dim: int, output_dim: int) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function init_weights
| 6 / (input_dim + output_dim) | ||
| ) | ||
|
|
||
| def sigmoid(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function sigmoid
Please provide descriptive name for the parameter: x
| return x * (1 - x) | ||
| return 1 / (1 + np.exp(-x)) | ||
|
|
||
| def tanh(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function tanh
Please provide descriptive name for the parameter: x
| exp_x = np.exp(x - np.max(x)) | ||
| return exp_x / exp_x.sum(axis=0) | ||
|
|
||
| def reset(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function reset
| self.input_gates = {} | ||
| self.outputs = {} | ||
|
|
||
| def forward(self, inputs: list[np.ndarray]) -> list[np.ndarray]: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function forward
|
|
||
| return outputs | ||
|
|
||
| def backward(self, errors: list[np.ndarray], inputs: list[np.ndarray]) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function backward
| self.wy += d_wy * self.lr | ||
| self.by += d_by * self.lr | ||
|
|
||
| def train(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function train
|
|
||
| self.backward(errors, inputs) | ||
|
|
||
| def test(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function test
|
if it gets accepted, please give me hacktober fest accepted tag. Thank you! |
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
|
|
||
| self.initialize_weights() | ||
|
|
||
| def one_hot_encode(self, char: str) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function one_hot_encode
| vector[self.char_to_index[char]] = 1 | ||
| return vector | ||
|
|
||
| def initialize_weights(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function initialize_weights
| ) | ||
| self.output_layer_bias: np.ndarray = np.zeros((self.vocabulary_size, 1)) | ||
|
|
||
| def init_weights(self, input_dim: int, output_dim: int) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function init_weights
| 6 / (input_dim + output_dim) | ||
| ) | ||
|
|
||
| def sigmoid(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function sigmoid
Please provide descriptive name for the parameter: x
| return x * (1 - x) | ||
| return 1 / (1 + np.exp(-x)) | ||
|
|
||
| def tanh(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function tanh
Please provide descriptive name for the parameter: x
| return 1 - x**2 | ||
| return np.tanh(x) | ||
|
|
||
| def softmax(self, x: np.ndarray) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function softmax
Please provide descriptive name for the parameter: x
| exp_x = np.exp(x - np.max(x)) | ||
| return exp_x / exp_x.sum(axis=0) | ||
|
|
||
| def reset_network_state(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function reset_network_state
| self.output_gate_activations = {} | ||
| self.network_outputs = {} | ||
|
|
||
| def forward_pass(self, inputs: list[np.ndarray]) -> list[np.ndarray]: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function forward_pass
|
|
||
| return outputs | ||
|
|
||
| def backward_pass(self, errors: list[np.ndarray], inputs: list[np.ndarray]) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function backward_pass
|
|
||
| return output | ||
|
|
||
| def test_lstm_workflow(): |
There was a problem hiding this comment.
Please provide return type hint for the function: test_lstm_workflow. If the function does not return a value, please provide the type hint as: def function() -> None:
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
|
|
||
| self.initialize_weights() | ||
|
|
||
| def one_hot_encode(self, char: str) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function one_hot_encode
| vector[self.char_to_index[char]] = 1 | ||
| return vector | ||
|
|
||
| def initialize_weights(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function initialize_weights
| ) | ||
| self.output_layer_bias: np.ndarray = np.zeros((self.vocabulary_size, 1)) | ||
|
|
||
| def init_weights(self, input_dim: int, output_dim: int) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function init_weights
| 6 / (input_dim + output_dim) | ||
| ) | ||
|
|
||
| def sigmoid(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function sigmoid
Please provide descriptive name for the parameter: x
| return x * (1 - x) | ||
| return 1 / (1 + np.exp(-x)) | ||
|
|
||
| def tanh(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function tanh
Please provide descriptive name for the parameter: x
| exp_x = np.exp(x - np.max(x)) | ||
| return exp_x / exp_x.sum(axis=0) | ||
|
|
||
| def reset_network_state(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function reset_network_state
| self.output_gate_activations = {} | ||
| self.network_outputs = {} | ||
|
|
||
| def forward_pass(self, inputs: list[np.ndarray]) -> list[np.ndarray]: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function forward_pass
|
|
||
| return outputs | ||
|
|
||
| def backward_pass(self, errors: list[np.ndarray], inputs: list[np.ndarray]) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function backward_pass
| self.output_layer_weights += d_output_layer_weights * self.learning_rate | ||
| self.output_layer_bias += d_output_layer_bias * self.learning_rate | ||
|
|
||
| def train(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function train
|
|
||
| self.backward_pass(errors, inputs) | ||
|
|
||
| def test(self): |
There was a problem hiding this comment.
Please provide return type hint for the function: test. 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 neural_network/lstm.py, please provide doctest for the function test
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
| ) | ||
| self.output_layer_bias = np.zeros((self.vocabulary_size, 1)) | ||
|
|
||
| def init_weights(self, input_dim: int, output_dim: int) -> np.ndarray: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function init_weights
| 6 / (input_dim + output_dim) | ||
| ) | ||
|
|
||
| def sigmoid(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: x
| return x * (1 - x) | ||
| return 1 / (1 + np.exp(-x)) | ||
|
|
||
| def tanh(self, x: np.ndarray, derivative: bool = False) -> np.ndarray: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: x
| return 1 - x**2 | ||
| return np.tanh(x) | ||
|
|
||
| def softmax(self, x: np.ndarray) -> np.ndarray: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: x
| self.output_gate_activations = {} | ||
| self.network_outputs = {} | ||
|
|
||
| def forward_pass(self, inputs: list[np.ndarray]) -> list[np.ndarray]: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function forward_pass
|
|
||
| return outputs | ||
|
|
||
| def backward_pass(self, errors: list[np.ndarray], inputs: list[np.ndarray]) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function backward_pass
| self.output_layer_weights += d_output_layer_weights * self.learning_rate | ||
| self.output_layer_bias += d_output_layer_bias * self.learning_rate | ||
|
|
||
| def train(self) -> None: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file neural_network/lstm.py, please provide doctest for the function train
|
|
||
| self.backward_pass(errors, inputs) | ||
|
|
||
| def test(self): |
There was a problem hiding this comment.
Please provide return type hint for the function: test. 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 neural_network/lstm.py, please provide doctest for the function test
…names in sigmoid function from x to input array
|
@priya-sundaram-dev Your review, please. |
priya-sundaram-dev
left a comment
There was a problem hiding this comment.
Thanks for the ping, @cclauss — happy to review. And nice work @LEVIII007: this is a genuine from-scratch LSTM with a full forward pass and BPTT backward pass, clear docstrings, and doctests throughout — a good fit for neural_network/. I pulled the branch and ran the doctests locally (numpy 2.5.2): all pass, and train()/backward_pass() run cleanly on the sample.
A few things I'd tidy before merge:
Should fix
-
Remove the commented-out code. There are dead
# print(...)lines in__init__andtest(), and the entire demo inif __name__ == "__main__":is commented out — sosample_datais currently unused and the block only runsdoctest.testmod(). The repo style is to avoid commented-out code. I'd either wire up a small runnable demo (a short string, a few epochs, print the accuracy) or drop the block entirely. -
test()computesaccuracybut never uses it — its only consumer is a commented-outprint. Either return/report it (e.g. return the accuracy alongside the output, orprintit in the demo) or remove the counter so there's no unused-variable smell. -
Docstring parameter names don't match the signatures.
sigmoid,tanh, andsoftmaxdocument:param x:but the parameter isinput_array. Aligning these keeps the docs accurate (and quiets the doc checks).
Nits
self.unique_chars: set→set[str]to match the precise annotations you use elsewhere (dict[str, int], etc.).- Consider accepting an optional
seedand usingnp.random.default_rng(seed)so demos are reproducible. Not blocking — your doctests already avoid asserting on random draws — but the repo leans deterministic.
None of these touch the algorithm itself, which reads correctly. Once the dead code and the accuracy/docstring items are cleaned up, this looks close to mergeable. Thanks again for contributing it!
Describe your change:
Checklist: