okay I see. To my understanding, the members of torch::jit::script::Module are private and you’ll only be able to do inference on the loaded module. Maybe you could try to remove the last layer in python with
# remove the last layer
resnet18 = nn.Sequential(*list(resnet18.children())[:-1])

then, trace resnet18 and load the module with the removed last layer to C++, where you could add a linear layer to it and pass the paramters of the linear layer to the optimizer. Something like
std::shared_ptr<torch::jit::script::Module> module;
module = torch::jit::load("../script_module.pt");

// replacement for the last layer
torch::nn::Linear lin(512 , 2); // the last layer of resnet, which you want to replace, has dimensions 512x1000
torch::optim::Adam opt(lin->parameters(), torch::optim::AdamOptions(1e-3 /*learning rate*/));

// example input
std::vector<torch::jit::IValue> input;
input.push_back(torch::zeros({1, 3, 224, 224}));
	
// your cat/dog labels
torch::Tensor label = torch::zeros({2}); 
label[0] = 1.; // one hot encoder

// some training loop
torch::Tensor out = module->forward(input).toTensor().squeeze();
out = lin(out);
torch::Tensor loss = torch::mse_loss(out, label);
opt.zero_grad();
loss.backward();
opt.step();

hope it helps. It may feel a little hacky at the moment, but I am sure that there will be some actual models of popular architectures for future releases. Please report back if it works for you