import torch
import torch.nn as nn
m = nn.Conv1d(2, 5, 2, stride=2)
print(m)
input = torch.randn(2,2, 5)
print(input)
output = m(input)
print(m.weight)
print(m.bias)
print(output)
print(output.size())
print(m.weight[0][0][0] * input[0][0][0] + m.weight[0][0][1] * input[0][0][1]
+ m.weight[0][1][0] * input[0][1][0] + m.weight[0][1][1] * input[0][1][1] + m.bias[0])
输出为:
Conv1d(2, 5, kernel_size=(2,), stride=(2,))
tensor([[[ 0.2060, -0.7951, -2.7771, 0.2033, -1.1268],
[ 0.4578, 0.5288, -1.7435, 1.0188, -0.4615]],
[[-0.7615, -2.3001, 1.3399, 0.9165, -0.0547],
[ 2.1604, -0.8994, -1.2320, 0.2077, 1.5822]]])
Parameter containing:
tensor([[[ 0.4840, 0.2625],
[ 0.4189, -0.1825]],
[[-0.0461, 0.4118],
[-0.2437, 0.0243]],
[[-0.1979, -0.1668],
[-0.3049, -0.3615]],
[[ 0.3453, 0.4335],
[ 0.4266, -0.3455]],
[[ 0.1638, 0.2814],
[-0.2100, 0.3497]]])
Parameter containing:
tensor([-0.4633, 0.3096, -0.4810, 0.1805, 0.3115])
tensor([[[-0.4770, -2.6703],
[-0.1260, 0.9710],
[-0.7199, 0.1979],
[-0.0805, -1.7862],
[ 0.2103, 0.6362]],
[[-0.3663, -0.1284],
[-1.1508, 0.9305],
[-0.2801, -0.5985],
[ 0.1528, 0.4432],
[-1.2287, 1.1203]]])
torch.Size([2, 5, 2])
tensor(-0.4770)
结论:一维卷积第一个输出 =
m.weight[0][0][0] * input[0][0][0] + m.weight[0][0][1] * input[0][0][1]
+ m.weight[0][1][0] * input[0][1][0] + m.weight[0][1][1] * input[0][1][1] + m.bias[0]