conv1d code
Starting from the simplest, no bias, no padding, stride=1, no grouping calculation , these conditions can be added gradually later, this time to implement the most basic, to understand the underlying process.
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 12 15:04:51 2022
@author: masteryi
"""
def myconv1d(infeat, convkernel, padding=0, stride=1):
b, c, h = len(infeat), len(infeat[0]), len(infeat[0][0])
out_c, in_c, lenk = len(convkernel), len(convkernel[0]), len(convkernel[0][0])
# 不使用分组卷积,c = in_c
res = [[[0] * (h-lenk+1) for _ in range(out_c) for _ in range(b)]]
# 最终输出形状:b*out_c*(h-lenk+1)
for i in range(b):
# 关于batch,目前只能串行完成
for j in range(out_c):
# 计算每一组的结果
for m in range(c):
for n in range(h-lenk+1):
# 计算每一个位置的值
ans = 0
for k in range(lenk):
ans += infeat[i][m][n+k] * convkernel[j][m][k]
res[i][j][n] += ans
return res
# 我的卷积
infeat = [[[1,2,3,4], [1,2,4,3]]]
convkernel = [[[0,1,2], [0,2,1]], [[1,0,2], [1,2,0]], [[2,0,1], [2,1,0]]]
outfeat = myconv1d(infeat, convkernel)
print(outfeat)
# pytorch源码计算结果
from torch.nn.functional import conv1d
import torch
import numpy
infeat = torch.tensor(numpy.array(infeat))
convkernel = torch.tensor(numpy.array(convkernel))
outfeat_pytorch = conv1d(infeat, convkernel)
print(outfeat_pytorch)
The output is as follows, which is the same as the official calculation result:
[[[16, 22], [12, 20], [9, 16]]]
tensor([[[16, 22],
[12, 20],
[ 9, 16]]], dtype=torch.int32)
think
- If the input channel is in_c and the output channel is out_c, the convolutional layer is constructed to have a total of
out_c组
convolutions, each set of convolutions hasin_c个
a convolution kernel, and the size of each convolution kernel is kh ∗ kw k_h*k_wkh∗kw。 - Multiple convolution kernels in a group have their own weights, do not affect each other, and the weights of convolution kernels in each group are also different; so the parameter amount of the convolution layer is cout ∗ cin ∗ kh ∗ kw c_{out}*c_{ in}*k_h*k_wcout∗cin∗kh∗kw。
- Reference materials: Zhihu: Convolutional Neural Network CNN (2), a detailed understanding of the convolution process