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


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

Tensorflow.js是Google開發的一個開放源代碼庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。。它可以幫助開發人員使用JavaScript開發ML模型,並直接在瀏覽器或Node.js中使用ML。

tf.randomNormal()函數用於創建一個tf.Tensor,其值從正態分布中采樣。

用法:

tf.randomNormal (shape, mean, stdDev, dtype, seed)

參數:

  • shape:定義輸出張量形狀的整數數組。
  • mean:它是一個可選參數。正態分布的平均值。
  • stdDev:這也是一個可選參數。正態分布的標準偏差。
  • dtype:輸出的數據類型。可能的數據類型值為‘float32’或‘int32’。這也是一個可選參數。
  • seed:它是一個可選參數。隨機數生成器的種子。

返回值:它返回tf.Tensor。



範例1:

Javascript


// Creating the tensor with values 
// sampled from a normal distribution
const x = tf.randomNormal([5]);
  
// Printing the tensor
x.print();

輸出:

Tensor
   [1.5322036, 2.2685387, -0.4921667, 1.1309422, 1.470457]

範例2:

Javascript


// Creating the tensor with values 
// sampled from a normal distribution
const x = tf.randomNormal([2, 2]);
  
// Printing the tensor
x.print();

輸出:

Tensor
   [[1.9162624 , -0.9760998],
    [-0.2262698, -2.1717837]]

例子3

Javascript


// Creating the tensor with values 
// sampled from a normal distribution
const x=tf.randomNormal([5], 5, 1, 'int32', 2);
  
// Printing the tensor
x.print();

輸出:

​Tensor
   [5, 7, 6, 5, 6]

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

相關用法


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