往期学习资料推荐:

1.Pytorch实战笔记_GoAI的博客-CSDN博客

2.Pytorch入门教程_GoAI的博客-CSDN博客

本系列目录:

PyTorch学习笔记(一):PyTorch环境安装

PyTorch学习笔记(二):简介与基础知识

PyTorch学习笔记(三):PyTorch主要组成模块

PyTorch学习笔记(四):PyTorch基础实战

PyTorch学习笔记(五):模型定义、修改、保存

PyTorch学习笔记(六):PyTorch进阶训练技巧

PyTorch学习笔记(七):PyTorch可视化

PyTorch学习笔记(八):PyTorch生态简介

后续继续更新!!!!

1 深度学习步骤

(1)数据预处理:通过专门的数据加载,通过批训练提高模型表现,每次训练读取固定数量的样本输入到模型中进行训练
(2)深度神经网络搭建:逐层搭建,实现特定功能的层(如积层、池化层、批正则化层、LSTM层等)
(3)损失函数和优化器的设定:保证反向传播能够在用户定义的模型结构上实现
(4)模型训练:使用并行计算加速训练,将数据按批加载,放入GPU中训练,对损失函数反向传播回网络最前面的层,同时使用优化器调整网络参数

2 基本配置

  • 导入相关的包
import os
import numpy as py
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import torch.optim as optimizer
  • 统一设置超参数:batch size、初始学习率、训练次数、GPU配置
# set batch size
batch_size = 16# 初始学习率
lr = 1e-4# 训练次数
max_epochs = 100# 配置GPU
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
devicedevice(type='cuda', index=1)

3 数据读入

  • 读取方式:通过Dataset+DataLoader的方式加载数据,Dataset定义好数据的格式和数据变换形式,DataLoader用iterative的方式不断读入批次数据。

  • 自定义Dataset类:实现__init_____getitem____len__函数

  • torch.utils.data.DataLoader参数:

    1. batch_size:样本是按“批”读入的,表示每次读入的样本数
    2. num_workers:表示用于读取数据的进程数
    3. shuffle:是否将读入的数据打乱
    4. drop_last:对于样本最后一部分没有达到批次数的样本,使其不再参与训练

4 模型构建

4.1 神经网络的构造

通过Module类构造模型,实例化模型之后,可完成模型构造

# 构造多层感知机
class MLP(nn.Module):def __init__(self, **kwargs):super(MLP, self).__init__(**kwargs)self.hidden = nn.Linear(784, 256)self.act = nn.ReLU()self.output = nn.Linear(256, 10)def forward(self, X):o = self.act(self.hidden(x))return self.output(o)x = torch.rand(2, 784)
net = MLP()
print(x)
net(x)
tensor([[0.8924, 0.9624, 0.3262,  ..., 0.8376, 0.1889, 0.9060],[0.3609, 0.8005, 0.5175,  ..., 0.6255, 0.1462, 0.9846]])tensor([[-0.0902,  0.0199,  0.0677, -0.0679,  0.0799,  0.0826,  0.0628,  0.1809,-0.2387,  0.0366],[-0.2271,  0.0056, -0.0984, -0.0432, -0.0160, -0.0038,  0.0953,  0.0545,-0.1530, -0.0214]], grad_fn=<AddmmBackward>)

4.2 神经网络常见的层

  • 不含模型参数的层
# 构造一个输入减去均值后输出的层
class MyLayer(nn.Module):def __init__(self, **kwargs):super(MyLayer, self).__init__(**kwargs)def forward(self, x):return x - x.mean()
x = torch.tensor([0, 5, 10, 15, 20], dtype=torch.float)
layer = MyLayer()
layer(x)
tensor([-10.,  -5.,   0.,   5.,  10.])
  • 含模型参数的层:如果一个TensorParameter,那么它会⾃动被添加到模型的参数列表里
