New To Rust, hitting issue with borrowing etc

Hello!

I decided to make a project in rust, and learn more about how it works as a language.

Seems stricter than other languages, and I still trying to figure out the rules to it.

My issue:
So I am hitting error[E0308]: mismatched types

due to this line in my code:
layers : make_layers(&settings, &mut activator),

it is complaining about the variable settings. It says its of type ().

I know this has something to do with using references that is/was owned by a different variable, but I don't see what I am missing.

Here is some of the other pieces of code (the full file is 500 lines):

Ignore the activator, that is a struct with a bunch of selectable activation functions.
The Layer struct as well, that just represents a single layer in the Neural Network.

Both of those are working, and are done. They are not causing this issue.

The weights & outputs are represented on one single vector, that's the state vector

struct NeuralNetwork {
      state : Vec<f64>,
      layers : Vec<Layer>,
      activator : Activator
}

impl NeuralNetwork {
      pub fn new(json_str : String) -> NeuralNetwork {
          let mut settings : Vec<LayerSettings> = serde_json::from_str(&json_str[..]).unwrap();
          //let mut settings : Rc<Vec<LayerSettings>> = Rc::new(raw_settings); //was toying with this
          let mut activator = Activator::new();
          NeuralNetwork {
                state : make_state_vector(&mut settings),
                layers : make_layers(&settings, &mut activator),
                activator : activator
          }
      }
}

fn make_state_vector(settings : &mut Vec<LayerSettings>) -> Vec<f64> {
      let mut state_vector : Vec<f64> = Vec::new();
      let mut rng = rand::thread_rng();
      for layer in settings {
          let input_num : usize = layer.input_size;
          let output_size : usize = layer.output_size;
          for weight_option in &layer.layer_weights {
              match weight_option {
                  Some(weight_vector) => {
                      for weight in *weight_vector {
                          let weight_copy : f64 = weight;
                          state_vector.push(weight_copy);
                      }
                  }
                  None => {
                      for _ in 0 .. input_num * output_size {
                          state_vector.push(rng.gen_range(-1.0, 1.0));
                      }
                  }
              }
          }
          // For next layer to read as input from this layer.
          for _ in 0 .. output_size {
              state_vector.push(0.0);
          }
      }
      return state_vector;
}

fn make_layers(settings : &Vec<LayerSettings>, activator : &mut Activator) {
    //todo
}

#[derive(Deserialize)]
struct LayerSettings {
      activation_function : String,
      input_size : usize,
      layer_weights : Vec<Option<Vec<f64>>>,
      output_size : usize,
      bias_value : f64,
      learning_rate : f64,
      momentum : f64
}

I am tossing this in as the "Settings / config" of the NeuralNetwork.
let json_str = r#"[

    {
        "activation_function" : "sigmoid",
        "input_size" : 2,
        "layer_weights" : [
            [0.9, 0.84],
            [0.6, 0.54]
        ],
        "output_size" : 2,
        "bias_value" : 0.6,
        "learning_rate" : 0.01,
        "momentum" : 0.068
    }

]"#.to_string();

I know the culprit to this is the "make_state_vector" function.

The compiler is telling me that it is a () type.
I am suspecting that it is from the reading & use of the contents of the LayerSettings.

Can someone point out how I should adjust my code so that I can reuse the LayerSettings vector multiple times?

Thanks!

I think you might have misread that message, because a problem I can see in your code is that NeuralNetwork defines layers as a Vec<Layer> but the function make_layers returns () implicitly because you haven't defined its return type. The compiler is likely not complaining about settings, but the function you are applying to it "returning" (or not) the wrong type.

Ideally the signature for make_layers would look something like:

fn make_layers(settings : &Vec<LayerSettings>, activator : &mut Activator) -> Vec<Layer>

Oops!

I totally skipped over that.

I fixed that, and that fixed that error. I'll take the L on that one.

Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.