Rust nalgebra - new_random() throws error for DMatrix

431 Views Asked by At

Im trying to port https://github.com/markkraay/mnist-from-scratch to rust as an introduction to ML and the rust programming language.

I've decided to use nalgebra instead of rewriting a matrix library. However, im running into an error stating function or associated item not found in `Matrix<f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>> when attempting to run new_random() on a DMatrix and I cant see how to fix It.

For context this is my code

pub fn new(input: usize, hidden: usize, output: usize, learning_rate: usize) -> NeuralNetwork {
        let hidden_weights = na::DMatrix::<f64>::new_random(hidden, input);
        let output_weights = na::DMatrix::<f64>::new_random(output, hidden);
        
        NeuralNetwork {
            input,
            hidden,
            output,
            learning_rate,
            hidden_weights,
            output_weights
        }
    }

Ive tried removing <f64> so that it is instead

na::DMatrix::new_random(hidden, input);

but there is no difference

1

There are 1 best solutions below

0
On

To use new_random you have to enable the rand feature of nalgebra like so in Cargo.toml:

[dependencies]
nalgebra = { version = "0.31.4", features = ["rand"] }

after that your code should work as you posted it.

If you have cargo-edit installed you can also do:

cargo add nalgebra --features rand