PyTorch学习笔记(六)——卷积层

torch.nn提供的工具:

  • Containers:神经网络的骨架
  • Convolution Layers:卷积层
  • Pooling Layers:池化层
  • Non-linear Activations:非线性激活
  • Normalization Layers:正则化层

1 Containers

Containers包含的模块:

 其中最常用的是 Module 模块(为所有神经网络提供基本骨架)

1.1  Module 模块

  • __init__:初始化函数
  • forward: 前向传播
    • conv为卷积,relu为非线性处理

举例:

import torch
from torch import nn

class MyNN(nn.Module): #搭建的神经网络MyNN继承了Module类(父类)
    def __init__(self): #初始化函数
        super().__init__() #必须调用父类的初始化函数

    def forward(self,input): #前向传播(输入和输出中间的处理过程)
        output = input + 1
        return output

mynn = MyNN()
x = torch.tensor(1.0)
output = mynn(x)
print(output)

2 Convolution Layers

 2.1 关于卷积操作  

torch.nn 是 torch.nn.functional 的封装,可先查看torch.nn.functional

TORCH.NN.FUNCTIONAL.CONV2D:

  •  input:输入,尺寸要求4个参数 (batch,通道数,高,宽)
  • weight:权重/卷积核,尺寸要求4个参数

使用 torch.reshape 函数改变维度

  • bias:偏置
  • stride:步进
  • padding:填充,对输入图像进行填充,值可以为一个数或一个元组,默认情况下不进行填充例如padding=1:将输入图像左右上下两边都拓展一个像素,空的地方默认为0

举例:

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]])

kernal = torch.tensor([[1,2,1],
                       [0,1,0],
                       [2,1,0]])

# 尺寸只有高和宽,不符合要求
print(input.shape) #5×5
print(kernal.shape) #3×3


# 尺寸变换为符合条件的四个参数
input = torch.reshape(input,(1,1,5,5))
kernal = torch.reshape(kernal,(1,1,3,3))

# 尺寸符合要求
print(input.shape)
print(kernal.shape)

# 无padding,步进为1
output = F.conv2d(input,kernal,stride=1)
print('output',output)

# 无padding,步进为2
output2 = F.conv2d(input,kernal,stride=2)
print('output2',output2)

# padding为1,步进为1
output3 = F.conv2d(input,kernal,stride=1,padding=1)
print('output3',output3)

 2.2 Conv2d

  • in_channels :输入的通道数,彩色图像 in_channels的值为3
  • out_channels :输出的通道数
    • in_channels 和 out_channels 都为 1 时,拿一个卷积核在输入图像中进行操作
    • out_channels 为 2 时,卷积层会生成两个卷积核(不一定一样),得到两个输出,叠加后作为最后输出
  • kernel_size :数或元组,卷积核大小 
    • 卷积核的参数是从一些分布中进行采样得到的
    • 实际训练过程中,卷积核中的值会不断进行调整
  • stride=1 :卷积过程中的步进大小
  • padding=0 :卷积过程中对原始图像边缘是否进行填充
  • dilation=1 :每一个卷积核对应位的距离
  • groups=1 :一般设置为1,很少改动,改动的话为分组卷积
  • bias=True :通常为True,对卷积后的结果是否加减一个常数的偏置
  • padding_mode='zeros' :选择padding填充的模式

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("./datasets",train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset,batch_size=64)

# 搭建神经网络
class MyNN(nn.Module):
    def __init__(self):
        super(MyNN, 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 x

mynn = MyNN()
# 打印网络结构
print(mynn) #MyNN((conv1): Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1)))

writer = SummaryWriter("log3")
step = 0
for data in dataloader:
    imgs,targets = data
    output = mynn(imgs)
    print(imgs.shape) #输入大小 torch.Size([64, 3, 32, 32])  batch_size=64,in_channels=3(彩色图像),每张图片是32×32的
    print(output.shape) #经过卷积后的输出大小 torch.Size([64, 6, 30, 30])  卷积后变成6个channels,但原始图像减小,所以是30×30的
    writer.add_images("input",imgs,step)
    #经过卷积后的输出大小 torch.Size([64, 6, 30, 30])  卷积后变成6个channels,但原始图像减小,所以是30×30的
    output = torch.reshape(output,(-1,3,30,30))
    writer.add_images("output",output,step)
    step += 1

writer.close()
  • 其中out_channels=6代表输出图片的通道数是6通道的,6通道数的图片无法显示,直接使用tensorboard可视化会报错,所以在执行writer.add_images("output",output,step)这行代码前对output进行reshape,reshape成3通道数的图片。
  • reshape的第一个参数未知时,可以填入-1,会自动进行计算
  • 改变通道数相当于把多余的像素给切出来了,放到了batch_size中。

 64张输入图像,卷积后的图像数64×2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pytorch是机器学习中的一个重要框架,它与TensorFlow一起被认为是机器学习的两大框架。Pytorch学习可以从以下几个方面入手: 1. Pytorch基本语法:了解Pytorch的基本语法和操作,包括张量(Tensors)的创建、导入torch库、基本运算等\[2\]。 2. Pytorch中的autograd:了解autograd的概念和使用方法,它是Pytorch中用于自动计算梯度的工具,可以方便地进行反向传播\[2\]。 3. 使用Pytorch构建一个神经网络:学习使用torch.nn库构建神经网络的典型流程,包括定义网络结构、损失函数、反向传播和更新网络参数等\[2\]。 4. 使用Pytorch构建一个分类器:了解如何使用Pytorch构建一个分类器,包括任务和数据介绍、训练分类器的步骤以及在GPU上进行训练等\[2\]。 5. Pytorch的安装:可以通过pip命令安装Pytorch,具体命令为"pip install torch torchvision torchaudio",这样就可以在Python环境中使用Pytorch了\[3\]。 以上是一些关于Pytorch学习笔记,希望对你有帮助。如果你需要更详细的学习资料,可以参考引用\[1\]中提到的网上帖子,或者查阅Pytorch官方文档。 #### 引用[.reference_title] - *1* [pytorch自学笔记](https://blog.csdn.net/qq_41597915/article/details/123415393)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Pytorch学习笔记](https://blog.csdn.net/pizm123/article/details/126748381)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值