# 使用ParameterList定义参数的列表
class MyListDense(nn.Module):def __init__(self):super(MyListDense, self).__init__()self.params = nn.ParameterList([nn.Parameter(torch.randn(4, 4)) for i in range(3)])self.params.append(nn.Parameter(torch.randn(4, 1)))def forward(self, x):for i in range(len(self.params)):x = torch.mm(x, self.params[i])return x
net = MyListDense()
print(net)
MyListDense((params): ParameterList((0): Parameter containing: [torch.FloatTensor of size 4x4](1): Parameter containing: [torch.FloatTensor of size 4x4](2): Parameter containing: [torch.FloatTensor of size 4x4](3): Parameter containing: [torch.FloatTensor of size 4x1])
)
# 使用ParameterDict定义参数的字典
class MyDictDense(nn.Module):def __init__(self):super(MyDictDense, self).__init__()self.params = nn.ParameterDict({'linear1': nn.Parameter(torch.randn(4, 4)),'linear2': nn.Parameter(torch.randn(4, 1))})# 新增参数linear3self.params.update({'linear3': nn.Parameter(torch.randn(4, 2))}) def forward(self, x, choice='linear1'):return torch.mm(x, self.params[choice])net = MyDictDense()
print(net)
MyDictDense((params): ParameterDict((linear1): Parameter containing: [torch.FloatTensor of size 4x4](linear2): Parameter containing: [torch.FloatTensor of size 4x1](linear3): Parameter containing: [torch.FloatTensor of size 4x2])
)
  • 二维卷积层:使用nn.Conv2d类构造,模型参数包括卷积核和标量偏差,在训练模式时,通常先对卷积核随机初始化,再不断迭代卷积核和偏差
# 计算卷积层,对输入和输出做相应的升维和降维
def comp_conv2d(conv2d, X):# (1, 1)代表批量大小和通道数X = X.view((1, 1) + X.shape)Y = conv2d(X)# 排除不关心的前两维:批量和通道return Y.view(Y.shape[2:])
# 注意这里是两侧分别填充1⾏或列,所以在两侧一共填充2⾏或列
conv2d = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3,padding=1)X = torch.rand(8, 8)
comp_conv2d(conv2d, X).shape
torch.Size([8, 8])
  • 池化层:直接计算池化窗口内元素的最大值或者平均值,分别叫做最大池化或平均池化
# 二维池化层
def pool2d(X, pool_size, mode='max'):p_h, p_w = pool_sizeY = 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]):if mode == 'max':Y[i, j] = X[i: i + p_h, j: j + p_w].max()elif mode == 'avg':Y[i, j] = X[i: i + p_h, j: j + p_w].mean()return Y
X = torch.tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=torch.float)
pool2d(X, (2, 2), 'max')
tensor([[4., 5.],[7., 8.]])
pool2d(X, (2, 2), 'avg')
tensor([[2., 3.],[5., 6.]])

4.3 模型示例

  • 神经网络训练过程:

    1. 定义可学习参数的神经网络
    2. 在输入数据集上进行迭代训练
    3. 通过神经网络处理输入数据
    4. 计算loss(损失值)
    5. 将梯度反向传播给神经网络参数
    6. 更新网络参数,使用梯度下降
  • LeNet(前馈神经网络)


import torch.nn.functional as Fclass Net(nn.Module):def __init__(self):super(Net, self).__init__()# 输入图像channel是1;输出channel是6;5x5卷积核self.conv1 = nn.Conv2d(1, 6, 5)self.conv2 = nn.Conv2d(6, 16, 5)# an affine operation: y = Wx + bself.fc1 = nn.Linear(16 * 5 * 5, 120)self.fc2 = nn.Linear(120, 84)self.fc3 = nn.Linear(84, 10)def forward(self, x):# 2x2 Max poolingx = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))# 如果是方阵,则可以只使用一个数字进行定义x = F.max_pool2d(F.relu(self.conv2(x)), 2)x = x.view(-1, self.num_flat_features(x))x = F.relu(self.fc1(x))x = F.relu(self.fc2(x))x = self.fc3(x)return xdef num_flat_features(self, x):# 除去批处理维度的其他所有维度size = x.size()[1:]  num_features = 1for s in size:num_features *= sreturn num_featuresnet = Net()
net
Net((conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))(conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))(fc1): Linear(in_features=400, out_features=120, bias=True)(fc2): Linear(in_features=120, out_features=84, bias=True)(fc3): Linear(in_features=84, out_features=10, bias=True)
)
# 假设输入的数据为随机的32x32
input = torch.randn(1, 1, 32, 32)
out = net(input)
print(out)
tensor([[-0.0921, -0.0605, -0.0726, -0.0451,  0.1399, -0.0087,  0.1075,  0.0799,-0.1472,  0.0288]], grad_fn=<AddmmBackward>)
# 清零所有参数的梯度缓存,然后进行随机梯度的反向传播
net.zero_grad()
out.backward(torch.randn(1, 10))
  • AlexNet
