ValueError:层 cu_dnnlstm 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2.收到完整形状:[无,175] [英] ValueError: Input 0 of layer cu_dnnlstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 175]

查看:43
本文介绍了ValueError:层 cu_dnnlstm 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2.收到完整形状:[无,175]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验 CuDNNLSTM,我不知道为什么,尽管我正在学习有关此的教程,但我收到了这个奇怪的错误,我可以理解,但我无法调试:所以我有一个 4073 个时间序列 * 175 个特征数组,我试图将这 175 个特征传递给 Sequential 模型,一次一个,传递给 CuDNNLSTM 层,以便模型从中学习一些东西.

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"是RNN的目标.

"AlvoH" is the target of the RNN.

代码:

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])

错误:

回溯(最近一次调用最后一次):

Traceback (most recent call last):

文件ml.py",第 64 行,

File "ml.py", line 64, in

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

文件C:\Users\Anaconda3\lib\site-packages\tensorflow\python\training\tracking\base.py",第 456 行,在 _method_wrapper 中结果 = 方法(自我,*args,**kwargs)文件C:\Users\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\sequential.py",第198行,添加层(x)文件C:\Users\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\recurrent.py",第 654 行,调用return super(RNN, self).call(inputs, **kwargs)文件C:\Users\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py",第 886 行,调用自己的名字)文件C:\Users\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\input_spec.py",第 180 行,在 assert_input_compatibilitystr(x.shape.as_list()))ValueError:层 cu_dnnlstm 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2.收到完整形状:[无,175]

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]

据我所知,本教程是在 Tensorflow 2.0 之前制作的,并且安装了 2.0,我注意到一些事情发生了变化,特别是 CuDNNLSTMs 层,它有一个 diff 方法可以导入,所以问题可能在那里.

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.

这是 2.0 更改的结果吗?我尝试了所有方法,从通过 train_x.shape、train_x.shape[1:]、train_x.shape[:1],即使它应该有任何意义,等等,但我感觉卡住了.

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.

提前感谢您的回答!

推荐答案

为了正确训练代码而必须做出的更改.

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])

第一个方面已解决,因为我必须按照 https://machinelearningmastery.com/reshape-in​​put-data-long-short-term-memory-networks-keras/但是现在,我有另一个问题,这让我很困惑,并且与此有关:如果我编码,则在培训结束时:

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:]) 

我会得到:

4096 
(174, 1) 

这意味着,我想,我的第一个 LSTM 层有 4096 个权重,而我应该只有 174 个.我对吗?

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

这篇关于ValueError:层 cu_dnnlstm 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2.收到完整形状:[无,175]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