【问题标题】:I have a question on tensorflow/tfjs LSTM inputShape and also LSTM basic understaning我对 tensorflow/tfjs LSTM inputShape 以及 LSTM 基本理解有疑问
【发布时间】:2020-05-17 07:40:15
【问题描述】:

我的训练数据集是针对一个用户的

let training = [
    [[2019.1], [10]],
    [[2019.2], [2]],
    [[2019.4], [11]],
    [[2019.5], [31]]
]

对于这个简单的输入,我想获得下个月的预测。首先要了解 LSTM 时间序列。使用以下代码生成训练模型

let train_x = training.map(i => {return i[0]})
let train_y = training.map(j=> {return j[1]})

const model = tf.sequential();
**model.add(tf.layers.lstm({units: 128, returnSequences: false,  inputShape:[train_x.length]}));**
model.add(tf.layers.dropout(0.2))
model.add(tf.layers.dense({units: training.length, activation: 'softmax'}));

model.compile({loss: 'categoricalCrossentropy', optimizer: tf.train.rmsprop(0.002)});

**const xs = tf.tensor3d([train_x]);**
const ys = tf.tensor2d(train_y, [training.length, train_y[0].length]);

错误:

Error: Input 0 is incompatible with layer lstm_LSTM1: expected ndim=3, found ndim=2

问题是应该给定什么输入形状,什么应该是 tf.tensor3d 输入。据我了解,我正在尝试这个简单的例子。在没有 keras 的情况下尝试一下

【问题讨论】:

  • 因为我只有一个用户的这么多数据。我想预测下个月的数字示例 --> [[2019.6], [22]]

标签: tensorflow deep-learning model lstm tensorflow.js


【解决方案1】:

错误信息很简单:

Input 0 is incompatible with layer lstm_LSTM1: expected ndim=3, found ndim=2

lstm 层需要 3d 输入。这意味着 inputShape 应该是[a, b],其中ab 都是数字(a 也可以是null)。有一个由 2 个元素组成的序列。因此a1(单个序列:我们希望层在进行预测之前看到的序列数;看这里的数据似乎是1,但可以更改为不同的值)和b2(每个序列 2 个元素)。

培训将是:

training = [
    [[2019.1, 10]],
    [[2019.2, 2]],
    [[2019.4], [11]],
    [[2019.5], [31]]
]
xs = tf.tensor(training).reshape([-1, 1, 2])

而lstm层变成如下:

model.add(tf.layers.lstm({units: 128, returnSequences: false,  inputShape:[1, 2]}));

瞧,整个模型如下所示:

const model = tf.sequential();
model.add(tf.layers.lstm({units: 128, returnSequences: false,  inputShape:[1, 2]}));
model.add(tf.layers.dropout(0.2))
model.add(tf.layers.dense({units: 20, activation: 'softmax'}));
model.summary()
model.compile({loss: 'categoricalCrossentropy', optimizer: tf.train.rmsprop(0.002)});
model.summary()
const training = [
        [[2019.1, 10]],
        [[2019.2, 2]],
        [[2019.4], [11]],
        [[2019.5], [31]]
    ]
const xs = tf.tensor(training).reshape([-1, 1, 2])
await model.fit(xs, tf.ones([4, 20]))
model.predict(tf.ones([1, 1, 2])).print()

【讨论】:

  • 得到这样的输出[0.02468624711036682,0.02503986470401287,0.026008365675807,0.024018235504627228,0.024773791432380676,0.025004612281918526,0.02489587850868702,0.025202354416251183,0.025096451863646507,0.024056443944573402,0.026048900559544563,0.026084518060088158] 跨度>
  • 你的数组长度和最后一层的units有关。这个问题已经被接受了。如果您还有其他问题,请考虑打开一个新主题
  • 我为其他人添加了这个输出,他们将来会检查这个。这样他们就可以确定输出。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 2018-09-16
  • 2017-01-12
  • 1970-01-01
  • 2016-12-19
  • 2017-09-28
  • 1970-01-01
相关资源
最近更新 更多