sunpongber

土堆说卷积操作(可选看)

主要来学习关于神经网络中的一些基本的神经结构的使用

PyTorch官网

Convolution Layers(卷积层)

nn.Conv1d->对由多个输入平面组成的输入信号进行一维卷积。
nn.Conv2d->对由多个输入平面组成的输入信号进行二维卷积。
nn.Conv3d->对由多个输入平面组成的输入信号进行三维卷积。

Conv2d,用代码的方式据一些例子,看看卷积是如何计算的,或者说图像中如何运用卷积操作

左侧API中,有以下两个:
torch.nn
torch.nn.functional
torch.nn是对functional进行一个封装,更利于我们使用

想更细致的了解一下卷积的操作,需要学习torch.nn.functional
其实学会了torch.nn即可,torch.nn.functional不需要了解

torch.nn.functional.conv2d

Parameters: input, weight, bias, stride, padding, dilation, groups

输入图像(5×5),卷积核(3×3),stride=1走一步,输入图像×卷积核=卷积后的输出

the stride of the convolving kernel. Can be a single number or a tuple (sH, sW). Default: 1
横向步进、纵向步进,默认为1

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

print(input.shape)
print(kernel.shape)

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)

output2 = F.conv2d(input, kernel, stride=2)
print(output2)

output3 = F.conv2d(input, kernel, stride=1, padding=1)
print(output3)

返回:

torch.Size([5, 5])
torch.Size([3, 3])
torch.Size([1, 1, 5, 5])
torch.Size([1, 1, 3, 3])
tensor([[[[10, 12, 12],
          [18, 16, 16],
          [13,  9,  3]]]])
tensor([[[[10, 12],
          [13,  3]]]])
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]]]])

这里的维度顺序通常是 (batch_size, channels, height, width),这是PyTorch中CNN的标准输入格式
这样的形状(1, 1, 5, 5)往往是将一个5x5的二维数据(比如一个 5x5 的灰度图像区域)转换成一个符合CNN输入要求的4维张量(批量大小、通道数、高度、宽度)。

原始资料地址:
土堆说卷积操作(可选看)
如有侵权联系删除 仅供学习交流使用