Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Building a Neural Network from Scratch in Rust
EvolveDev
EvolveDev

Posted on

     

Building a Neural Network from Scratch in Rust

In this blog, we will build a simple neural network from scratch in Rust. We'll start by setting up our project, then implement the core components of a neural network, and finally train it on a basic dataset.

Project Setup

First, let's set up a new Rust project. Open your terminal and run:

cargo new neural_networkcdneural_network
Enter fullscreen modeExit fullscreen mode

Next, we'll add thendarray crate for numerical operations andrand crate for random number generation. Update yourCargo.toml file to include these dependencies:

[dependencies]ndarray="0.15"rand="0.8"
Enter fullscreen modeExit fullscreen mode

Implementing the Neural Network

We'll start by creating a network.rs file in the src directory to hold our neural network implementation.

Defining the Network Structure
Create a Network struct that will hold our weights and biases:

// src/network.rsusendarray::{Array1,Array2,Axis};userand::thread_rng;userand::Rng;pubstructNetwork{weights1:Array2<f64>,biases1:Array1<f64>,weights2:Array2<f64>,biases2:Array1<f64>,}implNetwork{pubfnnew(input_size:usize,hidden_size:usize,output_size:usize)->Self{letmutrng=thread_rng();letweights1=Array2::from_shape_fn((hidden_size,input_size),|_|rng.gen_range(-1.0..1.0));letbiases1=Array1::from_shape_fn(hidden_size,|_|rng.gen_range(-1.0..1.0));letweights2=Array2::from_shape_fn((output_size,hidden_size),|_|rng.gen_range(-1.0..1.0));letbiases2=Array1::from_shape_fn(output_size,|_|rng.gen_range(-1.0..1.0));Network{weights1,biases1,weights2,biases2,}}}
Enter fullscreen modeExit fullscreen mode

Forward Pass
Implement the forward pass of the network, which involves calculating the activations for each layer:

implNetwork{fnsigmoid(x:&Array1<f64>)->Array1<f64>{x.mapv(|x|1.0/(1.0+(-x).exp()))}fnsigmoid_derivative(x:&Array1<f64>)->Array1<f64>{x*&(1.0-x)}pubfnforward(&self,input:&Array1<f64>)->(Array1<f64>,Array1<f64>,Array1<f64>){lethidden_input=self.weights1.dot(input)+&self.biases1;lethidden_output=Self::sigmoid(&hidden_input);letfinal_input=self.weights2.dot(&hidden_output)+&self.biases2;letfinal_output=Self::sigmoid(&final_input);(hidden_output,final_input,final_output)}}
Enter fullscreen modeExit fullscreen mode

Read the full article here!

Conclusion

In this blog, we built a simple neural network from scratch in Rust. We covered the core components, including initialization, forward pass, and backpropagation. This example can be expanded to more complex networks and datasets, providing a solid foundation for neural network implementation in Rust.

Feel free to experiment with different architectures, activation functions, and learning rates to see how they affect the network's performance.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

We understand the challenges faced by developers in today's fast-paced tech landscape. That's why we are committed to delivering products and services that streamline workflows, improve productivity,
  • Location
    Kolkata
  • Joined

More fromEvolveDev

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp