Hi,
I was trying the option 2 by using the mnist example, my steps are:

Building/Training a model in Python-pyTorch using the python mnist example and save it into torch script using the script compiler method.

# Using the example from https://github.com/pytorch/examples/tree/master/mnist/main.py with following modification
    if (args.save_model):
        my_model = torch.jit.script(model)
        my_model.save("mymodel.pt")



Using the model for inference works fine in C++:

#include <torch/torch.h>
#include <torch/script.h>
#include <iostream>

int main() {
	//Net model;	
	torch::jit::script::Module model;
	std::string module_path = "mymodel.pt";
	model=  torch::jit::load(module_path);
	
	// Create a vector of inputs.
	std::vector<torch::jit::IValue> inputs;
	inputs.push_back(torch::ones({1, 1, 28, 28}));

	// Execute the model and turn its output into a tensor.
	at::Tensor output = model.forward(inputs).toTensor();
	std::cout << output << std::endl;	
    
return 0;
}	


However, I was facing difficulty when I wanted to use the model and train in C++

// Using the example from https://github.com/pytorch/examples/blob/master/cpp/mnist/mnist.cpp, by removing the net definition block on the beginning of the codes, and loading the model previously trained in python by using jit::load:

  //Net model;
  //model.to(device);
 
  torch::jit::script::Module model;
  std::string module_path = "mymodel.pt";
  model = torch::jit::load(module_path);  
  model.to(device);

  auto train_dataset = torch::data::datasets::MNIST(kDataRoot)
                           .map(torch::data::transforms::Normalize<>(0.1307, 0.3081))
                           .map(torch::data::transforms::Stack<>());
  const size_t train_dataset_size = train_dataset.size().value();
  auto train_loader =
      torch::data::make_data_loader<torch::data::samplers::SequentialSampler>(
          std::move(train_dataset), kTrainBatchSize);

  auto test_dataset = torch::data::datasets::MNIST(
                          kDataRoot, torch::data::datasets::MNIST::Mode::kTest)
                          .map(torch::data::transforms::Normalize<>(0.1307, 0.3081))
                          .map(torch::data::transforms::Stack<>());
  const size_t test_dataset_size = test_dataset.size().value();
  auto test_loader = torch::data::make_data_loader(std::move(test_dataset), kTestBatchSize);

  torch::optim::SGD optimizer(model.parameter, torch::optim::SGDOptions(0.01).momentum(0.5));

  for (size_t epoch = 1; epoch <= kNumberOfEpochs; ++epoch) {
    train(epoch, model, device, *train_loader, optimizer, train_dataset_size);
    test(model, device, *test_loader, test_dataset_size);
  }


I get error as below:
 error: ‘struct torch::jit::script::Module’ has no member named ‘parameter’; did you mean ‘set_parameter’?
   torch::optim::SGD optimizer(model.parameter, torch::optim::SGDOptions(0.01).momentum(0.5));

I read the torch::jit and it did mentioned on defining the nn.parameters could save the attributes, however, how to make this possible in the model definition replacing nn.Conv2d and nn.Linear?
Thanks again in advance.
Regards,
Chin Luh