<>DAY 3-4 完结

1.一些简单损失函数的调用
损失函数loss的作用
1.计算实际输出和目标之间的差距
2.为我们更新输出提供一定的依据(反向传播), grad
import torch from torch.nn import L1Loss from torch import nn # 损失函数loss的作用 #
1.计算实际输出和目标之间的差距 # 2.为我们更新输出提供一定的依据(反向传播), grad inputs = torch.tensor([1,2,3] ,
dtype=torch.float32) targets = torch.tensor([1,2,5] , dtype=torch.float32) #
reshape成 batchsize:1 , channels:1 ,行数:1 ,列数: 1 inputs = torch.reshape(inputs ,
(1, 1,1,3) ) targets = torch.reshape(targets , (1,1,1,3)) #
L1Loss()是对应位相减,然后结果取平均 loss = L1Loss() result = loss(inputs , targets)
print(result) # MSE 平方差损失函数 # MSE = (0 + 0 +2^2)/3 = 1.333 loss_mse =
nn.MSELoss() result_mse = loss_mse(inputs , targets) print(result_mse) #
交叉熵CrossEntropyLoss , 用于分类问题中 x = torch.tensor([0.1 , 0.2 , 0.3]) y =
torch.tensor([1]) x = torch.reshape(x , [1,3]) loss_cross =
nn.CrossEntropyLoss() result_cross = loss_cross(x , y) print(result_cross)
2.在网络中加入损失函数
import torch import torchvision.datasets from torch import nn from torch.nn
import Conv2d, MaxPool2d, Linear, Flatten, Sequential from torch.utils.data
import DataLoader from torch.utils.tensorboard import SummaryWriter dataset =
torchvision.datasets.CIFAR10("dataseset_CIFAR10" , train=False ,
transform=torchvision.transforms.ToTensor(), download=True) dataloader =
DataLoader(dataset , batch_size=1) class Tudui(nn.Module): def __init__(self):
super(Tudui, self).__init__() # self.conv1 = Conv2d(3 , 32 , 5, stride=1 ,
padding=2) # self.maxpool1 = MaxPool2d(2) # self.conv2 = Conv2d( 32 , 32 , 5
,padding=2) # self.maxpool2 = MaxPool2d(2) # self.conv3 = Conv2d(32, 64 , 5
,padding=2) # self.maxpool3 = MaxPool2d(2) # # 输入层到隐藏层 # self.linear1 =
Linear(1024 , 64) # # 隐藏层到输出层 # self.linear2 = Linear(64 , 10) # self.flatten =
Flatten() # 引入一个Sequential,将做的操作打包成model1,以便下面使用 # 下面这段代码和上面注释的代码作用相同
self.model1 = Sequential(Conv2d(3 , 32 , 5, stride=1 , padding=2),
MaxPool2d(2), Conv2d( 32 , 32 , 5 ,padding=2), MaxPool2d(2), Conv2d(32, 64 , 5
,padding=2), MaxPool2d(2), Flatten(), Linear(1024 , 64), Linear(64 , 10)) def
forward(self , x): # x = self.conv1(x) # x = self.maxpool1(x) # x =
self.conv2(x) # x = self.maxpool2(x) # x = self.conv3(x) # x = self.maxpool3(x)
# x = self.flatten(x) # x = self.linear1(x) # x = self.linear2(x) #
下面的代码作用和上面相同 x = self.model1(x) return x # 在网络中加入损失函数 loss =
nn.CrossEntropyLoss() tudui = Tudui() for data in dataloader: imgs , targets =
data outputs = tudui(imgs) result_loss = loss(outputs , targets)
result_loss.backward() print("ok")
result_loss = loss(outputs , targets)
参数是实际结果与目标结果
result_loss.backward()
然后反向传播一下

