Share your repls and programming experiences

← Back to all posts
Artificial Intelligence (Neural Networks) in Rust
RyanYanko

This is kind of a "library" that I made for creating and training neural networks, very loosely based on synaptic.js. I originally started working in javascript, but I decided to port it to rust for practice and performance.

The library is in src/synaptic.rs. There are 3 structs:
First, the Neuron, the basic building block of neural networks. It contains its weights and its bias (which I should probably include in the weights), as well as some activation and propagation data for training.
Next is the Layer, which is just a group of Neurons. Importantly, all of its neurons have the same number of inputs, which will come from the Networks input, or the previous Layer's output. Because the inputs are all the same, the Layer also stores its inputs for training.
And at the top is the Network, which is a collection of Layers. It stores a collection of its hidden Layers, and 1 output Layer. The layers should probably all be in 1 vector, but whatever.

For each struct, I first implemented 3 methods: new, activate, and propagate, for creation, activation, and back-propagation. new will give you a new neuron/layer/network, activate activates the neurons, gives you the activation, and keeps some state for propagate, which will train the network towards the target output.

For easier network training, I made a struct TrainingSet (which is a collection of TrainingPoints, which are inputs and their target outputs). A TrainingSet can be created from an array (or vec) of inputs and a corresponding collection of target outputs. I don't like how currently you need to add & in front of everything, but whatever. When you have your training set, training is as easy as network.train(training_set, iterations, learning_rate).

Lastly, there are some functions that I'm working on in hopes of creating a genetic algorithm interface. They are bad, pay them no mind.

main.rs is what runs and it contains a XOR training demo using the interface. The "RUN" button should work (despite the annoying inability to use onBoot). The first time it will have to download the dependencies (just "rand") which might take a hot sec, but then you should start seeing some output.

Voters
DarkCarnage456
RyanYanko