[Python implementation of convolutional neural network] upsampling2D implementation of upsampling layer

Code source: https://github.com/eriklindernoren/ML-From-Scratch

Concrete implementation of Conv2D (with stride, padding) in convolutional neural network: https://www.cnblogs.com/xiximayou/p/12706576.html

Implementation of activation function (sigmoid, softmax, tanh, relu, leakyrelu, elu, selu, softplus): https://www.cnblogs.com/xiximayou/p/12713081.html

Definition of loss function (mean square error, cross entropy loss): https://www.cnblogs.com/xiximayou/p/12713198.html

Optimizer implementation (SGD, Nesterov, Adagrad, Adadelta, RMSprop, Adam): https://www.cnblogs.com/xiximayou/p/12713594.html

Convolution layer back propagation process: https://www.cnblogs.com/xiximayou/p/12713930.html

Fully connected layer implementation: https://www.cnblogs.com/xiximayou/p/12720017.html

Batch normalization layer implementation: https://www.cnblogs.com/xiximayou/p/12720211.html

Pooling layer implementation: https://www.cnblogs.com/xiximayou/p/12720324.html

padding2D implementation: https://www.cnblogs.com/xiximayou/p/12720454.html

Flatten layer implementation: https://www.cnblogs.com/xiximayou/p/12720518.html

 

class UpSampling2D(Layer):
    """ Nearest neighbor up sampling of the input. Repeats the rows and
    columns of the data by size[0] and size[1] respectively.
    Parameters:
    -----------
    size: tuple
        (size_y, size_x) - The number of times each axis will be repeated.
    """
    def __init__(self, size=(2,2), input_shape=None):
        self.prev_shape = None
        self.trainable = True
        self.size = size
        self.input_shape = input_shape

    def forward_pass(self, X, training=True):
        self.prev_shape = X.shape
        # Repeat each axis as specified by size
        X_new = X.repeat(self.size[0], axis=2).repeat(self.size[1], axis=3)
        return X_new

    def backward_pass(self, accum_grad):
        # Down sample input to previous shape
        accum_grad = accum_grad[:, :, ::self.size[0], ::self.size[1]]
        return accum_grad

    def output_shape(self):
        channels, height, width = self.input_shape
        return channels, self.size[0] * height, self.size[1] * width

The core is the numpy.repeat () function.

Guess you like

Origin www.cnblogs.com/xiximayou/p/12720558.html