tensorflow - lstm 不同的输入和输出形状

标签 tensorflow tensorflow.js

输入形状

tf.tensor3d([
    [
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
    ],
    [
      [0.02, 0.03, 0.04],
      [0.02, 0.03, 0.04],
      [0.02, 0.03, 0.04],
      [0.02, 0.03, 0.04],
      [0.01, 0.02, 0.03],
    ],
    [
      [0.03, 0.05, 0.06],
      [0.03, 0.05, 0.06],
      [0.03, 0.05, 0.06],
      [0.03, 0.05, 0.06],
      [0.01, 0.02, 0.03],
    ],
  ]);

输出形状

  const ys = tf.tensor3d([
    [
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
      [0.01, 0.02, 0.03],
    ],
    [
      [0.02, 0.03, 0.04],
      [0.02, 0.03, 0.04],
      [0.02, 0.03, 0.04],
    ],
    [
      [-0.03, 0.05, 0.06],
      [0.03, -0.05, 0.06],
      [0.03, 0.05, -0.06],
    ],
  ]);

我正在尝试使用lstm层来创建预测模型。问题是我只知道如何更改 lstm 层的 units 变量。

我一直在寻找一种转换为 tensor3d 但具有不同行的方法。我只能找到一种方法将其变成一维或二维形状。

  model.add(
    tf.layers.lstm({
      units: 30,
      returnSequences: true,
      inputShape: [5, 3],
      batchInputShape: [3, 3, 3],
    })
  );
  model.add(tf.layers.lstm({ units: 3, returnSequences: true }));
  // Prepare the model for training: Specify the loss and the optimizer.
  model.compile({ loss: "meanSquaredError", optimizer: "adam" });

我必须放入哪些层和变量才能将[3,5,3]的输入转换为[3,3,3]

最佳答案

这是可以做什么的示例

 const model = tf.sequential();

 model.add(
   tf.layers.lstm({
    units: 30,
    returnSequences: true,
    inputShape: [5, 3],
    batchInputShape: [3, 3, 3],
   })
 );
 model.add(tf.layers.lstm({ units: 3, returnSequences: true }));
 model.add(tf.layers.flatten());
 // flatten is used so as to be able to change the size of the second dimension using the dense layer   
 model.add(tf.layers.dense({ units: 15}));
 // dense allow to remap the size of the previous layer to a different size 

 model.add(tf.layers.reshape({targetShape: [5, 3]}))
 // reshape to the appropriate shape
 model.summary() // will print the shape of all the layers; last layer will be [3, 5, 3]

关于tensorflow - lstm 不同的输入和输出形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71238448/

相关文章:

python - 如何将 tf.glorot_uniform_initializer() 传递到 tf.Variable(0 而不是 get_variable()

python - 有没有办法通过 JavaScript 在我的网站中使用 TensorFlow 模型?

math - tensorflow.js 使用 webgl 绘制心脏

javascript - Tensorflow Js 中多时间序列预测的最佳方法

javascript - 如何在javascript中导入tensorflow?导入文件,由本地 http 服务器提供服务

python - 从 keras.backend.tensorflow_backend 导入 set_session

python - 如何在 Arch Linux 中安装 Python 3.8 和 Python 3.9?

python - TensorFlow:EVAL 和 INFER 之间的区别

javascript - tensorflow.js 有没有办法输入一个以 -1 作为形状值之一的形状

javascript - 如何在 Tensorflow.js 中保护(混淆/DRM)经过训练的模型权重?