0

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

  • The `new_random` function does not take any arguments. I think you want to call `new_random_generic`. Also make sure the `rand` feature is enabled on `nalgebra`. – Locke Nov 14 '22 at 11:03
  • @Locke [`new_random`](https://docs.rs/nalgebra/0.31.4/nalgebra/base/struct.Matrix.html#method.new_random-3) can take arguments and OPs code compiles as posted with `rand` feature enabled. But admittedly it's a bit weird since it is implemented inside a macro. – cafce25 Nov 14 '22 at 11:22
  • @cafce how do you enable rand feature on nalgebra? – Dane Madsen Nov 14 '22 at 11:26
  • See my answer below. – cafce25 Nov 14 '22 at 11:27

1 Answers1

0

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
cafce25
  • 15,907
  • 4
  • 25
  • 31