代碼來源:https://github.com/eriklindernoren/ML-From-Scratch
卷積神經網絡中卷積層Conv2D(帶stride、padding)的具體實現:https://www.cnblogs.com/xiximayou/p/12706576.html
激活函數的實現(sigmoid、softmax、tanh、relu、leakyrelu、elu、selu、softplus):https://www.cnblogs.com/xiximayou/p/12713081.html
損失函數定義(均方誤差、交叉熵損失):https://www.cnblogs.com/xiximayou/p/12713198.html
優化器的實現(SGD、Nesterov、Adagrad、Adadelta、RMSprop、Adam):https://www.cnblogs.com/xiximayou/p/12713594.html
卷積層反向傳播過程:https://www.cnblogs.com/xiximayou/p/12713930.html
全連接層實現:https://www.cnblogs.com/xiximayou/p/12720017.html
批量歸一化層實現:https://www.cnblogs.com/xiximayou/p/12720211.html
池化層實現:https://www.cnblogs.com/xiximayou/p/12720324.html
padding2D實現:https://www.cnblogs.com/xiximayou/p/12720454.html
這就相當於是pytorch中的在全連接層之前使用view()函數類似的操作:
class Flatten(Layer): """ Turns a multidimensional matrix into two-dimensional """ def __init__(self, input_shape=None): self.prev_shape = None self.trainable = True self.input_shape = input_shape def forward_pass(self, X, training=True): self.prev_shape = X.shape return X.reshape((X.shape[0], -1)) def backward_pass(self, accum_grad): return accum_grad.reshape(self.prev_shape) def output_shape(self): return (np.prod(self.input_shape),)
需要注意反向傳播時的形狀的改變。
還有Reshape層:
class Reshape(Layer): """ Reshapes the input tensor into specified shape Parameters: ----------- shape: tuple The shape which the input shall be reshaped to. """ def __init__(self, shape, input_shape=None): self.prev_shape = None self.trainable = True self.shape = shape self.input_shape = input_shape def forward_pass(self, X, training=True): self.prev_shape = X.shape return X.reshape((X.shape[0], ) + self.shape) def backward_pass(self, accum_grad): return accum_grad.reshape(self.prev_shape) def output_shape(self): return self.shape
