Files
nn/chapter5-9.ipynb
2026-04-22 15:23:35 +08:00

474 KiB

In [2]:

import torch
import d2l
import numpy
import torch.nn as nn
import torch.nn.functional as F
In [3]:
net = nn.Sequential(nn.Linear(20, 256), nn.ReLU(), nn.Linear(256, 10))
X = torch.rand(2, 20)
net(X)
Out [3]:
tensor([[ 0.0041, -0.3465, -0.2096,  0.2304, -0.1043,  0.0066,  0.1817,  0.0355,
          0.2685, -0.0461],
        [-0.0932, -0.1621, -0.1244,  0.2398, -0.0759,  0.0680,  0.1511,  0.0224,
          0.2522, -0.0228]], grad_fn=<AddmmBackward0>)
In [4]:
class MLP(nn.Module):
    def __init__(self):
        super().__init__()
        self.hidden=nn.Linear(20,256)
        self.out=nn.Linear(256,10)
    def forward(self,X):
        return self.out(F.relu(self.hidden(X)))
In [5]:
net=MLP()
net(X)
Out [5]:
tensor([[-0.2165,  0.1394,  0.0867,  0.0692,  0.2914, -0.1427,  0.2218, -0.0533,
         -0.2137,  0.0044],
        [-0.2020,  0.0648,  0.0514,  0.0500,  0.2555, -0.1679,  0.1621, -0.1462,
         -0.2527,  0.0386]], grad_fn=<AddmmBackward0>)
In [6]:
class FixedHiddenMLP(nn.Module):
    def __init__(self):
        super().__init__()
        # 不计算梯度的随机权重参数。因此其在训练期间保持不变
        self.rand_weight = torch.rand((20, 20), requires_grad=False)
        self.linear = nn.Linear(20, 20)
    def forward(self, X):
        X = self.linear(X)
        # 使用创建的常量参数以及relu和mm函数
        X = F.relu(torch.mm(X, self.rand_weight) + 1)
        # 复用全连接层。这相当于两个全连接层共享参数
        X = self.linear(X)
        # 控制流
        while X.abs().sum() > 1:
            X /= 2
        return X.sum()
In [7]:
net = FixedHiddenMLP()
net(X)
Out [7]:
tensor(-0.0023, grad_fn=<SumBackward0>)
In [8]:
class NestMLP(nn.Module):
    def __init__(self):
        super().__init__()
        self.net = nn.Sequential(nn.Linear(20, 64), nn.ReLU(),
                                    nn.Linear(64, 32), nn.ReLU())
        self.linear = nn.Linear(32, 16)
    def forward(self, X):
        return self.linear(self.net(X))
        chimera = nn.Sequential(NestMLP(), nn.Linear(16, 20), FixedHiddenMLP())
        chimera(X)
In [9]:
net = nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 1))
X = torch.rand(size=(2, 4))
net(X)
Out [9]:
tensor([[-0.1265],
        [-0.0471]], grad_fn=<AddmmBackward0>)
In [10]:
print(net[2].state_dict())
OrderedDict([('weight', tensor([[ 0.0136, -0.1015,  0.1191,  0.2722,  0.3456, -0.0650, -0.0437, -0.2806]])), ('bias', tensor([-0.0945]))])
In [11]:
net[2].state_dict()
Out [11]:
OrderedDict([('weight',
              tensor([[ 0.0136, -0.1015,  0.1191,  0.2722,  0.3456, -0.0650, -0.0437, -0.2806]])),
             ('bias', tensor([-0.0945]))])
In [12]:
print(type(net[2].bias))
<class 'torch.nn.parameter.Parameter'>
In [13]:
print(net[2].bias)
print(net[2].bias.data)
Parameter containing:
tensor([-0.0945], requires_grad=True)
tensor([-0.0945])
In [14]:
net[2].weight.grad==None
Out [14]:
True
In [15]:
print(*[(name, param.shape) for name, param in net[0].named_parameters()])
print(*[(name, param.shape) for name, param in net.named_parameters()])
('weight', torch.Size([8, 4])) ('bias', torch.Size([8]))
('0.weight', torch.Size([8, 4])) ('0.bias', torch.Size([8])) ('2.weight', torch.Size([1, 8])) ('2.bias', torch.Size([1]))
In [16]:
net.state_dict()['2.bias'].data
Out [16]:
tensor([-0.0945])
In [17]:
def block1():
    return nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 4),nn.ReLU())
def block2():
    net = nn.Sequential()
    for i in range(4):
        net.add_module(f'block{i}', block1())
    return net
In [18]:
rgnet = nn.Sequential(block2(),nn.Linear(4,1))
rgnet(X)
Out [18]:
tensor([[0.0117],
        [0.0117]], grad_fn=<AddmmBackward0>)
In [19]:
print(rgnet)
Sequential(
  (0): Sequential(
    (block0): Sequential(
      (0): Linear(in_features=4, out_features=8, bias=True)
      (1): ReLU()
      (2): Linear(in_features=8, out_features=4, bias=True)
      (3): ReLU()
    )
    (block1): Sequential(
      (0): Linear(in_features=4, out_features=8, bias=True)
      (1): ReLU()
      (2): Linear(in_features=8, out_features=4, bias=True)
      (3): ReLU()
    )
    (block2): Sequential(
      (0): Linear(in_features=4, out_features=8, bias=True)
      (1): ReLU()
      (2): Linear(in_features=8, out_features=4, bias=True)
      (3): ReLU()
    )
    (block3): Sequential(
      (0): Linear(in_features=4, out_features=8, bias=True)
      (1): ReLU()
      (2): Linear(in_features=8, out_features=4, bias=True)
      (3): ReLU()
    )
  )
  (1): Linear(in_features=4, out_features=1, bias=True)
)
In [20]:
rgnet[0][1][0].bias.data
Out [20]:
tensor([ 0.2396, -0.2293, -0.3365,  0.0070, -0.0166, -0.2328, -0.1627,  0.3407])
In [21]:
def init_normal(m):
    if type(m) == nn.Linear:
        nn.init.normal_(m.weight, mean=0, std=0.01)
        nn.init.zeros_(m.bias)
net.apply(init_normal)
net[0].weight.data[0], net[0].bias.data[0]
Out [21]:
(tensor([ 0.0166,  0.0092,  0.0013, -0.0031]), tensor(0.))
In [22]:
def init_xavier(m):
    if type(m) == nn.Linear:
        nn.init.xavier_uniform_(m.weight)
def init_42(m):
    if type(m) == nn.Linear:
        nn.init.constant_(m.weight, 42)

net[0].apply(init_xavier)
net[2].apply(init_42)
print(net[0].weight.data[0])
print(net[2].weight.data)
tensor([-0.2085,  0.4344, -0.3960,  0.5868])
tensor([[42., 42., 42., 42., 42., 42., 42., 42.]])
In [23]:
x = torch.arange(4)
torch.save(x, 'x-file')
In [24]:
x2 = torch.load('x-file')
x2
Out [24]:
tensor([0, 1, 2, 3])
In [25]:
class MLP(nn.Module):
    def __init__(self):
        super().__init__()
        self.hidden = nn.Linear(20, 256)
        self.output = nn.Linear(256, 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 [26]:
torch.save(net.state_dict(), 'mlp.params')
In [27]:
clone = MLP()
clone.load_state_dict(torch.load('mlp.params'))
clone.eval()
Out [27]:
MLP(
  (hidden): Linear(in_features=20, out_features=256, bias=True)
  (output): Linear(in_features=256, out_features=10, bias=True)
)
In [28]:
Y_clone = clone(X)
Y_clone == Y
Out [28]:
tensor([[True, True, True, True, True, True, True, True, True, True],
        [True, True, True, True, True, True, True, True, True, True]])
In [29]:
def corr2d(X,K):
    h,w=K.shape
    Y=torch.ones((X.shape[0]-h+1,X.shape[1]-w+1))
    for i in range(Y.shape[0]):
        for j in range(Y.shape[1]):
            Y[i,j]=(X[i:i+h,j:j+w]*K).sum()
    return Y
In [30]:
X = torch.tensor([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]])
K = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
corr2d(X,K)
Out [30]:
tensor([[19., 25.],
        [37., 43.]])
In [31]:
class Conv2D(nn.Module):
    def __init__(self, kernel_size):
        super().__init__()
        self.weight = nn.Parameter(torch.rand(kernel_size))
        self.bias = nn.Parameter(torch.zeros(1))
    def forward(self, x):
        return corr2d(x, self.weight) + self.bias
In [32]:
X = torch.ones((6, 8))
X[:, 2:6] = 0
X
Out [32]:
tensor([[1., 1., 0., 0., 0., 0., 1., 1.],
        [1., 1., 0., 0., 0., 0., 1., 1.],
        [1., 1., 0., 0., 0., 0., 1., 1.],
        [1., 1., 0., 0., 0., 0., 1., 1.],
        [1., 1., 0., 0., 0., 0., 1., 1.],
        [1., 1., 0., 0., 0., 0., 1., 1.]])
In [33]:
K = torch.tensor([[1.0, -1.0]])
Y = corr2d(X, K)
Y
Out [33]:
tensor([[ 0.,  1.,  0.,  0.,  0., -1.,  0.],
        [ 0.,  1.,  0.,  0.,  0., -1.,  0.],
        [ 0.,  1.,  0.,  0.,  0., -1.,  0.],
        [ 0.,  1.,  0.,  0.,  0., -1.,  0.],
        [ 0.,  1.,  0.,  0.,  0., -1.,  0.],
        [ 0.,  1.,  0.,  0.,  0., -1.,  0.]])
In [34]:
corr2d(X.t(), K)
Out [34]:
tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])
In [35]:
conv2d = nn.Conv2d(1,1, kernel_size=(1, 2), bias=False)
In [36]:
X = X.reshape((1, 1, 6, 8))
Y = Y.reshape((1, 1, 6, 7))
lr = 3e-2
In [37]:
for i in range(100):
    Y_hat = conv2d(X)
    l = (Y_hat - Y) ** 2
    conv2d.zero_grad()
    l.sum().backward()
    # 迭代卷积核
    conv2d.weight.data[:] -= lr * conv2d.weight.grad
    if (i + 1) % 20 == 0:
        print(f'epoch {i+1}, loss {l.sum():.3f}')
