【pytorch卷1】=2= CNN, 卷积函数

目录

1. Introduction组建卷积函数

2. 把input,padding,卷积核,卷积函数走一波

2.1 input

2.2 padding

2.3 卷积核

3. 卷积函数走一走


1. Introduction组建卷积函数

torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1

包括的几个部分

  • input:输入
  • weight:卷积核权重
  • bias:卷积计算时,偏置权重
  • stride: 步长,
  • padding
  • dilation:卷积核每个元素之间的间距,默认为1
  • groups:将输入进行分组操作

2. 把input,padding,卷积核,卷积函数走一波

2.1 input

import torch

input1 = torch.ones([1, 1, 5, 5])
input2 = torch.ones([1,2,5,5])
input3 = torch.ones([1,1,4,4])


print(input1)
print(input2)
print(input3)
【一个batch的图片数量,图像通道数(决定有几个tensor阵),图片高度,图片宽度】

输出。
tensor([[[[1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.]]]])
tensor([[[[1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.]],

         [[1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.],
          [1., 1., 1., 1., 1.]]]])
tensor([[[[1., 1., 1., 1.],
          [1., 1., 1., 1.],
          [1., 1., 1., 1.],
          [1., 1., 1., 1.]]]])

2.2 padding

padding1 = torch.nn.functional.conv2d(input1, torch.ones([1,1,1,1]), stride=1, padding=(1,1))
print(padding1)
padding2 = torch.nn.functional.conv2d(input1, torch.ones([1,1,1,1,]),stride=1, padding=(1,2))
print(padding2)

输出--padding=(行,列)

tensor([[[[0., 0., 0., 0., 0., 0., 0.],
          [0., 1., 1., 1., 1., 1., 0.],
          [0., 1., 1., 1., 1., 1., 0.],
          [0., 1., 1., 1., 1., 1., 0.],
          [0., 1., 1., 1., 1., 1., 0.],
          [0., 1., 1., 1., 1., 1., 0.],
          [0., 0., 0., 0., 0., 0., 0.]]]])
tensor([[[[0., 0., 0., 0., 0., 0., 0., 0., 0.],
          [0., 0., 1., 1., 1., 1., 1., 0., 0.],
          [0., 0., 1., 1., 1., 1., 1., 0., 0.],
          [0., 0., 1., 1., 1., 1., 1., 0., 0.],
          [0., 0., 1., 1., 1., 1., 1., 0., 0.],
          [0., 0., 1., 1., 1., 1., 1., 0., 0.],
          [0., 0., 0., 0., 0., 0., 0., 0., 0.]]]])

2.3 卷积核

filters1 = torch.tensor([-1.0,0,0,-1]).reshape([1,1,2,2])
filters2 = torch.tensor([-1.0, 0, 0, -1, -1,0,0,-1]).reshape([2,1,2,2])
filters3 = torch.tensor([-1.0,0,0,-1, -1.0,0,0,-1, -1.0,0,0,-1]).reshape([3,1,2,2])
filters4 = torch.tensor([-1.0,0,0,-1, -1.0,0,0,-1, -1.0,0,0,-1, -1.0,0,0,-1]).reshape([2,2,2,2])
filters5 = torch.tensor([-1.0,0,0,-1, -1.0, 0,0,-1]).reshape([1,2,2,2])

print(filters1)
print(filters2)
print(filters3)
print(filters4)
print(filters5)

后面reshape决定卷积核的形状【卷积核个数,图像通道数(几个阵),卷积核高度,卷积核宽度】

输出。

tensor([[[[-1.,  0.],
          [ 0., -1.]]]])
tensor([[[[-1.,  0.],
          [ 0., -1.]]],


        [[[-1.,  0.],
          [ 0., -1.]]]])
tensor([[[[-1.,  0.],
          [ 0., -1.]]],


        [[[-1.,  0.],
          [ 0., -1.]]],


        [[[-1.,  0.],
          [ 0., -1.]]]])
tensor([[[[-1.,  0.],
          [ 0., -1.]],

         [[-1.,  0.],
          [ 0., -1.]]],


        [[[-1.,  0.],
          [ 0., -1.]],

         [[-1.,  0.],
          [ 0., -1.]]]])
tensor([[[[-1.,  0.],
          [ 0., -1.]],

         [[-1.,  0.],
          [ 0., -1.]]]])

3. 卷积函数走一走

op1 = torch.nn.functional.conv2d(input1,filters1,stride=2,padding=1)
op2 = torch.nn.functional.conv2d(input1,filters2,stride=2,padding=1)
op3 = torch.nn.functional.conv2d(input1,filters3,stride=2,padding=1)
op4 = torch.nn.functional.conv2d(input2,filters4,stride=2,padding=1)
op5 = torch.nn.functional.conv2d(input2,filters5,stride=2,padding=1)
op6 = torch.nn.functional.conv2d(input1,filters1,stride=2,padding=0)

print(op1)
print(op2)
print(op3)
print(op4)
print(op5)
print(op6)

输出。

tensor([[[[-1., -1., -1.],
          [-1., -2., -2.],
          [-1., -2., -2.]]]])
tensor([[[[-1., -1., -1.],
          [-1., -2., -2.],
          [-1., -2., -2.]],

         [[-1., -1., -1.],
          [-1., -2., -2.],
          [-1., -2., -2.]]]])
tensor([[[[-1., -1., -1.],
          [-1., -2., -2.],
          [-1., -2., -2.]],

         [[-1., -1., -1.],
          [-1., -2., -2.],
          [-1., -2., -2.]],

         [[-1., -1., -1.],
          [-1., -2., -2.],
          [-1., -2., -2.]]]])
tensor([[[[-2., -2., -2.],
          [-2., -4., -4.],
          [-2., -4., -4.]],

         [[-2., -2., -2.],
          [-2., -4., -4.],
          [-2., -4., -4.]]]])
tensor([[[[-2., -2., -2.],
          [-2., -4., -4.],
          [-2., -4., -4.]]]])
tensor([[[[-2., -2.],
          [-2., -2.]]]])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值