import matplotlib.pyplot as plt
defshow_fashion_mnist(images, labels):
d2l.use_svg_display()# 这里的_表示我们忽略(不使用)的变量
_, figs = plt.subplots(1,len(images), figsize=(12,12))for f, img, lbl inzip(figs, images, labels):
f.imshow(img.view((28,28)).numpy())
f.set_title(lbl)
f.axes.get_xaxis().set_visible(False)
f.axes.get_yaxis().set_visible(False)
plt.show()for Xdata,ylabel in train_iter:break
X, y =[],[]for i inrange(10):print(Xdata[i].shape,ylabel[i].numpy())
X.append(Xdata[i])# 将第i个feature加到X中
y.append(ylabel[i].numpy())# 将第i个label加到y中
show_fashion_mnist(X, y)
#use GPUdeftry_gpu():"""If GPU is available, return torch.device as cuda:0; else return torch.device as cpu."""if torch.cuda.is_available():
device = torch.device('cuda:0')else:
device = torch.device('cpu')return device
device = try_gpu()
device
# 使用evaluate_accuracy函数计算数据集上准确率#计算准确率'''
(1). net.train()
启用 BatchNormalization 和 Dropout,将BatchNormalization和Dropout置为True
(2). net.eval()
不启用 BatchNormalization 和 Dropout,将BatchNormalization和Dropout置为False
'''defevaluate_accuracy(data_iter, net,device=torch.device('cpu')):"""Evaluate accuracy of a model on the given data set."""
acc_sum,n = torch.tensor([0],dtype=torch.float32,device=device),0for X,y in data_iter:# If device is the GPU, copy the data to the GPU.
X,y = X.to(device),y.to(device)
net.eval()with torch.no_grad():
y = y.long()
acc_sum += torch.sum((torch.argmax(net(X), dim=1)== y))#[[0.2 ,0.4 ,0.5 ,0.6 ,0.8] ,[ 0.1,0.2 ,0.4 ,0.3 ,0.1]] => [ 4 , 2 ]
n += y.shape[0]return acc_sum.item()/n
#训练函数deftrain_ch5(net, train_iter, test_iter,criterion, num_epochs, batch_size, device,lr=None):"""Train and evaluate a model with CPU or GPU."""print('training on', device)
net.to(device)
optimizer = optim.SGD(net.parameters(), lr=lr)for epoch inrange(num_epochs):
train_l_sum = torch.tensor([0.0],dtype=torch.float32,device=device)
train_acc_sum = torch.tensor([0.0],dtype=torch.float32,device=device)
n, start =0, time.time()for X, y in train_iter:
net.train()
optimizer.zero_grad()
X,y = X.to(device),y.to(device)
y_hat = net(X)
loss = criterion(y_hat, y)
loss.backward()
optimizer.step()with torch.no_grad():
y = y.long()
train_l_sum += loss.float()
train_acc_sum +=(torch.sum((torch.argmax(y_hat, dim=1)== y))).float()
n += y.shape[0]
test_acc = evaluate_accuracy(test_iter, net,device)print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, ''time %.1f sec'%(epoch +1, train_l_sum/n, train_acc_sum/n, test_acc,
time.time()- start))