代码来源: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)的更多相关文章

  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. Java 并发容器(转)

    转自:https://blog.ouyangsihai.cn/%2Fjava-gao-bing-fa-zhi-bing-fa-rong-qi-xiang-jie-cong-ru-men-dao-cha ...

  2. EF Core-1

    带着问题去思考,大家好! 前几天了解到EF Core的开发模式:DB First(数据库优先),Model First(模式优先),Code First(代码优先). 我所接触的大多是DB First ...

  3. 测试工程师需要了解的shell变量知识

    欢迎访问个人博客 什么是变量 本地变量:手动定义的,在当前系统的某个环境下才能生效,作用范围小 普通变量: 单引号:原字符输出,变量名='变量值' ➜ shell name='tom' ➜ shell ...

  4. shell编程之循环语句

    for #! /bin/sh for FRUIT in apple banana pear; do echo "I like $FRUIT" done while #! /bin/ ...

  5. dfs 例题皇后问题

    题目描述 一个如下的 6 \times 66×6 的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列  ...

  6. CF230B T-primes 题解

    原题链接 简要题意: 判断一个数是否只有 \(3\) 个因数. 首先,如果一个数有奇数个因数,那么这个数是完全平方数. 道理很简单:因数是成对的,那么必然存在 \(k^2 = n\),此时 \(k\) ...

  7. leetcode 945. 使数组唯一的最小增量

    题目 给定整数数组 A,每次 move 操作将会选择任意 A[i],并将其递增 1. 返回使 A 中的每个值都是唯一的最少操作次数. 示例 1: 输入:[1,2,2] 输出:1 解释:经过一次 mov ...

  8. 关于Anaconda安装以后使用Jupyter Notebook无法直接打开浏览器的解决方法

    关于Anaconda安装以后使用Jupyter Notebook无法直接打开浏览器的解决方法 1.首先打开Anoconda Prompt,输入命令 jupyter notebook --generat ...

  9. 【深度学习】perceptron(感知机)

    目录 1.感知机的描述 2.感知机解决简单逻辑电路,与门的问题. 2.多层感应机,解决异或门 个人学习笔记,有兴趣的朋友可参考. 1.感知机的描述 感知机(perceptron)由美国学者Frank ...

  10. cookie sessionStorage localStorage 使用小结

    1.cookie 随http 一起发送 2.webStorage 客户端本地存储功能 可以在客户端 本地建立 一个数据库 不参与与服务器的通讯 setItem (key, value)   —— 保存 ...