generated from javidahmed64592/template-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from javidahmed64592/refactoring-code
Refactor code and add README, license
- Loading branch information
Showing
13 changed files
with
296 additions
and
252 deletions.
There are no files selected for viewing
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Javid Ahmed | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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 |
---|---|---|
|
@@ -15,3 +15,6 @@ mypy = "*" | |
|
||
[requires] | ||
python_version = "3.11" | ||
|
||
[scripts] | ||
test = "python -m pytest -vx" |
This file contains 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,73 @@ | ||
[![python](https://img.shields.io/badge/Python-3.11-3776AB.svg?style=flat&logo=python&logoColor=ffd343)](https://docs.python.org/3.11/) | ||
<!-- omit from toc --> | ||
# Neural Network | ||
This is a neural network library in Python which can be used to feedforward arrays of inputs, generate outputs, and be trained with expected outputs. | ||
|
||
<!-- omit from toc --> | ||
## Table of Contents | ||
- [Installing Dependencies](#installing-dependencies) | ||
- [Using the Neural Network](#using-the-neural-network) | ||
- [Testing](#testing) | ||
- [Formatting, Type Checking and Linting](#formatting-type-checking-and-linting) | ||
|
||
## Installing Dependencies | ||
Install the required dependencies using [pipenv](https://github.com/pypa/pipenv): | ||
|
||
pipenv install | ||
pipenv install --dev | ||
|
||
## Using the Neural Network | ||
The neural network can be created in the following way: | ||
|
||
``` | ||
from src.nn.neural_network import NeuralNetwork | ||
nn = NeuralNetwork(input_nodes=num_inputs, hidden_nodes=num_hidden, output_nodes=num_outputs) | ||
``` | ||
|
||
where | ||
|
||
- `num_inputs`: Number of inputs to pass through neural network | ||
- `num_hidden`: Number of nodes in the hidden layer | ||
- `num_outputs`: Number of outputs to be generated | ||
|
||
To feedforward an array of inputs: | ||
|
||
``` | ||
outputs = nn.feedforward([x_i, ..., x_n]) # n: Number of inputs | ||
``` | ||
|
||
The neural network can also be trained by providing an array of inputs and expected outputs, and backpropagating the error using gradient descent. | ||
|
||
``` | ||
inputs = [x_i, ..., x_n] | ||
expected_outputs = [y_i, ..., y_m] # m: Number of outputs | ||
errors = nn.train(inputs, expected_outputs) | ||
``` | ||
|
||
## Testing | ||
This library uses Pytest for the unit tests. | ||
These tests are located in the `tests` directory. | ||
To run the tests: | ||
|
||
pipenv run test | ||
|
||
## Formatting, Type Checking and Linting | ||
This library uses a number of tools for code formatting and linting. These tools are configured in `pyproject.toml`, `setup.cfg` and `mypy.ini`. | ||
|
||
Black is used as a code formatter: | ||
|
||
black . | ||
|
||
isort is used for tidying up imports: | ||
|
||
isort . | ||
|
||
Mypy is used as a type checker: | ||
|
||
mypy . | ||
|
||
Flake8 is used for linting: | ||
|
||
flake8 |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,66 @@ | ||
import pytest | ||
|
||
from src.math.matrix import Matrix | ||
from src.nn.layer import Layer | ||
from src.nn.neural_network import NeuralNetwork | ||
from src.nn.node import Node | ||
|
||
|
||
@pytest.fixture | ||
def mock_activation(): | ||
return lambda x: x * 3 | ||
|
||
|
||
@pytest.fixture | ||
def mock_weights_range(): | ||
return [-1.0, 1.0] | ||
|
||
|
||
@pytest.fixture | ||
def mock_bias_range(): | ||
return [-1.0, 1.0] | ||
|
||
|
||
@pytest.fixture | ||
def mock_inputs(): | ||
return [1.0, 0.5, 0.7] | ||
|
||
|
||
@pytest.fixture | ||
def mock_len_inputs(mock_inputs): | ||
return len(mock_inputs) | ||
|
||
|
||
@pytest.fixture | ||
def mock_len_hidden(): | ||
return 5 | ||
|
||
|
||
@pytest.fixture | ||
def mock_outputs(): | ||
return [0.0, 1.0] | ||
|
||
|
||
@pytest.fixture | ||
def mock_len_outputs(mock_outputs): | ||
return len(mock_outputs) | ||
|
||
|
||
@pytest.fixture | ||
def mock_nn(mock_len_inputs, mock_len_outputs): | ||
return NeuralNetwork(mock_len_inputs, 5, mock_len_outputs) | ||
|
||
|
||
@pytest.fixture | ||
def mock_layer(mock_len_hidden, mock_len_inputs, mock_activation): | ||
return Layer(mock_len_hidden, mock_len_inputs, mock_activation) | ||
|
||
|
||
@pytest.fixture | ||
def mock_node(mock_len_inputs, mock_weights_range, mock_bias_range, mock_activation): | ||
return Node.random_node(mock_len_inputs, mock_weights_range, mock_bias_range, mock_activation) | ||
|
||
|
||
@pytest.fixture | ||
def mock_input_matrix(mock_inputs): | ||
return Matrix.from_array(mock_inputs) |
Oops, something went wrong.