学习pytorch9 神经网络-卷积层

神经网络-卷积层

官网

https://pytorch.org/docs/stable/nn.html#convolution-layers

图像识别常用conv2d 二维卷积 nn.Conv2d
https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d

卷积数据公式


参数说明

Parameters:

  • in_channels (int) -- Number of channels in the input image 输入通道

  • out_channels (int) -- Number of channels produced by the convolution 输出通道

  • kernel_size (int or tuple) -- Size of the convolving kernel 卷积核大小

  • stride (int or tuple, optional) -- Stride of the convolution. Default: 1 每次卷积走多少步,横向纵向的步径大小

  • padding (int, tuple or str, optional) -- Padding added to all four sides of the input. Default: 0 是否在卷积过程中对输入图像的边缘进行填充

  • padding_mode (str, optional) -- 'zeros', 'reflect', 'replicate' or 'circular'. Default: 'zeros' 填充数据的模式是什么,默认为zeros,填充的都是0

  • dilation (int or tuple, optional) -- Spacing between kernel elements. Default: 1 卷积核中间的距离? 一般不改 不常用

  • groups (int, optional) -- Number of blocked connections from input channels to output channels. Default: 1 一般不改 不常用

  • bias (bool, optional) -- If True, adds a learnable bias to the output. Default: True 添加偏置值,默认为True添加偏置值

卷积运算演示

https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md

绿色方格:表示输出图像

蓝色方格:表示输入图像

蓝色方格中的深色阴影部分:表示kernel 卷积核

白色虚线:表示padding填充

动画中深色阴影上下左右整体移动的方格数,表示stride的大小

输入输出channel

两个卷积核做两次卷积,叠加输出一起是out_channel=2

代码

注意点:

  1. super()括号里面没有内容,自动填充的self应该去掉
py 复制代码
super().__init__() 

code

py 复制代码
import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

test_set = torchvision.datasets.CIFAR10(root='./dataset', train=False, transform=torchvision.transforms.ToTensor(), download=True)

dataloader = DataLoader(test_set, batch_size=64, shuffle=False)

class NnConv2d(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)

    def forward(self, x):
        x = self.conv1(x)
        return x

nnconvd = NnConv2d()
writer = SummaryWriter('./logs')
step = 0
for data in dataloader:
    imgs, targets = data
    output = nnconvd(imgs)
    print(imgs.shape)
    print(output.shape)
    writer.add_images("input", imgs)
    output = output.reshape([-1, 3, 30, 30])
    writer.add_images("output", output)
    step += 1

执行结果

sh 复制代码
......
torch.Size([64, 6, 30, 30])
torch.Size([64, 3, 32, 32])
torch.Size([64, 6, 30, 30])
torch.Size([64, 3, 32, 32])
torch.Size([64, 6, 30, 30])
torch.Size([64, 3, 32, 32])
torch.Size([64, 6, 30, 30])
torch.Size([64, 3, 32, 32])
torch.Size([64, 6, 30, 30])
torch.Size([64, 3, 32, 32])
torch.Size([64, 6, 30, 30])
torch.Size([16, 3, 32, 32])
torch.Size([16, 6, 30, 30])  # 最后一个batch16是因为drop_last默认为False,最后没除尽的也要保留,参与训练
相关推荐
Tom·Ge5 分钟前
【AI前沿】2026.08.17 SpaceX 600亿收购Cursor正式完成·DeepSeek V4 Pro万亿开源·人形机器人生态全面爆发
人工智能·大模型·ai前沿
郑州光合科技余经理1 小时前
本地生活服务系统:成品模块和定制接口怎么划界
java·前端·人工智能·后端·系统架构·php·ai编程
2603_965148111 小时前
家居百货蓝海:API挖掘高复购率生活小商品
大数据·服务器·人工智能·python·生活
2501_930472441 小时前
踩坑|CodeBuddy权限配置:AI误删文件、乱跑命令、.env泄露怎么防
人工智能·ai编程
小淮AI4 小时前
从“刷题”到“追问”:AI课堂正在重塑哪些学习旧习惯?
人工智能·学习
阿童木写作4 小时前
跨境电商图片翻译工具推荐:批量图片翻译+视频字幕翻译+智能抠图
python·macos·音视频·xcode
cui_ruicheng5 小时前
LangChain 应用开发(十四):Agent 上下文与记忆机制
服务器·人工智能·python·langchain
飞哥数智坊5 小时前
直播半小时,我聊了聊 Agent 最常见的 3 个疑问
人工智能
circuitsosk5 小时前
任务规划器的三种范式对比:ReAct、Plan-and-Execute 与 Tree-of-Thought 在真实业务中的取舍
前端·javascript·python·react.js·llm·ai agent
shehuiyuelaiyuehao5 小时前
算法31,前缀和,可被k整除的子数组
数据结构·python·算法