epoch 20, loss 0.000
epoch 40, loss 0.000
epoch 60, loss 0.000
epoch 80, loss 0.000
epoch 100, loss 0.000
In [38]:
conv2d.weight.data.reshape((1, 2))
Out [38]:
tensor([[ 1.0000, -1.0000]])
In [39]:

# 为了方便起见,我们定义了一个计算卷积层的函数。
# 此函数初始化卷积层权重,并对输入和输出提高和缩减相应的维数
def comp_conv2d(conv2d, X):
# 这里的(1,1)表示批量大小和通道数都是1
    X = X.reshape((1, 1) + X.shape)
    Y = conv2d(X)
    # 省略前两个维度:批量大小和通道
    return Y.reshape(Y.shape[2:])
# 请注意,这里每边都填充了1行或1列,因此总共添加了2行或2列
conv2d = nn.Conv2d(1, 1, kernel_size=3, padding=1)
In [40]:
X = torch.rand(size=(8, 8))
comp_conv2d(conv2d, X).shape
Out [40]:
torch.Size([8, 8])
In [41]:
conv2d = nn.Conv2d(1, 1, kernel_size=(5, 3), padding=(2, 1))
comp_conv2d(conv2d, X).shape
Out [41]:
torch.Size([8, 8])
In [42]:
conv2d = nn.Conv2d(1, 1, kernel_size=3, padding=1, stride=2)
comp_conv2d(conv2d, X).shape
Out [42]:
torch.Size([4, 4])
In [43]:
conv2d = nn.Conv2d(1, 1, kernel_size=(3, 5), padding=(0, 1), stride=(3, 4))
comp_conv2d(conv2d, X).shape
Out [43]:
torch.Size([2, 2])
In [44]:
def corr2d_multi_in(X,K):
    return sum(corr2d(x,k) for x,k in zip(X,K))
X = torch.tensor([[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]])
K = torch.tensor([[[0.0, 1.0], [2.0, 3.0]], [[1.0, 2.0], [3.0, 4.0]]])
corr2d_multi_in(X, K)
Out [44]:
tensor([[ 56.,  72.],
        [104., 120.]])
In [45]:
def corr2d_multi_in_out(X,K) ->torch.Tensor :
    return torch.stack([corr2d_multi_in(X,k) for k in K],0)
In [46]:
K = torch.stack((K, K + 1, K + 2), 0)
K.shape
Out [46]:
torch.Size([3, 2, 2, 2])
In [47]:
corr2d_multi_in_out(X, K)
Out [47]:
tensor([[[ 56.,  72.],
         [104., 120.]],

        [[ 76., 100.],
         [148., 172.]],

        [[ 96., 128.],
         [192., 224.]]])
In [48]:
def corr2d_multi_in_out_1x1(X, K):
    h_i,h,w=X.shape
    h_o=K.shape[0]
    X=X.reshape((h_i,h*w))
    print(X.shape)
    K=K.reshape((h_o,h_i))
    print(K.shape)
    Y=torch.matmul(K,X)
    return Y.reshape((h_o,h,w))
In [49]:
X = torch.normal(0, 1, (3, 3, 3))
K = torch.normal(0, 1, (2, 3, 1, 1))
In [50]:
Y1 = corr2d_multi_in_out_1x1(X, K)
torch.Size([3, 9])
torch.Size([2, 3])
In [51]:
Y2 = corr2d_multi_in_out(X, K)
assert float(torch.abs(Y1 - Y2).sum()) < 1e-6
In [52]:
def pool2d(X,pool_size,mode='max'):
    p_h,p_w =pool_size
    Y = torch.zeros((X.shape[0]-p_h+1,X.shape[1]-p_w+1))
    for i in range(Y.shape[0]):
        for j in range(Y.shape[1]):
            match mode:
                case 'max':
                    Y[i,j]=X[i:i+p_h,j:j+p_w].max()
                case 'avg':
                    Y[i,j]=X[i:i+p_h,j:j+p_w].mean()

    return Y
In [53]:
X = torch.tensor([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]])
pool2d(X, (2, 2))
Out [53]:
tensor([[4., 5.],
        [7., 8.]])
In [54]:
pool2d(X, (2, 2), 'avg')
Out [54]:
tensor([[2., 3.],
        [5., 6.]])
In [55]:
X = torch.arange(16, dtype=torch.float32).reshape((1, 1, 4, 4))
X
Out [55]:
tensor([[[[ 0.,  1.,  2.,  3.],
          [ 4.,  5.,  6.,  7.],
          [ 8.,  9., 10., 11.],
          [12., 13., 14., 15.]]]])
In [56]:
pool2d=nn.MaxPool2d(3)
pool2d(X)
Out [56]:
tensor([[[[10.]]]])
In [57]:
pool2d = nn.MaxPool2d(3, padding=1, stride=2)
pool2d(X)
Out [57]:
tensor([[[[ 5.,  7.],
          [13., 15.]]]])
In [58]:
pool2d = nn.MaxPool2d((2, 3), stride=(2, 3), padding=(0, 1))
pool2d(X)
Out [58]:
tensor([[[[ 5.,  7.],
          [13., 15.]]]])
In [59]:
X = torch.cat((X, X + 1), 1)
X
Out [59]:
tensor([[[[ 0.,  1.,  2.,  3.],
          [ 4.,  5.,  6.,  7.],
          [ 8.,  9., 10., 11.],
          [12., 13., 14., 15.]],

         [[ 1.,  2.,  3.,  4.],
          [ 5.,  6.,  7.,  8.],
          [ 9., 10., 11., 12.],
          [13., 14., 15., 16.]]]])
In [60]:
pool2d = nn.MaxPool2d(3, padding=1, stride=2)
pool2d(X)
Out [60]:
tensor([[[[ 5.,  7.],
          [13., 15.]],

         [[ 6.,  8.],
          [14., 16.]]]])
In [61]:
net = nn.Sequential(
    nn.Conv2d(1,6,kernel_size=5,padding=2), #1*1*28*28 -> 1*6*28*28
    nn.Sigmoid(),
    nn.AvgPool2d(kernel_size=2, stride=2),  #1*6*28*28 -> 1*6*14*14
    nn.Conv2d(6, 16, kernel_size=5), nn.Sigmoid(), #1*6*14*14 -> 1*16*10*10
    nn.AvgPool2d(kernel_size=2, stride=2), #1*16*10*10 -> 1*16*5*5
    nn.Flatten(),
    nn.Linear(16 * 5 * 5, 120), nn.Sigmoid(),
    nn.Linear(120, 84), nn.Sigmoid(),
    nn.Linear(84, 10)
)
X = torch.rand(size=(1,1,28,28),dtype=torch.float32)
for layer in net:
    X=layer(X)
    print(layer.__class__.__name__,'output shape: \t',X.shape)
Conv2d output shape: 	 torch.Size([1, 6, 28, 28])
Sigmoid output shape: 	 torch.Size([1, 6, 28, 28])
AvgPool2d output shape: 	 torch.Size([1, 6, 14, 14])
Conv2d output shape: 	 torch.Size([1, 16, 10, 10])
Sigmoid output shape: 	 torch.Size([1, 16, 10, 10])
AvgPool2d output shape: 	 torch.Size([1, 16, 5, 5])
Flatten output shape: 	 torch.Size([1, 400])
Linear output shape: 	 torch.Size([1, 120])
Sigmoid output shape: 	 torch.Size([1, 120])
Linear output shape: 	 torch.Size([1, 84])
Sigmoid output shape: 	 torch.Size([1, 84])
Linear output shape: 	 torch.Size([1, 10])
In [62]:
import d2l.torch as d2l
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size)
In [63]:
lr, num_epochs = 0.9, 10
#d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
In [64]:
class Inception(nn.Module):
    def __init__(self,in_channels,c1,c2,c3,c4,**kwargs):
        super(Inception,self).__init__(**kwargs)
        self.p1_1 = nn.Conv2d(in_channels,c1,kernel_size=1)
        self.p2_1 = nn.Conv2d(in_channels,c2[0],kernel_size=1)
        self.p2_2 = nn.Conv2d(c2[0],c2[1],kernel_size=3,padding=1)
        self.p3_1 = nn.Conv2d(in_channels,c3[0],kernel_size=1)
        self.p3_2 = nn.Conv2d(c3[0],c3[1],kernel_size=5,padding=2)
        self.p4_1 = nn.MaxPool2d(kernel_size=3, stride=1, padding=1)
        self.p4_2 = nn.Conv2d(in_channels, c4, kernel_size=1)
    def forward(self,x):
        p1 = F.relu(self.p1_1(x))
        p2 = F.relu(self.p2_2(F.relu(self.p2_1(x))))
        p3 = F.relu(self.p3_2(F.relu(self.p3_1(x))))
        p4 = F.relu(self.p4_2(self.p4_1(x)))
        return torch.cat((p1,p2,p3,p4),dim=1)
In [65]:
b1 = nn.Sequential(nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3),
                    nn.ReLU(),
                    nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
