測試代碼:
import torch import torch.nn as nn import torch.autograd as autograd m = nn.Conv3d(2, 1, 3, stride=2) input = autograd.Variable(torch.randn(1, 2, 3, 5, 6)) output = m(input) print("輸入圖片:") print(input) print("卷積的權重:") print(m.weight) print("卷積的偏重:") print(m.bias) print("二維卷積後的輸出:") print(output) print("輸出的尺度:") print(output.size()) convBlockOne = 0 convBlockOneStride = 0 for i in range(3): for j in range(3): for k in range(3): # 第一個卷積核與圖片對應相乘 convBlockOne += m.weight[0][0][i][j][k] * input[0][0][i][j][k] \ + m.weight[0][1][i][j][k] * input[0][1][i][j][k]\ # 第一個卷積核與圖片對應相乘(向下stride 2) convBlockOneStride += m.weight[0][0][i][j][k] * input[0][0][i][j+2][k] \ + m.weight[0][1][i][j][k] * input[0][1][i][j+2][k] convBlockOne += m.bias[0] convBlockOneStride += m.bias[0] print("第一個卷積核的輸出:") print(convBlockOne) print("第一個卷積核與圖片對應相乘(向下stride 2)的輸出:") print(convBlockOneStride)
輸出爲:
輸入圖片: tensor([[[[[-0.2675, 0.0556, -1.9855, 1.7392, -0.6632, 0.1453], [ 0.7349, 0.9476, 0.5355, -0.5594, 2.1242, 1.4058], [ 1.6077, 0.4459, -0.9089, -1.0717, 0.9295, -0.4676], [ 0.5669, 2.0196, -0.5530, 0.5664, -0.0614, -0.5880], [-0.2644, 2.4068, 0.8025, 0.7653, -0.4151, -1.7002]], [[-0.7315, 0.5338, -0.8345, -0.5890, -0.3181, 0.1182], [-0.4932, 0.4393, -2.2104, 1.6638, -1.9973, -0.5577], [ 1.5920, 0.9191, 0.0097, 1.3372, -0.7334, -0.1244], [-0.3916, -0.7088, 0.2680, -0.1301, -1.3089, -1.6015], [-0.5146, 0.3306, -0.0946, 0.7855, 0.6388, 0.1586]], [[ 1.9393, -1.4019, 0.2299, -0.9965, 0.7452, 0.3587], [-1.3883, -1.1940, 0.8678, -0.5619, 0.0258, -0.4928], [-2.0170, -0.3001, -1.0647, -0.7010, -1.4591, -0.0430], [-0.2462, 0.1500, 0.1057, 0.4905, 0.0698, 0.4276], [ 1.3770, -0.3881, 0.5839, -1.2195, 0.1621, 1.7151]]], [[[-0.5271, 1.1861, -0.0419, -1.3207, -0.5641, 0.3916], [ 1.3733, 0.6005, 0.1389, -1.4528, -0.4616, -0.5287], [ 1.2038, 0.6351, -1.0303, -0.2564, 0.5147, -1.1273], [ 1.6099, -1.8206, -0.6026, 1.3047, -0.5663, 0.2222], [-0.3679, -0.2235, 0.4051, 1.3802, 0.7523, -0.5938]], [[-0.1804, -1.8722, 2.2938, 2.7384, 0.1001, 0.8501], [-0.1188, 1.1934, -1.4962, -2.2822, 0.7555, 0.3891], [ 2.1036, -0.8809, 0.1145, -0.8379, 0.2282, 2.1451], [ 1.4755, -2.1665, 0.7929, -0.0130, -0.3857, -0.0294], [-1.0380, 0.1234, 2.1946, -1.3495, -0.4712, 0.9102]], [[-1.0044, -1.5893, -1.2136, -0.0563, 2.0776, 0.9950], [-0.9101, 0.4796, 0.3117, 0.1360, -2.4304, 0.4099], [ 0.0862, 1.0328, 0.9845, -0.9481, 1.9113, 1.3451], [-0.1578, -0.4626, 0.3089, -0.9687, -1.5955, 0.8957], [ 0.6446, -0.0345, 0.2948, 0.8574, -0.5022, -0.5117]]]]]) 卷積的權重: Parameter containing: tensor([[[[[ 0.0134, 0.1058, -0.0204], [-0.1219, 0.0881, -0.0455], [-0.0813, 0.0803, 0.0774]], [[-0.0703, -0.0120, -0.0227], [-0.0820, 0.0459, -0.0942], [ 0.0043, -0.0486, -0.1331]], [[-0.1315, -0.0889, 0.1048], [-0.1034, 0.0881, 0.0259], [-0.0182, 0.0609, 0.0272]]], [[[-0.1038, 0.0391, 0.0325], [-0.0242, 0.1138, 0.0522], [ 0.0649, 0.0600, -0.0476]], [[ 0.1099, -0.0557, 0.0714], [-0.0997, -0.0301, -0.0354], [-0.1291, -0.0227, 0.0915]], [[-0.0529, -0.0907, 0.0566], [-0.0386, -0.0340, -0.0872], [-0.0612, -0.0455, -0.0208]]]]]) 卷積的偏重: Parameter containing: tensor(1.00000e-02 * [-7.2022]) 二維卷積後的輸出: tensor([[[[[ 0.4036, 0.7170], [ 0.4118, 0.1331]]]]]) 輸出的尺度: torch.Size([1, 1, 1, 2, 2]) 第一個卷積核的輸出: tensor(0.4036) 第一個卷積核與圖片對應相乘(向下stride 2)的輸出: tensor(0.4118)
結論:
和二維卷積一樣,大家只要記住卷積核(此例爲一個卷積核,維度爲2*3*3*3)[[[[[ 0.0134, 0.1058, -0.0204],
[-0.1219, 0.0881, -0.0455],
[-0.0813, 0.0803, 0.0774]],
[[-0.0703, -0.0120, -0.0227],
[-0.0820, 0.0459, -0.0942],
[ 0.0043, -0.0486, -0.1331]],
[[-0.1315, -0.0889, 0.1048],
[-0.1034, 0.0881, 0.0259],
[-0.0182, 0.0609, 0.0272]]],
[[[-0.1038, 0.0391, 0.0325],
[-0.0242, 0.1138, 0.0522],
[ 0.0649, 0.0600, -0.0476]],
[[ 0.1099, -0.0557, 0.0714],
[-0.0997, -0.0301, -0.0354],
[-0.1291, -0.0227, 0.0915]],
[[-0.0529, -0.0907, 0.0566],
[-0.0386, -0.0340, -0.0872],
[-0.0612, -0.0455, -0.0208]]]]]
並且用它裏的維度和輸入圖片的維度對應相乘最後加上偏重就可以了。