Keras LSTM从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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注