Pytorch学习之卷积层

本节对卷积层进行介绍

主要是对二维卷积层进行介绍conv2d

import torch
import torch.nn.functional as F

官方文档讲解

torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride=1, padding=0,
dilation=1, groups=1, bias=True,
padding_mode=‘zeros’,
device=None, dtype=None)

参数讲解

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

滑动步长(呃呃我是这么叫的)====>这个默认是1,可以不设置

padding (int, tuple or str, optional) – Padding added to all four sides of the input. Default: 0

填充,默认是0

padding_mode (string, optional) – ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’

padding填充时填充的数值

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

需要输入的参数

conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) -> Tensor
input:输入图片 ====>(n,in_channels,h,w) n:代表几张图片 h,w:皆是输入图片的大小
weight:卷积核 ====>(n,out_channels,h,w) n:代表几张图片,h,w:皆是输出图片的大小
bias:偏置默认为0
stride:滑动步长
padding:填充,默认为0

其他参数可不管他

实验

假设给出一个二维图片只有一个通道
image = 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]])
image = torch.reshape(image, (1, 1, 5, 5))  # 改变形状===>(n,in_channels,h,w)  n:代表几张图片 h,w:皆是输入图片的大小
kernel = torch.Tensor([[1, 2, 1],
                       [0, 1, 0],
                       [2, 1, 0]])
kernel = torch.reshape(kernel, (1, 1, 3, 3))  # 改变形状===>(n,out_channels,h,w)  n:代表几张图片,h,w:皆是输出图片的大小
## 测试stride
conv_image1 = F.conv2d(input=image, weight=kernel, padding=0, stride=1)
conv_image2 = F.conv2d(input=image, weight=kernel, padding=0, stride=2)
print(conv_image1)
print(conv_image2)
tensor([[[[10., 12., 12.],
          [18., 16., 16.],
          [13.,  9.,  3.]]]])
tensor([[[[10., 12.],
          [13.,  3.]]]])
## 测试padding  =======>padding=数,这个数是padding的长度,也可以是元祖(h,w)分别表示长和宽方向上padding的长度
conv_image3 = F.conv2d(input=image, weight=kernel, padding=1, stride=1)
print(conv_image3)
tensor([[[[ 1.,  3.,  4., 10.,  8.],
          [ 5., 10., 12., 12.,  6.],
          [ 7., 18., 16., 16.,  8.],
          [11., 13.,  9.,  3.,  4.],
          [14., 13.,  9.,  7.,  4.]]]])
# 实验一维卷积
x = torch.Tensor([1, 2, 3, 4, 5, 6, 7])
x = torch.reshape(x, (1, 1, 7))
kernel = torch.Tensor([8, 9, 10])
kernel = torch.reshape(kernel, (1, 1, 3))
conv_x = F.conv1d(x, kernel, stride=1, padding=2)  # 和一维卷积效果一样,不填充时,和一维卷积same模式一样
print(conv_x)
tensor([[[ 10.,  29.,  56.,  83., 110., 137., 164., 111.,  56.]]])

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值