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

核能气质少年 提交于 2021-01-29 13:58:06

问题


I am experimenting with CuDNNLSTMs and, i dont know why, even though i am following a tutorial on this, i get this weird error, that i can understand, but i can't debug: So i have a 4073 time-series * 175 features array and i am trying to pass those 175 features to the Sequential model, one at a time, to a CuDNNLSTM layer, in order for the model to learn something from it.

"AlvoH" is the target of the RNN.

The code:

train_x, train_y = trainDF, trainDF["AlvoH"]
validation_x, validation_y = validationDF[:-Sequencia], validationDF["AlvoH"][:-Sequencia]

print(train_x.shape[1:])

model = Sequential()
model.add(CuDNNLSTM(512, input_shape=(train_x.shape[1:]), return_sequences=True))
model.add(Dropout(0.2))
model.add(BatchNormalization())

model.add(Dense(3, activation="softmax"))

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

model.compile(loss="sparse_categorical_crossentropy", optimizer=opt, metrics=["accuracy"])

tensorboard = TensorBoard(log_dir=f'logs/{NAME}')

checkpoint = tf.keras.callbacks.ModelCheckpoint("models/{}.model".format("RNN_Final-{EPOCH:02d}", 
        monitor="val_acc", 
        verbose=1, 
        save_best_only=True, 
        mode="max"))

history = model.fit(train_x, train_y,
        batch_size=BATCH_SIZE, 
        epochs=EPOCHS, 
        validation_data=(validation_x, validation_y), 
        callbacks=[tensorboard, checkpoint])

the error:

Traceback (most recent call last):

File "ml.py", line 64, in

model.add(CuDNNLSTM(512, input_shape=(train_x.shape[1:None]), return_sequences=True))

File "C:\Users\Anaconda3\lib\site-packages\tensorflow\python\training\tracking\base.py", line 456, in _method_wrapper result = method(self, *args, **kwargs) File "C:\Users\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 198, in add layer(x) File "C:\Users\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\recurrent.py", line 654, in call return super(RNN, self).call(inputs, **kwargs) File "C:\Users\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 886, in call self.name) File "C:\Users\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\input_spec.py", line 180, in assert_input_compatibility str(x.shape.as_list())) ValueError: Input 0 of layer cu_dnnlstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 175]

As long as i can understand, this tutorial was made before Tensorflow 2.0 and, have 2.0 installed, i noticed some things have changed, in particular, to the CuDNNLSTMs layer, which have a dif method to import, so the problem may be there.

Is this a result of those 2.0 changes? I tried everything, from passing train_x.shape, train_x.shape[1:], train_x.shape[:1], even though it should make any sense, and so on and i am feeling stuck.

Thanks for the answers, in advance!


回答1:


In tensorflow 2.x you don't have to use CuDNNLSTM and simple LSTM layer will use the CuDNNLSTM at low level by default. the shape of input_shape=(train_x.shape[1:]) must be of rang 2 ,change the input to shape (4073 ,175 ,1 ) and try e.g :

model = Sequential()
model.add(LSTM(512, input_shape=(175 ,1), return_sequences=True))
model.add(Dropout(0.2))
model.add(BatchNormalization())



回答2:


Changes that i had to make in order for the code to properly train.

    train_x, train_y = array(trainDF[trainDF.columns.tolist()[:-1]]), array(trainDF["AlvoH"])
    validation_x, validation_y = array(validationDF[validationDF.columns.tolist()[:-1]][:-Sequencia]), array(validationDF["AlvoH"][:-Sequencia])
    train_x = train_x.reshape(train_x.shape[0],train_x.shape[1], 1)
    train_y = train_y.reshape(train_y.shape[0], 1, 1)
    validation_x = validation_x.reshape(validation_x.shape[0],validation_x.shape[1], 1)
    validation_y = validation_y.reshape(validation_y.shape[0], 1, 1)
    
    model = Sequential()
    model.add(LSTM(1024, input_shape=(train_x.shape[1:]), return_sequences=True))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())
    
    model.add(Dense(3, activation="softmax"))
    
    opt = tf.keras.optimizers.Adam(learning_rate=0.0001, decay=1e-8)
    
    model.compile(loss="sparse_categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
    
    tensorboard = TensorBoard(log_dir=f'logs/{NAME}')
    
    filepath = "RNN_Final-{epoch:02d}"
    checkpoint = ModelCheckpoint("TEMP/{}.model".format(filepath, 
            monitor="val_acc", 
            verbose=1, 
            save_best_only=True, 
            mode="max"))
    
    history = model.fit(train_x, train_y,
            batch_size=BATCH_SIZE, 
            epochs=EPOCHS, 
            validation_data=(validation_x, validation_y), 
            callbacks=[tensorboard, checkpoint])

The first aspect was SOLVED, as i had to pass the array to a numpy array and do some changes, as suggested by https://machinelearningmastery.com/reshape-input-data-long-short-term-memory-networks-keras/ But now, i have another issue, which is confusing me and it is related with this: if i code, at the end of the training:

print(len(model.layers[0].get_weights()[0][0])) 
print(train_x.shape[1:]) 

I will get:

4096 
(174, 1) 

which means, i think, i that i have 4096 weights for the first LSTM layer, where i should have only 174. Am i right?



来源:https://stackoverflow.com/questions/63361829/valueerror-input-0-of-layer-cu-dnnlstm-is-incompatible-with-the-layer-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!