pytorch.save
时间: 2023-09-26 22:12:32 浏览: 69
PyTorch provides the `torch.save()` function to save models, tensors, or any other PyTorch objects to disk. This function takes two arguments: the object to be saved and the file path where it should be saved. Here's an example:
```python
import torch
# Save a model or tensor
model = MyModel()
torch.save(model.state_dict(), "model.pt")
# Save multiple objects
data1 = torch.tensor([1, 2, 3])
data2 = torch.tensor([4, 5, 6])
torch.save({"data1": data1, "data2": data2}, "data.pt")
```
In the first example, we save the state dictionary of a model using `model.state_dict()`. This allows us to later load the model and load the saved parameters into it.
In the second example, we save multiple objects by creating a dictionary with named keys and saving it. When loading, we can access these objects by their respective keys.
Remember to provide a unique file path for each saved object to avoid overwriting existing files.
阅读全文