简体   繁体   中英

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

I am new to Keras and am attempting to implement an RNN.

My dataset as a whole consists of 431 records and 818 attributes. Each record is a 1D array of 818 values (each "value" being associated with an attribute for that record). Lastly, there is a 70-30 split to build the training and test sets, respectively.

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. 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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM