课程学习笔记,课程链接
卷积操作
官网 Pytorch 的 nn 模块有 Convolution Layers,有3种卷积操作,nn.Conv1d、nn.Conv2d、nn.Conv3d 分别对应一维二维以及三维。
注:在Pytorch 官网文档左侧,有 torch.nn 和 torch.nn.fuctional,torch.nn 是对 torch.nn.fuctional 进行了一个封装,方便用户使用。想细致的了解一些 nn 模块中的函数可以从 torch.nn.fuctional 入手。这里主要介绍 nn.Conv2d,打开 torch.nn.fuctional 对应页面,可以看到对 conv2d 函数的介绍:
conv2d 需要的参数有 输入 input、权重 weight(更专业的名称是卷积核)、偏置 bias、步长 stride等。
import torch
import torch.nn.functional as F
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]])
kernel = torch.tensor([[1, 2, 1],
[0, 1, 0],
[2, 1, 0]])
# ptyorch 提供的尺寸变换
# conv2d(N,C,H,W):N 为 batchsize 即输入图片的数量,C 为通道数,这是一个二维张量故通道为1,H 为高,W 为宽
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))
print(input.shape)
print(kernel.shape)
output = F.conv2d(input, kernel, stride=1)
print(output)
卷积层
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
import ssl
from torch.utils.tensorboard import SummaryWriter
ssl._create_default_https_context = ssl._create_unverified_context
dataset = torchvision.datasets.CIFAR10("./data", train=False, transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=16)
class Jiaolong(nn.Module):
def __init__(self) -> None:
super().__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 x
jiaolong = Jiaolong()
print(jiaolong)
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
imgs, targets = data
output = jiaolong(imgs)
print(output.shape)
print(imgs.shape)
# torch.Size([64, 3, 32, 32])
writer.add_images("input", imgs, step)
# torch.Size([64, 6, 30, 30]) -> torch.Size([xx, 3, 30, 30])
output = torch.reshape(output, (-1,3, 30, 30)) # 第一个数不知道是多少用 -1,它会自动根据值计算
writer.add_images("output", output,step)
step += 1
池化层
最大池化的作用是在保存数据特征的前提下去减小数据量。
cell_mode::当cell_mode为 True时,将用
cell模式代替
floor` 模式去计算输出。
import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("./data", train=False, download=True,
transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=36)
'''
# 最大池化无法对 Long 数据类型进行实现
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 Jiaolong(nn.Module):
def __init__(self) -> None:
super().__init__()
self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)
def forward(self, input):
output = self.maxpool1(input)
return output
jiaolong = Jiaolong()
# output = jiaolong(input)
# print(output)
writer = SummaryWriter("logs_maxpool")
step = 0
for data in dataloader:
imgs, targets = data
writer.add_images("input", imgs, step)
output = jiaolong(imgs)
writer.add_images("output", output, step)
step += 1
writer.close()
非线性激活
非线性激活主要目的就是给网络增加非线性特征,以便训练出符合要求的泛化模型。
import torch
from torch import nn
from torch.nn import ReLU
input = torch.tensor([[1, -0.5],
[-1, 3]])
input = torch.reshape(input, (-1, 1, 2, 2))
print(input.shape)
class Jiaolong(nn.Module):
def __init__(self) -> None:
super().__init__()
'''
inplace 为替换的意思
如 input = -1
ReLU(input, inplace = True)
input = 0
'''
self.relu = ReLU()
def forward(self, input):
output = self.relu(input)
return output
jiaolong = Jiaolong()
output = jiaolong(input)
print(output)
线性层
线性层又叫全连接层,其中每个神经元与上一层所有神经元相连,多看官方文档。
import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader
dataset = torchvision.datasets.CIFAR10("./data", train=False,
transform=torchvision.transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)
class Jiaolong(nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = Linear(196608, 10)
def forward(self, input):
output = self.linear(input)
return output
jiaolong = Jiaolong()
for data in dataloader:
imgs, targets = data
print(imgs.shape)
output = torch.flatten(imgs) # 展平
# output = torch.reshape(imgs, (1, 1, 1, -1))
print(output.shape)
output = jiaolong(output)
print((output.shape))