我正在尝试使用CuDNNLSTM,尽管我正在关注有关此的教程,但我不知道为什么会得到这个奇怪的错误,我可以理解,但我无法调试: 因此,我有一个4073个时间序列* 175个特征数组,我正尝试将这175个特征一次传递给一个顺序模型,传递给CuDNNLSTM层,以使模型从中学习一些东西。
“ AlvoH”是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])
错误:
回溯(最近通话最近一次):
中的文件“ ml.py”,第64行
model.add(CuDNNLSTM(512, input_shape=(train_x.shape[1:None]), return_sequences=True))
_method_wrapper中的第456行“ C:\ Users \ Anaconda3 \ lib \ site-packages \ tensorflow \ python \ training \ tracking \ base.py” 结果=方法(自己,* 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 返回超级(RNN,自身)。调用(输入,** kwargs) 在调用中的文件“ C:\ Users \ Anaconda3 \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ base_layer.py”,第886行 self.name) 文件“ C:\ Users \ Anaconda3 \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ input_spec.py”,行180,处于assert_input_compatibility str(x.shape.as_list())) ValueError:层cu_dnnlstm的输入0与该层不兼容:预期ndim = 3,找到的ndim = 2。收到完整的图形:[无,175]
只要我能理解,本教程是在Tensorflow 2.0之前完成的,并且安装了2.0,我注意到有些变化,特别是CuDNNLSTMs层已经改变了,该层具有dif方法导入,因此问题可能是在那里。
这是这些2.0更改的结果吗?我尝试了所有方法,从传递train_x.shape,train_x.shape [1:],train_x.shape [:1]开始,尽管它应该有意义,依此类推,但我感到被卡住了。
提前感谢您的回答!
答案 0 :(得分:0)
在tensorflow 2.x中,您不必使用CuDNNLSTM,默认情况下,简单的LSTM层将在较低级别使用CuDNNLSTM。 input_shape =(train_x.shape [1:])的形状必须在范围2内,将输入更改为shape(4073,175,1)并尝试例如:
model = Sequential()
model.add(LSTM(512, input_shape=(175 ,1), return_sequences=True))
model.add(Dropout(0.2))
model.add(BatchNormalization())
答案 1 :(得分:0)
为了使代码正确训练,我必须进行更改。
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])
第一个方面是解决的,因为我必须将数组传递给numpy数组并进行一些更改,如https://machinelearningmastery.com/reshape-input-data-long-short-term-memory-networks-keras/所建议 但是现在,我还有另一个问题,这使我感到困惑,并且与此有关:如果我编写代码,则在培训结束时:
print(len(model.layers[0].get_weights()[0][0]))
print(train_x.shape[1:])
我会得到:
4096
(174, 1)
这意味着,我认为我在第一个LSTM层上有4096个权重,而在这里我应该只有174个。我对吗?