pth_load: Read the tensors of a PyTorch checkpoint
Description
Reads a .pth file saved by torch.save() into plain R arrays.
No Python and no torch: a checkpoint is a zip of one metadata pickle and one
entry per tensor, and both are read here directly.
Usage
pth_load(path, names = NULL)
Value
A named list of numeric arrays, in the checkpoint's own order.
Scalars come back as length-1 vectors; everything else carries a
dim. The attribute "file" carries the path.
Arguments
path
Path to a .pth file saved by torch.save().
names
Optional character vector of tensor names to read. The default
reads all of them. Use pth_catalogue to see what is there.
Row-major, and what that means for shapes
PyTorch stores a tensor row-major and R stores an array column-major, so the
same bytes describe transposed arrays in the two languages. The tensors come
back with their dimensions reversed --- a PyTorch [768, 256]
weight arrives as a 256 x 768 R matrix --- which keeps the elements
where they belong without moving a single one. For a linear layer that is
the happy case: x %*% w is then the layer's own arithmetic, no
transpose needed.
See Also
pth_catalogue to list a checkpoint without reading it.