javascript
TenorFlowJS-激活函数
概述
激活函數(shù)用來決定一個(gè)神經(jīng)元的最終的輸出。譬如對(duì)一個(gè)細(xì)胞來說,理想的輸出是0和1。但是如果真實(shí)的輸出是0.85的話,這個(gè)時(shí)候用來決定輸出是0還是1的函數(shù)就叫激活函數(shù)(從另一個(gè)角度來看,有些激活函數(shù)有點(diǎn)像數(shù)字信號(hào)處理里面的將連續(xù)信號(hào)離散化)。
激活函數(shù)或者位于網(wǎng)絡(luò)的尾部,用于調(diào)整輸出,或者位于Layer之間。本文介紹激活函數(shù)用于Dense Layer的情況。
圖片來自:
https://cdn-images-1.medium.com/max/1400/0*44z992IXd9rqyIWk.png
Dense layer
Dense Layer的功能由下面的函數(shù)來描述:
output = activation(dot(input, kernel) + bias
這里的kernel不是過濾器的內(nèi)核,而是weights matrix。所以kernel的大小,應(yīng)該和輸入的大小一致。
從這個(gè)公式看,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();參考文獻(xiàn):
https://keras.io/layers/core/#dense
linear
linear其實(shí)就是什么都不干,也是默認(rèn)的操作。所以如果輸入數(shù)據(jù)都大于0, relu和linear得到的結(jié)果是一樣的。
如果輸入是1x1,下面的代碼可以用來測(cè)試linear, relu, softmax等激活函數(shù)。
relu
relu也比較簡(jiǎn)單,就是將所有非負(fù)數(shù)變?yōu)?,正數(shù)不變,適合處理內(nèi)容都是正數(shù)的部分,譬如圖片。
softmax
Wiki對(duì)softmax的解釋是:對(duì)向量進(jìn)行歸一化,凸顯其中最大的值并抑制遠(yuǎn)低于最大值的其他分量。所以如果使用了激活函數(shù)softmax的Layer的inputShape是1,那么,softmax輸出會(huì)一直是1(因?yàn)橹挥幸粋€(gè)數(shù))。
來自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。不過,如果深入文獻(xiàn)的內(nèi)容,參數(shù)cs231n里面的softmax,和激活函數(shù)的概念都是一樣的:即都是將輸出做進(jìn)一步處理,以得到期望的輸出。
圖片來自:http://cs231n.github.io/assets/svmvssoftmax.png
可以單獨(dú)對(duì)Tensor進(jìn)行softmax運(yùn)算:
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結(jié)合起來。下面的示例演示了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個(gè)元素。Output is:
Tensor[[0.1261015, 0.8738984],] Tensor[[0.1033671, 0.8966329],]參考文獻(xiàn):
https://zh.wikipedia.org/wiki/Softmax函數(shù)
https://medium.com/@udemeudofia01/basic-overview-of-convolutional-neural-network-cnn-4fcc7dbb4f17
https://www.quora.com/What-is-activation-in-convolutional-neural-networks
總結(jié)
以上是生活随笔為你收集整理的TenorFlowJS-激活函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4r照片尺寸是多大_数码照片4D、4R、
- 下一篇: 基于SOM算法的Iris数据分类