概述

激活函数用来决定一个神经元的最终的输出。譬如对一个细胞来说,理想的输出是0和1。但是如果真实的输出是0.85的话,这个时候用来决定输出是0还是1的函数就叫激活函数(从另一个角度来看,有些激活函数有点像数字信号处理里面的将连续信号离散化)。

激活函数或者位于网络的尾部,用于调整输出,或者位于Layer之间。本文介绍激活函数用于Dense Layer的情况。

图片来自:
https://cdn-images-1.medium.com/max/1400/0*44z992IXd9rqyIWk.png

Dense layer

Dense Layer的功能由下面的函数来描述:
output = activation(dot(input, kernel) + bias
这里的kernel不是过滤器的内核,而是weights matrix。所以kernel的大小,应该和输入的大小一致。
从这个公式看,Dense 层解决的是多元一次方程的求解问题。如果输入是1,则是y = kx +b。

tfjs-examples/getting-started:

async function run() {// Create a simple model.const model = tf.sequential();model.add(tf.layers.dense({units: 1, inputShape: [1]}));// Prepare the model for training: Specify the loss and the optimizer.model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});/* 38.2320556640625// Generate some synthetic data for training. (y = 2x - 1)const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]);*/// 19.857912063598633// Generate some synthetic data for training. (y = x)const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);const ys = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);// Train the model using the data.await model.fit(xs, ys, {epochs: 250});// Use the model to do inference on a data point the model hasn't seen.// Should print approximately 39.document.getElementById('micro-out-div').innerText =model.predict(tf.tensor2d([20], [1, 1])).dataSync();
}run();

参考文献:
https://keras.io/layers/core/#dense

linear

linear其实就是什么都不干,也是默认的操作。所以如果输入数据都大于0, relu和linear得到的结果是一样的。
如果输入是1x1,下面的代码可以用来测试linear, relu, softmax等激活函数。

import * as tf from '@tensorflow/tfjs';
function createModel() {// Build and compile model.const model = tf.sequential();// 200,5: 8.4533968; 200, 2: 3.2494676;// model.add(tf.layers.dense({units: 1, inputShape: [1]}));// 200,5 8.5429592; 200, 2 3.2085927// model.add(tf.layers.dense({units: 1, inputShape: [1], activation: 'relu'}));// 200,5, 1; 200, 2: 1// model.add(tf.layers.dense({units: 1, inputShape: [1], activation: 'softmax'}));// 200,5, 8.4282207; 200, 2: 3.260958model.add(tf.layers.dense({units: 1, inputShape: [1], activation: 'linear'}));model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});return model;
}
async function predict(model) {// Generate some synthetic data for training.const xs = tf.tensor2d([[1], [2], [3], [4]], [4, 1]);const ys = tf.tensor2d([[1], [3], [5], [7]], [4, 1]);// Train model with fit().await model.fit(xs, ys, {epochs: 200});// Run inference with predict().model.predict(tf.tensor2d([[5]], [1, 1])).print();model.predict(tf.tensor2d([[2]], [1, 1])).print();
}
function main() {const model = createModel();predict(model);
}
main();

relu

relu也比较简单,就是将所有非负数变为0,正数不变,适合处理内容都是正数的部分,譬如图片。

softmax

Wiki对softmax的解释是:对向量进行归一化,凸显其中最大的值并抑制远低于最大值的其他分量。所以如果使用了激活函数softmax的Layer的inputShape是1,那么,softmax输出会一直是1(因为只有一个数)。

来自Wiki的例子能够很直观的理解softmax:

import math
z = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0]
z_exp = [math.exp(i) for i in z]
print(z_exp)  # Result: [2.72, 7.39, 20.09, 54.6, 2.72, 7.39, 20.09]
sum_z_exp = sum(z_exp)
print(sum_z_exp)  # Result: 114.98
softmax = [round(i / sum_z_exp, 3) for i in z_exp]
print(softmax)  # Result: [0.024, 0.064, 0.175, 0.475, 0.024, 0.064, 0.175]

但是在http://cs231n.github.io/linear-classify/里面,作者将softmax解释为classifier。不过,如果深入文献的内容,参数cs231n里面的softmax,和激活函数的概念都是一样的:即都是将输出做进一步处理,以得到期望的输出。


图片来自:http://cs231n.github.io/assets/svmvssoftmax.png

可以单独对Tensor进行softmax运算:

import * as tf from '@tensorflow/tfjs';function testSoftmax() {const a = tf.tensor2d([2, 4, 6, 1, 2, 3], [2, 3]);a.softmax().print();  // or tf.softmax(a)
}
function main() {testSoftmax();
}
main();

输出是:

Tensor[[0.0158762, 0.1173104, 0.8668135],[0.0900306, 0.2447284, 0.6652408]]

也可以将softmax和Layer结合起来。下面的示例演示了softmax用于dense Layer的情况:

import * as tf from '@tensorflow/tfjs';function createModel() {// Build and compile model.const model = tf.sequential();model.add(tf.layers.dense({units: 2, inputShape: [2], activation: 'softmax'}));model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});return model;
}async function predict(model) {// Generate some synthetic data for training.const xs = tf.tensor2d([1, 2], [1, 2]);const ys = tf.tensor2d([0.2, 0.8], [1, 2]);// Train model with fit().await model.fit(xs, ys, {epochs: 200});// Run inference with predict().model.predict(tf.tensor2d([2, 3], [1, 2])).print();model.predict(tf.tensor2d([2, 1.5], [1, 2])).print();
}function main() {const model = createModel();predict(model);
}main();

和之前的例子不同的是,这里的输入是2个元素。Output is:

Tensor[[0.1261015, 0.8738984],]
Tensor[[0.1033671, 0.8966329],]

参考文献:
https://zh.wikipedia.org/wiki/Softmax函数
https://medium.com/@udemeudofia01/basic-overview-of-convolutional-neural-network-cnn-4fcc7dbb4f17
https://www.quora.com/What-is-activation-in-convolutional-neural-networks

TenorFlowJS-激活函数相关推荐

  1. 为什么神经网络的激活函数必须使用线性函数?

    什么是线性函数? 函数本来是输入某个值后会返回一个值的转换器.向这个转换器输入某个值后,输出值是输入值的常数倍的函数称为线性函数(用数学式表示为h(x) = cx. c为常数).因此,线性函数是一条笔 ...

  2. TensorFlow六种激活函数

    TensorFlow六种激活函数 每个神经元都必须有激活函数.神经元提供了模拟复杂非线性数据集所必需的非线性特性.该函数取所有输入的加权和,进而生成一个输出信号.把它看作输入和输出之间的转换.使用适当 ...

  3. 机器学习入门(12)— 激活函数层 ReLU、Sigmoid 层的实现

    1. ReLU 函数层 激活函数 ReLU(Rectified Linear Unit)由下式(5.7)表示. 通过式(5.7),可以求出 y 关于 x 的导数,如式(5.8)所示. 在式(5.8)中 ...

  4. 机器学习入门(02)— 由感知机到神经网络的过渡进化,激活函数在神经网络中的作用

    1. 神经网络示例 用图来表示神经网络的话,如图3-1 所示.我们把最左边的一列称为输入层,最右边的一列称为输出层,中间的一列称为中间层.中间层有时也称为隐藏层."隐藏"一词的意思 ...

  5. 神经网络常用激活函数

    激活函数:只是一个抽象概念,使用激活函数时为了让中间输出多样化,能够处理更复杂的问题. 激活函数给神经元引入了非线性因素,若无激活函数,则每一层最后输出的都是上一层输入的线性函数,不管加多少层神经网络 ...

  6. 深度学习--TensorFlow(4)BP神经网络(损失函数、梯度下降、常用激活函数、梯度消失梯度爆炸)

    目录 一.概念与定义 二.损失函数/代价函数(loss) 三.梯度下降法 二维w与loss: 三维w与loss: 四.常用激活函数 1.softmax激活函数 2.sigmoid激活函数 3.tanh ...

  7. 深度学习(4)基础4 -- 神经网络架构激活函数过拟合处理

    目录 一.神经网络架构 1.结构与意义 2.过程 1.输入数据,得到输入层 2.得到隐藏层1 3.得到隐藏层2 4.得到输出层 二.激活函数 激活函数概念 激活函数的选择 1.阶跃函数 2.Sigmo ...

  8. 【深度学习理论】(3) 激活函数

    各位同学好,最近学习了CS231N斯坦福计算机视觉公开课,讲的太精彩了,和大家分享一下. 激活函数的作用是把神经元的输入线性求和后,放入非线性的激活函数中激活,正因为有非线性的激活函数,神经网络才能拟 ...

  9. 【深度学习】(3) 全连接层、激活函数

    各位同学好,今天和大家分享一下tensorflow2.0深度学习中的相关操作.内容有: (1) 全连接层创建: tf.keras.Sequential(),tf.keras.layers.Dense( ...

  10. 常用几种激活函数的总结

    目录 一.激活函数是什么 二.为什么要用到激活函数呢 三.常用的激活函数 sigmoid函数 Tanh函数 ReLU函数 一.激活函数是什么 所谓激活函数(Activation Function),就 ...

最新文章

  1. linux负载均衡(什么是负载均衡)
  2. ML基石_2_LearnAnswer2
  3. 热评一箩筐——《黑客攻防技术宝典》
  4. php缓存mysql表_PHP 中的数据库缓存原理
  5. 如何发现 Redis 热点 Key ,解决方案有哪些?
  6. [How TO]-ubuntu下快速搭建http
  7. php调用c windows,php:在WINDOWS中设置计划任务执行PHP文件_PHP教程
  8. Python+Selenium WebDriver API:浏览器及元素的常用函数及变量整理总结
  9. 在Ocelot中使用自定义的中间件(一)
  10. lombok依赖_使用Lombok 前你需要知道这些
  11. PLSQL使用技巧 如何设置默认显示My Objects、记住密码等
  12. java静态类是只有一个吗,一个只有“私有最终静态”变量的java类。这是一个好主意吗?...
  13. 用C语言实现:打印100-200之间的素数。
  14. C/C++学习笔记: 字符串匹配Sunday算法
  15. SEO站内优化教程-SEO站内优化方法教程
  16. 亲自动手写一个深度学习框架
  17. NEO 交易所钱包开发之离线签名【区块链】JAVA
  18. 山西省忻州市水泥厂能耗监测系统的设计与应用
  19. 助推建筑业数字化转型升级,紫光云再出招
  20. [转]solaris 10 使用手册

热门文章

  1. 90后人工智能程序猿小哥,我就是传说中的007
  2. 如何恢复计算机隐藏的文件夹,Win7文件夹隐藏了怎么恢复 如何快速恢复系统隐藏文件【图文】...
  3. 手机归属地查询示例代码
  4. App 用户新体验——Agora Native SDK 3.4.0
  5. Weird Rounding
  6. python画三维立体图完整代码_如何用Matplotlib 画三维图的示例代码
  7. 米卢:梅西是世界最佳 弗格森没有犯错误
  8. 连续复利 continuous compounding
  9. Windows窗口消息介绍
  10. 杨辉三角的c语言代码,杨辉三角C语言代码