當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.Sequential用法及代碼示例

Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。

Tensorflow.js tf.Sequential 類是堆棧形式的層集合的模型。這些層連接到各自的相鄰層。我們使用 tf.Sequential() 函數創建 tf.Sequential 類實例。 tf.Sequential 類有許多用於在實例中應用的方法。

句法:

Sequential_instance.method(args);

參數:該方法接受以下參數:

  • args: 這取決於方法。不同的方法接受不同的參數。

返回值:不同的方法返回不同的返回值tf.Tensor對象等

示例 1:在此示例中,我們將看到 add() 方法,該方法用於在圖層頂部添加圖層。它以layer為參數並返回void。

Javascript


import * as tf from "@tensorflow/tfjs"
// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
async function run() {
    // Creating Instance 
    const gfg_Instance = tf.sequential();
    // Adding first Layer 
    const Layer1 = tf.layers.dense({ units: 6, inputShape: [2] });
    gfg_Instance.add(Layer1);
    // Adding second layer 
    const layer2 = tf.layers.dense({ units: 3, activation: 'sigmoid' })
    gfg_Instance.add(layer2);
    // Adding third layer
    const layer3 = tf.layers.dense({ units: 2, activation: 'sigmoid' })
    gfg_Instance.add(layer3);
    // Predicting data with layer model.
    const random = tf.randomNormal([4, 2]);
    gfg_Instance.predict(random).print();
}
// Function call
run();


輸出:

Tensor
    [[0.5581576, 0.3110509],
     [0.546664 , 0.3369413],
     [0.5634928, 0.2920811],
     [0.5309308, 0.3545613]]

示例 2:在此示例中,我們將看到summary()方法,用於打印圖層實例的摘要。它采用行長度(摘要的自定義長度)和位置(摘要列的寬度),最後使用打印函數(用於自定義摘要的輸出)。它返回無效。

Javascript


// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
async function run() {
    // Creating Instance 
    const gfg_Instance = tf.sequential();
    // Adding first Layer 
    const Layer1 = tf.layers.dense({ units: 6, inputShape: [2] });
    gfg_Instance.add(Layer1);
     
    // Adding second layer 
    const layer2 = tf.layers.dense({ units: 3, activation: 'sigmoid' })
    gfg_Instance.add(layer2);
     
    // Adding third layer
    const layer3 = tf.layers.dense({ units: 2, activation: 'sigmoid' })
    gfg_Instance.add(layer3);
     
    // Printing summary of layer model 
    gfg_Instance.summary();
}
// Function call
run();


輸出:

_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense34 (Dense)        [null,6]                  18        
_________________________________________________________________
dense_Dense35 (Dense)        [null,3]                  21        
_________________________________________________________________
dense_Dense36 (Dense)        [null,2]                  8         
=================================================================
Total params: 47
Trainable params: 47
Non-trainable params: 0
_________________________________________________________________

參考:https://js.tensorflow.org/api/latest/#class:Sequential



相關用法


注:本文由純淨天空篩選整理自satyam00so大神的英文原創作品 Tensorflow.js tf.Sequential Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。