torch.nn.Conv2d()函数详解

import torch

x = torch.randn(2,1,7,3)
conv = torch.nn.Conv2d(1,8,(2,3))
res = conv(x)

print(res.shape)    # shape = (2, 8, 6, 1)

输入:

x
[ batch_size, channels, height_1, width_1 ]
batch_size 一个batch中样例的个数 2
channels 通道数,也就是当前层的深度 1
height_1, 图片的高 7
width_1, 图片的宽 3

Conv2d的参数
[ channels, output, height_2, width_2 ]

channels, 通道数,和上面保持一致,也就是当前层的深度 1
output 输出的深度 8
height_2, 过滤器filter的高 2
width_2, 过滤器filter的宽 3

输出:

res
[ batch_size,output, height_3, width_3 ]

batch_size, 一个batch中样例的个数,同上 2
output 输出的深度 8
height_3, 卷积结果的高度 6 = height_1 - height_2 + 1 = 7-2+1
width_3, 卷积结果的宽度 1 = width_1 - width_2 +1 = 3-3+1

import numpy
import torch
import torch.nn as nn

conv1 = nn.Conv2d( in_channels = 4,   out_channels = 32, kernel_size=8, stride=4)

image = np.random.rand(84,84)
image = image.astype(np.float32) / 255.0

state = np.stack([image]*4,axis = 0)
print('shape_state =', np.shape(state))

input_x = np.reshape(state, (1, 4, 84,84)) 
print('shape_input_x =', np.shape(input_x))

output = conv1(torch.from_numpy(input_x))
print('shape_output  =', np.shape(output))

参考
torch.nn.Conv2d
torch.nn.MaxPool2d

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值