前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【python实现卷积神经网络】上采样层upSampling2D实现

【python实现卷积神经网络】上采样层upSampling2D实现

作者头像
西西嘛呦
发布2020-08-26 11:00:19
1.1K0
发布2020-08-26 11:00:19
举报
文章被收录于专栏:数据分析与挖掘

代码来源:https://github.com/eriklindernoren/ML-From-Scratch

卷积神经网络中卷积层Conv2D(带stride、padding)的具体实现:https://cloud.tencent.com/developer/article/1686529

激活函数的实现(sigmoid、softmax、tanh、relu、leakyrelu、elu、selu、softplus):https://cloud.tencent.com/developer/article/1686496

损失函数定义(均方误差、交叉熵损失):https://cloud.tencent.com/developer/article/1686498

优化器的实现(SGD、Nesterov、Adagrad、Adadelta、RMSprop、Adam):https://cloud.tencent.com/developer/article/1686499

卷积层反向传播过程:https://cloud.tencent.com/developer/article/1686503

全连接层实现:https://cloud.tencent.com/developer/article/1686504

批量归一化层实现:https://cloud.tencent.com/developer/article/1686506

池化层实现:https://cloud.tencent.com/developer/article/1686507

padding2D实现:https://cloud.tencent.com/developer/article/1686509

Flatten层实现:https://cloud.tencent.com/developer/article/1686511

代码语言:javascript
复制
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

核心就是numpy.repeat()函数。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档