【python实现卷积神经网络】激活函数的实现(sigmoid、softmax、tanh、relu、leakyrelu、elu、selu、softplus)
代码来源:https://github.com/eriklindernoren/ML-From-Scratch
卷积神经网络中卷积层Conv2D(带stride、padding)的具体实现:https://www.cnblogs.com/xiximayou/p/12706576.html
激活函数并没有多少要说的,根据公式定义好就行了,需要注意的是梯度公式的计算。
- import numpy as np
- # Collection of activation functions
- # Reference: https://en.wikipedia.org/wiki/Activation_function
- class Sigmoid():
- def __call__(self, x):
- return 1 / (1 + np.exp(-x))
- def gradient(self, x):
- return self.__call__(x) * (1 - self.__call__(x))
- class Softmax():
- def __call__(self, x):
- e_x = np.exp(x - np.max(x, axis=-1, keepdims=True))
- return e_x / np.sum(e_x, axis=-1, keepdims=True)
- def gradient(self, x):
- p = self.__call__(x)
- return p * (1 - p)
- class TanH():
- def __call__(self, x):
- return 2 / (1 + np.exp(-2*x)) - 1
- def gradient(self, x):
- return 1 - np.power(self.__call__(x), 2)
- class ReLU():
- def __call__(self, x):
- return np.where(x >= 0, x, 0)
- def gradient(self, x):
- return np.where(x >= 0, 1, 0)
- class LeakyReLU():
- def __init__(self, alpha=0.2):
- self.alpha = alpha
- def __call__(self, x):
- return np.where(x >= 0, x, self.alpha * x)
- def gradient(self, x):
- return np.where(x >= 0, 1, self.alpha)
- class ELU():
- def __init__(self, alpha=0.1):
- self.alpha = alpha
- def __call__(self, x):
- return np.where(x >= 0.0, x, self.alpha * (np.exp(x) - 1))
- def gradient(self, x):
- return np.where(x >= 0.0, 1, self.__call__(x) + self.alpha)
- class SELU():
- # Reference : https://arxiv.org/abs/1706.02515,
- # https://github.com/bioinf-jku/SNNs/blob/master/SelfNormalizingNetworks_MLP_MNIST.ipynb
- def __init__(self):
- self.alpha = 1.6732632423543772848170429916717
- self.scale = 1.0507009873554804934193349852946
- def __call__(self, x):
- return self.scale * np.where(x >= 0.0, x, self.alpha*(np.exp(x)-1))
- def gradient(self, x):
- return self.scale * np.where(x >= 0.0, 1, self.alpha * np.exp(x))
- class SoftPlus():
- def __call__(self, x):
- return np.log(1 + np.exp(x))
- def gradient(self, x):
- return 1 / (1 + np.exp(-x))
【python实现卷积神经网络】激活函数的实现(sigmoid、softmax、tanh、relu、leakyrelu、elu、selu、softplus)的更多相关文章
- 基于Python的卷积神经网络和特征提取
基于Python的卷积神经网络和特征提取 用户1737318发表于人工智能头条订阅 224 在这篇文章中: Lasagne 和 nolearn 加载MNIST数据集 ConvNet体系结构与训练 预测 ...
- 【python实现卷积神经网络】开始训练
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- Python CNN卷积神经网络代码实现
# -*- coding: utf-8 -*- """ Created on Wed Nov 21 17:32:28 2018 @author: zhen "& ...
- 【python实现卷积神经网络】卷积层Conv2D反向传播过程
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- 【python实现卷积神经网络】损失函数的定义(均方误差损失、交叉熵损失)
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- 【python实现卷积神经网络】优化器的实现(SGD、Nesterov、Adagrad、Adadelta、RMSprop、Adam)
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- 【python实现卷积神经网络】全连接层实现
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- 【python实现卷积神经网络】批量归一化层实现
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- 【python实现卷积神经网络】池化层实现
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
随机推荐
- VS配置C++依赖包
处理好三个东西 1.头文件,Configuration Properties → VC++ Directories → Include Directories 2.静态库,Configuration ...
- div或者p标签单行和多行超出显示省略号
单行文本溢出显示省略号 overflow: hidden;text-overflow:ellipsis;white-space: nowrap;多行文本显示省略号 display: -webkit-b ...
- Untargeted lipidomics reveals specific lipid abnormality in nonfunctioning human pituitary adenomas 非靶向脂质组学揭示非功能人类脑垂体瘤中的特异性脂质 (解读人:胡丹丹)
文献名:Untargeted lipidomics reveals specific lipid abnormality in nonfunctioning human pituitary adeno ...
- C# 通过反射访问类库DLL的路径打开窗体功能
//通过访问目录下的DLL Assembly ass = Assembly.LoadFile(Application.StartupPath + "\\Design.dll"); ...
- zabbix笔记_008 zabbix监控交换机路由器
zabbix监控交换机路由器 要监控路由器交换机,需要使用到SNMP协议 SNMP是一个简单网络管理协议,他基于C/S模型实现的监控和管理. 服务器安装SNMP: yum -y install net ...
- 【原创】(六)Linux进程调度-实时调度器
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- 设计模式—建造者模式(Builder)
title: 设计模式-建造者模式 建造者模式(Builder)是一步一步创建一个复杂的对象,它允许用户只通过指定复杂对象的类型和内容就可以构建它们,用户不需要知道内部的具体构建细节.建造者模式属于对 ...
- 【Unity游戏开发】跟着马三一起魔改LitJson
一.引子 在游戏开发中,我们少不了和数据打交道,数据的存储格式可谓是百花齐放,xml.json.csv.bin等等应有尽有.在这其中Json以其小巧轻便.可读性强.兼容性好等优点受到广大程序员的喜爱. ...
- PyTorch 实战-张量
Numpy 是一个非常好的框架,但是不能用 GPU 来进行数据运算. Numpy is a great framework, but it cannot utilize GPUs to acceler ...
- 【SQL SERVER】锁机制
锁定是 SQL Server 数据库引擎用来同步多个用户同时对同一个数据块的访问的一种机制. 基本概念 利用SQL Server Profiler观察锁 死锁产生的原因及避免 总结 基本概念 数据库引 ...