pytorch.PyTorchDataset
kedro_datasets_experimental.pytorch.PyTorchDataset ¶
PyTorchDataset(
*,
filepath,
load_args=None,
save_args=None,
version=None,
credentials=None,
fs_args=None,
metadata=None
)
Bases: AbstractVersionedDataset[Any, Any]
PyTorchDataset loads and saves PyTorch models' state_dict using torch.save
and torch.load.
Warning
Loading is not safe for untrusted files. torch.load deserializes
a pickle stream (the zipfile produced by torch.save is only a
container around that pickle), so a maliciously crafted .pt file can
execute arbitrary code on load. To mitigate this, PyTorchDataset
enforces weights_only=True by default, which restricts loading to
tensors and a small allow-list of safe types. Only set
load_args: {weights_only: false} for files you fully trust, and
prefer torch>=2.6 (where weights_only=True is also the upstream
default) or a non-pickle format such as safetensors when handling
untrusted inputs.
Example usage for the YAML API:¶
model:
type: kedro_datasets_experimental.pytorch.PyTorchDataset
filepath: data/06_models/model.pt
Example usage for the Python API¶
from kedro_datasets_experimental.pytorch import PyTorchDataset
import torch
# Define your model
model: torch.nn.Module
model = torch.nn.Sequential(torch.nn.Linear(10, 10), torch.nn.ReLU())
# Save model state dict
dataset = PyTorchDataset(filepath="data/06_models/model.pt")
dataset.save(model)
# Reload model state dict
reloaded = TheModelClass(*args, **kwargs)
reloaded.load_state_dict(dataset.load())
Source code in kedro_datasets_experimental/pytorch/pytorch_dataset.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
load ¶
load()
Source code in kedro_datasets_experimental/pytorch/pytorch_dataset.py
125 126 127 128 129 130 131 | |
save ¶
save(data)
Source code in kedro_datasets_experimental/pytorch/pytorch_dataset.py
133 134 135 136 137 138 | |