摘要
本文使用纯 Python 和 PyTorch 对比实现 convolution 函数及其反向传播.
相关
原理和详细解释, 请参考文章 :
卷积convolution函数详解及反向传播中的梯度求导
系列文章索引 :
https://blog.youkuaiyun.com/oBrightLamp/article/details/85067981
正文
import torch
import numpy as np
class Conv2d:
def __init__(self, stride=1):
self.weight = None
self.bias = None
self.stride = stride
self.x = None
self.dw = None
self.db = None
self.input_height = None
self.input_width = None
self.weight_height = None
self.weight_width = None
self.output_height = None
self.output_width = None
def __call__(self, x):
self.x = x
self.input_height = np.shape(x)[0]
self.input_width = np.shape(x)[1]
self.weight_height = np.shape(self.weight)[0]
self.weight_width = np.shape(self.weight)[1]
self.output_height = int((self.input_height - self.weight_height) / self.stride) + 1
self.output_width = int((self.input_width - self.weight_width) / self.stride) + 1
out = np.zeros((self.output_height