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

卷积神经网络中卷积层Conv2D(带stride、padding)的具体实现:https://www.cnblogs.com/xiximayou/p/12706576.html

激活函数并没有多少要说的,根据公式定义好就行了,需要注意的是梯度公式的计算。

  1. import numpy as np
  2.  
  3. # Collection of activation functions
  4. # Reference: https://en.wikipedia.org/wiki/Activation_function
  5.  
  6. class Sigmoid():
  7. def __call__(self, x):
  8. return 1 / (1 + np.exp(-x))
  9.  
  10. def gradient(self, x):
  11. return self.__call__(x) * (1 - self.__call__(x))
  12.  
  13. class Softmax():
  14. def __call__(self, x):
  15. e_x = np.exp(x - np.max(x, axis=-1, keepdims=True))
  16. return e_x / np.sum(e_x, axis=-1, keepdims=True)
  17.  
  18. def gradient(self, x):
  19. p = self.__call__(x)
  20. return p * (1 - p)
  21.  
  22. class TanH():
  23. def __call__(self, x):
  24. return 2 / (1 + np.exp(-2*x)) - 1
  25.  
  26. def gradient(self, x):
  27. return 1 - np.power(self.__call__(x), 2)
  28.  
  29. class ReLU():
  30. def __call__(self, x):
  31. return np.where(x >= 0, x, 0)
  32.  
  33. def gradient(self, x):
  34. return np.where(x >= 0, 1, 0)
  35.  
  36. class LeakyReLU():
  37. def __init__(self, alpha=0.2):
  38. self.alpha = alpha
  39.  
  40. def __call__(self, x):
  41. return np.where(x >= 0, x, self.alpha * x)
  42.  
  43. def gradient(self, x):
  44. return np.where(x >= 0, 1, self.alpha)
  45.  
  46. class ELU():
  47. def __init__(self, alpha=0.1):
  48. self.alpha = alpha
  49.  
  50. def __call__(self, x):
  51. return np.where(x >= 0.0, x, self.alpha * (np.exp(x) - 1))
  52.  
  53. def gradient(self, x):
  54. return np.where(x >= 0.0, 1, self.__call__(x) + self.alpha)
  55.  
  56. class SELU():
  57. # Reference : https://arxiv.org/abs/1706.02515,
  58. # https://github.com/bioinf-jku/SNNs/blob/master/SelfNormalizingNetworks_MLP_MNIST.ipynb
  59. def __init__(self):
  60. self.alpha = 1.6732632423543772848170429916717
  61. self.scale = 1.0507009873554804934193349852946
  62.  
  63. def __call__(self, x):
  64. return self.scale * np.where(x >= 0.0, x, self.alpha*(np.exp(x)-1))
  65.  
  66. def gradient(self, x):
  67. return self.scale * np.where(x >= 0.0, 1, self.alpha * np.exp(x))
  68.  
  69. class SoftPlus():
  70. def __call__(self, x):
  71. return np.log(1 + np.exp(x))
  72.  
  73. def gradient(self, x):
  74. return 1 / (1 + np.exp(-x))

【python实现卷积神经网络】激活函数的实现(sigmoid、softmax、tanh、relu、leakyrelu、elu、selu、softplus)的更多相关文章

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

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

  2. 【python实现卷积神经网络】开始训练

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

  3. Python CNN卷积神经网络代码实现

    # -*- coding: utf-8 -*- """ Created on Wed Nov 21 17:32:28 2018 @author: zhen "& ...

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

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

  5. 【python实现卷积神经网络】损失函数的定义(均方误差损失、交叉熵损失)

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

  6. 【python实现卷积神经网络】优化器的实现(SGD、Nesterov、Adagrad、Adadelta、RMSprop、Adam)

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

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

    代码来源: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. VS配置C++依赖包

    处理好三个东西 1.头文件,Configuration Properties → VC++ Directories → Include Directories 2.静态库,Configuration ...

  2. div或者p标签单行和多行超出显示省略号

    单行文本溢出显示省略号 overflow: hidden;text-overflow:ellipsis;white-space: nowrap;多行文本显示省略号 display: -webkit-b ...

  3. Untargeted lipidomics reveals specific lipid abnormality in nonfunctioning human pituitary adenomas 非靶向脂质组学揭示非功能人类脑垂体瘤中的特异性脂质 (解读人:胡丹丹)

    文献名:Untargeted lipidomics reveals specific lipid abnormality in nonfunctioning human pituitary adeno ...

  4. C# 通过反射访问类库DLL的路径打开窗体功能

    //通过访问目录下的DLL Assembly ass = Assembly.LoadFile(Application.StartupPath + "\\Design.dll"); ...

  5. zabbix笔记_008 zabbix监控交换机路由器

    zabbix监控交换机路由器 要监控路由器交换机,需要使用到SNMP协议 SNMP是一个简单网络管理协议,他基于C/S模型实现的监控和管理. 服务器安装SNMP: yum -y install net ...

  6. 【原创】(六)Linux进程调度-实时调度器

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...

  7. 设计模式—建造者模式(Builder)

    title: 设计模式-建造者模式 建造者模式(Builder)是一步一步创建一个复杂的对象,它允许用户只通过指定复杂对象的类型和内容就可以构建它们,用户不需要知道内部的具体构建细节.建造者模式属于对 ...

  8. 【Unity游戏开发】跟着马三一起魔改LitJson

    一.引子 在游戏开发中,我们少不了和数据打交道,数据的存储格式可谓是百花齐放,xml.json.csv.bin等等应有尽有.在这其中Json以其小巧轻便.可读性强.兼容性好等优点受到广大程序员的喜爱. ...

  9. PyTorch 实战-张量

    Numpy 是一个非常好的框架,但是不能用 GPU 来进行数据运算. Numpy is a great framework, but it cannot utilize GPUs to acceler ...

  10. 【SQL SERVER】锁机制

    锁定是 SQL Server 数据库引擎用来同步多个用户同时对同一个数据块的访问的一种机制. 基本概念 利用SQL Server Profiler观察锁 死锁产生的原因及避免 总结 基本概念 数据库引 ...