12 KiB
12 KiB
In [1]:
import torch
from torch import nn
from torch.nn import functional as FIn [2]:
x = torch.arange(4)
torch.save(x, 'x-file')In [3]:
x2 = torch.load('x-file')
x2Out [3]:
tensor([0, 1, 2, 3])
In [4]:
y = torch.zeros(4)
torch.save([x, y],'x-files')
x2, y2 = torch.load('x-files')
(x2, y2)Out [4]:
(tensor([0, 1, 2, 3]), tensor([0., 0., 0., 0.]))
In [5]:
mydict = {'x': x, 'y': y}
torch.save(mydict, 'mydict')
mydict2 = torch.load('mydict')
mydict2Out [5]:
{'x': tensor([0, 1, 2, 3]), 'y': tensor([0., 0., 0., 0.])}In [6]:
class MLP(nn.Module):
def __init__(self):
super().__init__()
self.hidden = nn.LazyLinear(256)
self.output = nn.LazyLinear(10)
def forward(self, x):
return self.output(F.relu(self.hidden(x)))
net = MLP()
X = torch.randn(size=(2, 20))
Y = net(X)In [7]:
torch.save(net.state_dict(), 'mlp.params')In [8]:
clone = MLP()
clone.load_state_dict(torch.load('mlp.params'))
clone.eval()Out [8]:
MLP( (hidden): LazyLinear(in_features=0, out_features=256, bias=True) (output): LazyLinear(in_features=0, out_features=10, bias=True) )
In [9]:
Y_clone = clone(X)
Y_clone == YOut [9]:
tensor([[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True]])