目录
1、神经网络的基本骨架-nn.Module的使用
2、卷积操作实例
3、神经网络-卷积层
4、神经网络-最大池化的使用
(1)最大池化画图理解:
(2)代码实现:
5、神经网络-非线性激活
(1)代码实现(调用sigmoid 函数)
6、神经网络-线性层
(1)代码
7、网络搭建-小实战
(1)完整代码
1、神经网络的基本骨架-nn.Module的使用
官网地址:pytorch里的nn
import torch
from torch import nnclass Tudui(nn.Module):def __init__(self):super().__init__()def forward(self, input):output = input + 1return outputtudui = Tudui()
x = torch.tensor(1.0)
output = tudui(x)
print(output)
2、卷积操作实例
import torch
import torch.nn.functional as Finput = torch.tensor([[1, 2, 0, 3, 1],[0, 1, 2, 3, 1],[1, 2, 1, 0, 0],[5, 2, 3, 1, 1],[2, 1, 0, 1, 1]])
kernel = torch.tensor([[1, 2, 1],[0, 1, 0],[2, 1, 0]])# 转换成要求的格式 shape(N,C,H,W)
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))print(input.shape)
print(kernel.shape)
# stride=1 的情况
output = F.conv2d(input, kernel, stride=1)
print(output)# stride=2 的情况
output2 = F.conv2d(input, kernel, stride=2)
print(output2)# 设置了padding
output3 = F.conv2d(input, kernel, stride=1, padding=1)
print(output3)
运行结果:
3、神经网络-卷积层
Conv2d:文档地址torch.nn.Conv2d
in_channels 输入的通道数
out_channels 输出的通道数
kernel_size 卷积核大小
stride 默认为移动为1
padding是否在边缘进行填充
例子:
import torch
import torchvision
import ssl
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriterssl._create_default_https_context = ssl._create_unverified_contextdataset = torchvision.datasets.CIFAR10(root='./test11_data', train=False,transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=64)class Tudui(nn.Module):def __init__(self):super(Tudui, self).__init__()self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0 )def forward(self, x):x = self.conv1(x)return xtudui = Tudui()
writer = SummaryWriter('test11_logs')
step = 0
for data in dataloader:imgs, targets = dataoutput = tudui(imgs)writer.add_images("input", imgs, step)output = torch.reshape(output, (-1, 3, 30, 30), ) # 不知道是多少的时候,直接写-1writer.add_images("output", output, step)step = step + 1writer.close()
结果输出:
4、神经网络-最大池化的使用
(1)最大池化画图理解:
(2)代码实现:
import torch
from torch import nn
from torch.nn import MaxPool2dinput = torch.tensor([[1, 2, 0, 3, 1],[0, 1, 2, 3, 1],[1, 2, 1, 0, 0],[5, 2, 3, 1, 1],[2, 1, 0, 1, 1]], dtype=torch.float32)input = torch.reshape(input, (-1, 1, 5, 5))
print(input.shape)class Tudui(nn.Module):def __init__(self):super(Tudui, self).__init__()self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)def forward(self,input):output = self.maxpool1(input)return outputtudui = Tudui()
output = tudui(input)
print(output)
运行结果:
(3)展示池化的图片(代码)
import torch
import ssl
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
ssl._create_default_https_context = ssl._create_unverified_contextdataset = torchvision.datasets.CIFAR10("./test12_data", train=False, download=True,transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64)# input = torch.tensor([[1, 2, 0, 3, 1],
# [0, 1, 2, 3, 1],
# [1, 2, 1, 0, 0],
# [5, 2, 3, 1, 1],
# [2, 1, 0, 1, 1]], dtype=torch.float32)
#
#
# input = torch.reshape(input, (-1, 1, 5, 5))
# print(input.shape)class Tudui(nn.Module):def __init__(self):super(Tudui, self).__init__()self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)def forward(self,input):output = self.maxpool1(input)return outputwriter = SummaryWriter("test12_logs_maxpool")
tudui = Tudui()
step = 0
for data in dataloader:imgs, targets = datawriter.add_images("input", imgs, step)output = tudui(imgs)writer.add_images("output", output, step)step = step + 1writer.close()# tudui = Tudui()
# output = tudui(input)
# print(output)
运行结果:
5、神经网络-非线性激活
非线性激活函数
(1)代码实现(调用sigmoid 函数)
import torch
import ssl
import torchvision
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
ssl._create_default_https_context = ssl._create_unverified_contextinput = torch.tensor([[1, -0.5],[-1, 3]])input = torch.reshape(input, (-1, 1, 2, 2))
print(input.shape)dataset = torchvision.datasets.CIFAR10("./test13_data", train=False, download=True,transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64)class Tudui(nn.Module):def __init__(self):super(Tudui, self).__init__()self.relu1 = ReLU()self.sigmoid1 = Sigmoid()def forward(self,input):output_ = self.sigmoid1(input)return output_tudui = Tudui()
writer = SummaryWriter("test13_logs_sigmoid")
step = 0
for data in dataloader:imgs, targets = datawriter.add_images("input", imgs, global_step=step)output = tudui(imgs)writer.add_images("output", output, step)step = step + 1writer.close()
输出结果:
6、神经网络-线性层
(1)代码
import torch
import ssl
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader
ssl._create_default_https_context = ssl._create_unverified_contextdataset = torchvision.datasets.CIFAR10('./test14_data', train=False, download=True,transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)class Tudui(nn.Module):def __init__(self):super(Tudui, self).__init__()self.linear1 = Linear(196608, 10)def forward(self,input):output = self.linear1(input)return outputtudui = Tudui()for data in dataloader:imgs, targets = dataprint(imgs.shape)output = torch.reshape(imgs, (1,1,1,-1)) # torch.Size([1, 1, 1, 196608])# output = torch.flatten(imgs) # 会变成一行 torch.Size([196608])print(output.shape)output = tudui(output)print(output.shape)
结果展示:
7、网络搭建-小实战
(1)完整代码
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriterclass Tudui(nn.Module):def __init__(self):super(Tudui, self).__init__()# self.conv1 = Conv2d(in_channels=3, out_channels=32, kernel_size=5, padding=2) # 卷积# self.maxpool1 = MaxPool2d(2) # 池化# self.conv2 = Conv2d(in_channels=32, out_channels=32, kernel_size=5, padding=2)# self.maxpool2 = MaxPool2d(2)# self.conv3 = Conv2d(in_channels=32, out_channels=64, kernel_size=5, padding=2)# self.maxpool3 = MaxPool2d(2)# self.flatten = Flatten()# self.linear1 = Linear(in_features=1024, out_features=64)# self.linear2 = Linear(in_features=64, out_features=10)self.model1 = Sequential(Conv2d(in_channels=3, out_channels=32, kernel_size=5, padding=2),MaxPool2d(2),Conv2d(in_channels=32, out_channels=32, kernel_size=5, padding=2),MaxPool2d(2),Conv2d(in_channels=32, out_channels=64, kernel_size=5, padding=2),MaxPool2d(2),Flatten(),Linear(in_features=1024, out_features=64),Linear(in_features=64, out_features=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 xtudui = Tudui()
print(tudui) # 输出网络结构input = torch.ones((64, 3, 32, 32))
output = tudui(input)
print(output.shape)writer = SummaryWriter('test15_logs')
writer.add_graph(tudui, input)
writer.close()
运行结果: