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


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

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

tf.randomUniform()函數用於創建tf.Tensor,其值從均勻分布中采樣。

用法:

tf.randomUniform (shape, minval, maxval, dtype, seed)

參數:該函數接受五個參數,如下所示:

  • shape:定義輸出張量形狀的整數數組。
  • minval:它是一個可選參數。均勻分布範圍的下限。默認值為0。
  • maxval:這也是一個可選參數。它是穩定分布範圍的上限。它不包括在範圍內。預設值是1。
  • dtype:輸出的數據類型。可能的數據類型的值為‘float32’,‘int32’,'‘bool’,‘complex64’,‘string’。這也是一個可選參數。默認值為‘float32’
  • seed:它是一個可選參數。隨機數生成器的種子。

返回:它返回tf.Tensor。



範例1:

Javascript


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

輸出:

Tensor
    [0.0008758, 0.3491586, 0.3466536, 0.9614096, 0.7892056]

範例2:

Javascript


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

輸出:

Tensor
    [[0.7312108, 0.5003704],
    [0.8552292, 0.082417 ]]

範例3:

Javascript


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

輸出:

Tensor
    [12, 14, 10, 13, 12]

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

相關用法


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