📜  Tensorflow.js tf.layers.lstm()函数

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

Tensorflow.js tf.layers.lstm()函数

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

tf.layers.lstm()函数用于创建由一个 LSTMCell 组成的 RNN 层,LSTM 的 apply 方法对一系列输入进行操作。输入的形状需要至少是二维的,第一维是时间步长。 lstm是长短期记忆层。

句法:

tf.layers.lstm( args ); 

参数:

  • args:它指定给定的配置对象:
    • 循环激活:它指定将用于循环步骤的激活函数。此参数的默认值是硬 sigmoid。
    • unitForgetBias:它应该是布尔值。它用于在初始化时将遗忘门的偏差加 1。
    • implementation:指定实现方式。它可以是 1 或 2。为了获得卓越的性能,建议实施。
    • 单位:它是一个数字,表示输出形状的维度。
    • 激活:它是张量输入,是要使用的激活函数,默认为双曲正切。如果传递 null,将应用线性激活。
    • useBias:它是一个布尔值,指示是否使用偏置向量。
    • kernelInitializer:是核权重矩阵的Initializer,用于对输入进行线性变换。
    • reverseInitializer:是recurrentInitializer权重矩阵的Initializer,用于循环状态的线性变换。
    • biasInitializer:它是偏置向量的初始化器。
    • kernelRegularizer:它是应用于核权重矩阵的正则化函数。
    • 循环正则化器:它是一个应用于循环核权重矩阵的正则化函数。
    • biasRegularizer:它是应用于偏置向量的正则化函数。
    • kernelConstraint:它是应用于核权重矩阵的约束函数。
    • 循环约束:它是一个应用于循环内核权重矩阵的约束函数。
    • biasConstraint:它是应用于偏置向量的约束函数。
    • dropout:它是一个介于 0 到 1 之间的数字。表示输入的线性变换要丢弃的单位。
    • reverseDropout:它是一个介于 0 到 1 之间的数字。它表示循环状态的线性变换要丢弃的单元。
    • dropoutFunc:这是一个为测试 DI 目的而添加的函数。
    • 单元格:它应该是 RNN 单元格的一个实例或 RNN 单元格实例的数组。
    • returnSequences:它应该是布尔值。它定义了这两种天气最后输出或输出序列中的完整序列返回。
    • returnState:它应该是布尔值。它定义了输出中的返回最后状态。
    • goBackwards:它应该是布尔值。它定义是否以反向和反向顺序处理输入。
    • 有状态的:它应该是布尔值。它定义是否使用批次的最后状态作为当前批次的初始状态。
    • 展开:它应该是布尔值。它告诉是否使用展开的网络,否则使用符号循环。
    • inputDim:应该是一个数字。当该层用作模型的第一层时使用此层。它定义了层中输入的维度。
    • inputLength:应该是一个数字。当输入数据中的序列是恒定的时,该字段应该是。
    • inputShape:它应该是一个数字数组。该字段用于创建输入层,该输入层用于插入该层之前。
    • batchInputShape:应该是一个数字数组。如果输入形状和此字段作为创建用于在此图层之前插入的输入图层的参数提供,则将使用此字段。
    • batchSize:应该是一个数字。在没有 batchInputShape 的情况下,该字段用于创建具有输入形状的 batchInputShape。 batchInputShape:[batchSize,…inputShape]。
    • dtype:如果该层用作输入层,则该字段用作该层的数据类型。
    • name:应该是字符串类型。此字段定义此层的名称。
    • 可训练:它应该是布尔值。该字段定义了该层的权重是否可以进行拟合训练。
    • 权重:这应该是定义该层初始权重值的张量。
    • inputDType:这是用于 Legacy 支持的数据类型。

返回值:它返回 LSTM。

示例 1:

Javascript
import * as tf from "@tensorflow/tfjs"
  
// Long short term memrory layer
const LSTM = tf.layers.lstm({units: 4, returnSequences: true});
  
// Create an input with 10 time steps.
const input = tf.input({shape: [10, 40]});
const output = LSTM.apply(input);
  
// Here we get three value 1st value is unknown batch size;
// 2nd value is the sequence length of `input`,
// 3rd value is the `LSTMCell`'s number of units
console.log(JSON.stringify(output.shape));


Javascript
// Importing the tensorflow.js library
//import * as tf from "@tensorflow/tfjs"
  
// Create a new model with lstm Layer
const LSTM = tf.layers.lstm({units: 4, returnSequences: true});
  
// Create a 3d tensor
const x = tf.tensor([1, 2, 3, 4], [2, 2,1]);
  
// Apply lstm layer to x
const output = LSTM.apply(x);
  
// Print output
output.print()


输出:

[null,10,4]

示例 2:

Javascript

// Importing the tensorflow.js library
//import * as tf from "@tensorflow/tfjs"
  
// Create a new model with lstm Layer
const LSTM = tf.layers.lstm({units: 4, returnSequences: true});
  
// Create a 3d tensor
const x = tf.tensor([1, 2, 3, 4], [2, 2,1]);
  
// Apply lstm layer to x
const output = LSTM.apply(x);
  
// Print output
output.print()

输出:

Tensor
    [[[0.0829882, -0.0169818, -0.1132356, 0.0627953],
      [0.1211651, -0.0538836, -0.2851864, 0.1661802]],

     [[0.0739072, -0.0640151, -0.296207 , 0.1846195],
      [0.0578168, -0.1612808, -0.5005066, 0.3471084]]]

参考: https://js.tensorflow.org/api/latest/#layers.lstm