<jupyter_start><jupyter_text>4.5 读取和存储<jupyter_code>import torch
from torch import nn<jupyter_output><empty_output><jupyter_text>4.5.1 读写> 1. **使用`save()`和`load()`分别存储和读取`Tensor`**> 2. **`save()`使用`Python`的`pickle`将对象序列化,可以保存各种对象,包括模型,张量,字典**> 3. **`laod()`使用`pickle`将对象进行反序列化**<jupyter_code>x = torch.ones(3)
torch.save(x, 'x.pt')
x2 = torch.load('x.pt')
x2
y = torch.zeros(4)
torch.save([x, y], 'xy.pt')
xy_list = torch.load('xy.pt')
xy_list
torch.save({'x': x, 'y': y}, 'xy_dict.pt')
xy = torch.load('xy_dict.pt')
xy<jupyter_output><empty_output><jupyter_text>4.5.2 读写模型  4.5.2.1 state_dict> 1. **`Pytorch`中,`Module`可以学习参数,包含在`model.parameters()`访问**> 2. **`state_dict`是一个从参数名称映射到参数`Tensor`的字典对象**> 3. **只有可学习参数的层(卷积层,线性层)才有`state_dict()`**> 4. **优化器(`optim`)也有对应的`state_dict()`**<jupyter_code>class MLP(nn.Module):
    def __init__(self):
        super(MLP, self).__init__()
        self.hidden = nn.Linear(3, 2)
        self.act = nn.ReLU()
        self.output = nn.Linear(2, 1)

    def forward(self, x):
        a = self.act(self.hidden(x))
        return self.output(a)
    
net = MLP()
net.state_dict()
optim = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
optim.state_dict()<jupyter_output><empty_output><jupyter_text>4.5.2.2 保存和加载模型> 1. **仅保存和加载模型参数(`state_dict()`),后缀名是pt或pth (推荐)**> 2. **保存和加载整个模型**```python  保存模型参数        torch.save(model.state_dict(), PATH)  推荐后缀名是pt或pth    加载模型参数    model = TheModelClass(*args, **kwargs)    model.load_state_dict(torch.load(PATH))``````python  保存模型        torch.save(model, PATH)  推荐后缀名是pt或pth    加载模型    model = torch.load(PATH)```<jupyter_code>X = torch.randn(2, 3)
Y = net(X)
PATH = './net.pt'
torch.save(net.state_dict(), PATH)
net2 = MLP()
net2.load_state_dict(torch.load(PATH))
Y2 = net2(X)
Y2 == Y<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20589872008903728, 'fraction_numerical': 0.025041736227045076, 'mean_word_length': 5.115646258503402, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '503533', 'license': 'MIT', 'n_tokens_mistral': 907, 'n_tokens_neox': 931, 'n_words': 112, 'path': 'dl/dive-into-dl/chapter04-DL-computation/4.05_read-write.ipynb', 'repo_name': 'tangchunbo/machine-learning', 'size': '1797'}