3.优化器
# 定义一个优化器 , 学习速率lr设为 0.01 optim = torch.optim.SGD(tudui.parameters() ,
lr=0.01) 然后注意将每次循环的梯度清零,因为之前的梯度对现在没用```
import torch
import torchvision.datasets
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Linear, Flatten, Sequential
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10(“dataseset_CIFAR10” , train=False ,
transform=torchvision.transforms.ToTensor(),
download=True)
dataloader = DataLoader(dataset , batch_size=1)

class Tudui(nn.Module):
def init(self):
super(Tudui, self).init()
# self.conv1 = Conv2d(3 , 32 , 5, stride=1 , padding=2)
# self.maxpool1 = MaxPool2d(2)
# self.conv2 = Conv2d( 32 , 32 , 5 ,padding=2)
# self.maxpool2 = MaxPool2d(2)
# self.conv3 = Conv2d(32, 64 , 5 ,padding=2)
# self.maxpool3 = MaxPool2d(2)
# # 输入层到隐藏层
# self.linear1 = Linear(1024 , 64)
# # 隐藏层到输出层
# self.linear2 = Linear(64 , 10)
# self.flatten = Flatten()
# 引入一个Sequential,将做的操作打包成model1,以便下面使用 # 下面这段代码和上面注释的代码作用相同 self.model1 =
Sequential(Conv2d(3 , 32 , 5, stride=1 , padding=2), MaxPool2d(2), Conv2d( 32 ,
32 , 5 ,padding=2), MaxPool2d(2), Conv2d(32, 64 , 5 ,padding=2), MaxPool2d(2),
Flatten(), Linear(1024 , 64), Linear(64 , 10)) def forward(self , x): # x =
self.conv1(x) # x = self.maxpool1(x) # x = self.conv2(x) # x = self.maxpool2(x)
# x = self.conv3(x) # x = self.maxpool3(x) # x = self.flatten(x) # x =
self.linear1(x) # x = self.linear2(x) # 下面的代码作用和上面相同 x = self.model1(x) return x
loss = nn.CrossEntropyLoss()
tudui = Tudui()

<>定义一个优化器 , 学习速率lr设为 0.01

optim = torch.optim.SGD(tudui.parameters() , lr=0.01)
for epoch in range(20):
running_loss = 0.0
for data in dataloader:
imgs , targets = data
outputs = tudui(imgs)
result_loss = loss(outputs , targets)
# 将梯度清零 , 因为之前的梯度对现在没用
optim.zero_grad()
# 将梯度反向传播
result_loss.backward()
optim.step()
running_loss = running_loss + result_loss
print(running_loss)
***4.下载训练好和没训练的模型***
import torchvision

<># False是没训练的网络模型

from torch import nn

<>现有模型的使用和修改

vgg16_false = torchvision.models.vgg16(pretrained=False , progress=True)

<># True是训练好的网络模型

vgg16_true = torchvision.models.vgg16(pretrained=True , progress=True)
print(vgg16_true)

train_data = torchvision.datasets.CIFAR10(“dataseset_CIFAR10” , train=True ,
transform=torchvision.transforms.ToTensor(),
download=True)

<>在网络模型的classifier里添加一个线性层

<>因为CIFAR10输出的features是10 , 而vgg16最后输出的features是1000 , 所以需要转换一下

vgg16_true.classifier.add_module(‘7’ , nn.Linear(1000 , 10))
vgg16_false.classifier.add_module(‘7’ , nn.Linear(1000 , 10))
print(vgg16_true)
在现有模型中改动
vgg16_true.classifier.add_module(‘7’ , nn.Linear(1000 , 10))
***5.模型的两种保存方式***
import torch
import torchvision
from torch import nn

vgg16 = torchvision.models.vgg16(pretrained=False)

<>保存方式1–模型结构+模型参数

torch.save(vgg16 , “vgg16_method1.pth”)

<>保存方式2–模型参数(官方推荐)

<>把参数保存成字典

torch.save(vgg16.state_dict() , “vgg16_method2.pth”)

<>陷阱

class Tudui(nn.Module):
def init(self):
super(Tudui , self).init()
self.conv1 = nn.Conv2d(3 , 64 ,kernel_size=3)
def _slow_forward(self, x): x = self.conv1(x) return x
tudui = Tudui()
torch.save(tudui , “tudui_method1.pth”)
***6.两种保存方式对应的加载模型方式***
import torch
from torch import nn

<>方式1 —》 保存方式1 , 加载模型

import torchvision.models

model = torch.load(“vgg16_method1.pth”)
print(model)

<>方式2 加载模型

vgg16 =torchvision.models.vgg16(pretrained=False)
vgg16.load_state_dict(torch.load(“vgg16_method2.pth”))

<>陷阱1: 是运行不出来的 需要#注释部分 仅仅省略了##部分

<>class Tudui(nn.Module):

<>def init(self):

<>super(Tudui , self).init()

<>self.conv1 = nn.Conv2d(3 , 64 ,kernel_size=3)

<>

<>def _slow_forward(self, x):

<>x = self.conv1(x)

<>return x

<>#tudui = Tudui()

model = torch.load(‘tudui_method1.pth’)
print(model)
***8.一个完整的用cpu训练的模型***
import torch
from torch import nn

<>方式1 —》 保存方式1 , 加载模型

import torchvision.models

model = torch.load(“vgg16_method1.pth”)
print(model)

<>方式2 加载模型

vgg16 =torchvision.models.vgg16(pretrained=False)
vgg16.load_state_dict(torch.load(“vgg16_method2.pth”))

<>陷阱1: 是运行不出来的 需要#注释部分 仅仅省略了##部分

<>class Tudui(nn.Module):

<>def init(self):

<>super(Tudui , self).init()

<>self.conv1 = nn.Conv2d(3 , 64 ,kernel_size=3)

<>

<>def _slow_forward(self, x):

<>x = self.conv1(x)

<>return x

<>#tudui = Tudui()

model = torch.load(‘tudui_method1.pth’)
print(model)
**9.一个完整的用gpu训练的模型**
import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import time

#可以用cuda训练的东西

<>网络模型

<>数据(输入,标注)

<>损失函数

<>.cuda()

<>准备数据集

train_data = torchvision.datasets.CIFAR10(“dataseset_CIFAR10”,train=True
,transform=torchvision.transforms.ToTensor(),
download=True)
test_data = torchvision.datasets.CIFAR10(“dataseset_CIFAR10”,train=False
,transform=torchvision.transforms.ToTensor(),
download=True)

<>length 长度

train_data_size = len(train_data)
test_data_size = len(test_data)
print(“训练数据集长度为:{}”.format(train_data_size))
print(“测试数据集长度为:{}”.format(test_data_size))

<>利用DataLoader来加载数据集

train_data = DataLoader(train_data , batch_size=64)
test_data = DataLoader(test_data , batch_size=64)

<>搭建神经网络

class Tudui(nn.Module):
def init(self):
super(Tudui, self).init()
self.model = nn.Sequential(
nn.Conv2d(3,32,5,1,2),
nn.MaxPool2d(2),
nn.Conv2d(32,32,5,1,2),
nn.MaxPool2d(2),
nn.Conv2d(32,64,5,1,2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(1024,64),
nn.Linear(64,10)
)
def forward(self,x): x = self.model(x) return x
<>创建网络模型

tudui = Tudui()
tudui = tudui.cuda()

<>损失函数

loss_fn = nn.CrossEntropyLoss()
loss_fn = loss_fn.cuda()

<>优化器

learning_rate = 0.01
optimizer = torch.optim.SGD(tudui.parameters(),lr=learning_rate,)

<>设置训练网络的一些参数

<>记录训练的次数

total_train_step = 0

<>记录测试的次数

total_test_step = 0

<>训练的轮数

epoch = 10

<>添加Tensorboard

writer = SummaryWriter(“logs”)
start_time = time.time()

for i in range(epoch):
print("------第{}轮训练开始了----".format(i+1))
# 训练步骤开始 tudui.train() #这一行代码对一些特定的网络层有用,如dropout for data in train_data: imgs
, targets = data imgs = imgs.cuda() targets = targets.cuda() outputs =
tudui(imgs) loss = loss_fn(outputs , targets) #优化器优化模型 optimizer.zero_grad()
loss.backward() optimizer.step() total_train_step += 1 if total_train_step %
100 == 0: end_time = time.time() print("第{}轮训练时间是:{}".format(total_train_step,
end_time - start_time )) print("训练次数:{},Loss:{}".format(total_train_step ,
loss)) writer.add_scalar("train_loss" , loss.item(),total_train_step) # 测试步骤开始
tudui.eval() #同理,对一些特定的层有用 total_test_loss = 0 with torch.no_grad():
<>即不需要调优
for data in test_data: imgs , targets = data imgs = imgs.cuda() targets =
targets.cuda() outputs = tudui(imgs) loss = loss_fn(outputs , targets)
total_test_loss += loss.item() print("整体测试集上的Loss:{}".format(total_test_loss))
writer.add_scalar("test_loss",total_test_loss,total_test_step) total_test_step
+= 1
<>模型的保存
torch.save(tudui,"tudui_{}_gpu.pth".format(i)) print("模型已保存")
total_end_time = time.time()
print(“总训练时间是:{}”.format( total_end_time - start_time))

writer.close()
***10.模型的测试***
import torch
import torchvision.transforms
from PIL import Image
from torch import nn

image_path = “imgs/dog.png”
image = Image.open(image_path)
print(image)
image = image.convert(‘RGB’)

transform =
torchvision.transforms.Compose([torchvision.transforms.Resize((32,32)),
torchvision.transforms.ToTensor()])
image = transform(image)
print(image.shape)

class Tudui(nn.Module):
def init(self):
super(Tudui, self).init()
self.model = nn.Sequential(
nn.Conv2d(3,32,5,1,2),
nn.MaxPool2d(2),
nn.Conv2d(32,32,5,1,2),
nn.MaxPool2d(2),
nn.Conv2d(32,64,5,1,2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(1024,64),
nn.Linear(64,10)
)
def forward(self,x): x = self.model(x) return x
<>加载训练好的模型

model = torch.load(“tudui_9_gpu.pth”,map_location= torch.device(‘cpu’))
print(model)
image = torch.reshape(image , (1,3,32,32))
model.eval()
with torch.no_grad():
output = model(image)
print(output)
print(output.argmax(1))

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:766591547
关注微信