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


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

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

.multinomial() 函數用於生成 tf.Tensor 以及從多項式分布中拖出的輸入。

用法:

tf.multinomial(logits, numSamples, seed?, normalized?)

Parameters: 

  • logits:它是一個聲明的一維數組以及雜亂無章的對數期望,或者是一個形狀為 [batchSize, numOutcomes] 的二維數組,它可以是 tf.Tensor1D、tf.Tensor2D、TypedArray 或 Array 類型。
  • numSamples:它是為每個行部分拖動的規定樣本數。它是類型號。
  • seed:它是規定的種子編號,是編號類型的可選參數。
  • normalized:它檢查給定的 logits 是否組織了真正的期望值,即(總和為 1)。默認值為 false,是 Boolean 類型的可選參數。

返回值:它返回 tf.Tensor1D 或 tf.Tensor2D。



範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining logits
const logits = tf.tensor([35, 158]);
  
// Calling tf.multinomial() method and
// Printing output
tf.multinomial(logits, 4).print();

輸出:

Tensor
    [1, 1, 1, 1]

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling tf.multinomial() method and
// Printing output
tf.multinomial(tf.tensor(
  [5.7, 8.7, NaN, 'a', null, 0]), 6).print();

輸出:

Tensor
    [5, 5, 5, 5, 5, 5]

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

相關用法


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