residual neural network in rust with tch-rs

I am trying to implement a feed-forward residual neural network in rust using tch-rs (Torch).

So far, this is my code: (this is a minimal reproducible example)

use tch::{nn::{self, batch_norm1d, layer_norm, BatchNormConfig, ConvConfigND, LayerNormConfig, Module, ModuleT}, Tensor};
const NUM_HIDDEN: i64 = 10;

fn res_block(vs: &nn::Path) -> impl ModuleT {
    let mut default = ConvConfigND::default();
    default.padding = 1;
    let conv1 = nn::conv1d(vs, NUM_HIDDEN, NUM_HIDDEN, 3, default);
    let bn1 = batch_norm1d(vs, NUM_HIDDEN, BatchNormConfig::default());
    let conv2 = nn::conv1d(vs, NUM_HIDDEN, NUM_HIDDEN, 3, default);
    let bn2 = batch_norm1d(vs, NUM_HIDDEN, BatchNormConfig::default());
    nn::func_t(|x,train| {
        let mut residual = Tensor::new();
        x.clone(&residual);
        let x = bn1.forward_t(&conv1.forward(x),train).relu();
        let x = bn2.forward_t(&conv2.forward(&x),train);
        let x = x + residual;
        return x.relu();
    })
}

When I compile this code I get this error:

`*mut torch_sys::C_tensor` cannot be shared between threads safely
within `BatchNorm`, the trait `Sync` is not implemented for `*mut torch_sys::C_tensor`, which is required by `{closure@src\nn.rs:11:16: 11:25}: Send`
required for `&BatchNorm` to implement `Send`

This issue happens when I put the forward_t lines in the func_t.
How do I make this work?

I tried using sequential networks as well but they don't work with the passing of the residual variable further. Is there a way to make that work? or do I need to do something else?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.