https://github.com/nielsouvrard/neural-networks
Neural network implementation in Python, focused on chess problems
https://github.com/nielsouvrard/neural-networks
Last synced: 3 months ago
JSON representation
Neural network implementation in Python, focused on chess problems
- Host: GitHub
- URL: https://github.com/nielsouvrard/neural-networks
- Owner: NielsOuvrard
- Created: 2024-10-29T18:56:06.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-10-29T19:29:54.000Z (7 months ago)
- Last Synced: 2025-01-08T12:14:23.586Z (4 months ago)
- Language: Python
- Homepage:
- Size: 1.64 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Neural Network
## Introduction
First, a neural network implementation in Python.
Secondly, a converter from chessboards txt files to JSON files according to the neural network input format.## Requirements
- Python 3.8
- [NumPy](https://numpy.org/)## my_torch
### Usage
```bash
./my_torch [--new IN_LAYER [HIDDEN_LAYERS...] OUT_LAYER | --load LOADFILE] [--train | --predict] [--save SAVEFILE] FILE
```### Description
- `--new` Creates a new neural network with random weights. Each subsequent number represents the number of neurons on each layer, from left to right. For example, `./my_torch –new 3 4 5` will create a neural network with an input layer of 3 neurons, a hidden layer of 4 neurons, and an output layer of 5 neurons.
- `--load` Loads an existing neural network from `LOADFILE`, which must be a JSON file.
- `--train` Launches the neural network in training mode. Each board in `FILE` must contain inputs to send to the neural network, as well as the expected output.
- `--rate` Sets the learning rate to `RATE`.
- `--epochs` Sets the number of epochs to `EPOCHS`.
- `--predict` Launches the neural network in prediction mode. Each board in `FILE` must contain inputs to send to the neural network, and optionally an expected output.
- `--save` Save neural network internal state into `SAVEFILE`.
- `FILE` FILE containing chessboards, which must be a JSON file.### Example
#### Example of usage
```bash
$ ./my_torch --new 64 20 20 1 --train --save save.json trainset.json
``````bash
$ ./my_torch --load save.json --predict testset.json
...
Expected: 1, Predicted: 0.88 = 1 -> True
Accuracy: 0.88
```#### Example of input file
#### logical AND
```json
{
"inputs": [
[0, 0],
[0, 1],
[1, 0],
[1, 1]
],
"output": [0, 0, 0, 1]
}
```### Example of save file
```json
{
"weights": [
[
//
]
],
"biases": [
[
///
]
],
"layer_sizes": [
// inputs
// hidden layers (nb neutrons for each layer)
// outputs
]
}
```## Neural Network Class
```python
NeuralNetwork(layers_sizes=..., evaluation_function=..., evaluation_function_derivative=...)
```### Parameters
- `layers_sizes` (list of int) – The number of neurons in each layer of the network. The length of the list represents the number of layers in the network, and the value of each element represents the number of neurons in that layer.
- `evaluation_function` (function) – The function used to evaluate the output of the network. The function must take a single parameter, a numpy.ndarray, and return a numpy.ndarray.
- `evaluation_function_derivative` (function) – The derivative of the evaluation function. The function must take a single parameter, a numpy.ndarray, and return a numpy.ndarray.### Example of Neural Network
```python
nn = NeuralNetwork(
layers_sizes=[2, 2, 1],
evaluation_function=lambda x: 1 / (1 + np.exp(-x)),
evaluation_function_derivative=lambda x: x * (1 - x)
)
```### Test
We have implemented a test suite for the neural network class. With the following command, you can run the tests.
```bash
./tests/tests.sh
```It will run the network with the datasets several times changing the learning rate, the number of epochs and the number of hidden layers.
With this test suite, we found that the best parameters for the network are:```bash
./my_torch --new 64 64 64 64 1 --train --epochs 50 --rate 0.01 --save save.json all_10.json
```The graph below shows the accuracy of the network with the best parameters.

## Authors
- [**Niels Ouvrard**](mailto:[email protected])
- [**Leo Martin**](mailto:[email protected])