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


Tensorflow.js tf.model()用法及代碼示例

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

tf.model() 函數用於創建一個模型,其中包含以輸入和輸出參數形式提供的層和層。

用法:

tf.model( args ) 

這裏的參數是,

  • Inputs:模型的輸入。它可以是對象或對象列表。
  • Outputs:模型的輸出。
  • Name:型號名稱。

範例1:在這個例子中,我們將在 tf.model() 函數的幫助下創建一個模型,輸入大小為 4,然後是 2 個具有激活函數 relu 和 softmax 的密集層,並使用 model.predict() 函數進行預測。



Javascript


// Create input of size 4
var input = tf.input({shape:[4]});
  
// Dense layer 1 with relu activation 
var dLayer1 = tf.layers.dense({units:12,activation:'relu'});
  
// Dense layer 1 with softmax activation
var dLayer2 = tf.layers.dense({units:7, activation:'softmax'});
  
var output = dLayer2.apply(dLayer1.apply(input));
  
// Model function
var model = tf.model({inputs:input, outputs:output});
  
// Prediction
model.predict(tf.ones([2,4])).print();

輸出:

Tensor
[[0.309215, 0.0659644, 0.122767, 0.1150663, 0.1592857, 0.1232278, 0.1044738],
[0.309215, 0.0659644, 0.122767, 0.1150663, 0.1592857, 0.1232278, 0.1044738]]

範例2:在這個例子中,我們將創建一個輸入數組大小為 2 的模型,並使用 model.summery() 函數生成摘要。summery() 函數也使用 apply() 和 concatenate() 函數。

Javascript


// Input 1
var inp1 = tf.input({shape:[12]});
  
// Input 2
var inp2 = tf.input({shape:[24]});
  
// Apply input one to the first dense layer
// uisng apply() function
var denseLayer1 = tf.layers.dense({units:4}).apply(inp1);
  
// Apply input two to second dense layer
var denseLayer2 = tf.layers.dense({units:8}).apply(inp2);
  
// Concatinate both dense layer using concatinate() function
var concatAll = tf.layers.concatenate()
        .apply([denseLayer1,denseLayer2]);
  
var output =tf.layers.dense({units:8, 
        activation:'softmax'}).apply(concatAll);
       
// Create model
var model = tf.model({inputs:[inp1, inp2], outputs:output});
  
// Generate summery for model
model.summary();

輸出:

__________________________________________________________________________________________________
Layer (type)                    Output shape         Param #     Receives inputs                  
==================================================================================================
input7 (InputLayer)             [null,12]            0                                            
__________________________________________________________________________________________________
input8 (InputLayer)             [null,24]            0                                            
__________________________________________________________________________________________________
dense_Dense10 (Dense)           [null,4]             52          input7[0][0]                     
__________________________________________________________________________________________________
dense_Dense11 (Dense)           [null,8]             200         input8[0][0]                     
__________________________________________________________________________________________________
concatenate_Concatenate4 (Conca [null,12]            0           dense_Dense10[0][0]              
                                                                 dense_Dense11[0][0]              
__________________________________________________________________________________________________
dense_Dense12 (Dense)           [null,8]             104         concatenate_Concatenate4[0][0]   
==================================================================================================
Total params:356
Trainable params:356
Non-trainable params:0
__________________________________________________________________________________________________

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

相關用法


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