Keras LSTM ValueError: Input 0 of layer "sequential" is in compatible with the layer: expe

Posted

技术标签:

【中文标题】Keras LSTM ValueError: Input 0 of layer "sequential" is in compatible with the layer: expected shape=(None, 478405, 33), found shape=(1, 33)【英文标题】:Keras LSTM ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 478405, 33), found shape=(1, 33) 【发布时间】:2022-01-03 07:05:33 【问题描述】:

代码:

Y = Y.to_numpy()
X = X.to_numpy()

X.reshape((1, 478405, 33))

opt = tf.keras.optimizers.Adam(lr=0.001, decay=1e-6)

model = Sequential()
model.add(LSTM(33, return_sequences=True, input_shape=(X.shape[1],  X.shape[0]), activation='sigmoid'))
model.add(Dropout(0.2))
model.add(LSTM(33, return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(1, activation = "sigmoid"))

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

filepath = "RNN_Final-epoch:02d-val_acc:.3f"  # unique file name that will include the epoch and the validation acc for that epoch
checkpoint = ModelCheckpoint("models/.model".format(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')) # saves only the best ones

history = model.fit(X, Y,  epochs=35, batch_size=1, shuffle=False)

scores = model.evaluate(X, Y)

错误:

WARNING:tensorflow:Model was constructed with shape (None, 33, 478405) for input KerasTensor(type_spec=TensorSpec(shape=(None, 33, 478405), dtype=tf.float32, name='lstm_input'), name='lstm_input', description="created by layer 'lstm_input'"), but it was called on an input with incompatible shape (1, 33).
Traceback (most recent call last):
  File "C:\Users\W10\PycharmProjects\TheCryptoBot\cryptobot\app\ai-model -2.py", line 84, in <module>
    history = model.fit(X, Y,  epochs=35, batch_size=1, shuffle=False)
  File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1129, in autograph_handler
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\engine\training.py", line 878, in train_function  *
        return step_function(self, iterator)
    File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\engine\training.py", line 867, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\engine\training.py", line 860, in run_step  **
        outputs = model.train_step(data)
    File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\engine\training.py", line 808, in train_step
        y_pred = self(x, training=True)
    File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "C:\Users\W10\PycharmProjects\TheCryptoBot\venv\lib\site-packages\keras\engine\input_spec.py", line 213, in assert_input_compatibility
        raise ValueError(f'Input input_index of layer "layer_name" '

    ValueError: Exception encountered when calling layer "sequential" (type Sequential).
    
    Input 0 of layer "lstm" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (1, 33)
    
    Call arguments received:
      • inputs=tf.Tensor(shape=(1, 33), dtype=float32)
      • training=True
      • mask=None


Process finished with exit code 1

型号:

_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm (LSTM)                 (None, 478405, 33)        63153948  
                                                                 
 dropout (Dropout)           (None, 478405, 33)        0         
                                                                 
 lstm_1 (LSTM)               (None, 478405, 33)        8844      
                                                                 
 dropout_1 (Dropout)         (None, 478405, 33)        0         
                                                                 
 dense (Dense)               (None, 478405, 1)         34        
                                                                 
=================================================================
Total params: 63,162,826
Trainable params: 63,162,826
Non-trainable params: 0
_________________________________________________________________

【问题讨论】:

【参考方案1】:

我认为问题在于您正在像 X.reshape((1, 478405, 33)) 那样重塑变量 X,但是,这并不会自行改变 X 的形状。您需要将结果设置为 X,例如 X = X.reshape((1, 478405, 33))

【讨论】:

它不起作用,如果我像这样 len(X) 变成 1 那样重塑 X,并且这个错误 ValueError: Data cardinality is ambiguous: x sizes: 1 y sizes: 478405 确保所有数组都包含相同的样本数。【参考方案2】:

对于时间序列,您必须使用 TimeseriesGenerator

    generator = TimeseriesGenerator(X, Y, length=478404, batch_size=100)
# print each sample
#for i in range(len(generator)):
    #x, y = generator[i]
    #print('%s => %s' % (x, y))


opt = tf.keras.optimizers.Adam(learning_rate=0.001, decay=1e-6)

print("Adding layer 1...")
model = Sequential()

model.add(LSTM(33, return_sequences=True, input_shape=(478404, 33), activation='sigmoid'))
print("Adding layer 2...")
model.add(Dropout(0.2))
print("Adding layer 3...")
model.add(LSTM(33, return_sequences=True))
print("Adding layer 4...")
model.add(Dropout(0.2))
print("Adding layer 5...")
model.add(Dense(1, activation="sigmoid"))


print("Adding layer 6...")


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

print ('model compiled')

print (model.summary())

# Compile model




filepath = "RNN_Final-epoch:02d-val_acc:.3f"  # unique file name that will include the epoch and the validation acc for that epoch
checkpoint = ModelCheckpoint("models/.model".format(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')) # saves only the best ones

history = model.fit(generator, steps_per_epoch=1, epochs=30, verbose=0)
print("Fit DOne")
print(history.history.keys())
# evaluate the model
scores = model.evaluate(generator)

【讨论】:

以上是关于Keras LSTM ValueError: Input 0 of layer "sequential" is in compatible with the layer: expe的主要内容,如果未能解决你的问题,请参考以下文章

Shap LSTM (Keras, TensorFlow) ValueError: shape mismatch: objects cannot be broadcast to a single sh

Keras ConvLSTM2D:保存模型时出现ValueError

Keras LSTM 输入维度设置

LSTM错误python keras

用于 LSTM 的 Keras 多元形状

尝试从 Keras 运行顺序模型时出现 ValueError