ValueError:第lstm层的输入0与该层不兼容:预期ndim = 3,找到的ndim = 2。收到完整的形状:[无,18]

时间:2019-09-26 14:42:22

标签: python tensorflow keras lstm

我是Keras的新手,我正在尝试建立个人使用/未来学习的模型。我刚开始使用python,并想出了这段代码(在视频和教程的帮助下)。我有16324个实例的数据,每个实例包含18个特征和1个因变量。

File image = await ImagePicker.pickImage(source: ImageSource.camera);

在线

import pandas as pd import os import time import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout, LSTM, BatchNormalization from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint EPOCHS = 10 BATCH_SIZE = 64 NAME = f"-TEST-{int(time.time())}" df = pd.read_csv("EntryData.csv", names=['1SH5', '1SHA', '1SA5', '1SAA', '1WH5', '1WHA', '2SA5', '2SAA', '2SH5', '2SHA', '2WA5', '2WAA', '3R1', '3R2', '3R3', '3R4', '3R5', '3R6', 'Target']) df_val = 14554 validation_df = df[df.index > df_val] df = df[df.index <= df_val] train_x = df.drop(columns=['Target']) train_y = df[['Target']] validation_x = validation_df.drop(columns=['Target']) validation_y = validation_df[['Target']] model = Sequential() model.add(LSTM(128, input_shape=(train_x.shape[1:]), return_sequences=True)) model.add(Dropout(0.2)) model.add(BatchNormalization()) model.add(LSTM(128, return_sequences=True)) model.add(Dropout(0.1)) model.add(BatchNormalization()) model.add(LSTM(128)) model.add(Dropout(0.2)) model.add(BatchNormalization()) model.add(Dense(32, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(2, activation='softmax')) opt = tf.keras.optimizers.Adam(lr=0.001, decay=1e-6) model.compile(loss='sparse_categorical_crossentropy', optimizer=opt, metrics=['accuracy']) tensorboard = TensorBoard(log_dir=f'logs/{NAME}') filepath = "RNN_Final-{epoch:02d}-{val_acc:.3f}" 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( train_x, train_y, batch_size=BATCH_SIZE, epochs=EPOCHS, validation_data=(validation_x, validation_y), callbacks=[tensorboard, checkpoint],) score = model.evaluate(validation_x, validation_y, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) model.save("models/{}".format(NAME))

抛出错误:

  

ValueError:lstm层的输入0与该层不兼容:   预期ndim = 3,找到的ndim = 2。收到完整的图形:[无,18]

我已经在这个网站和google上搜索解决方案已有几个小时了,但我无法为此找到合适的答案,或者我也无法针对类似问题实施解决方案。

谢谢您的提示。

1 个答案:

答案 0 :(得分:1)

一个LSTM网络需要一个三维输入。这是对我有用的代码:

在将其馈送到神经网络之前:

X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])

在神经网络中:

LSTM(64, input_shape=(X_train.shape[1:])),