Pytorch 学习笔记04 神经网络各层简单应用

Pytorch 学习笔记04

神经网络的基本骨架——nn.Module的使用

进入Pytorch官网,进入Doc找到torch.nn,选择Module,可以看到该类的基本使用方法。
在这里插入图片描述

自定义的神经网络都要继承该类,并重写函数__init__()和forward()。
代码:

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

# 数据集
dataset = torchvision.datasets.CIFAR10(root="./dataset/CIFAR10", train=True, transform=torchvision.transforms.ToTensor(), download=True)
# 数据加载器
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)

# 自定义神经网络
class MyNN(nn.Module):

    def __init__(self) -> None:
        super().__init__()
        # 卷积操作
        self.conv = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, )
    def forward(self, x):
        output = self.conv(x)
        return output

# 神经网络对象
mynn = MyNN()

# print(mynn)

# 用tensorboard展示
writer = SummaryWriter("logs")
step = 0
for imgs, tars in dataloader:
    output = mynn(imgs)
    # print(imgs.shape)
    # torch.Size([64, 3, 32, 32])
    # print(output.shape)
    # torch.Size([64, 3, 30, 30])
    output = torch.reshape(output,(-1,3,30,30))
    writer.add_images("conv2_input", imgs, step)
    writer.add_images("conv2_output", output, step)
    step += 1

writer.close()

卷积操作,以TORCH.NN.FUNCTIONAL.CONV2D为例

Applies a 2D convolution over an input image composed of several input planes.
对一个由多个输入平面组成的图像进行二维卷积操作。
主要参数

Parameters:
	inputinput tensor of shape (minibatch,in_channels,iH,iW) # 最小batch,通道数,高,宽 。 input为输入图像
	
	weight – filters of shape (out_channels, in_channels/groups,kH,kW) # 输出通道数,(没搞明白),高,宽。 weight是卷积核
	
	stride – the stride of the convolving kernel. Can be a single number or a tuple (sH, sW). Default: 1 # 卷积核移动的跨度
	
	padding –implicit paddings on both sides of the input.  # 对输入的图片进行填充

代码:

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]])
# 输入矩阵和卷积核不满足卷积函数所要求参数的格式,所以进行修改
input = torch.reshape(input,(1,1,5,5))
kernel = torch.reshape(kernel,(1,1,3,3))
# 卷积,跨度为1
output1 = F.conv2d(input,kernel,stride=1)
print(output1)
#卷积,跨度为2
output2 = F.conv2d(input,kernel,stride=2)
print(output2)

池化操作,以torch.nn.MaxPool2d为例

池化操作在深度学习中起到了降维、减少参数、提高模型鲁棒性和泛化能力等作用,有助于简化模型并提取关键特征。

Applies a 2D max pooling over an input signal composed of several input planes.

主要参数

Parameters:
	kernel_size (Union[int, Tuple[int, int]]) – the size of the window to take a max over  # 池化核的尺寸
	stride (Union[int, Tuple[int, int]]) – the stride of the window. Default value is kernel_size   # 池化核的移动跨度
	dilation (Union[int, Tuple[int, int]]) – a parameter that controls the stride of elements in the window # 用于控制池化核中元素的跨度
	ceil_mode (bool) – when True, will use ceil instead of floor to compute the output shape  # 看图

Ceil_model

代码

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("./dataset/CIFAR10", train=True,
                                       transform=torchvision.transforms.ToTensor(),
                                       download= True)
# 数据加载器
dataloader = DataLoader(dataset, 32)

# 自定义神经网络
class nn_pool(nn.Module):
    def __init__(self):
        super(nn_pool, self).__init__()
        self.pool = MaxPool2d(kernel_size=3, stride=1, ceil_mode= True) # 池化
    def forward(self, input):
        output = self.pool(input)
        return output

# 神经网络对象
pool = nn_pool()

# 通过tensorboard展示
step = 0
writer = SummaryWriter("logs")
for imgs,tars in dataloader:
    writer.add_images("maxpool_no", imgs, step)
    output = pool(imgs) # 对图像进行池化, 得到结果
    writer.add_images("maxpool_yes", output, step)
    step += 1
writer.close()

在这里插入图片描述

非线性变换,以torch.nn.Sigmoid为例

非线性变换的作用在于增强模型的表达能力、拟合复杂的非线性关系、提取高层次抽象表示,并改善模型对数据的刻画能力。这使得深度学习模型能够更好地适应现实世界中的复杂数据和任务。

主要参数: 无

代码

import torchvision
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

# 数据集
dataset = torchvision.datasets.CIFAR10("./dataset/CIFAR10", train=True,
                                       transform=torchvision.transforms.ToTensor(),
                                       download=True)
# 数据加载器
dataloader = DataLoader(dataset, batch_size=32)

# 自定义神经网络
class nn_sigmod(nn.Module):
    def __init__(self):
        super(nn_sigmod, self).__init__()
        self.sigmod = nn.Sigmoid()
    def forward(self, input):
        return self.sigmod(input)

# 对象
sigmod = nn_sigmod()

# 用tensorboard展示
writer = SummaryWriter("logs")
step = 0
for imgs, tars in dataloader:
    writer.add_images("Sigmod_no", imgs, step)
    output = sigmod(imgs)
    writer.add_images("Sigmod_yes", output, step)
    step += 1

writer.close()

在这里插入图片描述

总结

多查阅官方文档,有详细的使用方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值