[英]Tensorflow: Shape error in LSTM, The layer "lstm" has multiple inbound nodes, with different output shapes
[英]TensorFlow different input and output shapes for stateful LSTM model
我想创建一个“顺序”model(您可能已经猜到的时间序列 model),它需要20
天的特征大小为2
的过去数据,并预测未来1
天的特征大小为2
.
我发现您需要为有状态 LSTM model 指定批量大小,因此如果我指定批量大小为32
,则 model 的最终 output 形状为(32, 2)
,我认为这意味着 model 正在预测未来32
天而不是1
.
我将如何修复它 go?
另外,在我解决问题之前询问; 例如,如果我指定批处理大小为32
,但我想预测形状为(1, 20, 2)
的输入,那么 model 会正确预测还是什么,因为我将批处理大小从32
更改为1
。 谢谢你。
您不需要指定 batch_size。 但是你应该输入 3-d 张量:
import tensorflow as tf
from tensorflow.keras.layers import Input, LSTM, Dense
from tensorflow.keras import Model, Sequential
features = 2
dim = 128
new_model = Sequential([
LSTM(dim, stateful=True, return_sequences = True),
Dense(2)
])
number_of_sequences = 1000
sequence_length = 20
input = tf.random.uniform([number_of_sequences, sequence_length, features], dtype=tf.float32)
output = new_model(input) # shape is (number_of_sequences, sequence_length, features)
predicted = output[:,-1] # shape is (number_of_sequences, 1, features)
(32, 2) 的形状表示您的序列长度为 32。
批量大小是训练的一个参数(在反向传播错误之前应将多少序列馈送到 model - 请参阅随机梯度下降法)。 它不会影响您的数据(应该是 3-d -(序列数、序列长度、特征))。
如果您只需要预测一个序列 - 只需将形状为 (1, 20, 2) 的张量提供给 model。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.