pytorch训练模型大体流程

学习记录:

常用api:

#卷积层
conv2d = nn.Conv2d(in_channels,out_channels,kernel_size=3,stride=1,padding=1)
#输入通道数,输出通道数,卷积核大小,移动步长,补0大小

#批量归一化
bn = nn.BatchNorm2d(out_channels) #紧跟在卷积层后,参数为卷积层输出通道数

#激活层
relu = nn.ReLU(inplace=True)

#池化层
pool = nn.MaxPool2d(kernel_size=2,stride=2)

#反卷积层
deconv = nn.ConvTranspose2d(in_channels,out_channels,kernel_size=3,stride=2,padding=1)

训练大体流程

# -*- coding:utf-8 -*-
import torch
import torchvision
from torch import nn,optim
from torchvision import datasets
from torchvision import transforms
from torch.utils.data import DataLoader
from torch.nn import functional as F

#外部定义
criterion = nn.NLLLoss().cuda()
optimizer = optim.Adam(net.parameters(),lr=LR)
#训练过程
optimizer.zero_grad() # 梯度清0
out = net(imgData)
out = F.log_softmax(out,dim=1)
loss = criterion(out,imgLabel)
loss.backward()  #反向传播
optimizer.step() #更新梯度信息



 

你可能感兴趣的:(深度学习,图像处理,python)