Tensorflow.js tf.layers.simpleRNNCell()函数
Tensorflow.js 是一个由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。
.layers.simpleRNNCell( )函数用于为 simpleRNN 查找单元类。
句法:
tf.layers.simpleRNNCell (args)
参数:
- 单位:它是一个张量输入,具有正整数单位,是输出空间的一个维度。
- 激活:它是张量输入,是要使用的激活函数,默认为双曲正切。如果传递 null,将应用线性激活。
- useBias:这是一个张量输入,其中偏置向量用于该层。
- kernelInitializer:它是一个张量输入,是内核权重矩阵的初始化器,用于输入的线性变换。
- 循环初始化器:它是一个张量输入,是循环核权重矩阵的初始化器,用于循环状态的线性变换。
- biasInitializer:它是一个张量输入,是偏置向量的初始化器。
- kernelRegularizer:它是张量输入,其中正则化函数应用于内核权重矩阵。
- reverseRegularizer:它是张量输入,其中正则化函数应用于recurrent_kernel 权重矩阵。
- biasRegularizer:它是张量输入,其中正则化函数应用于偏置向量。
- kernelConstraint:它是一个张量输入,其中约束函数应用于核权重矩阵。
- 循环约束:它是一个张量输入,其中约束函数应用于循环内核权重矩阵。
- biasConstraint:这是一个张量输入,其中约束函数应用于偏置向量。
- dropout:它是一个张量输入,其中要丢弃的部分单位用于输入的线性变换和 0 到 1 之间的浮点数。
- reverseDropout:这是一个张量输入,其中要丢弃的部分单位用于循环状态的线性变换和 0 到 1 之间的浮点数。
- inputShape:这是一个张量输入,将用于创建一个输入层以插入到该层之前(如果已定义)。它仅适用于输入层。
- batchInputShape:这是一个张量输入,将用于创建一个输入层以插入到该层之前(如果已定义)。它仅适用于输入层。
- batchSize:如果指定了 inputShape 而未指定 batchInputShape,则它是一个张量输入,其中 batchSize 用于构造 batchInputShape。
- dType:这是一个张量输入,是该层的数据类型,默认为“float32”。
- name:这是一个张量输入,是该层的名称。
- trainable:这是一个张量输入,默认为 true,该层的权重是否可通过 fit 更新。
- weights:它是一个张量输入,可以是层的初始权重值。
- inputDType:它是一个张量输入,具有遗留支持,不用于新代码。
返回值:返回 SimpleRNNCell。
示例 1:在此示例中,SimpleRNNCell 与 RNN 子类不同。在 SimpleRNN 中,它的 apply 方法仅获取单个时间步的输入数据并在该时间步返回单元格的输出,而 SimpleRNN 获取多个时间步的输入数据。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining input elements
const cell = tf.layers.simpleRNNCell({units: 3});
const input = tf.input({shape: [11]});
const output = cell.apply(input);
console.log(JSON.stringify(output.shape));
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining input elements
const cells = [
tf.layers.simpleRNNCell({units: 8}),
tf.layers.simpleRNNCell({units: 16}),
];
const rnn = tf.layers.rnn({cell: cells, returnSequences: true});
// Create an input with 20 time steps and
// a length-30 vector at each step
const input = tf.input({shape: [20, 30]});
const output = rnn.apply(input);
console.log(JSON.stringify(output.shape));
输出:
[null, 11]
示例 2:在此示例中,SimpleRNNCell 的实例可用于构建 RNN 层。这个工作流程最典型的用途是将多个单元格组合成一个堆叠的 RNN 单元格(即内部的 StackedRNNCell)并使用它来创建一个 RNN。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining input elements
const cells = [
tf.layers.simpleRNNCell({units: 8}),
tf.layers.simpleRNNCell({units: 16}),
];
const rnn = tf.layers.rnn({cell: cells, returnSequences: true});
// Create an input with 20 time steps and
// a length-30 vector at each step
const input = tf.input({shape: [20, 30]});
const output = rnn.apply(input);
console.log(JSON.stringify(output.shape));
输出:
[null, 20, 16]
参考: https://js.tensorflow.org/api/latest/#layers.simpleRNNCell