class AlexNet(nn.Module):def __init__(self):super(AlexNet, self).__init__()self.conv = nn.Sequential(# in_channels, out_channels, kernel_size, stride, paddingnn.Conv2d(1, 96, 11, 4), nn.ReLU(),# kernel_size, stridenn.MaxPool2d(3, 2), # 减小卷积窗口,使用填充为2来使得输入与输出的高和宽一致,且增大输出通道数nn.Conv2d(96, 256, 5, 1, 2),nn.ReLU(),nn.MaxPool2d(3, 2),# 连续3个卷积层,且使用更小的卷积窗口。# 除了最后的卷积层外,进一步增大了输出通道数。# 前两个卷积层后不使用池化层来减小输入的高和宽nn.Conv2d(256, 384, 3, 1, 1),nn.ReLU(),nn.Conv2d(384, 384, 3, 1, 1),nn.ReLU(),nn.Conv2d(384, 256, 3, 1, 1),nn.ReLU(),nn.MaxPool2d(3, 2))# 这里全连接层的输出个数比LeNet中的大数倍。使用丢弃层来缓解过拟合self.fc = nn.Sequential(nn.Linear(256*5*5, 4096),nn.ReLU(),nn.Dropout(0.5),nn.Linear(4096, 4096),nn.ReLU(),nn.Dropout(0.5),# 输出层。由于这里使用Fashion-MNIST,所以用类别数为10,而非论文中的1000nn.Linear(4096, 10),)def forward(self, img):feature = self.conv(img)output = self.fc(feature.view(img.shape[0], -1))return output
net = AlexNet()
print(net)
AlexNet((conv): Sequential((0): Conv2d(1, 96, kernel_size=(11, 11), stride=(4, 4))(1): ReLU()(2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)(3): Conv2d(96, 256, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(4): ReLU()(5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)(6): Conv2d(256, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(7): ReLU()(8): Conv2d(384, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(9): ReLU()(10): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(11): ReLU()(12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False))(fc): Sequential((0): Linear(in_features=6400, out_features=4096, bias=True)(1): ReLU()(2): Dropout(p=0.5, inplace=False)(3): Linear(in_features=4096, out_features=4096, bias=True)(4): ReLU()(5): Dropout(p=0.5, inplace=False)(6): Linear(in_features=4096, out_features=10, bias=True))
)

5 损失函数

  • 二分类交叉熵损失函数:torch.nn.BCELoss,用于计算二分类任务时的交叉熵
m = nn.Sigmoid()
loss = nn.BCELoss()
input = torch.randn(3, requires_grad=True)
target = torch.empty(3).random_(2)output = loss(m(input), target)
output.backward()
print('BCE损失函数的计算结果:',output)
BCE损失函数的计算结果: tensor(0.9389, grad_fn=<BinaryCrossEntropyBackward>)
  • 交叉熵损失函数:torch.nn.CrossEntropyLoss,用于计算交叉熵
loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)output = loss(input, target)
output.backward()
print('CrossEntropy损失函数的计算结果:',output)
CrossEntropy损失函数的计算结果: tensor(2.7367, grad_fn=<NllLossBackward>)
  • L1损失函数:torch.nn.L1Loss,用于计算输出y和真实值target之差的绝对值
loss = nn.L1Loss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5)output = loss(input, target)
output.backward()
print('L1损失函数的计算结果:',output)
L1损失函数的计算结果: tensor(1.0351, grad_fn=<L1LossBackward>)
  • MSE损失函数:torch.nn.MSELoss,用于计算输出y和真实值target之差的平方
loss = nn.MSELoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5)output = loss(input, target)
output.backward()
print('MSE损失函数的计算结果:',output)
MSE损失函数的计算结果: tensor(1.7612, grad_fn=<MseLossBackward>)
  • 平滑L1(Smooth L1)损失函数:torch.nn.SmoothL1Loss,用于计算L1的平滑输出,减轻离群点带来的影响,通过与L1损失的比较,在0点的尖端处,过渡更为平滑
loss = nn.SmoothL1Loss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5)output = loss(input, target)
output.backward()
print('Smooth L1损失函数的计算结果:',output)
Smooth L1损失函数的计算结果: tensor(0.7252, grad_fn=<SmoothL1LossBackward>)
  • 目标泊松分布的负对数似然损失:torch.nn.PoissonNLLLoss