b2 = nn.Sequential(nn.Conv2d(64, 64, kernel_size=1),
                    nn.ReLU(),
                    nn.Conv2d(64, 192, kernel_size=3, padding=1),
                    nn.ReLU(),
                    nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
b3 = nn.Sequential(Inception(192, 64, (96, 128), (16, 32), 32),
                    Inception(256, 128, (128, 192), (32, 96), 64),
                    nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
b4 = nn.Sequential(Inception(480, 192, (96, 208), (16, 48), 64),
                    Inception(512, 160, (112, 224), (24, 64), 64),
                    Inception(512, 128, (128, 256), (24, 64), 64),
                    Inception(512, 112, (144, 288), (32, 64), 64),
                    Inception(528, 256, (160, 320), (32, 128), 128),
                    nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
b5 = nn.Sequential(Inception(832, 256, (160, 320), (32, 128), 128),
                    Inception(832, 384, (192, 384), (48, 128), 128),
                    nn.AdaptiveAvgPool2d((1,1)),
                    nn.Flatten())
net = nn.Sequential(b1, b2, b3, b4, b5, nn.Linear(1024, 10))
X = torch.rand(size=(1, 1, 96, 96))
for layer in net:
    X = layer(X)
    print(layer.__class__.__name__,'output shape:\t', X.shape)
Sequential output shape:	 torch.Size([1, 64, 24, 24])
Sequential output shape:	 torch.Size([1, 192, 12, 12])
Sequential output shape:	 torch.Size([1, 480, 6, 6])
Sequential output shape:	 torch.Size([1, 832, 3, 3])
Sequential output shape:	 torch.Size([1, 1024])
Linear output shape:	 torch.Size([1, 10])
In [66]:
import torchinfo
torchinfo.summary(net,(1,1,96,96))
Out [66]:
==========================================================================================
Layer (type:depth-idx)                   Output Shape              Param #
==========================================================================================
Sequential                               [1, 10]                   --
├─Sequential: 1-1                        [1, 64, 24, 24]           --
│    └─Conv2d: 2-1                       [1, 64, 48, 48]           3,200
│    └─ReLU: 2-2                         [1, 64, 48, 48]           --
│    └─MaxPool2d: 2-3                    [1, 64, 24, 24]           --
├─Sequential: 1-2                        [1, 192, 12, 12]          --
│    └─Conv2d: 2-4                       [1, 64, 24, 24]           4,160
│    └─ReLU: 2-5                         [1, 64, 24, 24]           --
│    └─Conv2d: 2-6                       [1, 192, 24, 24]          110,784
│    └─ReLU: 2-7                         [1, 192, 24, 24]          --
│    └─MaxPool2d: 2-8                    [1, 192, 12, 12]          --
├─Sequential: 1-3                        [1, 480, 6, 6]            --
│    └─Inception: 2-9                    [1, 256, 12, 12]          --
│    │    └─Conv2d: 3-1                  [1, 64, 12, 12]           12,352
│    │    └─Conv2d: 3-2                  [1, 96, 12, 12]           18,528
│    │    └─Conv2d: 3-3                  [1, 128, 12, 12]          110,720
│    │    └─Conv2d: 3-4                  [1, 16, 12, 12]           3,088
│    │    └─Conv2d: 3-5                  [1, 32, 12, 12]           12,832
│    │    └─MaxPool2d: 3-6               [1, 192, 12, 12]          --
│    │    └─Conv2d: 3-7                  [1, 32, 12, 12]           6,176
│    └─Inception: 2-10                   [1, 480, 12, 12]          --
│    │    └─Conv2d: 3-8                  [1, 128, 12, 12]          32,896
│    │    └─Conv2d: 3-9                  [1, 128, 12, 12]          32,896
│    │    └─Conv2d: 3-10                 [1, 192, 12, 12]          221,376
│    │    └─Conv2d: 3-11                 [1, 32, 12, 12]           8,224
│    │    └─Conv2d: 3-12                 [1, 96, 12, 12]           76,896
│    │    └─MaxPool2d: 3-13              [1, 256, 12, 12]          --
│    │    └─Conv2d: 3-14                 [1, 64, 12, 12]           16,448
│    └─MaxPool2d: 2-11                   [1, 480, 6, 6]            --
├─Sequential: 1-4                        [1, 832, 3, 3]            --
│    └─Inception: 2-12                   [1, 512, 6, 6]            --
│    │    └─Conv2d: 3-15                 [1, 192, 6, 6]            92,352
│    │    └─Conv2d: 3-16                 [1, 96, 6, 6]             46,176
│    │    └─Conv2d: 3-17                 [1, 208, 6, 6]            179,920
│    │    └─Conv2d: 3-18                 [1, 16, 6, 6]             7,696
│    │    └─Conv2d: 3-19                 [1, 48, 6, 6]             19,248
│    │    └─MaxPool2d: 3-20              [1, 480, 6, 6]            --
│    │    └─Conv2d: 3-21                 [1, 64, 6, 6]             30,784
│    └─Inception: 2-13                   [1, 512, 6, 6]            --
│    │    └─Conv2d: 3-22                 [1, 160, 6, 6]            82,080
│    │    └─Conv2d: 3-23                 [1, 112, 6, 6]            57,456
│    │    └─Conv2d: 3-24                 [1, 224, 6, 6]            226,016
│    │    └─Conv2d: 3-25                 [1, 24, 6, 6]             12,312
│    │    └─Conv2d: 3-26                 [1, 64, 6, 6]             38,464
│    │    └─MaxPool2d: 3-27              [1, 512, 6, 6]            --
│    │    └─Conv2d: 3-28                 [1, 64, 6, 6]             32,832
│    └─Inception: 2-14                   [1, 512, 6, 6]            --
│    │    └─Conv2d: 3-29                 [1, 128, 6, 6]            65,664
│    │    └─Conv2d: 3-30                 [1, 128, 6, 6]            65,664
│    │    └─Conv2d: 3-31                 [1, 256, 6, 6]            295,168
│    │    └─Conv2d: 3-32                 [1, 24, 6, 6]             12,312
│    │    └─Conv2d: 3-33                 [1, 64, 6, 6]             38,464
│    │    └─MaxPool2d: 3-34              [1, 512, 6, 6]            --
│    │    └─Conv2d: 3-35                 [1, 64, 6, 6]             32,832
│    └─Inception: 2-15                   [1, 528, 6, 6]            --
│    │    └─Conv2d: 3-36                 [1, 112, 6, 6]            57,456
│    │    └─Conv2d: 3-37                 [1, 144, 6, 6]            73,872
│    │    └─Conv2d: 3-38                 [1, 288, 6, 6]            373,536
│    │    └─Conv2d: 3-39                 [1, 32, 6, 6]             16,416
│    │    └─Conv2d: 3-40                 [1, 64, 6, 6]             51,264
│    │    └─MaxPool2d: 3-41              [1, 512, 6, 6]            --
│    │    └─Conv2d: 3-42                 [1, 64, 6, 6]             32,832
│    └─Inception: 2-16                   [1, 832, 6, 6]            --
│    │    └─Conv2d: 3-43                 [1, 256, 6, 6]            135,424
│    │    └─Conv2d: 3-44                 [1, 160, 6, 6]            84,640
│    │    └─Conv2d: 3-45                 [1, 320, 6, 6]            461,120
│    │    └─Conv2d: 3-46                 [1, 32, 6, 6]             16,928
│    │    └─Conv2d: 3-47                 [1, 128, 6, 6]            102,528
│    │    └─MaxPool2d: 3-48              [1, 528, 6, 6]            --
│    │    └─Conv2d: 3-49                 [1, 128, 6, 6]            67,712
│    └─MaxPool2d: 2-17                   [1, 832, 3, 3]            --
├─Sequential: 1-5                        [1, 1024]                 --
│    └─Inception: 2-18                   [1, 832, 3, 3]            --
│    │    └─Conv2d: 3-50                 [1, 256, 3, 3]            213,248
│    │    └─Conv2d: 3-51                 [1, 160, 3, 3]            133,280
│    │    └─Conv2d: 3-52                 [1, 320, 3, 3]            461,120
│    │    └─Conv2d: 3-53                 [1, 32, 3, 3]             26,656
│    │    └─Conv2d: 3-54                 [1, 128, 3, 3]            102,528
│    │    └─MaxPool2d: 3-55              [1, 832, 3, 3]            --
│    │    └─Conv2d: 3-56                 [1, 128, 3, 3]            106,624
│    └─Inception: 2-19                   [1, 1024, 3, 3]           --
│    │    └─Conv2d: 3-57                 [1, 384, 3, 3]            319,872
│    │    └─Conv2d: 3-58                 [1, 192, 3, 3]            159,936
│    │    └─Conv2d: 3-59                 [1, 384, 3, 3]            663,936
│    │    └─Conv2d: 3-60                 [1, 48, 3, 3]             39,984
│    │    └─Conv2d: 3-61                 [1, 128, 3, 3]            153,728
│    │    └─MaxPool2d: 3-62              [1, 832, 3, 3]            --
│    │    └─Conv2d: 3-63                 [1, 128, 3, 3]            106,624
│    └─AdaptiveAvgPool2d: 2-20           [1, 1024, 1, 1]           --
│    └─Flatten: 2-21                     [1, 1024]                 --
├─Linear: 1-6                            [1, 10]                   10,250
==========================================================================================
Total params: 5,977,530
Trainable params: 5,977,530
Non-trainable params: 0
Total mult-adds (Units.MEGABYTES): 276.66
==========================================================================================
Input size (MB): 0.04
Forward/backward pass size (MB): 4.74
Params size (MB): 23.91
Estimated Total Size (MB): 28.69
==========================================================================================
In [67]:
lr, num_epochs, batch_size = 0.1, 10, 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=96)
#d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
In [68]:
class Residual(nn.Module):
    def __init__(self,input_channels,num_channels,use_1x1conv=False,strides=1):
        super().__init__()
        self.conv1 = nn.Conv2d(input_channels,num_channels,kernel_size=3,padding=1,stride=strides)
        self.conv2 = nn.Conv2d(num_channels,num_channels,kernel_size=3,padding=1)
        if use_1x1conv:
            self.conv3 = nn.Conv2d(input_channels,num_channels,kernel_size=1,stride=strides)
        else:
            self.conv3= None
        self.bn1=nn.BatchNorm2d(num_channels)
        self.bn2=nn.BatchNorm2d(num_channels)
    def forward(self,X):
        Y=F.relu(self.bn1(self.conv1(X)))
        Y=self.bn2(self.conv2(Y))
        if self.conv3:
            X = self.conv3(X)
        Y+=X
        return F.relu(Y)
In [69]:
blk = Residual(3,3)
X = torch.rand(4, 3, 6, 6)
In [70]:
blk = Residual(3,6, use_1x1conv=True, strides=2)
blk(X).shape
Out [70]:
torch.Size([4, 6, 3, 3])
In [71]:
b1 = nn.Sequential(nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3),
nn.BatchNorm2d(64), nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
In [72]:
def resnet_block(input_channels, num_channels, num_residuals,
                first_block=False):
    blk = []
    for i in range(num_residuals):
        if i == 0 and not first_block:
            blk.append(Residual(input_channels, num_channels,
                        use_1x1conv=True, strides=2))
        else:
            blk.append(Residual(num_channels, num_channels))
    return blk
In [73]:
b2 = nn.Sequential(*resnet_block(64, 64, 2, first_block=True))
b3 = nn.Sequential(*resnet_block(64, 128, 2))
b4 = nn.Sequential(*resnet_block(128, 256, 2))
b5 = nn.Sequential(*resnet_block(256, 512, 2))
In [74]:
net = nn.Sequential(b1, b2, b3, b4, b5,
nn.AdaptiveAvgPool2d((1,1)),
nn.Flatten(), nn.Linear(512, 10))
In [75]:
X = torch.rand(size=(1, 1, 224, 224))
for layer in net:
    X = layer(X)
    print(layer.__class__.__name__,'output shape:\t', X.shape)
Sequential output shape:	 torch.Size([1, 64, 56, 56])
Sequential output shape:	 torch.Size([1, 64, 56, 56])
Sequential output shape:	 torch.Size([1, 128, 28, 28])
Sequential output shape:	 torch.Size([1, 256, 14, 14])
Sequential output shape:	 torch.Size([1, 512, 7, 7])
AdaptiveAvgPool2d output shape:	 torch.Size([1, 512, 1, 1])
Flatten output shape:	 torch.Size([1, 512])
Linear output shape:	 torch.Size([1, 10])
In [76]:
lr, num_epochs, batch_size = 0.05, 10, 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=96)
#d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
In [77]:
import torch
import d2l.torch as d2l
import numpy
import torch.nn as nn
import torch.nn.functional as F
print(torch.version.__version__)
2.10.0+cu128
In [78]:
A=torch.Tensor([[1,2,0,0],[0,2,0,0],[0,0,2,1],[0,0,0,3]])
C=torch.Tensor([[1,0,0,0],[0,1,0,0],[0,0,-2,3],[0,0,0,-3]])
In [79]:
B=torch.Tensor([[2,0,0,0],[-2,1,0,0],[0,0,-3,0],[0,0,0,-3]])
In [80]:
torch.mm(A,C)
Out [80]:
tensor([[ 1.,  2.,  0.,  0.],
        [ 0.,  2.,  0.,  0.],
        [ 0.,  0., -4.,  3.],
        [ 0.,  0.,  0., -9.]])
In [81]:
torch.det(torch.mm(torch.mm(A,C),B))
Out [81]:
tensor(1296.)
In [82]:
1296**5
Out [82]:
3656158440062976
In [83]:
torch.mm(C,B)
Out [83]:
tensor([[ 2.,  0.,  0.,  0.],
        [-2.,  1.,  0.,  0.],
        [ 0.,  0.,  6., -9.],
        [ 0.,  0.,  0.,  9.]])
In [84]:
T = 1000 # 总共产生1000个点
time = torch.arange(1, T + 1, dtype=torch.float32)
x = torch.sin(0.01 * time) + torch.normal(0, 0.2, (T,))
d2l.plot(time, [x], 'time', 'x', xlim=[1, 1000], figsize=(6, 3))
In [85]:
tau = 4
features = torch.zeros((T - tau, tau))
for i in range(tau):
    features[:, i] = x[i: T - tau + i]
labels = x[tau:].reshape((-1, 1))
x,features,labels
Out [85]:
(tensor([-0.0948,  0.2143, -0.2523, -0.1235, -0.1826,  0.1189, -0.1963,  0.2347,
          0.1456, -0.1118,  0.3787,  0.3861,  0.2881,  0.1958,  0.0402,  0.0816,
          0.4793,  0.0351,  0.2378,  0.1459,  0.1108,  0.2544, -0.0127,  0.0733,
          0.3156,  0.0257,  0.3207,  0.3259,  0.3693,  0.0584,  0.1730,  0.3100,
          0.2328,  0.0525,  0.4465,  0.1293,  0.4330,  0.3193,  0.4704,  0.5238,
          0.5323,  0.4887,  0.0831,  0.5924,  0.6972,  0.3490,  0.7476,  0.6039,
          0.9995,  0.1455,  0.1417,  0.5968,  0.6673,  0.3425,  0.7685,  0.4904,
          0.2203,  0.2109,  0.4600,  0.5055,  0.3558,  0.7020,  0.7435,  0.4713,
          0.4318,  0.5861,  0.3592,  0.7750,  0.6640,  0.7908,  0.2776,  0.5868,
          0.6283,  0.3461,  0.6308,  0.7547,  0.5564,  0.7181,  0.7852,  0.7823,
          0.7238,  0.9294,  0.9023,  0.8100,  0.5561,  0.7124,  1.1566,  0.7628,
          0.9630,  0.4425,  1.0628,  0.7014,  0.4439,  0.7286,  0.8099,  0.5786,
          1.0638,  0.9519,  0.8388,  1.2088,  0.9172,  0.7014,  0.5667,  0.6040,
          0.5549,  0.7959,  0.9167,  0.9074,  0.6108,  0.8999,  0.9197,  0.8539,
          0.6566,  0.9941,  0.6902,  0.8782,  1.4898,  0.9888,  1.1911,  0.5683,
          0.8868,  0.7122,  0.8960,  1.1454,  1.2660,  1.0001,  0.6582,  0.9706,
          1.0110,  1.0355,  0.9761,  0.9439,  1.0824,  1.4095,  1.2544,  0.6541,
          1.0486,  1.0638,  0.9000,  0.9835,  1.2558,  1.1702,  0.8466,  0.7696,
          1.2446,  1.1460,  0.9258,  0.8150,  1.1086,  0.9475,  0.9675,  0.7330,
          1.1263,  1.1718,  0.9413,  1.0272,  0.7733,  0.9831,  0.8759,  0.7970,
          0.6360,  1.1815,  0.9689,  0.6976,  0.9265,  0.8338,  0.7960,  0.7705,
          1.2601,  1.2775,  0.7706,  1.0216,  1.1916,  0.8603,  0.9864,  1.0777,
          0.8930,  1.0063,  0.8376,  0.9923,  0.8081,  0.8020,  1.1461,  1.1018,
          0.8931,  1.0005,  0.8635,  0.7197,  1.2577,  1.0584,  1.4032,  0.8911,
          1.1415,  0.8241,  0.7946,  1.0221,  0.8792,  0.7211,  1.1821,  0.8079,
          0.8926,  1.0765,  0.9949,  0.9159,  0.7329,  0.9950,  0.7491,  0.8750,
          1.1863,  1.0095,  0.8046,  0.6274,  0.8936,  0.7595,  0.8423,  0.8655,
          0.6918,  0.7347,  1.1179,  0.5931,  0.8745,  0.4858,  0.9338,  1.1382,
          0.6084,  0.9479,  0.8726,  0.7202,  0.9596,  0.4386,  1.2525,  0.5120,
          0.7222,  0.6566,  0.8965,  0.7545,  1.1104,  0.6634,  0.5654,  1.0095,
          0.6558,  0.7260,  0.8515,  0.3430,  0.7703,  0.3753,  0.4490,  0.4373,
          0.8283,  0.5455,  0.7584,  0.8197,  0.4781,  0.3350,  0.6714,  0.3969,
          0.7131,  0.5609,  0.4327,  0.4293,  0.3552,  0.5445,  0.5609,  0.4110,
          0.8525,  0.3402,  0.4064,  0.5172,  0.5845,  0.6185,  0.4719,  0.9092,
          0.6964,  0.7267,  0.6934,  0.4337,  0.2031,  0.0898,  0.4377,  0.4203,
          0.2855,  0.4673,  0.6029,  0.4368,  0.1521, -0.0606,  0.2532,  0.4365,
          0.2989,  0.0743,  0.2734,  0.1060,  0.5543, -0.1211,  0.0968,  0.4911,
          0.5107,  0.4583,  0.2777, -0.0513,  0.1437,  0.0548,  0.0933,  0.1172,
          0.0718,  0.4027,  0.1805,  0.0869, -0.3066,  0.5615, -0.2721, -0.2765,
          0.0850, -0.1473, -0.1622, -0.1335,  0.1328,  0.0703, -0.6712, -0.1121,
         -0.1208,  0.0092, -0.0805, -0.2017,  0.2339, -0.3533, -0.4598,  0.0620,
         -0.5254,  0.0197, -0.0593, -0.1914, -0.4259, -0.0115, -0.5406,  0.0137,
         -0.4240, -0.2822, -0.0796, -0.3495, -0.4475,  0.2453, -0.3729, -0.4086,
         -0.2618, -0.4539, -0.6140, -0.2483, -0.4165, -0.3736, -0.0737, -0.0212,
         -0.3644, -0.0472, -0.4087, -0.6794, -0.5921, -0.5632, -0.5971, -0.1935,
         -0.9543, -0.7976, -0.3485, -0.9538, -0.6171, -0.7755, -0.4651, -0.8194,
         -0.3005, -0.5191, -0.5902, -0.2464, -0.6908, -0.5054, -0.5528, -1.1089,
         -0.7206, -0.8067, -0.6780, -0.2981, -0.6683, -0.4324, -0.8497, -0.5928,
         -0.7203, -0.3751, -0.7423, -0.4109, -0.7345, -0.6653, -0.5752, -0.5198,
         -0.7046, -1.1754, -0.9447, -0.7304, -0.6510, -0.5954, -0.7592, -0.5285,
         -0.4249, -0.7993, -1.3758, -0.6218, -1.0691, -0.5775, -0.8174, -0.7021,
         -0.7784, -0.7553, -1.2137, -0.7302, -0.7253, -0.6819, -1.3077, -1.3472,
         -0.7104, -0.8387, -0.5973, -0.8619, -1.1138, -1.1314, -0.9765, -1.2121,
         -0.8168, -0.7763, -1.2988, -0.9282, -1.1715, -0.7216, -0.7182, -0.2972,
         -0.7471, -1.0089, -1.1431, -1.0396, -1.0381, -0.5979, -0.7363, -0.7808,
         -0.9106, -1.1468, -1.1357, -0.6406, -0.9603, -1.2653, -1.5958, -1.0592,
         -0.9698, -0.8252, -1.2515, -1.0474, -1.1103, -1.0035, -0.6669, -0.9120,
         -0.9146, -1.1079, -0.8379, -0.9123, -0.5831, -1.6515, -0.9385, -1.0699,
         -1.1498, -0.7861, -0.8942, -1.0452, -1.0064, -0.9116, -1.1150, -0.7801,
         -1.0283, -1.0296, -1.0927, -0.7945, -1.0705, -1.3215, -1.2510, -0.9158,
         -0.9377, -0.7314, -0.9773, -1.1910, -1.0539, -1.1439, -1.0784, -0.8543,
         -1.1323, -1.3193, -0.8014, -0.7318, -0.5805, -0.8239, -1.1228, -1.0473,
         -0.8206, -0.6544, -1.2654, -1.0757, -0.5389, -0.9908, -0.7894, -0.7463,
         -1.0391, -0.8023, -0.8568, -1.2414, -0.9595, -1.1151, -0.9689, -1.1145,
         -0.6853, -0.7547, -1.1000, -0.9054, -1.2262, -1.1359, -1.0174, -0.3782,
         -0.8056, -1.1828, -0.8426, -0.9958, -0.9495, -1.2745, -0.7039, -0.5893,
         -0.5648, -1.0538, -0.6724, -0.6340, -0.5070, -1.0956, -1.0957, -0.6823,
         -0.5258, -0.5777, -0.9268, -0.5280, -0.5989, -0.8364, -0.7439, -0.7619,
         -1.0159, -1.0627, -0.9416, -0.6270, -0.4307, -0.8575, -1.0748, -0.5529,
         -0.9339, -0.7416, -0.6674, -0.3178, -0.6815, -0.7499, -0.6359, -0.8157,
         -0.5582, -0.5083, -0.4527, -0.8350, -0.6317, -0.4338, -0.4875, -0.4046,
         -0.3166, -0.3413, -0.4722, -0.7010, -1.2025, -0.2133, -0.3133, -0.4160,
         -0.6681, -0.8990, -0.5464, -0.4518, -0.4402, -0.5246, -0.4561, -0.6747,
         -0.1833, -0.4466, -0.4671, -0.5509, -0.6235, -0.2100, -0.3368, -0.3083,
         -0.5129, -0.2880, -0.4075, -0.2784,  0.0631, -0.4355, -0.4237, -0.2578,
         -0.1380, -0.5085,  0.1004, -0.1426, -0.2537, -0.1756, -0.2135, -0.1898,
         -0.2947, -0.3934, -0.3412, -0.3343, -0.1450, -0.3178, -0.2156, -0.3232,
         -0.3691, -0.2711,  0.1086, -0.2257, -0.0752, -0.0339, -0.0636,  0.0626,
         -0.1460,  0.0792,  0.1529,  0.4743,  0.0343, -0.0158, -0.1255, -0.4698,
         -0.0489,  0.2622,  0.0619, -0.2243, -0.1318,  0.0214,  0.2690,  0.0497,
          0.3451, -0.1116,  0.0173,  0.0708,  0.4135,  0.3188,  0.4808, -0.0340,
          0.4786,  0.4896,  0.1077,  0.3500,  0.1309,  0.1398,  0.1943,  0.1651,
          0.3227,  0.5541,  0.2688,  0.1892,  0.2509,  0.2078, -0.0140,  0.2443,
          0.3204,  0.5485,  0.4234,  0.3135,  0.4633,  0.0029,  0.2174,  0.6879,
          0.5089,  0.2479,  0.8608,  0.4307,  0.6205,  0.3482,  0.6469,  0.4475,
          0.6595,  0.3450,  0.3781,  0.4451,  0.1883,  0.6707,  0.8667,  0.5218,
          0.4004,  0.5271,  0.6446,  0.7222,  0.5722,  0.7676,  0.6824,  0.1981,
          0.8089,  0.6296,  0.6748,  0.7515,  0.5103,  0.9052,  0.8405,  0.9092,
          0.6918,  0.6477,  0.5402,  0.6477,  0.4210,  0.6973,  0.6019,  0.5364,
          0.8134,  0.5607,  0.7096,  0.5894,  0.3866,  1.0600,  0.7347,  0.8129,
          1.2088,  0.8825,  0.7179,  1.0115,  0.7013,  1.0128,  0.9747,  1.2759,
          0.7655,  1.0094,  0.7805,  0.6091,  1.2033,  0.9678,  0.8219,  0.8157,
          0.9188,  0.7436,  0.8910,  0.7291,  0.9559,  0.9389,  1.2030,  1.0495,
          1.1811,  0.8884,  0.8390,  0.9894,  0.9238,  0.7628,  0.5421,  1.5147,
          0.6971,  0.6740,  0.8342,  0.6554,  0.7455,  0.6916,  1.2706,  1.1277,
          0.9248,  0.9976,  1.2404,  0.6919,  1.3449,  1.1243,  1.0492,  0.9266,
          1.1194,  1.0304,  1.1323,  1.2372,  0.8300,  1.1916,  1.0923,  0.8313,
          0.8572,  1.1128,  1.0047,  1.1544,  0.9745,  1.0503,  0.9171,  0.8073,
          1.2056,  1.0976,  0.9910,  1.1834,  1.1389,  0.9142,  0.9367,  1.0121,
          0.7704,  1.0558,  0.7306,  0.8117,  0.7061,  1.2315,  0.9015,  0.9339,
          0.5016,  0.9227,  1.2568,  0.9444,  1.1198,  0.9431,  1.0997,  1.3078,
          0.8336,  1.2692,  0.8424,  0.8702,  1.4820,  1.3248,  0.9324,  0.6538,
          1.2011,  1.0170,  0.7863,  1.0178,  0.6519,  0.5970,  0.9052,  0.6846,
          0.7737,  0.9104,  0.8439,  1.0066,  1.0787,  0.9661,  0.9923,  0.7922,
          0.8316,  0.9553,  0.9952,  0.8680,  1.1226,  0.8213,  0.9151,  0.7748,
          0.9953,  0.7773,  0.7916,  0.7321,  0.9130,  1.1433,  0.7060,  0.8066,
          0.8709,  0.7426,  0.8718,  1.0973,  0.7097,  0.9438,  0.8164,  0.8013,
          0.6236,  0.7180,  0.9188,  0.8016,  0.9741,  0.6271,  0.5747,  0.8007,
          0.7754,  0.4877,  0.4746,  0.8654,  0.4743,  0.9015,  0.8082,  0.5449,
          0.9299,  0.2003,  0.5466,  0.4355,  0.7900,  0.4343,  0.7224,  0.8585,
          0.5714,  0.5306,  0.6594,  0.0640,  0.3203,  0.5463,  0.5048,  0.1935,
          0.2883,  0.6778,  0.5014,  0.5235,  0.5718,  0.4587,  0.2808,  0.4073,
          0.8632,  0.8862,  0.5757,  0.3372,  0.2566,  0.7858,  0.3713,  0.1589,
          0.3243,  0.4270,  0.0565,  0.2885,  0.3257,  0.2196,  0.3159,  0.2361,
          0.1087,  0.2224,  0.2633,  0.5037,  0.1980,  0.1530,  0.2780, -0.1399,
          0.5331,  0.3530,  0.3342,  0.2098, -0.0165,  0.1318,  0.4510, -0.1959,
          0.0966,  0.0789,  0.3381, -0.1917,  0.1518,  0.3640,  0.0956,  0.2535,
         -0.3988, -0.3479,  0.3864, -0.2639, -0.2368,  0.0258,  0.2441,  0.0687,
          0.0457,  0.2286, -0.0947, -0.1189,  0.1360, -0.0990, -0.2447,  0.2135,
         -0.1830, -0.4583, -0.1795, -0.1361, -0.0553, -0.2864, -0.2307, -0.4651,
         -0.1889, -0.3185, -0.5318, -0.3012,  0.0062,  0.1046, -0.2321, -0.2945,
         -0.0242, -0.0586, -0.2307, -0.2479, -0.0382, -0.1509, -0.5055, -0.3759,
          0.2139, -0.2129, -0.3605, -0.5222, -0.6530, -0.6716, -0.4330, -0.2577,
         -0.2672, -0.1297, -0.9203, -0.5832, -0.2640, -0.4996, -0.2625, -0.4407,
         -0.8864, -0.2508, -0.4827, -0.3131, -0.2570, -0.7116, -0.5357, -0.7074]),
 tensor([[-0.0948,  0.2143, -0.2523, -0.1235],
         [ 0.2143, -0.2523, -0.1235, -0.1826],
         [-0.2523, -0.1235, -0.1826,  0.1189],
         ...,
         [-0.2508, -0.4827, -0.3131, -0.2570],
         [-0.4827, -0.3131, -0.2570, -0.7116],
         [-0.3131, -0.2570, -0.7116, -0.5357]]),
 tensor([[-0.1826],
         [ 0.1189],
         [-0.1963],
         [ 0.2347],
         [ 0.1456],
         [-0.1118],
         [ 0.3787],
         [ 0.3861],
         [ 0.2881],
         [ 0.1958],
         [ 0.0402],
         [ 0.0816],
         [ 0.4793],
         [ 0.0351],
         [ 0.2378],
         [ 0.1459],
         [ 0.1108],
         [ 0.2544],
         [-0.0127],
         [ 0.0733],
         [ 0.3156],
         [ 0.0257],
         [ 0.3207],
         [ 0.3259],
         [ 0.3693],
         [ 0.0584],
         [ 0.1730],
         [ 0.3100],
         [ 0.2328],
         [ 0.0525],
         [ 0.4465],
         [ 0.1293],
         [ 0.4330],
         [ 0.3193],
         [ 0.4704],
         [ 0.5238],
         [ 0.5323],
         [ 0.4887],
         [ 0.0831],
         [ 0.5924],
         [ 0.6972],
         [ 0.3490],
         [ 0.7476],
         [ 0.6039],
         [ 0.9995],
         [ 0.1455],
         [ 0.1417],
         [ 0.5968],
         [ 0.6673],
         [ 0.3425],
         [ 0.7685],
         [ 0.4904],
         [ 0.2203],
         [ 0.2109],
         [ 0.4600],
         [ 0.5055],
         [ 0.3558],
         [ 0.7020],
         [ 0.7435],
         [ 0.4713],
         [ 0.4318],
         [ 0.5861],
         [ 0.3592],
         [ 0.7750],
         [ 0.6640],
         [ 0.7908],
         [ 0.2776],
         [ 0.5868],
         [ 0.6283],
         [ 0.3461],
         [ 0.6308],
         [ 0.7547],
         [ 0.5564],
         [ 0.7181],
         [ 0.7852],
         [ 0.7823],
         [ 0.7238],
         [ 0.9294],
         [ 0.9023],
         [ 0.8100],
         [ 0.5561],
         [ 0.7124],
         [ 1.1566],
         [ 0.7628],
         [ 0.9630],
         [ 0.4425],
         [ 1.0628],
         [ 0.7014],
         [ 0.4439],
         [ 0.7286],
         [ 0.8099],
         [ 0.5786],
         [ 1.0638],
         [ 0.9519],
         [ 0.8388],
         [ 1.2088],
         [ 0.9172],
         [ 0.7014],
         [ 0.5667],
         [ 0.6040],
         [ 0.5549],
         [ 0.7959],
         [ 0.9167],
         [ 0.9074],
         [ 0.6108],
         [ 0.8999],
         [ 0.9197],
         [ 0.8539],
         [ 0.6566],
         [ 0.9941],
         [ 0.6902],
         [ 0.8782],
         [ 1.4898],
         [ 0.9888],
         [ 1.1911],
         [ 0.5683],
         [ 0.8868],
         [ 0.7122],
         [ 0.8960],
         [ 1.1454],
         [ 1.2660],
         [ 1.0001],
         [ 0.6582],
         [ 0.9706],
         [ 1.0110],
         [ 1.0355],
         [ 0.9761],
         [ 0.9439],
         [ 1.0824],
         [ 1.4095],
         [ 1.2544],
         [ 0.6541],
         [ 1.0486],
         [ 1.0638],
         [ 0.9000],
         [ 0.9835],
         [ 1.2558],
         [ 1.1702],
         [ 0.8466],
         [ 0.7696],
         [ 1.2446],
         [ 1.1460],
         [ 0.9258],
         [ 0.8150],
         [ 1.1086],
         [ 0.9475],
         [ 0.9675],
         [ 0.7330],
         [ 1.1263],
         [ 1.1718],
         [ 0.9413],
         [ 1.0272],
         [ 0.7733],
         [ 0.9831],
         [ 0.8759],
         [ 0.7970],
         [ 0.6360],
         [ 1.1815],
         [ 0.9689],
         [ 0.6976],
         [ 0.9265],
         [ 0.8338],
         [ 0.7960],
         [ 0.7705],
         [ 1.2601],
         [ 1.2775],
         [ 0.7706],
         [ 1.0216],
         [ 1.1916],
         [ 0.8603],
         [ 0.9864],
         [ 1.0777],
         [ 0.8930],
         [ 1.0063],
         [ 0.8376],
         [ 0.9923],
         [ 0.8081],
         [ 0.8020],
         [ 1.1461],
         [ 1.1018],
         [ 0.8931],
         [ 1.0005],
         [ 0.8635],
         [ 0.7197],
         [ 1.2577],
         [ 1.0584],
         [ 1.4032],
         [ 0.8911],
         [ 1.1415],
         [ 0.8241],
         [ 0.7946],
         [ 1.0221],
         [ 0.8792],
         [ 0.7211],
         [ 1.1821],
         [ 0.8079],
         [ 0.8926],
         [ 1.0765],
         [ 0.9949],
         [ 0.9159],
         [ 0.7329],
         [ 0.9950],
         [ 0.7491],
         [ 0.8750],
         [ 1.1863],
         [ 1.0095],
         [ 0.8046],
         [ 0.6274],
         [ 0.8936],
         [ 0.7595],
         [ 0.8423],
         [ 0.8655],
         [ 0.6918],
         [ 0.7347],
         [ 1.1179],
         [ 0.5931],
         [ 0.8745],
         [ 0.4858],
         [ 0.9338],
         [ 1.1382],
         [ 0.6084],
         [ 0.9479],
         [ 0.8726],
         [ 0.7202],
         [ 0.9596],
         [ 0.4386],
         [ 1.2525],
         [ 0.5120],
         [ 0.7222],
         [ 0.6566],
         [ 0.8965],
         [ 0.7545],
         [ 1.1104],
         [ 0.6634],
         [ 0.5654],
         [ 1.0095],
         [ 0.6558],
         [ 0.7260],
         [ 0.8515],
         [ 0.3430],
         [ 0.7703],
         [ 0.3753],
         [ 0.4490],
         [ 0.4373],
         [ 0.8283],
         [ 0.5455],
         [ 0.7584],
         [ 0.8197],
         [ 0.4781],
         [ 0.3350],
         [ 0.6714],
         [ 0.3969],
         [ 0.7131],
         [ 0.5609],
         [ 0.4327],
         [ 0.4293],
         [ 0.3552],
         [ 0.5445],
         [ 0.5609],
         [ 0.4110],
         [ 0.8525],
         [ 0.3402],
         [ 0.4064],
         [ 0.5172],
         [ 0.5845],
         [ 0.6185],
         [ 0.4719],
         [ 0.9092],
         [ 0.6964],
         [ 0.7267],
         [ 0.6934],
         [ 0.4337],
         [ 0.2031],
         [ 0.0898],
         [ 0.4377],
         [ 0.4203],
         [ 0.2855],
         [ 0.4673],
         [ 0.6029],
         [ 0.4368],
         [ 0.1521],
         [-0.0606],
         [ 0.2532],
         [ 0.4365],
         [ 0.2989],
         [ 0.0743],
         [ 0.2734],
         [ 0.1060],
         [ 0.5543],
         [-0.1211],
         [ 0.0968],
         [ 0.4911],
         [ 0.5107],
         [ 0.4583],
         [ 0.2777],
         [-0.0513],
         [ 0.1437],
         [ 0.0548],
         [ 0.0933],
         [ 0.1172],
         [ 0.0718],
         [ 0.4027],
         [ 0.1805],
         [ 0.0869],
         [-0.3066],
         [ 0.5615],
         [-0.2721],
         [-0.2765],
         [ 0.0850],
         [-0.1473],
         [-0.1622],
         [-0.1335],
         [ 0.1328],
         [ 0.0703],
         [-0.6712],
         [-0.1121],
         [-0.1208],
         [ 0.0092],
         [-0.0805],
         [-0.2017],
         [ 0.2339],
         [-0.3533],
         [-0.4598],
         [ 0.0620],
         [-0.5254],
         [ 0.0197],
         [-0.0593],
         [-0.1914],
         [-0.4259],
         [-0.0115],
         [-0.5406],
         [ 0.0137],
         [-0.4240],
         [-0.2822],
         [-0.0796],
         [-0.3495],
         [-0.4475],
         [ 0.2453],
         [-0.3729],
         [-0.4086],
         [-0.2618],
         [-0.4539],
         [-0.6140],
         [-0.2483],
         [-0.4165],
         [-0.3736],
         [-0.0737],
         [-0.0212],
         [-0.3644],
         [-0.0472],
         [-0.4087],
         [-0.6794],
         [-0.5921],
         [-0.5632],
         [-0.5971],
         [-0.1935],
         [-0.9543],
         [-0.7976],
         [-0.3485],
         [-0.9538],
         [-0.6171],
         [-0.7755],
         [-0.4651],
         [-0.8194],
         [-0.3005],
         [-0.5191],
         [-0.5902],
         [-0.2464],
         [-0.6908],
         [-0.5054],
         [-0.5528],
         [-1.1089],
         [-0.7206],
         [-0.8067],
         [-0.6780],
         [-0.2981],
         [-0.6683],
         [-0.4324],
         [-0.8497],
         [-0.5928],
         [-0.7203],
         [-0.3751],
         [-0.7423],
         [-0.4109],
         [-0.7345],
         [-0.6653],
         [-0.5752],
         [-0.5198],
         [-0.7046],
         [-1.1754],
         [-0.9447],
         [-0.7304],
         [-0.6510],
         [-0.5954],
         [-0.7592],
         [-0.5285],
         [-0.4249],
         [-0.7993],
         [-1.3758],
         [-0.6218],
         [-1.0691],
         [-0.5775],
         [-0.8174],
         [-0.7021],
         [-0.7784],
         [-0.7553],
         [-1.2137],
         [-0.7302],
         [-0.7253],
         [-0.6819],
         [-1.3077],
         [-1.3472],
         [-0.7104],
         [-0.8387],
         [-0.5973],
         [-0.8619],
         [-1.1138],
         [-1.1314],
         [-0.9765],
         [-1.2121],
         [-0.8168],
         [-0.7763],
         [-1.2988],
         [-0.9282],
         [-1.1715],
         [-0.7216],
         [-0.7182],
         [-0.2972],
         [-0.7471],
         [-1.0089],
         [-1.1431],
         [-1.0396],
         [-1.0381],
         [-0.5979],
         [-0.7363],
         [-0.7808],
         [-0.9106],
         [-1.1468],
         [-1.1357],
         [-0.6406],
         [-0.9603],
         [-1.2653],
         [-1.5958],
         [-1.0592],
         [-0.9698],
         [-0.8252],
         [-1.2515],
         [-1.0474],
         [-1.1103],
         [-1.0035],
         [-0.6669],
         [-0.9120],
         [-0.9146],
         [-1.1079],
         [-0.8379],
         [-0.9123],
         [-0.5831],
         [-1.6515],
         [-0.9385],
         [-1.0699],
         [-1.1498],
         [-0.7861],
         [-0.8942],
         [-1.0452],
         [-1.0064],
         [-0.9116],
         [-1.1150],
         [-0.7801],
         [-1.0283],
         [-1.0296],
         [-1.0927],
         [-0.7945],
         [-1.0705],
         [-1.3215],
         [-1.2510],
         [-0.9158],
         [-0.9377],
         [-0.7314],
         [-0.9773],
         [-1.1910],
         [-1.0539],
         [-1.1439],
         [-1.0784],
         [-0.8543],
         [-1.1323],
         [-1.3193],
         [-0.8014],
         [-0.7318],
         [-0.5805],
         [-0.8239],
         [-1.1228],
         [-1.0473],
         [-0.8206],
         [-0.6544],
         [-1.2654],
         [-1.0757],
         [-0.5389],
         [-0.9908],
         [-0.7894],
         [-0.7463],
         [-1.0391],
         [-0.8023],
         [-0.8568],
         [-1.2414],
         [-0.9595],
         [-1.1151],
         [-0.9689],
         [-1.1145],
         [-0.6853],
         [-0.7547],
         [-1.1000],
         [-0.9054],
         [-1.2262],
         [-1.1359],
         [-1.0174],
         [-0.3782],
         [-0.8056],
         [-1.1828],
         [-0.8426],
         [-0.9958],
         [-0.9495],
         [-1.2745],
         [-0.7039],
         [-0.5893],
         [-0.5648],
         [-1.0538],
         [-0.6724],
         [-0.6340],
         [-0.5070],
         [-1.0956],
         [-1.0957],
         [-0.6823],
         [-0.5258],
         [-0.5777],
         [-0.9268],
         [-0.5280],
         [-0.5989],
         [-0.8364],
         [-0.7439],
         [-0.7619],
         [-1.0159],
         [-1.0627],
         [-0.9416],
         [-0.6270],
         [-0.4307],
         [-0.8575],
         [-1.0748],
         [-0.5529],
         [-0.9339],
         [-0.7416],
         [-0.6674],
         [-0.3178],
         [-0.6815],
         [-0.7499],
         [-0.6359],
         [-0.8157],
         [-0.5582],
         [-0.5083],
         [-0.4527],
         [-0.8350],
         [-0.6317],
         [-0.4338],
         [-0.4875],
         [-0.4046],
         [-0.3166],
         [-0.3413],
         [-0.4722],
         [-0.7010],
         [-1.2025],
         [-0.2133],
         [-0.3133],
         [-0.4160],
         [-0.6681],
         [-0.8990],
         [-0.5464],
         [-0.4518],
         [-0.4402],
         [-0.5246],
         [-0.4561],
         [-0.6747],
         [-0.1833],
         [-0.4466],
         [-0.4671],
         [-0.5509],
         [-0.6235],
         [-0.2100],
         [-0.3368],
         [-0.3083],
         [-0.5129],
         [-0.2880],
         [-0.4075],
         [-0.2784],
         [ 0.0631],
         [-0.4355],
         [-0.4237],
         [-0.2578],
         [-0.1380],
         [-0.5085],
         [ 0.1004],
         [-0.1426],
         [-0.2537],
         [-0.1756],
         [-0.2135],
         [-0.1898],
         [-0.2947],
         [-0.3934],
         [-0.3412],
         [-0.3343],
         [-0.1450],
         [-0.3178],
         [-0.2156],
         [-0.3232],
         [-0.3691],
         [-0.2711],
         [ 0.1086],
         [-0.2257],
         [-0.0752],
         [-0.0339],
         [-0.0636],
         [ 0.0626],
         [-0.1460],
         [ 0.0792],
         [ 0.1529],
         [ 0.4743],
         [ 0.0343],
         [-0.0158],
         [-0.1255],
         [-0.4698],
         [-0.0489],
         [ 0.2622],
         [ 0.0619],
         [-0.2243],
         [-0.1318],
         [ 0.0214],
         [ 0.2690],
         [ 0.0497],
         [ 0.3451],
         [-0.1116],
         [ 0.0173],
         [ 0.0708],
         [ 0.4135],
         [ 0.3188],
         [ 0.4808],
         [-0.0340],
         [ 0.4786],
         [ 0.4896],
         [ 0.1077],
         [ 0.3500],
         [ 0.1309],
         [ 0.1398],
         [ 0.1943],
         [ 0.1651],
         [ 0.3227],
         [ 0.5541],
         [ 0.2688],
         [ 0.1892],
         [ 0.2509],
         [ 0.2078],
         [-0.0140],
         [ 0.2443],
         [ 0.3204],
         [ 0.5485],
         [ 0.4234],
         [ 0.3135],
         [ 0.4633],
         [ 0.0029],
         [ 0.2174],
         [ 0.6879],
         [ 0.5089],
         [ 0.2479],
         [ 0.8608],
         [ 0.4307],
         [ 0.6205],
         [ 0.3482],
         [ 0.6469],
         [ 0.4475],
         [ 0.6595],
         [ 0.3450],
         [ 0.3781],
         [ 0.4451],
         [ 0.1883],
         [ 0.6707],
         [ 0.8667],
         [ 0.5218],
         [ 0.4004],
         [ 0.5271],
         [ 0.6446],
         [ 0.7222],
         [ 0.5722],
         [ 0.7676],
         [ 0.6824],
         [ 0.1981],
         [ 0.8089],
         [ 0.6296],
         [ 0.6748],
         [ 0.7515],
         [ 0.5103],
         [ 0.9052],
         [ 0.8405],
         [ 0.9092],
         [ 0.6918],
         [ 0.6477],
         [ 0.5402],
         [ 0.6477],
         [ 0.4210],
         [ 0.6973],
         [ 0.6019],
         [ 0.5364],
         [ 0.8134],
         [ 0.5607],
         [ 0.7096],
         [ 0.5894],
         [ 0.3866],
         [ 1.0600],
         [ 0.7347],
         [ 0.8129],
         [ 1.2088],
         [ 0.8825],
         [ 0.7179],
         [ 1.0115],
         [ 0.7013],
         [ 1.0128],
         [ 0.9747],
         [ 1.2759],
         [ 0.7655],
         [ 1.0094],
         [ 0.7805],
         [ 0.6091],
         [ 1.2033],
         [ 0.9678],
         [ 0.8219],
         [ 0.8157],
         [ 0.9188],
         [ 0.7436],
         [ 0.8910],
         [ 0.7291],
         [ 0.9559],
         [ 0.9389],
         [ 1.2030],
         [ 1.0495],
         [ 1.1811],
         [ 0.8884],
         [ 0.8390],
         [ 0.9894],
         [ 0.9238],
         [ 0.7628],
         [ 0.5421],
         [ 1.5147],
         [ 0.6971],
         [ 0.6740],
         [ 0.8342],
         [ 0.6554],
         [ 0.7455],
         [ 0.6916],
         [ 1.2706],
         [ 1.1277],
         [ 0.9248],
         [ 0.9976],
         [ 1.2404],
         [ 0.6919],
         [ 1.3449],
         [ 1.1243],
         [ 1.0492],
         [ 0.9266],
         [ 1.1194],
         [ 1.0304],
         [ 1.1323],
         [ 1.2372],
         [ 0.8300],
         [ 1.1916],
         [ 1.0923],
         [ 0.8313],
         [ 0.8572],
         [ 1.1128],
         [ 1.0047],
         [ 1.1544],
         [ 0.9745],
         [ 1.0503],
         [ 0.9171],
         [ 0.8073],
         [ 1.2056],
         [ 1.0976],
         [ 0.9910],
         [ 1.1834],
         [ 1.1389],
         [ 0.9142],
         [ 0.9367],
         [ 1.0121],
         [ 0.7704],
         [ 1.0558],
         [ 0.7306],
         [ 0.8117],
         [ 0.7061],
         [ 1.2315],
         [ 0.9015],
         [ 0.9339],
         [ 0.5016],
         [ 0.9227],
         [ 1.2568],
         [ 0.9444],
         [ 1.1198],
         [ 0.9431],
         [ 1.0997],
         [ 1.3078],
         [ 0.8336],
         [ 1.2692],
         [ 0.8424],
         [ 0.8702],
         [ 1.4820],
         [ 1.3248],
         [ 0.9324],
         [ 0.6538],
         [ 1.2011],
         [ 1.0170],
         [ 0.7863],
         [ 1.0178],
         [ 0.6519],
         [ 0.5970],
         [ 0.9052],
         [ 0.6846],
         [ 0.7737],
         [ 0.9104],
         [ 0.8439],
         [ 1.0066],
         [ 1.0787],
         [ 0.9661],
         [ 0.9923],
         [ 0.7922],
         [ 0.8316],
         [ 0.9553],
         [ 0.9952],
         [ 0.8680],
         [ 1.1226],
         [ 0.8213],
         [ 0.9151],
         [ 0.7748],
         [ 0.9953],
         [ 0.7773],
         [ 0.7916],
         [ 0.7321],
         [ 0.9130],
         [ 1.1433],
         [ 0.7060],
         [ 0.8066],
         [ 0.8709],
         [ 0.7426],
         [ 0.8718],
         [ 1.0973],
         [ 0.7097],
         [ 0.9438],
         [ 0.8164],
         [ 0.8013],
         [ 0.6236],
         [ 0.7180],
         [ 0.9188],
         [ 0.8016],
         [ 0.9741],
         [ 0.6271],
         [ 0.5747],
         [ 0.8007],
         [ 0.7754],
         [ 0.4877],
         [ 0.4746],
         [ 0.8654],
         [ 0.4743],
         [ 0.9015],
         [ 0.8082],
         [ 0.5449],
         [ 0.9299],
         [ 0.2003],
         [ 0.5466],
         [ 0.4355],
         [ 0.7900],
         [ 0.4343],
         [ 0.7224],
         [ 0.8585],
         [ 0.5714],
         [ 0.5306],
         [ 0.6594],
         [ 0.0640],
         [ 0.3203],
         [ 0.5463],
         [ 0.5048],
         [ 0.1935],
         [ 0.2883],
         [ 0.6778],
         [ 0.5014],
         [ 0.5235],
         [ 0.5718],
         [ 0.4587],
         [ 0.2808],
         [ 0.4073],
         [ 0.8632],
         [ 0.8862],
         [ 0.5757],
         [ 0.3372],
         [ 0.2566],
         [ 0.7858],
         [ 0.3713],
         [ 0.1589],
         [ 0.3243],
         [ 0.4270],
         [ 0.0565],
         [ 0.2885],
         [ 0.3257],
         [ 0.2196],
         [ 0.3159],
         [ 0.2361],
         [ 0.1087],
         [ 0.2224],
         [ 0.2633],
         [ 0.5037],
         [ 0.1980],
         [ 0.1530],
         [ 0.2780],
         [-0.1399],
         [ 0.5331],
         [ 0.3530],
         [ 0.3342],
         [ 0.2098],
         [-0.0165],
         [ 0.1318],
         [ 0.4510],
         [-0.1959],
         [ 0.0966],
         [ 0.0789],
         [ 0.3381],
         [-0.1917],
         [ 0.1518],
         [ 0.3640],
         [ 0.0956],
         [ 0.2535],
         [-0.3988],
         [-0.3479],
         [ 0.3864],
         [-0.2639],
         [-0.2368],
         [ 0.0258],
         [ 0.2441],
         [ 0.0687],
         [ 0.0457],
         [ 0.2286],
         [-0.0947],
         [-0.1189],
         [ 0.1360],
         [-0.0990],
         [-0.2447],
         [ 0.2135],
         [-0.1830],
         [-0.4583],
         [-0.1795],
         [-0.1361],
         [-0.0553],
         [-0.2864],
         [-0.2307],
         [-0.4651],
         [-0.1889],
         [-0.3185],
         [-0.5318],
         [-0.3012],
         [ 0.0062],
         [ 0.1046],
         [-0.2321],
         [-0.2945],
         [-0.0242],
         [-0.0586],
         [-0.2307],
         [-0.2479],
         [-0.0382],
         [-0.1509],
         [-0.5055],
         [-0.3759],
         [ 0.2139],
         [-0.2129],
         [-0.3605],
         [-0.5222],
         [-0.6530],
         [-0.6716],
         [-0.4330],
         [-0.2577],
         [-0.2672],
         [-0.1297],
         [-0.9203],
         [-0.5832],
         [-0.2640],
         [-0.4996],
         [-0.2625],
         [-0.4407],
         [-0.8864],
         [-0.2508],
         [-0.4827],
         [-0.3131],
         [-0.2570],
         [-0.7116],
         [-0.5357],
         [-0.7074]]))
In [86]:
batch_size, n_train = 16, 600
# 只有前n_train个样本用于训练
train_iter = d2l.load_array((features[:n_train], labels[:n_train]),
batch_size, is_train=True)
In [87]:
def init_weights(m):
    if type(m) == nn.Linear:
        nn.init.xavier_uniform_(m.weight)
In [88]:
def get_net():
    net = nn.Sequential(nn.Linear(4, 10),
    nn.ReLU(),
    nn.Linear(10, 1))
    net.apply(init_weights)
    return net
loss = nn.MSELoss(reduction='none')
In [89]:
def train(net, train_iter, loss, epochs, lr):
    trainer = torch.optim.Adam(net.parameters(), lr)
    for epoch in range(epochs):
        for X, y in train_iter:
            trainer.zero_grad()
            l = loss(net(X), y)
            l.sum().backward()
            trainer.step()
        print(f'epoch {epoch + 1}, '
                f'loss: {d2l.evaluate_loss(net, train_iter, loss):f}')
net = get_net()
train(net, train_iter, loss, 5, 0.01)
epoch 1, loss: 0.082748
epoch 2, loss: 0.066498
epoch 3, loss: 0.061828
epoch 4, loss: 0.059193
epoch 5, loss: 0.058498
/home/yukun/.conda/envs/nn/lib/python3.11/site-packages/d2l/torch.py:3179: UserWarning: Converting a tensor with requires_grad=True to a scalar may lead to unexpected behavior.
Consider using tensor.detach() first. (Triggered internally at /pytorch/torch/csrc/autograd/generated/python_variable_methods.cpp:836.)
  self.data = [a + float(b) for a, b in zip(self.data, args)]
In [90]:
onestep_preds = net(features)
d2l.plot([time, time[tau:]],
[x.detach().numpy(), onestep_preds.detach().numpy()], 'time',
'x', legend=['data', '1-step preds'], xlim=[1, 1000],
figsize=(6, 3))
In [91]:
multistep_preds = torch.zeros(T)
multistep_preds[: n_train + tau] = x[: n_train + tau]
for i in range(n_train + tau, T):
    multistep_preds[i] = net(
        multistep_preds[i - tau:i].reshape((1, -1)))
d2l.plot([time, time[tau:], time[n_train + tau:]],
            [x.detach().numpy(), onestep_preds.detach().numpy(),
            multistep_preds[n_train + tau:].detach().numpy()], 'time',
            'x', legend=['data', '1-step preds', 'multistep preds'],
                xlim=[1, 1000], figsize=(6, 3))
In [92]:
import collections
import re
In [93]:
d2l.DATA_HUB['time_machine'] = (d2l.DATA_URL + 'timemachine.txt',
'090b5e7e70c295757f55df93cb0a180b9691891a')
def read_time_machine(): #@save
    """将时间机器数据集加载到文本行的列表中"""
    with open(d2l.download('time_machine'), 'r') as f:
        lines = f.readlines()
    return [re.sub('[^A-Za-z]+', ' ', line).strip().lower() for line in lines]
lines = read_time_machine()
print(f'# 文本总行数: {len(lines)}')
print(lines[0])
print(lines[10])
# 文本总行数: 3221
the time machine by h g wells
twinkled and his usually pale face was flushed and animated the
In [94]:
def tokenize(lines, token='word'): #@save
    """将文本行拆分为单词或字符词元"""
    if token == 'word':
        return [line.split() for line in lines]
    elif token == 'char':
        return [list(line) for line in lines]
    else:
        print('错误:未知词元类型:' + token)
tokens = tokenize(lines)
for i in range(11):
    print(tokens[i])
['the', 'time', 'machine', 'by', 'h', 'g', 'wells']
[]
[]
[]
[]
['i']
[]
[]
['the', 'time', 'traveller', 'for', 'so', 'it', 'will', 'be', 'convenient', 'to', 'speak', 'of', 'him']
['was', 'expounding', 'a', 'recondite', 'matter', 'to', 'us', 'his', 'grey', 'eyes', 'shone', 'and']
['twinkled', 'and', 'his', 'usually', 'pale', 'face', 'was', 'flushed', 'and', 'animated', 'the']
In [95]:
def count_corpus(tokens): #@save
    """统计词元的频率"""
    # 这里的tokens是1D列表或2D列表
    if len(tokens) == 0 or isinstance(tokens[0], list):
    # 将词元列表展平成一个列表
        tokens = [token for line in tokens for token in line]
    return collections.Counter(tokens)
class Vocab: #@save
    """文本词表"""
    def __init__(self, tokens=None, min_freq=0, reserved_tokens=None):
        if tokens is None:
            tokens = []
        if reserved_tokens is None:
            reserved_tokens = []
        # 按出现频率排序
        counter = count_corpus(tokens)
        self._token_freqs = sorted(counter.items(), key=lambda x: x[1],
                                    reverse=True)
        # 未知词元的索引为0
        self.idx_to_token = ['<unk>'] + reserved_tokens
        self.token_to_idx = {token: idx
                                for idx, token in enumerate(self.idx_to_token)}
        for token, freq in self._token_freqs:
            if freq < min_freq:
                break
            if token not in self.token_to_idx:
                self.idx_to_token.append(token)
                self.token_to_idx[token] = len(self.idx_to_token) - 1
    def __len__(self):
        return len(self.idx_to_token)
    def __getitem__(self, tokens):
        if not isinstance(tokens, (list, tuple)):
            return self.token_to_idx.get(tokens, self.unk)
        return [self.__getitem__(token) for token in tokens]
    def to_tokens(self, indices):
        if not isinstance(indices, (list, tuple)):
            return self.idx_to_token[indices]
        return [self.idx_to_token[index] for index in indices]
    @property
    def unk(self): # 未知词元的索引为0
        return 0
    @property
    def token_freqs(self):
        return self._token_freqs
In [96]:
vocab = Vocab(tokens)
print(list(vocab.token_to_idx.items())[:10])
[('<unk>', 0), ('the', 1), ('i', 2), ('and', 3), ('of', 4), ('a', 5), ('to', 6), ('was', 7), ('in', 8), ('that', 9)]
In [97]:
for i in [0, 100]:
    print('文本:', tokens[i])
    print('索引:', vocab[tokens[i]])
文本: ['the', 'time', 'machine', 'by', 'h', 'g', 'wells']
索引: [1, 19, 50, 40, 2183, 2184, 400]
文本: ['were', 'three', 'dimensional', 'representations', 'of', 'his', 'four', 'dimensioned']
索引: [20, 175, 1452, 2250, 4, 25, 262, 2251]
In [98]:
def load_corpus_time_machine(max_tokens=-1): #@save
    """返回时光机器数据集的词元索引列表和词表"""
    lines = read_time_machine()
    tokens = tokenize(lines, 'char')
    vocab = Vocab(tokens)
    # 因为时光机器数据集中的每个文本行不一定是一个句子或一个段落,
    # 所以将所有文本行展平到一个列表中
    corpus = [vocab[token] for line in tokens for token in line]
    if max_tokens > 0:
        corpus = corpus[:max_tokens]
    return corpus, vocab
corpus, vocab = load_corpus_time_machine()

len(corpus), len(vocab)
Out [98]:
(170580, 28)
In [99]:
tokens = d2l.tokenize(read_time_machine())
# 因为每个文本行不一定是一个句子或一个段落,因此我们把所有文本行拼接到一起
corpus = [token for line in tokens for token in line]
vocab = d2l.Vocab(corpus)
vocab.token_freqs[:10]
Out [99]:
[('the', 2261),
 ('i', 1267),
 ('and', 1245),
 ('of', 1155),
 ('a', 816),
 ('to', 695),
 ('was', 552),
 ('in', 541),
 ('that', 443),
 ('my', 440)]
In [100]:
freqs = [freq for token, freq in vocab.token_freqs]
d2l.plot(freqs, xlabel='token: x', ylabel='frequency: n(x)',
xscale='log', yscale='log')
In [101]:
bigram_tokens = [pair for pair in zip(corpus[:-1], corpus[1:])]
bigram_vocab = Vocab(bigram_tokens)
bigram_vocab.token_freqs[:10]
Out [101]:
[(('of', 'the'), 309),
 (('in', 'the'), 169),
 (('i', 'had'), 130),
 (('i', 'was'), 112),
 (('and', 'the'), 109),
 (('the', 'time'), 102),
 (('it', 'was'), 99),
 (('to', 'the'), 85),
 (('as', 'i'), 78),
 (('of', 'a'), 73)]
Warning:
Output truncated. This notebook contains too many cells to display efficiently.