Dear community,
I find Rust absolutely amazing and I started implementing some DeepLearning with it. However, when scaling the algorithm to multiple dimensions using Vec, I seem to be facing some trouble. Can anybody please help me with my code? Many thanks in advance:
pub struct ConfigMetadata {
pub start: Vec<f32>,
pub step_size: f32,
pub precision: f32,
pub max_epochs: i32,
pub derrivatives: Vec<fn(f32) -> f32>,
pub epoch_printer: fn(i32, Vec<f32>, f32, Option<f32>),
}
pub struct GrandientDescent {}
impl GrandientDescent {
pub fn run(config_metadata: &ConfigMetadata) -> (bool, Vec<f32>, i32) {
let mut next = config_metadata.start.clone();
let mut current_x = Vec::new();
let mut epochs = 0;
for epoch in 0..config_metadata.max_epochs {
current_x= next.clone();
for dimmension in 0..config_metadata.derrivatives.len(){
next[dimmension] = current_x[dimmension] - config_metadata.step_size * (config_metadata.derrivatives[dimmension])(current_x[dimmension]);
let loss = next[dimmension] - current_x[dimmension];
(config_metadata.epoch_printer)(epoch, current_x, loss, None);
epochs += 1;
if loss.abs() <= config_metadata.precision {
return (true, current_x, epochs)
}
}
}
(false, Vec::with_capacity(0), epochs)
}
}
...
let (found, minimum_x, epochs) = GrandientDescent::run(&config_metadata);
The error is:
>error[E0382]: borrow of moved value: `current_x`
> --> src/optimizers.rs:20:36
>15 | let mut current_x = Vec::new();
> | ------------- move occurs because `current_x` has type `std::vec::Vec<f32>`, which does not implement the `Copy` trait
>...
>20 | next[dimmension] = current_x[dimmension] - config_metadata.step_size * (config_metadata.derrivatives[dimmension])(current_x[dimmension]);
> | ^^^^^^^^^ value borrowed here after move
>...
>23 | (config_metadata.epoch_printer)(epoch, current_x, loss, None);
> | --------- value moved here, in previous iteration of loop
So the actual error is on the line with next[dimmension] = current_x[dimmension]