loss = nn.PoissonNLLLoss()
log_input = torch.randn(5, 2, requires_grad=True)
target = torch.randn(5, 2)output = loss(log_input, target)
output.backward()
print('PoissonNL损失函数的计算结果:',output)
PoissonNL损失函数的计算结果: tensor(1.7593, grad_fn=<MeanBackward0>)
  • KL散度:torch.nn.KLDivLoss,用于连续分布的距离度量,可用在对离散采用的连续输出空间分布的回归场景
inputs = torch.tensor([[0.5, 0.3, 0.2], [0.2, 0.3, 0.5]])
target = torch.tensor([[0.9, 0.05, 0.05], [0.1, 0.7, 0.2]], dtype=torch.float)
loss = nn.KLDivLoss(reduction='batchmean')output = loss(inputs,target)
print('KLDiv损失函数的计算结果:',output)
KLDiv损失函数的计算结果: tensor(-1.0006)
  • MarginRankingLoss:torch.nn.MarginRankingLoss,用于计算两组数据之间的差异(相似度),可使用在排序任务的场景
loss = nn.MarginRankingLoss()
input1 = torch.randn(3, requires_grad=True)
input2 = torch.randn(3, requires_grad=True)
target = torch.randn(3).sign()output = loss(input1, input2, target)
output.backward()
print('MarginRanking损失函数的计算结果:',output)
MarginRanking损失函数的计算结果: tensor(1.1762, grad_fn=<MeanBackward0>)
  • 多标签边界损失函数:torch.nn.MultiLabelMarginLoss,用于计算多标签分类问题的损失
loss = nn.MultiLabelMarginLoss()
x = torch.FloatTensor([[0.9, 0.2, 0.4, 0.8]])
# 真实的分类是,第3类和第0类
y = torch.LongTensor([[3, 0, -1, 1]])output = loss(x, y)
print('MultiLabelMargin损失函数的计算结果:',output)
MultiLabelMargin损失函数的计算结果: tensor(0.4500)
  • 二分类损失函数:torch.nn.SoftMarginLoss,用于计算二分类的logistic损失
# 定义两个样本,两个神经元
inputs = torch.tensor([[0.3, 0.7], [0.5, 0.5]])
target = torch.tensor([[-1, 1], [1, -1]], dtype=torch.float)  # 该loss对每个神经元计算,需要为每个神经元单独设置标签
loss_f = nn.SoftMarginLoss()
output = loss_f(inputs, target)
print('SoftMargin损失函数的计算结果:',output)
SoftMargin损失函数的计算结果: tensor(0.6764)
  • 多分类的折页损失函数:torch.nn.MultiMarginLoss,用于计算多分类问题的折页损失
inputs = torch.tensor([[0.3, 0.7], [0.5, 0.5]])
target = torch.tensor([0, 1], dtype=torch.long) loss_f = nn.MultiMarginLoss()
output = loss_f(inputs, target)
print('MultiMargin损失函数的计算结果:',output)
MultiMargin损失函数的计算结果: tensor(0.6000)
  • 三元组损失函数:torch.nn.TripletMarginLoss,用于处理<实体1,关系,实体2>类型的数据,计算该类型数据的损失
triplet_loss = nn.TripletMarginLoss(margin=1.0, p=2)
anchor = torch.randn(100, 128, requires_grad=True)
positive = torch.randn(100, 128, requires_grad=True)
negative = torch.randn(100, 128, requires_grad=True)output = triplet_loss(anchor, positive, negative)
output.backward()
print('TripletMargin损失函数的计算结果:',output)
TripletMargin损失函数的计算结果: tensor(1.1507, grad_fn=<MeanBackward0>)
  • HingEmbeddingLoss:torch.nn.HingeEmbeddingLoss,用于计算输出的embedding结果的Hing损失
loss_f = nn.HingeEmbeddingLoss()
inputs = torch.tensor([[1., 0.8, 0.5]])
target = torch.tensor([[1, 1, -1]])output = loss_f(inputs,target)
print('HingEmbedding损失函数的计算结果:',output)
HingEmbedding损失函数的计算结果: tensor(0.7667)
  • 余弦相似度:torch.nn.CosineEmbeddingLoss,用于计算两个向量的余弦相似度,如果两个向量距离近,则损失函数值小,反之亦然
loss_f = nn.CosineEmbeddingLoss()
inputs_1 = torch.tensor([[0.3, 0.5, 0.7], [0.3, 0.5, 0.7]])
inputs_2 = torch.tensor([[0.1, 0.3, 0.5], [0.1, 0.3, 0.5]])
target = torch.tensor([1, -1], dtype=torch.float)output = loss_f(inputs_1,inputs_2,target)
print('CosineEmbedding损失函数的计算结果:',output)
CosineEmbedding损失函数的计算结果: tensor(0.5000)
  • CTC损失函数:torch.nn.CTCLoss,用于处理时序数据的分类问题,计算连续时间序列和目标序列之间的损失
# Target are to be padded
# 序列长度
T = 50
# 类别数(包括空类)
C = 20
# batch size
N = 16
# Target sequence length of longest target in batch (padding length)
S = 30
# Minimum target length, for demonstration purposes
S_min = 10  input = torch.randn(T, N, C).log_softmax(2).detach().requires_grad_()
# 初始化target(0 = blank, 1:C = classes)
target = torch.randint(low=1, high=C, size=(N, S), dtype=torch.long)input_lengths = torch.full(size=(N,), fill_value=T, dtype=torch.long)
target_lengths = torch.randint(low=S_min, high=S, size=(N,), dtype=torch.long)ctc_loss = nn.CTCLoss()
loss = ctc_loss(input, target, input_lengths, target_lengths)
loss.backward()
print('CTC损失函数的计算结果:',loss)
CTC损失函数的计算结果: tensor(6.1333, grad_fn=<MeanBackward0>)

6 优化器

6.1 Optimizer的属性和方法

  • 使用方向:为了使求解参数过程更快,使用BP+优化器逼近求解

  • Optimizer的属性:

    • defaults:优化器的超参数
    • state:参数的缓存
    • param_groups:参数组,顺序是params,lr,momentum,dampening,weight_decay,nesterov
  • Optimizer的方法:

    • zero_grad():清空所管理参数的梯度
    • step():执行一步梯度更新
    • add_param_group():添加参数组
    • load_state_dict():加载状态参数字典,可以用来进行模型的断点续训练,继续上次的参数进行训练
    • state_dict():获取优化器当前状态信息字典

6.2 基本操作

# 设置权重,服从正态分布  --> 2 x 2
weight = torch.randn((2, 2), requires_grad=True)# 设置梯度为全1矩阵  --> 2 x 2
weight.grad = torch.ones((2, 2))# 输出现有的weight和data
print("The data of weight before step:\n{}".format(weight.data))
print("The grad of weight before step:\n{}".format(weight.grad))
The data of weight before step:
tensor([[-0.5871, -1.1311],[-1.0446,  0.2656]])
The grad of weight before step:
tensor([[1., 1.],[1., 1.]])
# 实例化优化器
optimizer = torch.optim.SGD([weight], lr=0.1, momentum=0.9)# 进行一步操作
optimizer.step()# 查看进行一步后的值,梯度
print("The data of weight after step:\n{}".format(weight.data))
print("The grad of weight after step:\n{}".format(weight.grad))
The data of weight after step:
tensor([[-0.6871, -1.2311],[-1.1446,  0.1656]])
The grad of weight after step:
tensor([[1., 1.],[1., 1.]])
# 权重清零
optimizer.zero_grad()# 检验权重是否为0
print("The grad of weight after optimizer.zero_grad():\n{}".format(weight.grad))
The grad of weight after optimizer.zero_grad():
tensor([[0., 0.],[0., 0.]])
# 添加参数:weight2
weight2 = torch.randn((3, 3), requires_grad=True)
optimizer.add_param_group({"params": weight2, 'lr': 0.0001, 'nesterov': True})# 查看现有的参数
print("optimizer.param_groups is\n{}".format(optimizer.param_groups))# 查看当前状态信息
opt_state_dict = optimizer.state_dict()
print("state_dict before step:\n", opt_state_dict)
optimizer.param_groups is
[{'params': [tensor([[-0.6871, -1.2311],[-1.1446,  0.1656]], requires_grad=True)], 'lr': 0.1, 'momentum': 0.9, 'dampening': 0, 'weight_decay': 0, 'nesterov': False}, {'params': [tensor([[ 0.0411, -0.6569,  0.7445],[-0.7056,  1.1146, -0.4409],[-0.2302, -1.1507, -1.3807]], requires_grad=True)], 'lr': 0.0001, 'nesterov': True, 'momentum': 0.9, 'dampening': 0, 'weight_decay': 0}]
state_dict before step:{'state': {0: {'momentum_buffer': tensor([[1., 1.],[1., 1.]])}}, 'param_groups': [{'lr': 0.1, 'momentum': 0.9, 'dampening': 0, 'weight_decay': 0, 'nesterov': False, 'params': [0]}, {'lr': 0.0001, 'nesterov': True, 'momentum': 0.9, 'dampening': 0, 'weight_decay': 0, 'params': [1]}]}
# 进行5次step操作
for _ in range(50):optimizer.step()
# 输出现有状态信息
print("state_dict after step:\n", optimizer.state_dict())
state_dict after step:{'state': {0: {'momentum_buffer': tensor([[0.0052, 0.0052],[0.0052, 0.0052]])}}, 'param_groups': [{'lr': 0.1, 'momentum': 0.9, 'dampening': 0, 'weight_decay': 0, 'nesterov': False, 'params': [0]}, {'lr': 0.0001, 'nesterov': True, 'momentum': 0.9, 'dampening': 0, 'weight_decay': 0, 'params': [1]}]}

7 训练与评估

def train(epoch):# 设置训练状态model.train()train_loss = 0# 循环读取DataLoader中的全部数据for data, label in train_loader:# 将数据放到GPU用于后续计算data, label = data.cuda(), label.cuda()# 将优化器的梯度清0optimizer.zero_grad()# 将数据输入给模型output = model(data)# 设置损失函数loss = criterion(label, output)# 将loss反向传播给网络loss.backward()# 使用优化器更新模型参数optimizer.step()# 累加训练损失train_loss += loss.item()*data.size(0)train_loss = train_loss/len(train_loader.dataset)print('Epoch: {} \tTraining Loss: {:.6f}'.format(epoch, train_loss))
def val(epoch):  # 设置验证状态model.eval()val_loss = 0# 不设置梯度with torch.no_grad():for data, label in val_loader:data, label = data.cuda(), label.cuda()output = model(data)preds = torch.argmax(output, 1)loss = criterion(output, label)val_loss += loss.item()*data.size(0)# 计算准确率running_accu += torch.sum(preds == label.data)val_loss = val_loss/len(val_loader.dataset)print('Epoch: {} \tTraining Loss: {:.6f}'.format(epoch, val_loss))

