简体   繁体   English

在 Keras 中实现 LSTM。 ValueError:层顺序与层不兼容

[英]Implementing LSTM in Keras. ValueError: layer sequential is incompatible with the layer

I am new to Keras and am attempting to implement an RNN.我是 Keras 的新手,正在尝试实现 RNN。

My dataset as a whole consists of 431 records and 818 attributes.我的整个数据集包含 431 条记录和 818 个属性。 Each record is a 1D array of 818 values (each "value" being associated with an attribute for that record).每条记录都是 818 个值的一维数组(每个“值”都与记录的属性相关联)。 Lastly, there is a 70-30 split to build the training and test sets, respectively.最后,有一个 70-30 的拆分来分别构建训练集和测试集。

I am encountering an error that says the following:我遇到一个错误,内容如下:

ValueError: Input 0 of layer sequential_62 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 817]

I have unfortunately spent an unnecessary number of hours attempting to resolve this issue, and am hoping that some of the veteran users of this library can be of help.不幸的是,我花了不必要的时间试图解决这个问题,并希望这个库的一些资深用户可以提供帮助。 I believe I had a version of this error earlier, but was able to resolve it with the discussion on this link .我相信我之前有过这个错误的一个版本,但是能够通过关于这个链接的讨论来解决它。 I am not sure if the current error is still related to that discussion — I don't think it is.我不确定当前的错误是否仍然与该讨论有关——我不认为是。 Note that viewing this discussion is not necessary to answering the question.请注意,查看此讨论不是回答问题所必需的。

Below is a minimally reproducible example.下面是一个最小可重现的例子。 All of the implementation is absolutely the same, except for that I have hard-coded in a smaller dataset.所有的实现都是完全一样的,除了我在一个较小的数据集中进行了硬编码。 It is 6 1D arrays, each with six values.它是 6 个 1D arrays,每个有六个值。 The exact same error occurs.发生完全相同的错误。

import tensorflow as tf
import numpy as np
import pandas as pd
import math

from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout, Masking, Embedding

data = [[3.0616e-03, 3.2530e-03, 2.6789e-03, 0.0000e+00, 1.9135e-04, 1.0000e+00],
 [1.1148e-02, 1.4231e-03, 1.8975e-03, 0.0000e+00, 0.0000e+00, 1.0000e+00],
 [5.7723e-03, 7.5637e-03, 2.1895e-03, 0.0000e+00, 3.9809e-04, 1.0000e+00],
 [7.4699e-03, 1.2048e-03, 1.4458e-03, 0.0000e+00, 4.8193e-04, 1.0000e+00],
 [6.0682e-03, 4.1850e-04, 1.6740e-03, 0.0000e+00, 4.1850e-04, 1.0000e+00],
 [9.1189e-03, 7.6906e-04, 1.2085e-03, 0.0000e+00, 1.0987e-04, 1.0000e+00]]

df = pd.DataFrame(data)

#Separating out the features for training set. 
trainingInputs = df.iloc[0:(round(0.7 * df.shape[0])), :(df.shape[1] - 1)].values

#Separating out the target for training set.
trainingOutputs = df.iloc[0:(round(0.7 * df.shape[0])), (df.shape[1] - 1)].values

#Separating out the features for testing set.
testingInputs = df.iloc[(math.ceil(0.7 * df.shape[0])):, :(df.shape[1] - 1)].values

#Separating out the target for testing set. 
desiredOutputs = df.iloc[(math.ceil(0.7 * df.shape[0])):, (df.shape[1] - 1)].values

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

#trainingInputs = np.expand_dims(trainingInputs, 1)

model = Sequential()

# Going to another recurrent layer: return the sequence.
model.add(LSTM(128, input_shape = (trainingInputs.shape[1:]), activation = 'relu', return_sequences = True))
model.add(Dropout(0.2))

model.add(LSTM(64, activation = 'relu'))
model.add(Dropout(0.2))

model.add(Dense(32, activation = 'relu'))
model.add(Dropout(0.2))

model.add(Dense(10, activation = 'softmax'))

opt = tf.keras.optimizers.Adam(lr = 1e-3, decay = 1e-5)

#Mean squared error.
model.compile(loss = 'sparse_categorical_crossentropy',
             optimizer = opt,
             metrics = ['accuracy'])
model.fit(trainingInputs, trainingOutputs, epochs = 3, validation_data = (testingInputs, desiredOutputs))

Please let me know if any clarification is needed regarding the question.请让我知道是否需要对该问题进行任何澄清。 I would be happy to provide it.我很乐意提供。

All you've done is correct except you've forgotten to reshape your testing inputs:您所做的一切都是正确的,除了您忘记重塑您的测试输入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras LSTM ValueError:层“顺序”的输入 0 与层不兼容:预期形状 =(无,478405,33),找到形状 =(1、33) - Keras LSTM ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 478405, 33), found shape=(1, 33) Keras LSTM 层序贯_10的输入0与层不兼容 - Keras LSTM Input 0 of layer sequential_10 is incompatible with the layer LSTM:层序的输入0与层不兼容 - LSTM: Input 0 of layer sequential is incompatible with the layer ValueError: 层序列 1 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_1 is incompatible with the layer TensorFlow ValueError:层顺序的输入0与层不兼容 - TensorFlow ValueError: Input 0 of layer sequential is incompatible with the layer ValueError: 层序号_2 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_2 is incompatible with the layer "ValueError: Input 0 of layer "sequential" is in compatible with the layer" 在预测中 - "ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction LSTM Keras 层中的维度不兼容 - Incompatible dimensions in LSTM Keras layer ValueError: 层序号_3 的输入 0 与层不兼容: - ValueError: Input 0 of layer sequential_3 is incompatible with the layer: Keras:层顺序的输入 0 与层不兼容 - Keras: Input 0 of layer sequential is incompatible with the layer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM