神经网络的正向传播中进行的矩阵乘积运算在几何学领域被称为“仿射变换(Affine)” 1. 输入为单个数据
2. 批版本的Affine
# coding: utf-8import numpy as npclass Affine: """定义仿射层(矩阵乘积)""" def __init__(self, W, b): self.W = W self.b = b self.x = None self.dW = None self.db = None def forward(self, x): self.x = x out = np.dot(x, self.W) + self.b return out def backward(self, dout): dx = np.dot(dout, self.W.T) # T代表转置 self.dW = np.dot(self.x.T, dout) # x要在前面乘 self.db = np.sum(dout, axis=0) # 偏置反向传播需要汇总为偏置的元素 return dx