Keras LSTM 从 CSV 加载数据 "expected ndim=3, found ndim=2. Full shape received: (None, 150)"

Keras LSTM loading data from CSV "expected ndim=3, found ndim=2. Full shape received: (None, 150)"

我是 LSTM 的初学者,如果这是一个基本问题,我很抱歉。我一直在尝试制作一个简单的 LSTM 模型,该模型从 csv 文本文件加载数据进行训练

    trainX = pd.read_csv("Train\X_Data.txt", header=None, delim_whitespace=True).to_numpy()
    trainY = pd.read_csv("Train\Y_Data.txt", header=None, delim_whitespace=True).to_numpy()

    testX = pd.read_csv("Test\X_Data.txt", header=None, delim_whitespace=True).to_numpy()
    testY = pd.read_csv("Test\Y_Data.txt", header=None, delim_whitespace=True).to_numpy()

    n_timesteps = trainX.shape[0]
    n_features = trainX.shape[1]

    model = Sequential()
    model.add(LSTM(100, input_shape=trainX.shape, return_sequences=True))
    model.add(Dropout(0.5))
    model.add(Dense(100, activation='relu'))
    #may need 2 neurons as there are two classes
    model.add(Dense(1, activation='sigmoid'))

    model.summary()

    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

    # fit network
    model.fit(trainX, trainY, epochs=EPOCHS, batch_size=BATCH_SIZE, verbose=1)
    # evaluate model
    evalLosses, evalAccuracy = model.evaluate(testX, testY, batch_size=BATCH_SIZE, verbose=1)

    print("Overall Accuracy: " + str(evalAccuracy))
    print("Overall Loss: " + str(evalLosses))

我的输入是:

trainY.shape = (35, 1)
trainX.shape = (35, 150)


trainX = [[0.48597709 0.52190752 0.62556772 ... 0.09958187 0.12535847 0.0833305 ]
 [0.40917949 0.40525872 0.24515716 ... 0.33276069 0.40186229 0.36288622]
 [0.16203835 0.14811591 0.1618184  ... 0.08745848 0.09398027 0.1056776 ]
 ...
 [0.21770377 0.24859037 0.20659391 ... 0.01323494 0.01249982 0.01307911]
 [0.27596078 0.26605097 0.36028712 ... 0.10316001 0.10662966 0.10724351]
 [0.34860233 0.3500129  0.35434798 ... 0.04347154 0.02899346 0.02327774]]

trainY = [[0]
 [0]
 [0]
 [0]
  .
  .
  .
 [0]
 [0]
 [1]
 [1]
 [1]]

当我尝试将数据拟合到我的模型时,出现以下错误

ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 150)

如何加载我的数据?形状是 2 维 (35,150),那么为什么 keras 只能看到 (None, 150)?

谢谢

trainX.shape = (35, 150) 这意味着您有 35 个样本 150。但是根据 Keras,您需要在第一个位置传递带有 batch_size 的数据。因此,您必须将 2D 输入扩展为 3D:

trainX = tf.expand_dims(trainX, axis=-1) # new shape = (35, 150, 1)
trainY = tf.expand_dims(trainY, axis=-1) # new shape = (35, 150, 1)

然后您可以将数据传递给模型:

model = Sequential()
model.add(LSTM(100,input_shape=(trainX.shape[1], trainX.shape[2]), return_sequences=True)
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

编辑:

由于您正在处理二元分类任务,因此请将损失从 categorical_crossentropy 更改为 binary_crossentropy