简体   繁体   English

Keras LSTM ValueError:层“顺序”的输入 0 与层不兼容:预期形状 =(无,478405,33),找到形状 =(1、33)

[英]Keras LSTM ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 478405, 33), found shape=(1, 33)

Code:代码:

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)

Error:错误:

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

Model: Model:

_________________________________________________________________
 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
_________________________________________________________________

I think the problem is that you are reshaping the variable X like so X.reshape((1, 478405, 33)) , however, this does not change the shape of X on its own.我认为问题在于您正在像X.reshape((1, 478405, 33))那样重塑变量 X ,但是,这不会自行改变 X 的形状。 You need to set the result to X, like this X = X.reshape((1, 478405, 33)) .您需要将结果设置为 X,例如X = X.reshape((1, 478405, 33))

Try this尝试这个

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=False))
model.add(Dropout(0.2))
model.add(Dense(1, activation = "sigmoid"))

For time series you must use a TimeseriesGenerator对于时间序列,您必须使用 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)

暂无
暂无

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

相关问题 ValueError: 层序列 33 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:[64, 100] - ValueError: Input 0 of layer sequential_33 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [64, 100] ValueError:“顺序”层的输入 0 与层不兼容:预期形状 =(无,223461,5),找到形状 =(无,5) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(None, 455, 30),找到的形状 =(None, 30) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 455, 30), found shape=(None, 30) ValueError: 层“sequential_1”的输入 0 与层不兼容:预期形状=(None, 60, 1),发现形状=(None, 59, 1) - ValueError: Input 0 of layer "sequential_1" is incompatible with the layer: expected shape=(None, 60, 1), found shape=(None, 59, 1) ValueError:层“顺序”的输入0与层不兼容:预期形状=(无,90),发现形状=(无,2,90) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 90), found shape=(None, 2, 90) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(无,33714,12),找到形状 =(无,12) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 33714, 12), found shape=(None, 12) | ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(None, 28, 28),找到的形状 =(None, 28, 3) - | ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 28, 28), found shape=(None, 28, 3) ValueError: 层“sequential_32”的输入 0 与层不兼容:预期形状=(None, 3, 1),发现形状=(32, 0, 1) - ValueError: Input 0 of layer "sequential_32" is incompatible with the layer: expected shape=(None, 3, 1), found shape=(32, 0, 1) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状=(无,128,128,3),找到形状=(32,128,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(32, 128, 3) ValueError:层“顺序”的输入 0 与层不兼容:预期形状=(无,32,32,3),找到形状=(32,32,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM