Welcome to the neural_rs project implemented in Rust! 🦀
This program demonstrates the fundamentals of a neural network with forward and backpropagation from scratch.
The code is inspired from this video in which the guy coded the same but in C language.
Ensure you have Rust installed. You can install Rust using rustup
:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-
Clone the repository:
git clone https://github.com/Mario-SO/neural_rs/tree/main cd neural_rs
-
Run the project:
cargo run
This simple neural network aims to classify XOR gate outputs 🌓. The network consists of:
- An input layer with 2 neurons
- One hidden layer with 2 neurons
- An output layer with 1 neuron
- Weights and Biases: Initialized randomly between 0 and 1.
- Hidden Layer: Calculates activations using input data, weights, and biases, then applies the sigmoid function.
- Output Layer: Computes the final output using hidden layer activations, weights, and biases, then applies the sigmoid function.
- Adjusts weights and biases to minimize the error between the predicted and actual outputs using the learning rate and derivatives of the sigmoid function.
- Trains the network over several epochs and prints the accuracy on the training set after training.
- Displays final weights and biases after training is complete.
Each training iteration shows:
- Input Data
- Predicted Output
- Expected Output
After training, it prints the final accuracy, weights, and biases.
Example:
Input: [0.0, 0.0] -> Predicted output: [0.045] (Expected: [0.0])
Input: [0.0, 1.0] -> Predicted output: [0.912] (Expected: [1.0])
...
Final accuracy: 100.00%
Final hidden weights: [[0.2, 0.3], [0.4, 0.1]]
Final output weights: [[0.5, 0.7]]
...
- initialize_weights: Initializes weights randomly.
- sigmoid: Sigmoid activation function.
- sigmoid_derivative: Derivative of the sigmoid function for backpropagation.
Feel free to fork this repository and contribute by submitting a pull request. Please ensure all changes are well tested.
Happy coding! 🎉
🌟 Star this repository if you found it helpful!