【问题标题】:Convert 2d tensor to 3d for use in LSTM layers将 2d 张量转换为 3d 以用于 LSTM 层
【发布时间】:2018-05-20 10:13:02
【问题描述】:

我正在构建一个小程序来预测一维浮点数组中的一些浮点数。到目前为止,我一直在使用dense 层来实现这一点:

const model = sequential();
model.add(layers.dense({units: 32, inputShape: [numCols,]}));
model.add(layers.activation({activation: 'relu'}));
model.add(layers.dense({units: 4}));
model.add(layers.dense({units: 1}));

我的xs 输入形状是[numRows, numCols](例如[132, 100] - 在包含 132 个示例的数据集中:[[1, 2, 3, ...], [4, 5, 6, ... ], ...]) 并且我的ys 输出是单个值[num](例如[17.50])。

但我想尝试 LSTM 来测试它是否会表现得更好。问题是 LSTM 的层需要一个 3d 矩阵,我不知道该怎么做。

我尝试了以下方法:

const trainXs = xs.clone()
  .reshape([numRows, numCols, 1]);

以上将我的输入[[1, 2, 3, ...], [4, 5, 6, ...], ...] 转换为[[[1], [2], [3], ...], [[4], [5], [6], ...], ...]

还有层:

const model = sequential();
model.add(layers.simpleRNN({
  units: 32,
  inputShape: [numCols, numRows], // [100, 132]
  recurrentInitializer: 'glorotNormal',
  returnSequences: true
}));
model.add(layers.simpleRNN({
  units: 32,
  recurrentInitializer: 'glorotNormal',
  returnSequences: true
}));

但上述操作会失败并出现以下错误:

Error: Error when checking input: expected simple_rnn_SimpleRNN1_input to have shape [,100,132], but got array with shape [132,100,1].

我有点困惑,我不确定我应该如何重塑我的 2d 张量以适应 LSTM 层的要求。

更新

合适的电话:

model.fit(trainXs, trainYs, {
  epochs: 1000,
  batchSize: 12,
  validationData: [testXs, testYs] // Test data has the same shape as trainXs/trainYs
});

我现在只有一层:

model.add(layers.simpleRNN({
  units: 32,
  inputShape: [1, numCols, numRows],
  recurrentInitializer: 'glorotNormal',
  returnSequences: true
}));

【问题讨论】:

    标签: tensorflow machine-learning tensorflow.js


    【解决方案1】:

    参考说:

    输入的形状(不包括第一个批次维度)需要至少是二维的,第一个维度是时间步长。

    所以输入的第一个维度应该包含时间步长。为简单起见,只需使用 1。因此,在您的情况下,传递给单元格的张量的形状将是 [1, numCols, numRows],正如您已经在错误消息中得到的那样。

    【讨论】:

    • 这会导致不同的错误:Error: Input 0 is incompatible with layer simple_rnn_SimpleRNN1: expected ndim=3, found ndim=4
    • 你误会了,层中的inputShape还是[numCols, numRows],但是trainXs的形状应该是[1, numCols, numRows]
    • 我刚刚注意到tf.layers.simpleRNN 甚至没有inputshape 选项,结果总是[batchSize, sequenceLength, units]
    • 好的。所以我重塑了xs.reshape([1, numRows, numCols]); 并将inputShape 保留为图层。抛出:Error when checking target: expected dense_Dense1 to have 3 dimension(s). but got array with shape 132,1。但是你说要重塑[1, numCols, numRows],我该怎么做?
    • 列和行的顺序不是我认为的问题,我只是对你的例子感到困惑,它把它弄混了。只坚持一个订单就行了,但如果你真的想切换它,transpose() 是方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    相关资源
    最近更新 更多