<jupyter_start><jupyter_text>**File I/O** **Save & Load Tensor**<jupyter_code>import torch
import torch.nn as nn

x=torch.arange(4)
torch.save(x,"x-file")
x2 = torch.load("x-file")
print(x2)<jupyter_output>tensor([0, 1, 2, 3])<jupyter_text>Save & Load a List of Arrays<jupyter_code>y = torch.zeros(4)
torch.save([x, y], 'xy-files')
x2, y2 = torch.load('xy-files')
print(x2, y2)<jupyter_output>tensor([0, 1, 2, 3]) tensor([0., 0., 0., 0.])<jupyter_text>Save & Load a Dictionary of Arrays<jupyter_code>mydict = {'x': x, 'y': y}
torch.save(mydict, 'mydict')
mydict2 = torch.load('mydict')
mydict2<jupyter_output><empty_output><jupyter_text>**PyTorch Model Parameters**<jupyter_code>class MLP(nn.Module):
    def __init__(self):
        super(MLP, self).__init__()
        self.hidden = nn.Linear(20, 256)
        self.output = nn.Linear(256, 10)
        self.relu = nn.ReLU()

    def forward(self, x):
        return self.output(self.relu(self.hidden(x)))

net = MLP()
x = torch.randn(size=(2, 20))
y = net(x)<jupyter_output><empty_output><jupyter_text>Save<jupyter_code>torch.save(net.state_dict(), 'mlp.params')<jupyter_output><empty_output><jupyter_text>Load<jupyter_code>clone = MLP()
clone.load_state_dict(torch.load("mlp.params"))

yclone = clone(x)
yclone == y<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18455598455598454, 'fraction_numerical': 0.02702702702702703, 'mean_word_length': 5.75, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '402276', 'license': 'Apache-2.0', 'n_tokens_mistral': 568, 'n_tokens_neox': 549, 'n_words': 87, 'path': 'L10 Layers, Parameters, GPUs/L10_4_File_I_O.ipynb', 'repo_name': 'JiahongChen/d2l-pytorch-implementation', 'size': '1295'}