Torch tensors have a .numpy()
method, which you can call to convert them to numpy arrays, which can be converted by reticulate to R arrays. There are a few ways to make this work. You could register an S3 method for py_to_r
to make this work globally, or you can just convert it manually after unpickeling. E.g.,:
registerS3method("py_to_r", "torch.Tensor", function(x) x$numpy(), asNamespace("reticulate"))
or
x <- reticulate::py_load_object("my_tensor.pkl")
x <- rapply(list(x), \(x) x$numpy(), classes = "torch.Tensor", how = "replace")[[1]]
The R package torch
is not the same thing as using torch through reticulate. The R torch package wraps the C++ torch library and provides it's own R wrappers. Reticulate embeds a Python interpreter in an R session, and using torch through reticulate is the same as using it through Python interface (i.e, using pytorch
)