(libtorch) How to save model in MNIST cpp example?

@mhubii,

Thanks again for the response. I did try as you suggested before posting my problem here. Here it is again.

 // Saving the model.
  std::string model_path = "new_test.pt";
  torch::save(seqConvLayer, model_path);

  // Loading the model.
  std::string file = "new_test.pt";
  torch::nn::Sequential savedSeq;
  torch::load(savedSeq, file);
  std::cout << "Saved Model:\n\n";
  std::cout << c10::str(savedSeq) << "\n\n";
  return 0;

With this piece of code I’m expecting that sequence model should be loade appropriately and I’m trying to verify the same by printing its content as shown in the code below.

  std::cout << "Saved Model:\n\n";
  std::cout << c10::str(savedSeq) << "\n\n";

But this doesn’t print the model at all! Here is the output I’m getting after loading the stored model.

Saved Model:

torch::nn::Sequential

Where as I’m expecting the output to look something like as shown below.

torch::nn::Sequential(
  (0): torch::nn::Conv2d(input_channels=3, output_channels=16, kernel_size=[3, 3], stride=[1, 1])
  (1): ReLu
  (2): torch::max_pool2d(x, {2, 2})
  (3): torch::nn::Conv2d(input_channels=16, output_channels=32, kernel_size=[3, 3], stride=[1, 1])
  (3): torch::nn::Conv2d(input_channels=16, output_channels=32, kernel_size=[3, 3], stride=[1, 1])
  (4): ReLu
  (5): torch::max_pool2d(x, {2, 2})
  (6): torch::nn::Conv2d(input_channels=32, output_channels=64, kernel_size=[3, 3], stride=[1, 1])
  (7): ReLu
  (8): torch::max_pool2d(x, {2, 2})
  (9): Flatten
  (10): torch::nn::Dropout(rate=0.25)
  (11): torch::nn::Linear(in=1024, out=500, with_bias=true)
  (12): ReLu
  (13): torch::nn::Dropout(rate=0.25)
  (14): torch::nn::Linear(in=500, out=10, with_bias=true)
  (15): torch::log_softmax(x, dim=1)
)

I am not sure if the model is loaded properly or not. And why loaded model is not being printed as expected ?