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