【问题标题】:TF Keras: ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2TF Keras: ValueError: Input 0 of layer sequence is in compatible with the layer: expected ndim=3, found ndim=2
【发布时间】:2020-08-14 18:47:54
【问题描述】:

所以我有形成模式的二维向量序列。我想预测序列如何继续。 我有一个 start_xy 数组,由数组组成,顺序为 start_x 和 start_y: 例如[1、2.4、3.8] end_xy 也一样。

我想训练一个模型一个序列预测模型:

import numpy as np
import pickle
import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.callbacks import ModelCheckpoint
import training_data_generator
tdg = training_data_generator.training_data_generator(500)
trainingdata = tdg.produceTrainingSequences()
print("Printing DATA!:")
start_xy =[]
end_xy =[]

for batch in trainingdata:
    for pattern in batch:
        order = 1
        for sequence in pattern:
            start = [order,sequence[0],sequence[1]] 
            start_xy.append(start)
            end = [order,sequence[2],sequence[3]]
            end_xy.append(end)
            order = order +1

    
    model = Sequential()
    model.add(LSTM(64, return_sequences=False, input_shape=(2,len(start_xy))))
    model.add(Dense(2, activation='relu'))
    model.compile(loss='mse', optimizer='adam')
    model.fit(start_xy,end_xy,batch_size=len(start_xy), epochs=5000,  verbose=2)

但我收到错误消息:

 ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [320, 3]

我怀疑我必须以某种方式重塑我的输入,但我还不明白如何。 我该如何进行这项工作? 我这样做是否正确?

【问题讨论】:

标签: python tensorflow machine-learning keras


【解决方案1】:

您通常只需将数据转换为 numpy 数组并对该数据进行一些重塑,以便模型能够接受它。

首先将 start_xy 转换为 numpy 数组,然后将其重塑为 3 个暗淡:

start_xy = np.array(start_xy)
start_xy = start_xy.reshape(*start_xy.shape, 1)

接下来将 LSTM 层的输入形状固定为 [3, 1]:

model.add(LSTM(64, return_sequences=False, input_shape=start_xy.shape[1:]))

让我知道错误是否仍然存在或是否出现另一个错误!

【讨论】:

  • 这解决了我原来的问题,所以这绝对是解决这个问题的方法:-) 另一方面,我现在得到“找不到可以处理输入的数据适配器: , ( 包含类型 {'( 包含类型 {"", " "})'})" 但这是一个不同的问题
猜你喜欢
  • 1970-01-01
  • 2022-11-06
  • 2017-07-28
  • 2021-08-01
  • 2017-10-26
  • 2019-07-19
  • 2018-05-19
  • 2019-01-03
  • 1970-01-01
相关资源
最近更新 更多