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


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

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

.computeOutputShape() 函數用於枚舉所述層的輸出形狀。並且假定將創建圖層以匹配提供的輸入形狀。

用法:

computeOutputShape(inputShape)

參數:

  • inputShape:它是規定的形狀,即整數集或形狀集列表。此外,Shape 元組可以包含 void 以支持自由大小,而不是整數。它可以是 ((null | number)[]|(null | number)[][]) 類型。

返回值:它返回 (null | number)[]|(null | number)[][]。



範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating a model
const model = tf.sequential();
  
// Adding a layer
model.add(tf.layers.dense({units:1, inputShape:[3]}));
  
// Defining inputShape
const inputShape = [6, 2, 6];
  
// Calling computeOutputShape() method with its 
// parameter
const val = model.layers[0].computeOutputShape(inputShape);
  
// Printing output
console.log(val);

輸出:

6,2,1

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating a model
const model = tf.sequential();
  
// Adding layers
model.add(tf.layers.dense({units:1, inputShape:[3]}));
model.add(tf.layers.dense({units:5}));
  
// Defining inputShape
const inputShape1 = [6, 2, 6, null];
const inputShape2 = [6.5, 2.6, 9.1, NaN];
  
  
// Calling computeOutputShape() method with its 
// parameter
const val1 = model.layers[0].computeOutputShape(inputShape1);
const val2 = model.layers[1].computeOutputShape(inputShape2);
  
// Printing output
console.log(val1);
console.log(val2);

輸出:

6,2,6,1
6.5,2.6,9.1,5

參考: https://js.tensorflow.org/api/latest/#tf.layers.Layer.computeOutputShape




相關用法


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