Incorrect results obtained on running a model in LibTorch that was trained and exported from PyTorch
I am trying to export a trained model along with weights for inference in C++ using LibTorch. However, the output tensor results do not match.

The shape of the output tensor is the same.

model = FCN()
state_dict = torch.load('/content/gdrive/My Drive/model/trained_model.pth')
model.load_state_dict(state_dict)
example = torch.randn(1, 3, 768, 1024)
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save('/content/gdrive/My Drive/model/mymodel.pt')


However some warnings are generated which I think maybe causing the incorrect results to be generated.


  /usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:137:
  TracerWarning: Converting a tensor to a Python index might cause the
  trace to be incorrect. We can't record the data flow of Python values,
  so this value will be treated as a constant in the future. This means
  that the trace might not generalize to other inputs!
  /usr/local/lib/python3.6/dist-packages/torch/tensor.py:435:
  RuntimeWarning: Iterating over a tensor might cause the trace to be
  incorrect. Passing a tensor of different shape won't change the number
  of iterations executed (and might lead to errors or silently give
  incorrect results).'incorrect results).', category=RuntimeWarning)


Following is the LibTorch code to generate the output tensor

at::Tensor predict(std::shared_ptr&lt;torch::jit::script::Module&gt; model, at::Tensor &amp;image_tensor) {
    std::vector&lt;torch::jit::IValue&gt; inputs;
    inputs.push_back(image_tensor);

    at::Tensor result = model-&gt;forward(inputs).toTensor();

    return result;
}


Has anyone tried using a trained PyTorch model in LibTorch?

Just ran into the same issue, and found a solution:
add

model.eval() 

before 

traced_script_module = torch.jit.trace(model, example)


and the model gives the same result in c++ as in python
