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


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

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

tf.randomGamma()函數用於創建tf.Tensor,該值具有從伽馬分布中采樣的值。

用法:

tf.randomGamma(shape, alpha, beta, dtype, seed)

參數:此函數接受三個參數,如下所示:

  • shape:定義輸出張量形狀的整數數組。
  • alpha:伽瑪分布的形狀參數。
  • beta:這是一個可選參數。伽瑪分布的反比例參數。預設值是1。
  • dtype:輸出的數據類型。可能的數據類型值為‘float32’或‘int32’。這也是一個可選參數。
  • seed:它是一個可選參數。隨機數生成器的種子。

返回:返回tf.Tensor



範例1:

Javascript


// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled 
// from a gamma distribution
const x=tf.randomGamma([5], 0);
  
// Printing the tensor
x.print();

輸出:

Tensor
    [0, 0, 0, 0, 0]

範例2:

Javascript


// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled 
// from a gamma distribution
const x=tf.randomGamma([5], 1);
  
// Printing the tensor
x.print();

輸出:

Tensor
    [1.4808178, 1.6668015, 0.9527208, 1.6024575, 1.6021353]

範例3:

Javascript


// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled
// from a gamma distribution
const x=tf.randomGamma([2,2], 1);
  
// Printing the tensor
x.print();

輸出:

Tensor
    [[0.1157758, 1.4427431],
     [0.4978852, 0.1617882]]

範例4:

Javascript


// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled 
// from a gamma distribution
const x=tf.randomGamma([5], 1,2,'int32',98);
  
// Printing the tensor
x.print();

輸出:

Tensor
    [0, 1, 4, 0, 1]

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

相關用法


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