How to assign weights to linear op in libctorch

Hi,

Is it possible to assign weights & bias of linear op in C++?

auto m = torch::nn::Linear(
      torch::nn::LinearOptions(in_feature_dim, out_feature_dim).bias(true));

// assign weights & bias 
 m.get()->weight.copy_(weight_py_tensor);????

Searched around, didn’t find a good solution.

Thanks!!

I’m not sure, if you need the get() operation, but you might want to add the NoGrad guard:

torch::NoGradGuard no_grad;
linear->weight.copy_(tensor);

should work.

1 Like

Thanks @ptrblck . Works well!

1 Like