PyTorch学习笔记(三):PyTorch主要组成模块相关推荐

  1. PyTorch学习笔记:PyTorch初体验

    PyTorch学习笔记:PyTorch初体验 一.在Anaconda里安装PyTorch 1.进入虚拟环境mlcc 2.安装PyTorch 二.在PyTorch创建张量 1.启动mlcc环境下的Spy ...

  2. 【Pytorch学习笔记三】Pytorch神经网络包nn和优化器optm(一个简单的卷积神经网络模型的搭建)

    文章目录 一, 神经网络包nn 1.1定义一个网络 1.2 损失函数 二.优化器 nn构建于 Autograd之上,可用来定义和运行神经网络, PyTorch Autograd 让我们定义计算图和计算 ...

  3. pytorch学习笔记 1. pytorch基础 tensor运算

    pytorch与tensorflow是两个近些年来使用最为广泛的机器学习模块.开个新坑记录博主学习pytorch模块的过程,不定期更新学习进程. 文章较为适合初学者,欢迎对代码和理解指点讨论,下面进入 ...

  4. PyTorch学习笔记(15) ——PyTorch中的contiguous

    本文转载自栩风在知乎上的文章<PyTorch中的contiguous>.我觉得很好,特此转载. 0. 前言 本文讲解了pytorch中contiguous的含义.定义.实现,以及conti ...

  5. Spring Cloud 学习笔记(三) 之服务治理模块Spring Cloud 服务发现与消费

    2019独角兽企业重金招聘Python工程师标准>>> 一.前言 前面的文章我们已经讲了如何搭建服务注册中心,如何搭建客户端去注册.接下来我们讲一下服务的发现与消费 二.服务注册 1 ...

  6. PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 call

    您的位置 首页 PyTorch 学习笔记系列 PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 发布: 2017年8月4日 7,195阅读 ...

  7. PyTorch学习笔记(二)——回归

    PyTorch学习笔记(二)--回归 本文主要是用PyTorch来实现一个简单的回归任务. 编辑器:spyder 1.引入相应的包及生成伪数据 import torch import torch.nn ...

  8. 深度学习入门之PyTorch学习笔记:卷积神经网络

    深度学习入门之PyTorch学习笔记 绪论 1 深度学习介绍 2 深度学习框架 3 多层全连接网络 4 卷积神经网络 4.1 主要任务及起源 4.2 卷积神经网络的原理和结构 4.2.1 卷积层 1. ...

  9. pytorch学习笔记(2):在MNIST上实现一个CNN

    参考文档:https://mp.weixin.qq.com/s/1TtPWYqVkj2Gaa-3QrEG1A 这篇文章是在一个大家经常见到的数据集 MNIST 上实现一个简单的 CNN.我们会基于上一 ...

最新文章

  1. [ruby on rails] 跟我学之(6)显示指定数据
  2. JavaScript-方法
  3. 今晚直播丨用高效的Oracle性能诊断工具,让运维工作轻松起来!
  4. 安全数据通信:究竟什么是JSSE?
  5. pkm与png的相互转化
  6. TCP和UDP以及IP报文格式
  7. 2017年数模国赛B题第一小题的思路过程(个人思路)
  8. html数据如何分页显示,简单的html数据分页控件封装
  9. 关于3.3V稳压芯片
  10. 聊聊如何实施负载均衡
  11. MEM/MBA英语基础(07)复合句-定语从句
  12. Counterfeit Dollar(找硬币)
  13. 知物由学 | AI网络安全实战:生成对抗网络
  14. 网络安全专栏——修改电脑密码修改虚拟机密码(图文)
  15. 微信小程序怎么样与mysql一起开发_莲米粒是一个基于PHP+MySQL+微信小程序技术栈...
  16. DW大学生网页作业制作设计 ——旅游门户网站(21页)HTML+CSS+JavaScript
  17. android平板哪个好用吗,明明知道iPad更实用,却坚持买安卓平板,是一种什么心态?...
  18. 制作一枚有针对性的fork炸弹
  19. 【Electron-vue】创建桌面应用(12)- 修改electron窗口图标和桌面图标
  20. CVPR 2022 | “直面真实的世界”

热门文章

  1. 重新认识C#: 玩转指针
  2. mysqli得到记录数量
  3. AngularJS in Action读书笔记5(实战篇)——在directive中引入D3饼状图显示
  4. CentOS7下安装PostgresSQL9.4.4
  5. 剑指offer——面试题54:表示数值的字符串
  6. opencv多目标追踪容器
  7. 一些常用的google scholar网站
  8. 科研论文绘图:ppt, word,latex,python matplotlib绘图 ,矢量图,高清图,放大不失真
  9. Linux安装webmin
  10. 翻译连载 | 第 11 章:融会贯通 -《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇...