代码来源: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

class ConstantPadding2D(Layer):
"""Adds rows and columns of constant values to the input.
Expects the input to be of shape (batch_size, channels, height, width)
Parameters:
-----------
padding: tuple
The amount of padding along the height and width dimension of the input.
If (pad_h, pad_w) the same symmetric padding is applied along height and width dimension.
If ((pad_h0, pad_h1), (pad_w0, pad_w1)) the specified padding is added to beginning and end of
the height and width dimension.
padding_value: int or tuple
The value the is added as padding.
"""
def __init__(self, padding, padding_value=0):
self.padding = padding
self.trainable = True
if not isinstance(padding[0], tuple):
self.padding = ((padding[0], padding[0]), padding[1])
if not isinstance(padding[1], tuple):
self.padding = (self.padding[0], (padding[1], padding[1]))
self.padding_value = padding_value def forward_pass(self, X, training=True):
output = np.pad(X,
pad_width=((0,0), (0,0), self.padding[0], self.padding[1]),
mode="constant",
constant_values=self.padding_value)
return output def backward_pass(self, accum_grad):
pad_top, pad_left = self.padding[0][0], self.padding[1][0]
height, width = self.input_shape[1], self.input_shape[2]
accum_grad = accum_grad[:, :, pad_top:pad_top+height, pad_left:pad_left+width]
return accum_grad def output_shape(self):
new_height = self.input_shape[1] + np.sum(self.padding[0])
new_width = self.input_shape[2] + np.sum(self.padding[1])
return (self.input_shape[0], new_height, new_width) class ZeroPadding2D(ConstantPadding2D):
"""Adds rows and columns of zero values to the input.
Expects the input to be of shape (batch_size, channels, height, width)
Parameters:
-----------
padding: tuple
The amount of padding along the height and width dimension of the input.
If (pad_h, pad_w) the same symmetric padding is applied along height and width dimension.
If ((pad_h0, pad_h1), (pad_w0, pad_w1)) the specified padding is added to beginning and end of
the height and width dimension.
"""
def __init__(self, padding):
self.padding = padding
if isinstance(padding[0], int):
self.padding = ((padding[0], padding[0]), padding[1])
if isinstance(padding[1], int):
self.padding = (self.padding[0], (padding[1], padding[1]))
self.padding_value = 0

需要注意的是输入的维度是:[batchsize,channel,height,width],因此在进行padding的时候是在最后两个维度上进行操作的。

假设输入的图像维度为[1,3,32,32],输入的padding=((1,1),(1,1)),accm_grad是后一层传到该层的梯度,那么padding2D的反向传播的梯度accm_grad=accm_grad[:, :, 1:33, 1:33]。

【python实现卷积神经网络】padding2D层实现的更多相关文章

  1. 基于Python的卷积神经网络和特征提取

    基于Python的卷积神经网络和特征提取 用户1737318发表于人工智能头条订阅 224 在这篇文章中: Lasagne 和 nolearn 加载MNIST数据集 ConvNet体系结构与训练 预测 ...

  2. 关于LeNet-5卷积神经网络 S2层与C3层连接的参数计算的思考???

    https://blog.csdn.net/saw009/article/details/80590245 关于LeNet-5卷积神经网络 S2层与C3层连接的参数计算的思考??? 首先图1是LeNe ...

  3. 【python实现卷积神经网络】Flatten层实现

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

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

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  5. 【python实现卷积神经网络】Dropout层实现

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  6. 【python实现卷积神经网络】激活层实现

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  7. 【python实现卷积神经网络】卷积层Conv2D反向传播过程

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  8. 【python实现卷积神经网络】全连接层实现

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  9. 【python实现卷积神经网络】批量归一化层实现

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

随机推荐

  1. Natas20 Writeup(Session登录,注入参数)

    Natas20: 读取源码,发现把sessionID存到了文件中,按键值对存在,以空格分隔,如果$_SESSION["admin"]==1,则成功登陆,得到flag.并且通过查询所 ...

  2. 15. 获取类路径下文件对应的输入流(inputStream)方式

    //获取 inputStream 方式一Resource resource = new ClassPathResource("excel/template/test.xlsx"); ...

  3. 深入分析mysql为什么不推荐使用uuid或者雪花id作为主键

    前言:在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建 ...

  4. kerberos系列之hive认证配置

    大数据安全系列之hive的kerberos认证配置,其它系列链接如下 https://www.cnblogs.com/bainianminguo/p/12548076.html-----------安 ...

  5. settings源码

    自定义settings配置 创建python项目 settings.py NAME = '我是暴露给用户的自定义配置' __init__.py import os import importlib f ...

  6. 【西北大学集训队选拔赛】D温暖的签到题(自创数据结构)

    题目链接 #include <bits/stdc++.h> #define NUM #define ll long long using namespace std; int n, m; ...

  7. MP3音频文件的剪切合并方法

    package com.bytx.ai.service.base.controller.util; import it.sauronsoftware.jave.Encoder;import it.sa ...

  8. C 实战练习题目3

    题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 程序分析: 假设该数为 x. 1.则:x + 100 = n2, x + 100 + 168 = m2 ...

  9. 学习Angular1

    教程: 参考教程: https://www.runoob.com/angularjs/angularjs-tutorial.html 一.angular的简介AngularJS 是一个 JavaScr ...

  10. nginx使用手册+基本原理+优缺点

    一.nginx优点 1.反向代理 1.正向代理: 客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原 ...