手动计算conv1d 及pytorch源码
文章目录
- 1. conv1d
- 2. pytorch 源码
1. conv1d
conv1d的作用是进行一维的卷积计算,将卷积核沿着输入矩阵进行一维卷积,具体参考如下excel
通过网盘分享的文件:conv1d.xlsx
链接: https://pan.baidu.com/s/1WIM4Pp5nwa-uP67TMP-m8Q?pwd=uti7 提取码: uti7
2. pytorch 源码
import torch
import torch.nn as nn
import torch.nn.functional as F
torch.set_printoptions(precision=3, sci_mode=False)
if __name__ == "__main__":
run_code = 0
# in_channels: int,
# out_channels: int,
# kernel_size: _size_1_t,
# stride: _size_1_t = 1,
# padding: Union[str, _size_1_t] = 0,
# dilation: _size_1_t = 1,
# groups: int = 1,
# bias: bool = True,
# padding_mode: str = 'zeros', # TODO: refine this type
# device = None,
# dtype = None
in_channels = 3
out_channels = 4
kernel_size = 2
stride = 1
my_weight_total = out_channels * in_channels * kernel_size
my_weight = torch.arange(my_weight_total).reshape((out_channels, in_channels, kernel_size)).to(torch.float32)
my_conv1d = nn.Conv1d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride)
my_bias = torch.arange(out_channels).to(torch.float32)
print(my_conv1d)
print(my_conv1d.weight.shape)
my_conv1d.weight = nn.Parameter(my_weight)
my_conv1d.bias = nn.Parameter(my_bias)
for p in my_conv1d.named_parameters():
print(p)
batch_size = 2
seq_len = 5
in_total = batch_size * in_channels * seq_len
in_matrix = torch.arange(in_total).reshape(batch_size, in_channels, seq_len).to(torch.float32)
out_matrix = my_conv1d(in_matrix)
conv1d_weight = my_conv1d.weight
print(f"conv1d_weight.shape=\n{conv1d_weight.shape}")
print(f"conv1d_weight=\n{conv1d_weight}")
print(f"in_matrix.shape=\n{in_matrix.shape}")
print(f"in_matrix=\n{in_matrix}")
print(f"out_matrix.shape=\n{out_matrix.shape}")
print(f"out_matrix=\n{out_matrix}")
- 结果:
Conv1d(3, 4, kernel_size=(2,), stride=(1,))
torch.Size([4, 3, 2])
('weight', Parameter containing:
tensor([[[ 0., 1.],
[ 2., 3.],
[ 4., 5.]],
[[ 6., 7.],
[ 8., 9.],
[10., 11.]],
[[12., 13.],
[14., 15.],
[16., 17.]],
[[18., 19.],
[20., 21.],
[22., 23.]]], requires_grad=True))
('bias', Parameter containing:
tensor([0., 1., 2., 3.], requires_grad=True))
conv1d_weight.shape=
torch.Size([4, 3, 2])
conv1d_weight=
Parameter containing:
tensor([[[ 0., 1.],
[ 2., 3.],
[ 4., 5.]],
[[ 6., 7.],
[ 8., 9.],
[10., 11.]],
[[12., 13.],
[14., 15.],
[16., 17.]],
[[18., 19.],
[20., 21.],
[22., 23.]]], requires_grad=True)
in_matrix.shape=
torch.Size([2, 3, 5])
in_matrix=
tensor([[[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[10., 11., 12., 13., 14.]],
[[15., 16., 17., 18., 19.],
[20., 21., 22., 23., 24.],
[25., 26., 27., 28., 29.]]])
out_matrix.shape=
torch.Size([2, 4, 4])
out_matrix=
tensor([[[ 124., 139., 154., 169.],
[ 323., 374., 425., 476.],
[ 522., 609., 696., 783.],
[ 721., 844., 967., 1090.]],
[[ 349., 364., 379., 394.],
[1088., 1139., 1190., 1241.],
[1827., 1914., 2001., 2088.],
[2566., 2689., 2812., 2935.]]], grad_fn=<ConvolutionBackward0>)