import numpy as np import torch import torch.nn as nn import torch.nn.functional as F import pandas as pd from torchvision import transforms from torchvision import datasets
# 测试验证 defrightness(predictions, labels): pred = torch.max(predictions.data, 1)[1] rights = pred.eq(labels.data.view_as(pred)).sum() return rights, len(labels)
1 2 3 4 5 6 7 8 9 10 11
# 验证测试 rights = 0 length = 0 for i, data inenumerate(test_loader): x, y = data net.eval() pred = net(x.cuda()) rights = rights + rightness(pred, y.cuda())[0] length = length + rightness(pred, y.cuda())[1]
import numpy as np import torch import torch.nn as nn import torch.nn.functional as F import pandas as pd from torchvision import transforms from torchvision import datasets from torch.utils.data import DataLoader, Dataset, TensorDataset
638
torch.Size([3, 32, 32])
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
<matplotlib.image.AxesImage at 0x27e882a9c70>
from torchinfo import summary summary(net, input_size=(64, 3, 32, 32))
D:\02_soft\anaconda3\envs\pytorch\lib\site-packages\torch\nn\functional.py:1331: UserWarning: dropout2d: Received a 2-D input to dropout2d, which is deprecated and will result in an error in a future release. To retain the behavior and silence this warning, please use dropout instead. Note that dropout2d exists to provide channel-wise dropout on inputs with 2 spatial dimensions, a channel dimension, and an optional batch dimension (i.e. 3D or 4D inputs).
warnings.warn(warn_msg)
==========================================================================================
Layer (type:depth-idx) Output Shape Param #
==========================================================================================
model [64, 10] --
├─Conv2d: 1-1 [64, 4, 32, 32] 112
├─BatchNorm2d: 1-2 [64, 4, 32, 32] 8
├─Conv2d: 1-3 [64, 8, 32, 32] 296
├─BatchNorm2d: 1-4 [64, 8, 32, 32] 16
├─Conv2d: 1-5 [64, 16, 32, 32] 1,168
├─BatchNorm2d: 1-6 [64, 16, 32, 32] 32
├─MaxPool2d: 1-7 [64, 16, 16, 16] --
├─Conv2d: 1-8 [64, 32, 16, 16] 4,640
├─BatchNorm2d: 1-9 [64, 32, 16, 16] 64
├─AvgPool2d: 1-10 [64, 32, 8, 8] --
├─Conv2d: 1-11 [64, 64, 8, 8] 18,496
├─BatchNorm2d: 1-12 [64, 64, 8, 8] 128
├─MaxPool2d: 1-13 [64, 64, 4, 4] --
├─Linear: 1-14 [64, 512] 524,800
├─Dropout2d: 1-15 [64, 512] --
├─Linear: 1-16 [64, 10] 5,130
==========================================================================================
Total params: 554,890
Trainable params: 554,890
Non-trainable params: 0
Total mult-adds (M): 289.00
==========================================================================================
Input size (MB): 0.79
Forward/backward pass size (MB): 42.21
Params size (MB): 2.22
Estimated Total Size (MB): 45.22
==========================================================================================
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 开始训练 for epoch inrange(20): for i, data inenumerate(train_dataset): x, y = data x = x.to(device) y = y.to(device) net.train() pred = net(x) loss = loss_fn(pred, y)
opt.zero_grad() loss.backward() opt.step()
print('第', epoch, '个epoch,loss为:', loss)
D:\02_soft\anaconda3\envs\pytorch\lib\site-packages\torch\nn\functional.py:1331: UserWarning: dropout2d: Received a 2-D input to dropout2d, which is deprecated and will result in an error in a future release. To retain the behavior and silence this warning, please use dropout instead. Note that dropout2d exists to provide channel-wise dropout on inputs with 2 spatial dimensions, a channel dimension, and an optional batch dimension (i.e. 3D or 4D inputs).
warnings.warn(warn_msg)
第 0 个epoch,loss为: tensor(1.4621, device='cuda:0', grad_fn=<NllLossBackward0>)
第 1 个epoch,loss为: tensor(1.1412, device='cuda:0', grad_fn=<NllLossBackward0>)
第 2 个epoch,loss为: tensor(0.7093, device='cuda:0', grad_fn=<NllLossBackward0>)
第 3 个epoch,loss为: tensor(1.1387, device='cuda:0', grad_fn=<NllLossBackward0>)
第 4 个epoch,loss为: tensor(1.1270, device='cuda:0', grad_fn=<NllLossBackward0>)
第 5 个epoch,loss为: tensor(0.1355, device='cuda:0', grad_fn=<NllLossBackward0>)
第 6 个epoch,loss为: tensor(2.1455, device='cuda:0', grad_fn=<NllLossBackward0>)
第 7 个epoch,loss为: tensor(0.2261, device='cuda:0', grad_fn=<NllLossBackward0>)
第 8 个epoch,loss为: tensor(0.3833, device='cuda:0', grad_fn=<NllLossBackward0>)
第 9 个epoch,loss为: tensor(1.3244, device='cuda:0', grad_fn=<NllLossBackward0>)
第 10 个epoch,loss为: tensor(0.1666, device='cuda:0', grad_fn=<NllLossBackward0>)
第 11 个epoch,loss为: tensor(0.8256, device='cuda:0', grad_fn=<NllLossBackward0>)
第 12 个epoch,loss为: tensor(0.8673, device='cuda:0', grad_fn=<NllLossBackward0>)
第 13 个epoch,loss为: tensor(0.0255, device='cuda:0', grad_fn=<NllLossBackward0>)
第 14 个epoch,loss为: tensor(0.8387, device='cuda:0', grad_fn=<NllLossBackward0>)
第 15 个epoch,loss为: tensor(0.1123, device='cuda:0', grad_fn=<NllLossBackward0>)
第 16 个epoch,loss为: tensor(0.0009, device='cuda:0', grad_fn=<NllLossBackward0>)
第 17 个epoch,loss为: tensor(0.0298, device='cuda:0', grad_fn=<NllLossBackward0>)
第 18 个epoch,loss为: tensor(0.5178, device='cuda:0', grad_fn=<NllLossBackward0>)
第 19 个epoch,loss为: tensor(0.3033, device='cuda:0', grad_fn=<NllLossBackward0>)
1 2 3 4 5
# 测试验证 defrightness(predictions, labels): pred = torch.max(predictions.data, 1)[1] rights = pred.eq(labels.data.view_as(pred)).sum() return rights, len(labels)
1 2 3 4 5 6 7 8 9 10 11 12 13
# 验证测试 rights = 0 length = 0 for i, data inenumerate(test_dataset): x, y = data x = x.to(device) y = y.to(device) net.eval() pred = net(x) rights = rights + rightness(pred, y)[0] length = length + rightness(pred, y)[1]