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


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

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

.addWeight() 函數用於向所述層添加權重變量。

用法:

addWeight(name, shape, dtype?, initializer?, 
               regularizer?, trainable?, constraint?)

參數:

  • name:它是權重的新變量的聲明名稱,類型為字符串。
  • shape:這是重量的規定形狀。它的類型為 (null | number)[]。
  • dtype:它是重量的規定數據類型。它是可選的,可以是 float32、int32、bool、complex64 或 string 類型。
  • initializer:它是聲明的初始化程序實例。它是可選的,屬於 tf.initializers.Initializer 類型。
  • regularizer:它是規定的正則化器實例。它是可選的,屬於Regularizer 類型。
  • trainable:它通過假設層本身是類似可訓練的來說明是否應該通過反向傳播來指示權重。它是可選的並且是布爾類型的。
  • constraint:它是一個可選的可訓練對象,屬於 tf.constraints.Constraint 類型。

返回值:它返回 LayerVariable。



範例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:2, inputShape:[1]}));
  
// Calling addWeight() method
const res = model.layers[0].addWeight('wt_var', 
        [1, 5], 'int32', tf.initializers.ones());
  
// Printing output
console.log(res);
model.layers[0].getWeights()[0].print();

輸出:

{
  "dtype":"int32",
  "shape":[
    1,
    5
  ],
  "id":1582,
  "originalName":"wt_var",
  "name":"wt_var_2",
  "trainable_":true,
  "constraint":null,
  "val":{
    "kept":false,
    "isDisposedInternal":false,
    "shape":[
      1,
      5
    ],
    "dtype":"int32",
    "size":5,
    "strides":[
      5
    ],
    "dataId":{
      "id":2452
    },
    "id":2747,
    "rankType":"2",
    "trainable":true,
    "name":"wt_var_2"
  }
}
Tensor
     [[0.139703, 0.9717236],]

這裏使用 getWeights() 方法打印指定層的權重。

範例2:

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:2, inputShape:[1]}));
model.add(tf.layers.dense({units:3}));
  
// Calling addWeight() method
const res1 = model.layers[0].addWeight('w_v', 
    [1.2, 1.3], 'float32', tf.initializers.zeros(), true);
  
const res2 = model.layers[1].addWeight('wv', 
    ["a", "b"], 'int32', tf.initializers.ones(), false);
  
// Printing outputs
console.log(res1);
console.log(res2);
model.layers[0].getWeights()[0].print();
model.layers[1].getWeights()[0].print();

輸出:

{
  "dtype":"float32",
  "shape":[
    1.2,
    1.3
  ],
  "id":7,
  "originalName":"w_v",
  "name":"w_v",
  "trainable_":true,
  "constraint":null,
  "val":{
    "kept":false,
    "isDisposedInternal":false,
    "shape":[
      1.2,
      1.3
    ],
    "dtype":"float32",
    "size":1.56,
    "strides":[
      1.3
    ],
    "dataId":{
      "id":4
    },
    "id":9,
    "rankType":"2",
    "trainable":true,
    "name":"w_v"
  }
}
{
  "dtype":"int32",
  "shape":[
    "a",
    "b"
  ],
  "id":8,
  "originalName":"wv",
  "name":"wv",
  "trainable_":true,
  "constraint":null,
  "val":{
    "kept":false,
    "isDisposedInternal":false,
    "shape":[
      "a",
      "b"
    ],
    "dtype":"int32",
    "size":null,
    "strides":[
      "b"
    ],
    "dataId":{
      "id":5
    },
    "id":11,
    "rankType":"2",
    "trainable":true,
    "name":"wv"
  }
}
Tensor
     [[0.835237, 0.960075],]
Tensor
    [[0.4747705 , -0.6734858, 1.1417971],
     [-0.8185477, 0.1940626 , -0.98313 ]]

這裏, tf.initializers.zeros() 方法用於生成初始化為零的張量, tf.initializers.ones() 方法用於生成初始化為 1 的張量。

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




相關用法


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