卷积是如何计算的

使用代码,看卷积是如何计算的。

torch.nn
torch.nn.functional

srtide 的用法,代表卷积核的步幅

在这里插入图片描述
在这里插入图片描述

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

# 尺寸变化,5*5 ,3*3
input = torch.reshape(input, (1,1,5,5))
kernel  = torch.reshape(kernel, (1,1,3,3))

print(input.shape)
print(kernel.shape)
# stride 代表每次移动的次数或者步幅
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)

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

padding 的用法为1的时候,代表把之前的矩阵四周拓展一个像素,

卷积层的使用

图片的一般都是二维矩阵,都使用CONV2D,1D 和3D用的较少

CLASStorch.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)[SOURCE

in_channels 输入的通道数
out_channels 输出的通道数
kernel_size 卷积核的大小
stride=1 卷积核的步幅大小
padding =0 对原始数据的一个填充数
dilation =1 卷积核的对应位
groups =1
bias =True 对卷积后的数是否加减一个数
padding_mode=‘zeros’

Parameters:
输入的通道数
in_channels (int) – Number of channels in the input image
输出的通道数 当等于2,就会出现2个卷积核,就会有两个输出
out_channels (int) – Number of channels produced by the convolution
卷积核的大小例如设置为3, 卷积核就是3*3,训练过程中不断的调整。
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
填充的地方设置为0
padding_mode (str, optional) – 'zeros', 'reflect', 'replicate' or 'circular'. Default: 'zeros'

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

在这里插入图片描述

动图,蓝色部分为输入图像,青色部分为输出图像

卷积层,

池化层
最常用的nn.MaxPool2d

torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
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

padding (Union[int, Tuple[int, int]]) – Implicit negative infinity padding to be added on both sides

dilation (Union[int, Tuple[int, int]]) – a parameter that controls the stride of elements in the window

return_indices (bool) – if True, will return the max indices along with the outputs. Useful for torch.nn.MaxUnpool2d later

ceil_mode (bool) – when True, will use ceil instead of floor to compute the output shape

在这里插入图片描述
cell_mode 为True的时候,要保留最大的值,当cell_mode为False的时候,不保留最大值。

import torch
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("../data", train=False, download=True,
                                       transform=torchvision.transforms.ToTensor())

dataloader = DataLoader(dataset, batch_size=64)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=False)

    def forward(self, input):
        output = self.maxpool1(input)
        return output

tudui = Tudui()

writer = SummaryWriter("../logs_maxpool")
step = 0
for data in dataloader:
    imgs, targets = data
    writer.add_images("input", imgs, step)
    output = tudui(imgs)
    writer.add_images("output", output, step)
    step = step + 1

writer.close()

为什么池化的作用,保留输入的一个特征,同时把数据量减小,数据量减小了,训练的更快
经过一层卷积之后,来一层池化,然后再来一次非线性激活。

非线性激活层,RELU

import torch
import torchvision
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

input = torch.tensor([[1, -0.5],
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeilNiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值