Skip to content

DevWonder01/nn-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Neural Network in Rust

This project demonstrates a basic single-layer neural network (perceptron) implemented in Rust. It uses the sigmoid activation function and is trained using gradient descent.

Features

  • Single-layer neural network (perceptron)
  • Sigmoid activation function
  • Customizable learning rate and input size
  • Simple training and prediction API

Example: XOR-like Problem

The example in main.rs trains the network on a simple dataset:

let inputs = vec![
    vec![0.0, 1.0],
    vec![1.0, 0.0],
    vec![1.0, 1.0],
    vec![0.0, 0.0],
    vec![0.5, 0.5],
    vec![0.2, 0.8],
    vec![0.8, 0.2],
    vec![0.3, 0.7],
    vec![0.7, 0.3],
];
let outputs = vec![1.0, 1.0, 0.0, 0.0];

Usage

1. Clone the repository

git clone https://github.com/yourusername/nn-rust.git
cd nn-rust

2. Build and run

cargo run

3. Output

The program will print predictions for each input after training.

Code Structure

  • NeuralNetwork struct: Holds weights, bias, and learning rate.
  • sigmoid and derivative: Activation function and its derivative.
  • train: Trains the network using gradient descent.
  • predict: Makes predictions for new inputs.

Dependencies

  • rand for random weight initialization.

Add to your Cargo.toml:

[dependencies]
rand = "0.8"

License

MIT License


This is a simple educational example and not intended for production use.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages