日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

TenorFlowJS-激活函数

發(fā)布時(shí)間:2023/12/14 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TenorFlowJS-激活函数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

概述

激活函數(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ù)。

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也比較簡(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-激活函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。