Torch::save and torch::load not working for a simple example

Hi,

I have a very simple example:

int main(int argc, char* argv[]) {
  torch::NoGradGuard no_grad;

  auto linear = torch::nn::Linear(4, 2);
  linear->weight = linear->weight * 0;
  linear->bias = linear->bias * 0;

  auto x = torch::rand({3, 4}).toType(torch::kFloat);

  std::cout << linear->forward(x) << std::endl;
  torch::save(linear, "/tmp/linear.pt");

  linear = torch::nn::Linear(4, 2);
  torch::load(linear, "/tmp/linear.pt");
  std::cout << linear->forward(x) << std::endl;
}

I get the following output (different random output everytime):

 0  0
 0  0
 0  0
[ CPUFloatType{3,2} ]
-0.9355 -0.0190
-0.7813  0.2720
-0.6068  0.1082
[ CPUFloatType{3,2} ]

Why is this simple example not working?

Thanks!

The code should work as shown in these examples.
Which libtorch version are you using?