455 KiB
455 KiB
In [1]:
import torch
import d2l
import numpy
import torch.nn as nn
import torch.nn.functional as FIn [2]:
net = nn.Sequential(nn.Linear(20, 256), nn.ReLU(), nn.Linear(256, 10))
X = torch.rand(2, 20)
net(X)Out [2]:
tensor([[-0.0824, 0.0285, 0.1192, 0.0922, 0.0465, 0.2007, -0.0262, 0.1639,
-0.0899, 0.1057],
[-0.0524, 0.0180, 0.0952, 0.0921, -0.0702, 0.2043, 0.0393, 0.0629,
-0.1250, 0.0537]], grad_fn=<AddmmBackward0>)In [3]:
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 [4]:
net=MLP()
net(X)Out [4]:
tensor([[-0.1096, 0.0395, 0.1076, 0.0112, 0.1523, 0.0678, -0.4146, 0.1690,
0.0085, -0.0510],
[-0.0863, 0.0353, 0.0677, -0.0226, 0.1161, 0.0591, -0.3184, 0.1216,
-0.0316, -0.1315]], grad_fn=<AddmmBackward0>)In [5]:
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 [6]:
net = FixedHiddenMLP()
net(X)Out [6]:
tensor(0.2039, grad_fn=<SumBackward0>)
In [7]:
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 [8]:
net = nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 1))
X = torch.rand(size=(2, 4))
net(X)Out [8]:
tensor([[0.3055],
[0.0396]], grad_fn=<AddmmBackward0>)In [9]:
print(net[2].state_dict())OrderedDict([('weight', tensor([[-0.0619, -0.2581, -0.0887, 0.1497, 0.3016, 0.0745, 0.3351, -0.2275]])), ('bias', tensor([0.1878]))])
In [10]:
net[2].state_dict()Out [10]:
OrderedDict([('weight',
tensor([[-0.0619, -0.2581, -0.0887, 0.1497, 0.3016, 0.0745, 0.3351, -0.2275]])),
('bias', tensor([0.1878]))])In [11]:
print(type(net[2].bias))<class 'torch.nn.parameter.Parameter'>
In [12]:
print(net[2].bias)
print(net[2].bias.data)
Parameter containing: tensor([0.1878], requires_grad=True) tensor([0.1878])
In [13]:
net[2].weight.grad==NoneOut [13]:
True
In [14]:
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 [15]:
net.state_dict()['2.bias'].dataOut [15]:
tensor([0.1878])
In [16]:
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 netIn [17]:
rgnet = nn.Sequential(block2(),nn.Linear(4,1))
rgnet(X)Out [17]:
tensor([[-0.3406],
[-0.3406]], grad_fn=<AddmmBackward0>)In [18]:
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 [19]:
rgnet[0][1][0].bias.dataOut [19]:
tensor([ 0.3709, -0.2778, -0.1532, -0.4749, 0.4300, -0.0282, -0.0499, 0.3819])
In [20]:
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 [20]:
(tensor([-0.0090, 0.0195, 0.0008, 0.0062]), tensor(0.))
In [21]:
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.0184, 0.4366, -0.5272, 0.1226]) tensor([[42., 42., 42., 42., 42., 42., 42., 42.]])
In [22]:
x = torch.arange(4)
torch.save(x, 'x-file')In [23]:
x2 = torch.load('x-file')
x2Out [23]:
tensor([0, 1, 2, 3])
In [24]:
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 [25]:
torch.save(net.state_dict(), 'mlp.params')In [26]:
clone = MLP()
clone.load_state_dict(torch.load('mlp.params'))
clone.eval()Out [26]:
MLP( (hidden): Linear(in_features=20, out_features=256, bias=True) (output): Linear(in_features=256, out_features=10, bias=True) )
In [27]:
Y_clone = clone(X)
Y_clone == YOut [27]:
tensor([[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True]])In [28]:
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 [29]:
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 [29]:
tensor([[19., 25.],
[37., 43.]])In [30]:
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 [31]:
X = torch.ones((6, 8))
X[:, 2:6] = 0
XOut [31]:
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 [32]:
K = torch.tensor([[1.0, -1.0]])
Y = corr2d(X, K)
YOut [32]:
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 [33]:
corr2d(X.t(), K)Out [33]:
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 [34]:
conv2d = nn.Conv2d(1,1, kernel_size=(1, 2), bias=False)In [35]:
X = X.reshape((1, 1, 6, 8))
Y = Y.reshape((1, 1, 6, 7))
lr = 3e-2In [36]:
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.003 epoch 40, loss 0.000 epoch 60, loss 0.000 epoch 80, loss 0.000 epoch 100, loss 0.000
In [37]:
conv2d.weight.data.reshape((1, 2))
Out [37]:
tensor([[ 1.0000, -1.0000]])
In [38]:
# 为了方便起见,我们定义了一个计算卷积层的函数。
# 此函数初始化卷积层权重,并对输入和输出提高和缩减相应的维数
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 [39]:
X = torch.rand(size=(8, 8))
comp_conv2d(conv2d, X).shapeOut [39]:
torch.Size([8, 8])
In [40]:
conv2d = nn.Conv2d(1, 1, kernel_size=(5, 3), padding=(2, 1))
comp_conv2d(conv2d, X).shapeOut [40]:
torch.Size([8, 8])
In [41]:
conv2d = nn.Conv2d(1, 1, kernel_size=3, padding=1, stride=2)
comp_conv2d(conv2d, X).shapeOut [41]:
torch.Size([4, 4])
In [42]:
conv2d = nn.Conv2d(1, 1, kernel_size=(3, 5), padding=(0, 1), stride=(3, 4))
comp_conv2d(conv2d, X).shapeOut [42]:
torch.Size([2, 2])
In [43]:
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 [43]:
tensor([[ 56., 72.],
[104., 120.]])In [44]:
def corr2d_multi_in_out(X,K) ->torch.Tensor :
return torch.stack([corr2d_multi_in(X,k) for k in K],0)
In [45]:
K = torch.stack((K, K + 1, K + 2), 0)
K.shapeOut [45]:
torch.Size([3, 2, 2, 2])
In [46]:
corr2d_multi_in_out(X, K)Out [46]:
tensor([[[ 56., 72.],
[104., 120.]],
[[ 76., 100.],
[148., 172.]],
[[ 96., 128.],
[192., 224.]]])In [47]:
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 [48]:
X = torch.normal(0, 1, (3, 3, 3))
K = torch.normal(0, 1, (2, 3, 1, 1))In [49]:
Y1 = corr2d_multi_in_out_1x1(X, K)torch.Size([3, 9]) torch.Size([2, 3])
In [50]:
Y2 = corr2d_multi_in_out(X, K)
assert float(torch.abs(Y1 - Y2).sum()) < 1e-6In [51]:
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 YIn [52]:
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 [52]:
tensor([[4., 5.],
[7., 8.]])In [53]:
pool2d(X, (2, 2), 'avg')Out [53]:
tensor([[2., 3.],
[5., 6.]])In [54]:
X = torch.arange(16, dtype=torch.float32).reshape((1, 1, 4, 4))
XOut [54]:
tensor([[[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.]]]])In [55]:
pool2d=nn.MaxPool2d(3)
pool2d(X)Out [55]:
tensor([[[[10.]]]])
In [56]:
pool2d = nn.MaxPool2d(3, padding=1, stride=2)
pool2d(X)Out [56]:
tensor([[[[ 5., 7.],
[13., 15.]]]])In [57]:
pool2d = nn.MaxPool2d((2, 3), stride=(2, 3), padding=(0, 1))
pool2d(X)Out [57]:
tensor([[[[ 5., 7.],
[13., 15.]]]])In [58]:
X = torch.cat((X, X + 1), 1)
XOut [58]:
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 [59]:
pool2d = nn.MaxPool2d(3, padding=1, stride=2)
pool2d(X)Out [59]:
tensor([[[[ 5., 7.],
[13., 15.]],
[[ 6., 8.],
[14., 16.]]]])In [60]:
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).shapeOut [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 blkIn [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.1257, 0.4977, 0.1275, 0.0113, 0.1759, 0.1263, 0.0984, 0.0670,
0.3374, -0.3129, 0.3756, 0.0234, -0.0841, 0.4951, 0.3441, -0.0585,
-0.2159, 0.0357, 0.0667, -0.0126, 0.6966, -0.0548, 0.0864, 0.5669,
0.2040, 0.2158, 0.1378, 0.2790, 0.4541, 0.3656, 0.3050, 0.3321,
0.3818, 0.3404, 0.3803, 0.3527, 0.5237, 0.7250, 0.3400, 0.3136,
0.6944, 0.3985, 0.9682, 0.5841, 0.5376, 0.2229, 0.6266, 0.1417,
0.2132, 0.6786, 0.3201, 0.5340, 0.7747, 0.7968, 0.7266, 0.7018,
0.8106, 0.6221, 0.2093, 0.3683, 0.5998, 0.5546, 0.6686, 0.4981,
0.6079, 0.3726, 0.9469, 0.6261, 0.4213, 0.5943, 1.2487, 0.5027,
0.6524, 0.6218, 0.4721, 0.7688, 0.8629, 0.5897, 0.3414, 1.0822,
0.9223, 0.8020, 0.6607, 0.4673, 0.7155, 0.6349, 0.4676, 0.9303,
0.6977, 0.7986, 0.5661, 0.9401, 0.8111, 1.0929, 0.5887, 0.8674,
0.8081, 0.8682, 0.7049, 1.0303, 0.5297, 0.8990, 0.6131, 1.1693,
1.0146, 1.1179, 0.8550, 0.6801, 0.9054, 0.9622, 0.8227, 0.6969,
0.8629, 0.9992, 0.9735, 0.9114, 0.5090, 0.9698, 1.1530, 1.2176,
1.1019, 1.0681, 0.6768, 1.0307, 0.9873, 1.1988, 1.1947, 0.8704,
0.8378, 0.7581, 1.2643, 1.2095, 0.7556, 1.0024, 0.8649, 1.1953,
0.8106, 1.2512, 1.1907, 0.8453, 1.0807, 0.7710, 0.9226, 0.8100,
1.0641, 0.9683, 0.7675, 1.2630, 0.9153, 1.0170, 1.3423, 0.8989,
1.2243, 1.3355, 0.9849, 0.6055, 0.4062, 0.8255, 1.1904, 0.7565,
1.0362, 0.8106, 0.8765, 1.1825, 1.0300, 1.1883, 0.7432, 0.7962,
0.7900, 0.9459, 1.0081, 1.1498, 1.0555, 1.4386, 0.9888, 0.7890,
0.9454, 0.9568, 0.9832, 0.7835, 0.8084, 0.7282, 1.1450, 1.2708,
1.1315, 0.6742, 0.6001, 0.6483, 0.8992, 1.0016, 1.0392, 0.5630,
1.3330, 0.9323, 0.6719, 0.9954, 1.0855, 1.0105, 0.6578, 1.0974,
0.9163, 1.0161, 1.0866, 0.8661, 0.5516, 1.0398, 1.0476, 0.8525,
0.8723, 1.0883, 0.5629, 0.3963, 0.7161, 1.2104, 1.0025, 1.0816,
0.7881, 0.7980, 0.6719, 0.5641, 0.7839, 0.7183, 0.6777, 1.1626,
0.6991, 0.7296, 0.9149, 0.4818, 0.3593, 0.8057, 0.9782, 0.6981,
0.8359, 0.5616, 0.8751, 0.4524, 0.9480, 0.4057, 0.6413, 0.6728,
0.8040, 1.1152, 0.6752, 0.7030, 0.5862, 0.7373, 0.6680, 0.6739,
0.7372, 1.0807, 0.8491, 0.4628, 0.5695, 0.4675, 0.8295, 0.7881,
0.6622, 0.3701, 0.3987, 0.6082, 0.4924, 0.6136, 0.4755, 0.7166,
0.4721, 0.2420, 0.2503, 0.5961, 0.5344, 0.6053, 0.5369, 0.2291,
0.3503, 0.2833, 0.1630, 0.0821, 0.1769, 0.5129, 0.2650, 0.1519,
0.2660, 0.1505, 0.2407, 0.1766, 0.2215, 0.3759, 0.0643, 0.2909,
0.0220, 0.5878, 0.1559, 0.2339, 0.3533, -0.1447, 0.5657, 0.0656,
-0.1913, 0.1975, -0.0296, 0.3531, 0.0032, 0.1607, 0.2249, 0.0783,
0.1663, -0.0781, -0.0607, 0.3047, 0.2461, -0.0380, 0.0481, -0.0040,
0.0110, -0.0221, 0.1001, 0.0754, 0.2153, -0.1584, 0.0033, -0.2072,
0.1622, -0.1114, -0.0954, -0.2582, -0.0575, -0.0883, 0.3422, -0.1808,
-0.2768, -0.1964, 0.1526, -0.1362, 0.0674, -0.5093, -0.0344, -0.3681,
-0.2217, -0.1733, -0.0589, -0.1194, -0.0979, -0.2122, -0.5427, -0.5028,
0.0059, -0.2044, -0.2778, -0.3447, -0.0537, -0.4030, -0.7130, -0.5167,
-0.4477, -0.4382, 0.0076, -0.1804, -0.1491, 0.1210, -0.4279, -0.6204,
-0.7309, -0.1835, -0.9354, -0.6655, -0.7265, -0.5585, -0.8215, -0.3998,
-0.6667, -0.4026, -0.3606, -0.2286, -0.5571, -0.8246, -0.2567, -0.8022,
-0.3873, -0.6781, -0.8021, -0.7463, -0.6887, -0.5723, -0.6661, -0.4324,
-0.6482, -0.5130, -0.6848, -0.5460, -0.8493, -0.1809, -0.5165, -0.4671,
-0.8529, -0.9896, -0.8904, -0.4498, -1.0809, -0.9123, -0.7125, -0.4627,
-0.5643, -0.7416, -0.8990, -0.8161, -0.5500, -0.9439, -0.8327, -0.7132,
-0.8250, -0.9772, -0.8947, -0.4970, -0.4945, -0.4604, -0.7029, -0.7518,
-0.7635, -0.8060, -0.8300, -1.1194, -1.2429, -0.7834, -0.3628, -1.1099,
-0.8337, -1.0767, -0.7193, -0.6253, -0.9703, -0.5913, -1.0695, -0.9610,
-0.7796, -0.8729, -1.1516, -0.8974, -1.1277, -0.8297, -0.6336, -1.5144,
-1.0980, -1.0812, -0.5136, -0.6882, -0.9138, -0.9021, -1.0671, -1.1456,
-0.9467, -0.6042, -0.8922, -0.9499, -0.6512, -1.0729, -1.1589, -1.1675,
-0.9637, -0.7511, -0.8479, -0.8410, -1.1934, -0.8869, -0.9340, -1.0252,
-0.8195, -1.3040, -0.6508, -1.0083, -1.1282, -0.9536, -1.0764, -1.2750,
-1.0073, -1.0259, -0.8144, -1.2082, -0.9558, -0.9895, -1.0417, -1.0077,
-0.7460, -0.7199, -1.1118, -0.7411, -1.2156, -0.8967, -0.8194, -1.1041,
-0.9286, -0.9155, -0.7483, -0.9874, -1.0476, -0.9132, -0.7950, -0.8823,
-0.8565, -1.0017, -0.9736, -0.8743, -0.9509, -1.3399, -0.8861, -1.0557,
-0.8494, -0.6369, -1.0813, -0.7510, -0.8624, -1.1163, -0.9114, -0.7323,
-0.9083, -0.8352, -0.6851, -0.9174, -0.9412, -1.3040, -0.6257, -0.7814,
-0.7670, -1.0620, -0.9168, -1.0231, -0.5532, -0.7955, -0.9293, -0.7984,
-0.9475, -0.8074, -1.0046, -0.7866, -0.8110, -0.8169, -0.7929, -0.9577,
-0.7490, -0.6953, -0.7600, -0.6348, -0.5752, -0.6600, -1.1377, -1.0344,
-0.6518, -0.7506, -0.9227, -0.7814, -0.9301, -0.4463, -0.8153, -0.7221,
-0.6543, -1.0062, -0.4462, -0.5389, -0.3644, -0.3854, -0.5175, -0.3598,
-0.7745, -0.8278, -0.6843, -0.5519, -0.6849, -0.6662, -0.8282, -0.5927,
-0.8346, -0.5149, -0.0033, -0.7285, -0.8659, -0.4320, -0.5433, -0.5551,
-0.4936, -0.3990, -0.2697, -0.5388, -0.5527, -0.5663, -0.4017, -0.2667,
-0.3446, -0.3117, -0.3110, -0.8562, -0.2726, -0.5014, -0.4719, -0.5338,
-0.7666, -0.1854, -0.5822, -0.4734, -0.2585, -0.2755, -0.4047, -0.0902,
-0.0984, -0.3434, -0.0755, -0.5209, -0.2434, -0.3536, -0.0617, 0.1276,
-0.0150, -0.5196, -0.2691, -0.8314, 0.1469, -0.0438, -0.4816, 0.1779,
-0.1709, -0.2126, -0.2875, -0.4329, -0.0967, -0.5540, -0.2296, -0.0021,
-0.1871, 0.0261, -0.0573, 0.3196, 0.1587, 0.1620, -0.3062, 0.1800,
-0.0216, -0.0861, 0.3876, 0.2574, 0.2573, 0.3694, 0.1312, 0.6010,
0.0274, 0.0227, -0.1395, 0.0214, 0.3586, 0.0331, 0.2754, 0.4699,
0.3533, -0.0946, 0.1566, 0.2768, 0.6166, 0.3522, 0.2357, 0.2673,
0.2506, 0.4461, 0.6163, 0.1398, 0.3288, 0.4211, 0.3313, 0.1029,
0.4284, 0.1385, 0.1132, 0.0989, 0.3567, 0.2329, 0.4514, 0.7074,
0.3183, 0.2934, 0.4533, 0.2790, 0.4807, 0.8162, 0.6992, 0.1948,
0.5107, 0.8306, 0.2990, 0.2718, 0.7156, 0.8072, 0.6706, 0.5840,
0.8009, 0.5367, 0.8542, 0.4551, 0.6621, 0.6004, 0.6589, 0.4726,
0.5991, 0.8084, 0.5788, 0.7125, 0.6552, 0.9191, 0.3361, 0.8335,
0.2599, 0.6830, 0.6857, 0.4505, 0.7303, 0.5562, 0.3135, 0.7432,
0.8188, 0.7189, 0.6228, 0.8273, 0.6486, 0.9803, 0.6484, 0.7697,
1.1531, 0.9866, 1.3931, 0.9747, 1.2460, 1.0597, 0.7014, 0.9013,
0.9571, 0.7041, 1.0944, 1.1762, 1.1356, 1.0760, 1.0171, 0.8546,
0.9204, 0.9524, 1.3716, 0.7630, 0.9069, 1.0180, 1.0366, 1.0358,
0.8609, 0.8634, 0.8047, 0.7477, 0.9808, 1.0275, 1.2071, 0.5799,
0.8834, 0.8784, 1.1447, 1.0891, 0.5811, 0.9703, 1.2833, 0.9937,
1.1356, 0.8306, 0.9129, 1.0194, 1.4320, 1.2589, 0.9175, 0.8849,
1.1727, 0.9605, 0.7599, 0.8099, 1.0688, 0.7013, 1.0260, 0.7066,
0.8967, 1.0578, 0.8639, 1.0968, 0.9553, 1.0410, 0.7809, 0.8928,
0.9644, 0.8980, 0.9744, 0.6657, 1.0549, 0.9716, 1.0272, 0.9510,
1.0992, 0.8345, 1.0305, 1.0269, 0.9503, 1.0622, 0.9953, 1.3019,
1.0447, 0.9759, 0.9953, 1.0697, 0.9619, 1.0681, 1.0844, 0.6814,
0.7774, 1.1827, 1.1599, 0.7436, 0.8570, 0.7392, 1.2210, 0.8350,
0.7613, 0.7885, 1.0991, 0.6867, 0.5461, 1.1209, 1.1265, 0.9876,
0.8403, 0.9892, 0.7838, 0.5770, 0.7996, 1.1023, 1.1888, 0.8290,
0.9919, 0.7272, 0.6149, 0.8744, 0.7331, 0.9389, 0.8888, 0.4813,
1.1600, 0.6871, 0.7780, 0.9699, 0.3082, 0.8391, 0.5978, 0.5697,
0.9227, 0.4502, 0.5293, 0.7309, 0.7579, 0.5995, 0.5698, 0.5490,
0.7483, 0.9721, 0.9419, 0.5393, 0.9869, 0.9892, 0.5714, 0.7620,
0.6800, 0.8412, 0.6070, 0.1774, 0.6198, 0.7153, 0.7985, 0.5209,
1.1309, 0.6716, 0.7221, 0.5309, 0.6143, 0.9212, 0.6585, 0.5518,
0.7676, 0.7002, 0.5711, 0.5491, 0.7280, 1.2188, 0.3206, 0.5493,
0.7454, 0.5868, 0.6143, 0.8513, 0.1876, 0.5672, 0.4292, 0.5437,
0.4909, 0.7139, 0.5861, 0.3725, 0.5194, 0.4843, 0.0279, 0.3152,
0.4333, 0.5915, 0.2709, 0.4861, 0.1708, -0.0844, 0.1523, -0.2092,
0.2965, -0.1280, 0.4479, 0.4392, 0.1969, 0.1989, -0.0969, 0.2829,
0.1741, -0.1890, -0.0512, 0.4777, 0.0458, 0.0724, 0.1996, 0.2772,
-0.0650, 0.4351, 0.2693, -0.0298, -0.1171, 0.3714, 0.0992, 0.0090,
0.0618, 0.1225, 0.1389, 0.1166, 0.0821, 0.0435, -0.1259, -0.1045,
0.1779, -0.2051, -0.2457, -0.1619, -0.0991, 0.1651, 0.1712, -0.1440,
-0.0499, -0.0943, 0.1058, -0.3224, -0.2115, -0.1307, -0.2432, -0.1935,
-0.1462, -0.3798, -0.3857, -0.3871, 0.1132, -0.5729, 0.1458, -0.5250,
-0.1113, -0.1085, -0.3974, -0.2798, -0.2995, -0.0517, -0.1601, -0.5213,
-0.3897, -0.5143, -0.4268, -0.4268, -0.1593, -0.3720, -0.2030, -0.5328,
-0.8009, -0.5220, -0.5291, -0.3730, -0.4571, -0.3859, -0.3053, -0.3744,
-0.7439, -0.7338, -0.2856, -0.3440, -0.6041, -0.7940, -0.6112, -0.1943]),
tensor([[-0.1257, 0.4977, 0.1275, 0.0113],
[ 0.4977, 0.1275, 0.0113, 0.1759],
[ 0.1275, 0.0113, 0.1759, 0.1263],
...,
[-0.7338, -0.2856, -0.3440, -0.6041],
[-0.2856, -0.3440, -0.6041, -0.7940],
[-0.3440, -0.6041, -0.7940, -0.6112]]),
tensor([[ 0.1759],
[ 0.1263],
[ 0.0984],
[ 0.0670],
[ 0.3374],
[-0.3129],
[ 0.3756],
[ 0.0234],
[-0.0841],
[ 0.4951],
[ 0.3441],
[-0.0585],
[-0.2159],
[ 0.0357],
[ 0.0667],
[-0.0126],
[ 0.6966],
[-0.0548],
[ 0.0864],
[ 0.5669],
[ 0.2040],
[ 0.2158],
[ 0.1378],
[ 0.2790],
[ 0.4541],
[ 0.3656],
[ 0.3050],
[ 0.3321],
[ 0.3818],
[ 0.3404],
[ 0.3803],
[ 0.3527],
[ 0.5237],
[ 0.7250],
[ 0.3400],
[ 0.3136],
[ 0.6944],
[ 0.3985],
[ 0.9682],
[ 0.5841],
[ 0.5376],
[ 0.2229],
[ 0.6266],
[ 0.1417],
[ 0.2132],
[ 0.6786],
[ 0.3201],
[ 0.5340],
[ 0.7747],
[ 0.7968],
[ 0.7266],
[ 0.7018],
[ 0.8106],
[ 0.6221],
[ 0.2093],
[ 0.3683],
[ 0.5998],
[ 0.5546],
[ 0.6686],
[ 0.4981],
[ 0.6079],
[ 0.3726],
[ 0.9469],
[ 0.6261],
[ 0.4213],
[ 0.5943],
[ 1.2487],
[ 0.5027],
[ 0.6524],
[ 0.6218],
[ 0.4721],
[ 0.7688],
[ 0.8629],
[ 0.5897],
[ 0.3414],
[ 1.0822],
[ 0.9223],
[ 0.8020],
[ 0.6607],
[ 0.4673],
[ 0.7155],
[ 0.6349],
[ 0.4676],
[ 0.9303],
[ 0.6977],
[ 0.7986],
[ 0.5661],
[ 0.9401],
[ 0.8111],
[ 1.0929],
[ 0.5887],
[ 0.8674],
[ 0.8081],
[ 0.8682],
[ 0.7049],
[ 1.0303],
[ 0.5297],
[ 0.8990],
[ 0.6131],
[ 1.1693],
[ 1.0146],
[ 1.1179],
[ 0.8550],
[ 0.6801],
[ 0.9054],
[ 0.9622],
[ 0.8227],
[ 0.6969],
[ 0.8629],
[ 0.9992],
[ 0.9735],
[ 0.9114],
[ 0.5090],
[ 0.9698],
[ 1.1530],
[ 1.2176],
[ 1.1019],
[ 1.0681],
[ 0.6768],
[ 1.0307],
[ 0.9873],
[ 1.1988],
[ 1.1947],
[ 0.8704],
[ 0.8378],
[ 0.7581],
[ 1.2643],
[ 1.2095],
[ 0.7556],
[ 1.0024],
[ 0.8649],
[ 1.1953],
[ 0.8106],
[ 1.2512],
[ 1.1907],
[ 0.8453],
[ 1.0807],
[ 0.7710],
[ 0.9226],
[ 0.8100],
[ 1.0641],
[ 0.9683],
[ 0.7675],
[ 1.2630],
[ 0.9153],
[ 1.0170],
[ 1.3423],
[ 0.8989],
[ 1.2243],
[ 1.3355],
[ 0.9849],
[ 0.6055],
[ 0.4062],
[ 0.8255],
[ 1.1904],
[ 0.7565],
[ 1.0362],
[ 0.8106],
[ 0.8765],
[ 1.1825],
[ 1.0300],
[ 1.1883],
[ 0.7432],
[ 0.7962],
[ 0.7900],
[ 0.9459],
[ 1.0081],
[ 1.1498],
[ 1.0555],
[ 1.4386],
[ 0.9888],
[ 0.7890],
[ 0.9454],
[ 0.9568],
[ 0.9832],
[ 0.7835],
[ 0.8084],
[ 0.7282],
[ 1.1450],
[ 1.2708],
[ 1.1315],
[ 0.6742],
[ 0.6001],
[ 0.6483],
[ 0.8992],
[ 1.0016],
[ 1.0392],
[ 0.5630],
[ 1.3330],
[ 0.9323],
[ 0.6719],
[ 0.9954],
[ 1.0855],
[ 1.0105],
[ 0.6578],
[ 1.0974],
[ 0.9163],
[ 1.0161],
[ 1.0866],
[ 0.8661],
[ 0.5516],
[ 1.0398],
[ 1.0476],
[ 0.8525],
[ 0.8723],
[ 1.0883],
[ 0.5629],
[ 0.3963],
[ 0.7161],
[ 1.2104],
[ 1.0025],
[ 1.0816],
[ 0.7881],
[ 0.7980],
[ 0.6719],
[ 0.5641],
[ 0.7839],
[ 0.7183],
[ 0.6777],
[ 1.1626],
[ 0.6991],
[ 0.7296],
[ 0.9149],
[ 0.4818],
[ 0.3593],
[ 0.8057],
[ 0.9782],
[ 0.6981],
[ 0.8359],
[ 0.5616],
[ 0.8751],
[ 0.4524],
[ 0.9480],
[ 0.4057],
[ 0.6413],
[ 0.6728],
[ 0.8040],
[ 1.1152],
[ 0.6752],
[ 0.7030],
[ 0.5862],
[ 0.7373],
[ 0.6680],
[ 0.6739],
[ 0.7372],
[ 1.0807],
[ 0.8491],
[ 0.4628],
[ 0.5695],
[ 0.4675],
[ 0.8295],
[ 0.7881],
[ 0.6622],
[ 0.3701],
[ 0.3987],
[ 0.6082],
[ 0.4924],
[ 0.6136],
[ 0.4755],
[ 0.7166],
[ 0.4721],
[ 0.2420],
[ 0.2503],
[ 0.5961],
[ 0.5344],
[ 0.6053],
[ 0.5369],
[ 0.2291],
[ 0.3503],
[ 0.2833],
[ 0.1630],
[ 0.0821],
[ 0.1769],
[ 0.5129],
[ 0.2650],
[ 0.1519],
[ 0.2660],
[ 0.1505],
[ 0.2407],
[ 0.1766],
[ 0.2215],
[ 0.3759],
[ 0.0643],
[ 0.2909],
[ 0.0220],
[ 0.5878],
[ 0.1559],
[ 0.2339],
[ 0.3533],
[-0.1447],
[ 0.5657],
[ 0.0656],
[-0.1913],
[ 0.1975],
[-0.0296],
[ 0.3531],
[ 0.0032],
[ 0.1607],
[ 0.2249],
[ 0.0783],
[ 0.1663],
[-0.0781],
[-0.0607],
[ 0.3047],
[ 0.2461],
[-0.0380],
[ 0.0481],
[-0.0040],
[ 0.0110],
[-0.0221],
[ 0.1001],
[ 0.0754],
[ 0.2153],
[-0.1584],
[ 0.0033],
[-0.2072],
[ 0.1622],
[-0.1114],
[-0.0954],
[-0.2582],
[-0.0575],
[-0.0883],
[ 0.3422],
[-0.1808],
[-0.2768],
[-0.1964],
[ 0.1526],
[-0.1362],
[ 0.0674],
[-0.5093],
[-0.0344],
[-0.3681],
[-0.2217],
[-0.1733],
[-0.0589],
[-0.1194],
[-0.0979],
[-0.2122],
[-0.5427],
[-0.5028],
[ 0.0059],
[-0.2044],
[-0.2778],
[-0.3447],
[-0.0537],
[-0.4030],
[-0.7130],
[-0.5167],
[-0.4477],
[-0.4382],
[ 0.0076],
[-0.1804],
[-0.1491],
[ 0.1210],
[-0.4279],
[-0.6204],
[-0.7309],
[-0.1835],
[-0.9354],
[-0.6655],
[-0.7265],
[-0.5585],
[-0.8215],
[-0.3998],
[-0.6667],
[-0.4026],
[-0.3606],
[-0.2286],
[-0.5571],
[-0.8246],
[-0.2567],
[-0.8022],
[-0.3873],
[-0.6781],
[-0.8021],
[-0.7463],
[-0.6887],
[-0.5723],
[-0.6661],
[-0.4324],
[-0.6482],
[-0.5130],
[-0.6848],
[-0.5460],
[-0.8493],
[-0.1809],
[-0.5165],
[-0.4671],
[-0.8529],
[-0.9896],
[-0.8904],
[-0.4498],
[-1.0809],
[-0.9123],
[-0.7125],
[-0.4627],
[-0.5643],
[-0.7416],
[-0.8990],
[-0.8161],
[-0.5500],
[-0.9439],
[-0.8327],
[-0.7132],
[-0.8250],
[-0.9772],
[-0.8947],
[-0.4970],
[-0.4945],
[-0.4604],
[-0.7029],
[-0.7518],
[-0.7635],
[-0.8060],
[-0.8300],
[-1.1194],
[-1.2429],
[-0.7834],
[-0.3628],
[-1.1099],
[-0.8337],
[-1.0767],
[-0.7193],
[-0.6253],
[-0.9703],
[-0.5913],
[-1.0695],
[-0.9610],
[-0.7796],
[-0.8729],
[-1.1516],
[-0.8974],
[-1.1277],
[-0.8297],
[-0.6336],
[-1.5144],
[-1.0980],
[-1.0812],
[-0.5136],
[-0.6882],
[-0.9138],
[-0.9021],
[-1.0671],
[-1.1456],
[-0.9467],
[-0.6042],
[-0.8922],
[-0.9499],
[-0.6512],
[-1.0729],
[-1.1589],
[-1.1675],
[-0.9637],
[-0.7511],
[-0.8479],
[-0.8410],
[-1.1934],
[-0.8869],
[-0.9340],
[-1.0252],
[-0.8195],
[-1.3040],
[-0.6508],
[-1.0083],
[-1.1282],
[-0.9536],
[-1.0764],
[-1.2750],
[-1.0073],
[-1.0259],
[-0.8144],
[-1.2082],
[-0.9558],
[-0.9895],
[-1.0417],
[-1.0077],
[-0.7460],
[-0.7199],
[-1.1118],
[-0.7411],
[-1.2156],
[-0.8967],
[-0.8194],
[-1.1041],
[-0.9286],
[-0.9155],
[-0.7483],
[-0.9874],
[-1.0476],
[-0.9132],
[-0.7950],
[-0.8823],
[-0.8565],
[-1.0017],
[-0.9736],
[-0.8743],
[-0.9509],
[-1.3399],
[-0.8861],
[-1.0557],
[-0.8494],
[-0.6369],
[-1.0813],
[-0.7510],
[-0.8624],
[-1.1163],
[-0.9114],
[-0.7323],
[-0.9083],
[-0.8352],
[-0.6851],
[-0.9174],
[-0.9412],
[-1.3040],
[-0.6257],
[-0.7814],
[-0.7670],
[-1.0620],
[-0.9168],
[-1.0231],
[-0.5532],
[-0.7955],
[-0.9293],
[-0.7984],
[-0.9475],
[-0.8074],
[-1.0046],
[-0.7866],
[-0.8110],
[-0.8169],
[-0.7929],
[-0.9577],
[-0.7490],
[-0.6953],
[-0.7600],
[-0.6348],
[-0.5752],
[-0.6600],
[-1.1377],
[-1.0344],
[-0.6518],
[-0.7506],
[-0.9227],
[-0.7814],
[-0.9301],
[-0.4463],
[-0.8153],
[-0.7221],
[-0.6543],
[-1.0062],
[-0.4462],
[-0.5389],
[-0.3644],
[-0.3854],
[-0.5175],
[-0.3598],
[-0.7745],
[-0.8278],
[-0.6843],
[-0.5519],
[-0.6849],
[-0.6662],
[-0.8282],
[-0.5927],
[-0.8346],
[-0.5149],
[-0.0033],
[-0.7285],
[-0.8659],
[-0.4320],
[-0.5433],
[-0.5551],
[-0.4936],
[-0.3990],
[-0.2697],
[-0.5388],
[-0.5527],
[-0.5663],
[-0.4017],
[-0.2667],
[-0.3446],
[-0.3117],
[-0.3110],
[-0.8562],
[-0.2726],
[-0.5014],
[-0.4719],
[-0.5338],
[-0.7666],
[-0.1854],
[-0.5822],
[-0.4734],
[-0.2585],
[-0.2755],
[-0.4047],
[-0.0902],
[-0.0984],
[-0.3434],
[-0.0755],
[-0.5209],
[-0.2434],
[-0.3536],
[-0.0617],
[ 0.1276],
[-0.0150],
[-0.5196],
[-0.2691],
[-0.8314],
[ 0.1469],
[-0.0438],
[-0.4816],
[ 0.1779],
[-0.1709],
[-0.2126],
[-0.2875],
[-0.4329],
[-0.0967],
[-0.5540],
[-0.2296],
[-0.0021],
[-0.1871],
[ 0.0261],
[-0.0573],
[ 0.3196],
[ 0.1587],
[ 0.1620],
[-0.3062],
[ 0.1800],
[-0.0216],
[-0.0861],
[ 0.3876],
[ 0.2574],
[ 0.2573],
[ 0.3694],
[ 0.1312],
[ 0.6010],
[ 0.0274],
[ 0.0227],
[-0.1395],
[ 0.0214],
[ 0.3586],
[ 0.0331],
[ 0.2754],
[ 0.4699],
[ 0.3533],
[-0.0946],
[ 0.1566],
[ 0.2768],
[ 0.6166],
[ 0.3522],
[ 0.2357],
[ 0.2673],
[ 0.2506],
[ 0.4461],
[ 0.6163],
[ 0.1398],
[ 0.3288],
[ 0.4211],
[ 0.3313],
[ 0.1029],
[ 0.4284],
[ 0.1385],
[ 0.1132],
[ 0.0989],
[ 0.3567],
[ 0.2329],
[ 0.4514],
[ 0.7074],
[ 0.3183],
[ 0.2934],
[ 0.4533],
[ 0.2790],
[ 0.4807],
[ 0.8162],
[ 0.6992],
[ 0.1948],
[ 0.5107],
[ 0.8306],
[ 0.2990],
[ 0.2718],
[ 0.7156],
[ 0.8072],
[ 0.6706],
[ 0.5840],
[ 0.8009],
[ 0.5367],
[ 0.8542],
[ 0.4551],
[ 0.6621],
[ 0.6004],
[ 0.6589],
[ 0.4726],
[ 0.5991],
[ 0.8084],
[ 0.5788],
[ 0.7125],
[ 0.6552],
[ 0.9191],
[ 0.3361],
[ 0.8335],
[ 0.2599],
[ 0.6830],
[ 0.6857],
[ 0.4505],
[ 0.7303],
[ 0.5562],
[ 0.3135],
[ 0.7432],
[ 0.8188],
[ 0.7189],
[ 0.6228],
[ 0.8273],
[ 0.6486],
[ 0.9803],
[ 0.6484],
[ 0.7697],
[ 1.1531],
[ 0.9866],
[ 1.3931],
[ 0.9747],
[ 1.2460],
[ 1.0597],
[ 0.7014],
[ 0.9013],
[ 0.9571],
[ 0.7041],
[ 1.0944],
[ 1.1762],
[ 1.1356],
[ 1.0760],
[ 1.0171],
[ 0.8546],
[ 0.9204],
[ 0.9524],
[ 1.3716],
[ 0.7630],
[ 0.9069],
[ 1.0180],
[ 1.0366],
[ 1.0358],
[ 0.8609],
[ 0.8634],
[ 0.8047],
[ 0.7477],
[ 0.9808],
[ 1.0275],
[ 1.2071],
[ 0.5799],
[ 0.8834],
[ 0.8784],
[ 1.1447],
[ 1.0891],
[ 0.5811],
[ 0.9703],
[ 1.2833],
[ 0.9937],
[ 1.1356],
[ 0.8306],
[ 0.9129],
[ 1.0194],
[ 1.4320],
[ 1.2589],
[ 0.9175],
[ 0.8849],
[ 1.1727],
[ 0.9605],
[ 0.7599],
[ 0.8099],
[ 1.0688],
[ 0.7013],
[ 1.0260],
[ 0.7066],
[ 0.8967],
[ 1.0578],
[ 0.8639],
[ 1.0968],
[ 0.9553],
[ 1.0410],
[ 0.7809],
[ 0.8928],
[ 0.9644],
[ 0.8980],
[ 0.9744],
[ 0.6657],
[ 1.0549],
[ 0.9716],
[ 1.0272],
[ 0.9510],
[ 1.0992],
[ 0.8345],
[ 1.0305],
[ 1.0269],
[ 0.9503],
[ 1.0622],
[ 0.9953],
[ 1.3019],
[ 1.0447],
[ 0.9759],
[ 0.9953],
[ 1.0697],
[ 0.9619],
[ 1.0681],
[ 1.0844],
[ 0.6814],
[ 0.7774],
[ 1.1827],
[ 1.1599],
[ 0.7436],
[ 0.8570],
[ 0.7392],
[ 1.2210],
[ 0.8350],
[ 0.7613],
[ 0.7885],
[ 1.0991],
[ 0.6867],
[ 0.5461],
[ 1.1209],
[ 1.1265],
[ 0.9876],
[ 0.8403],
[ 0.9892],
[ 0.7838],
[ 0.5770],
[ 0.7996],
[ 1.1023],
[ 1.1888],
[ 0.8290],
[ 0.9919],
[ 0.7272],
[ 0.6149],
[ 0.8744],
[ 0.7331],
[ 0.9389],
[ 0.8888],
[ 0.4813],
[ 1.1600],
[ 0.6871],
[ 0.7780],
[ 0.9699],
[ 0.3082],
[ 0.8391],
[ 0.5978],
[ 0.5697],
[ 0.9227],
[ 0.4502],
[ 0.5293],
[ 0.7309],
[ 0.7579],
[ 0.5995],
[ 0.5698],
[ 0.5490],
[ 0.7483],
[ 0.9721],
[ 0.9419],
[ 0.5393],
[ 0.9869],
[ 0.9892],
[ 0.5714],
[ 0.7620],
[ 0.6800],
[ 0.8412],
[ 0.6070],
[ 0.1774],
[ 0.6198],
[ 0.7153],
[ 0.7985],
[ 0.5209],
[ 1.1309],
[ 0.6716],
[ 0.7221],
[ 0.5309],
[ 0.6143],
[ 0.9212],
[ 0.6585],
[ 0.5518],
[ 0.7676],
[ 0.7002],
[ 0.5711],
[ 0.5491],
[ 0.7280],
[ 1.2188],
[ 0.3206],
[ 0.5493],
[ 0.7454],
[ 0.5868],
[ 0.6143],
[ 0.8513],
[ 0.1876],
[ 0.5672],
[ 0.4292],
[ 0.5437],
[ 0.4909],
[ 0.7139],
[ 0.5861],
[ 0.3725],
[ 0.5194],
[ 0.4843],
[ 0.0279],
[ 0.3152],
[ 0.4333],
[ 0.5915],
[ 0.2709],
[ 0.4861],
[ 0.1708],
[-0.0844],
[ 0.1523],
[-0.2092],
[ 0.2965],
[-0.1280],
[ 0.4479],
[ 0.4392],
[ 0.1969],
[ 0.1989],
[-0.0969],
[ 0.2829],
[ 0.1741],
[-0.1890],
[-0.0512],
[ 0.4777],
[ 0.0458],
[ 0.0724],
[ 0.1996],
[ 0.2772],
[-0.0650],
[ 0.4351],
[ 0.2693],
[-0.0298],
[-0.1171],
[ 0.3714],
[ 0.0992],
[ 0.0090],
[ 0.0618],
[ 0.1225],
[ 0.1389],
[ 0.1166],
[ 0.0821],
[ 0.0435],
[-0.1259],
[-0.1045],
[ 0.1779],
[-0.2051],
[-0.2457],
[-0.1619],
[-0.0991],
[ 0.1651],
[ 0.1712],
[-0.1440],
[-0.0499],
[-0.0943],
[ 0.1058],
[-0.3224],
[-0.2115],
[-0.1307],
[-0.2432],
[-0.1935],
[-0.1462],
[-0.3798],
[-0.3857],
[-0.3871],
[ 0.1132],
[-0.5729],
[ 0.1458],
[-0.5250],
[-0.1113],
[-0.1085],
[-0.3974],
[-0.2798],
[-0.2995],
[-0.0517],
[-0.1601],
[-0.5213],
[-0.3897],
[-0.5143],
[-0.4268],
[-0.4268],
[-0.1593],
[-0.3720],
[-0.2030],
[-0.5328],
[-0.8009],
[-0.5220],
[-0.5291],
[-0.3730],
[-0.4571],
[-0.3859],
[-0.3053],
[-0.3744],
[-0.7439],
[-0.7338],
[-0.2856],
[-0.3440],
[-0.6041],
[-0.7940],
[-0.6112],
[-0.1943]]))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.069361 epoch 2, loss: 0.057280 epoch 3, loss: 0.054714 epoch 4, loss: 0.054167 epoch 5, loss: 0.050941
/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 reIn [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_freqsIn [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.