简体   繁体   English

Keras LSTM多维输出错误-预期time_distributed_17具有3个维度,但数组的形状为(1824,3)

[英]Keras LSTM multidimensional output error — expected time_distributed_17 to have 3 dimensions, but got array with shape (1824, 3)

I am trying to predict multidimensional values in sequence, eg [[0, 0, 2], [1, 0, 3], [2, 3, 4], [3, 2, 5], [4, 0, 6], [5, 0, 7] ... ] and want each of the [x, y, z] dimensions to be captured by the LSTM. 我正在尝试按顺序预测多维值,例如[[0, 0, 2], [1, 0, 3], [2, 3, 4], [3, 2, 5], [4, 0, 6], [5, 0, 7] ... ]并希望LSTM捕获每个[x, y, z]尺寸。

When I attempt to run model.fit() on the model below, I get the error in the title, 当我尝试在以下模型上运行model.fit()时,出现标题错误,

ValueError: Error when checking target: expected time_distributed_19 to have 3 dimensions, but got array with shape (1824, 3)

I know the output layer should have three dimensions, but I'm getting confused in my thinking about how I need the LSTM to deal with my sequence of n-dimensional values. 我知道输出层应该具有三个维度,但是在思考如何使用LSTM处理n维值序列时,我感到困惑。

Here is my model. 这是我的模特。 Note that if I uncomment the Flatten() line as some solutions suggest, I get a nondescript AssertionError on model.compile() 请注意,如果我按照一些解决方案的建议取消注释Flatten()行, model.compile()model.compile()上收到一个非描述性的AssertionError

# X shape: (1824, 256, 3)
# Y shape: (1824, 3)

model = Sequential()

model.add(LSTM(units=128, input_shape=(X.shape[1], X.shape[2]), return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(units=128, return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(units=128, return_sequences=True))
model.add(Dropout(0.2))

# model.add(Flatten())

model.add(TimeDistributed(Dense(Y.shape[1], activation='softmax')))

model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')

Here is the model summary: 这是模型摘要:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_145 (LSTM)              (None, 256, 128)          67584     
_________________________________________________________________
dropout_140 (Dropout)        (None, 256, 128)          0         
_________________________________________________________________
lstm_146 (LSTM)              (None, 256, 128)          131584    
_________________________________________________________________
dropout_141 (Dropout)        (None, 256, 128)          0         
_________________________________________________________________
time_distributed_19 (TimeDis (None, 256, 3)            387       
=================================================================
Total params: 199,555
Trainable params: 199,555
Non-trainable params: 0
_________________________________________________________________
None

This model was running before I added the TimeDistributed() wrapper (though I had to remove return_sequences=True from the last hidden layer for it to work), but I added TimeDistributed() because I don't think the individual variables of my 3-dimensional feature values were being captured. 在添加TimeDistributed()包装器之前,该模型已运行(尽管我必须从最后一个隐藏层中删除return_sequences=True才能使它起作用),但我添加了TimeDistributed()因为我认为我的3中没有单独的变量维特征值已被捕获。

Any insight is greatly appreciated, thank you. 非常感谢任何见解,谢谢。

UPDATE UPDATE

Thanks to nuric 's quick answer to my initial question, I confirmed that the way I was previously doing it was the "right way" and my confusion stems from the predictions I'm getting. 多亏nuric对我的第一个问题的快速回答,我确认我以前的做法是“正确的方法”,而我的困惑源于我得到的预测。 Given a sequence from X, I get a 3D vector like this: [9.915069e-01 1.084390e-04 8.384804e-03] (and it's always about [1, 0, 0] ) 给定一个来自X的序列,我得到一个3D向量,像这样: [9.915069e-01 1.084390e-04 8.384804e-03] (并且总是[1, 0, 0] [9.915069e-01 1.084390e-04 8.384804e-03] [1, 0, 0]

In my previous LSTM models, this prediction vector's max value corresponded to the index in my one-hot encoding of letters/words, but here what I want is predictions for the x, y, and z values of the next 3D vector in the sequence. 在我以前的LSTM模型中,此预测矢量的最大值对应于我对字母/单词的单次编码时的索引,但是在这里,我想要的是对序列中下一个3D矢量的x,y和z值的预测。

You have a mismatch in what the model predicts, currently 3D, and what the target is, 2D. 您对模型的预测(当前为3D)和目标(二维)不匹配。 You have 2 options: 您有2个选择:

  1. Apply Flatten and remove TimeDistributed which means the model will predict based on the entire sequence. 应用Flatten并删除TimeDistributed ,这意味着模型将基于整个序列进行预测。
  2. Remove return_sequences=True from last LSTM to let the LSTM compress the sequence and again remove TimeDistributed . 从上一个LSTM中删除return_sequences=True ,以使LSTM压缩序列,然后再次删除TimeDistributed This way the model will predict based on the last LSTM output not the sequences. 这样,模型将基于最后的LSTM输出而不是序列进行预测。

I would prefer the second option given the size of the sequence and the number of hidden units you have. 考虑到序列的大小和隐藏单元的数量,我希望使用第二个选项。 Option one will create a very large kernel for the Dense layer if you just flatten the sequence, ie too many parameters. 如果您只是展平序列,即参数过多,则选项一将为Dense层创建一个非常大的内核。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LSTM 和 CNN:ValueError:检查目标时出错:预期 time_distributed_1 有 3 个维度,但得到了形状为 (400, 256) 的数组 - LSTM and CNN: ValueError: Error when checking target: expected time_distributed_1 to have 3 dimensions, but got array with shape (400, 256) 检查目标时出错:预期 time_distributed_6 具有 3 个维度,但得到形状为 (200, 80) 的数组 - Error when checking target: expected time_distributed_6 to have 3 dimensions, but got array with shape (200, 80) Tensorflow/keras 错误:ValueError:检查输入时出错:预期 lstm_input 具有 3 个维度,但得到的数组具有形状(4012、42) - Tensorflow/keras error: ValueError: Error when checking input: expected lstm_input to have 3 dimensions, but got array with shape (4012, 42) Keras LSTM输入-ValueError:检查输入时出错:预期input_1具有3维,但数组的形状为(1745,1) - Keras LSTM Input - ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (1745, 1) 预期 lstm_1_input 有 3 个维度,但得到了形状为 (0, 1) 的数组 - expected lstm_1_input to have 3 dimensions, but got array with shape (0, 1) Keras LSTM模型-预期具有3维,但具有2维数组 - Keras LSTM model - expected to have 3 dimensions, but got array with 2 ValueError:检查输入时出错:预期 time_distributed_55_input 有 5 个维度,但得到了形状为 (10, 48, 48, 1) 的数组 - ValueError: Error when checking input: expected time_distributed_55_input to have 5 dimensions, but got array with shape (10, 48, 48, 1) ValueError:检查目标时出错:预期time_distributed_7具有4个维度,但数组的形状为(500,18,64) - ValueError: Error when checking target: expected time_distributed_7 to have 4 dimensions, but got array with shape (500, 18, 64) 检查输入时出错:预期 lstm_1_input 有 3 个维度,但得到形状为 (5, 3) 的数组 - Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (5, 3) 检查输入时出错:预期 lstm_input 有 3 个维度,但得到了形状为 (5, 10) 的数组 - Error when checking input: expected lstm_input to have 3 dimensions, but got array with shape (5, 10)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM