Torch tensors in R are pointers to Tensors allocated by LibTorch. This has one major consequence for serialization. One cannot simply use saveRDS for serializing tensors, as you would save the pointer but not the data itself. When reloading a tensor saved with saveRDS the pointer might have been deleted in LibTorch and you would get wrong results.

To solve this problem, torch implements specialized functions for serializing tensors to the disk:

Please note that this format is still experimental and you shouldn’t use it for long term storage.

Saving tensors

You can save any object of type torch_tensor to the disk using:

x <- torch_randn(10, 10)
torch_save(x, "")
x_ <- torch_load("")

torch_allclose(x, x_)

Saving modules

The torch_save and torch_load functions also work for nn_modules objects.

When saving an nn_module, all the object is serialized including the model structure and it’s state.

module <- nn_module(
  initialize = function() {
    self$fc1 <- nn_linear(10, 10)
    self$fc2 <- nn_linear(10, 1)
  forward = function(x) {
    x %>% 
      self$fc1() %>% 

model <- module()
torch_save(model, "")
model_ <- torch_load("")

# input tensor
x <- torch_randn(50, 10)
torch_allclose(model(x), model_(x))

Loading models saved in python

Currently the only way to load models from python is to rewrite the model architecture in R. All the parameter names must be identical.

You can then save the PyTorch model state_dict using:, fpath, _use_new_zipfile_serialization=True)

You can then reload the state dict in R and reload it into the model with:

state_dict <- load_state_dict(fpath)
model <- Model()

You can find working examples in torchvision. For example this is what we do for the AlexNet model.