lyt-cloud

导航

简述卷积操作

 卷积操作

将一个人为规定大小(3*3)的卷积核,先以左上角的输入数据重合,按对应元素做乘法运算,再将该卷积核范围内的所有乘积求和。

步长(stride 卷积核每次移动的步数) stride=1 纵向横向都是1步 stride=(sH,Sw)控制纵向横向的步长不一致。

当卷积核移动到超过边缘时,不会在进行计算。

卷积核的大小是自己设置的,卷积核上每个位置相当于一个权重w,比如一个3*3的卷积核,就是9个w,训练网络的目的就是学习这9个权值

 

 1 # -*- coding = utf-8 -*-
 2 # @Time: 2022/10/10 16:24
 3 # @Author: Liu Yun tao
 4 # @File:nn_conv.py
 5 # @Software: PyCharm
 6 
 7 import torch
 8 import torch.nn.functional as F
 9 
10 input = torch.tensor([[1, 2, 0, 3, 1],
11                       [0, 1, 2, 3, 1],
12                       [1, 2, 1, 0, 0],
13                       [5, 2, 3, 1, 1],
14                       [2, 1, 0, 1, 1]])
15 
16 kernel = torch.tensor([[1, 2, 1],
17                        [0, 1, 0],
18                        [2, 1, 0]])
19 '''
20 weight – filters of shape (out_channels, in_channels/groups , kH , kW)
21 按照说明文档将输入转换成对应格式
22 '''
23 input = torch.reshape(input, (1, 1, 5, 5)) # 通道数是1 batchsize是1
24 kernel = torch.reshape(kernel, (1, 1, 3, 3))
25 
26 print(input.shape)
27 print(kernel.shape)
28 
29 output = F.conv2d(input, kernel, stride=1)
30 print(output)
31 
32 output2 = F.conv2d(input, kernel, stride=2)
33 print(output2)
34 
35 output3 = F.conv2d(input, kernel, stride=1, padding=1) # padding是对输入进行纵向 横向进行填充
36 print(output3)
# 输出结果
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]]]])

进程已结束,退出代码0

 

padding是对输入图像进行四周行列的填充(一般默认为0)

padding = 1 时输出的尺寸会和原来输入的图像大小一致。

 

posted on 2022-10-10 17:00  云☁️  阅读(178)  评论(0编辑  收藏  举报