📜  Tensorflow.js tf.layers addWeight() 方法

📅  最后修改于: 2022-05-13 01:56:55.921000             🧑  作者: Mango

Tensorflow.js tf.layers addWeight() 方法

Tensorflow.js 是由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。

.addWeight()函数用于将权重变量添加到所述层。

句法:

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

参数:

  • name:它是新的权重变量的指定名称,类型为字符串。
  • 形状:它是重量的规定形状。它的类型为 (null | number)[]。
  • dtype:它是权重的规定数据类型。它是可选的,可以是 float32、int32、bool、complex64 或字符串类型。
  • 初始化器:它是声明的初始化器实例。它是可选的,类型为 tf.initializers.Initializer。
  • 正则化器:它是规定的正则化器实例。它是可选的并且是Regularizer 类型。
  • 可训练:它通过假设层本身是类似可训练的,来说明是否应该通过反向传播来指示权重。它是可选的并且是布尔类型。
  • 约束:它是一个可选的可训练对象,类型为 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();


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": "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