Search code examples
pythonpandaskerasdeep-learninglstm

ValueError: in LSTM model


This is my code. After run this code, this error msg is shown. How could I fix this?

ValueError: Unexpected result of predict_function (Empty batch_outputs). Please use Model.compile(..., run_eagerly=True), or tf.config.run_functions_eagerly(True) for more information of where went wrong, or file a issue/bug to tf.keras.

COLUMNS=['A'] #data set
dataset=df[COLUMNS]
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(np.array(dataset).reshape(-1,1))

train_size = int(len(dataset) * 0.60)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size], dataset[train_size:len(dataset)]

look_back=3
X_train=[]
testX=[]
y_train=[]
n_future = 1
features=2
timeSteps=4

model = Sequential()
X_train = np.asarray(X_train)
model.add(Bidirectional(LSTM(units=50, return_sequences=True, 
                             input_shape=(X_train.shape[0], 1))))

model.add(LSTM(units= 50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units= 50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units= 50))
model.add(Dropout(0.2))
model.add(Dense(units = n_future))

model.compile(optimizer="adam", loss="mean_squared_error", metrics=["acc"])

# make predictions
trainPredict = model.predict(X_train)
testPredict = model.predict(testX)

# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
y_train = scaler.inverse_transform([y_train])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])

Solution

  • I think changing your model.compile:

    model.compile(optimizer="adam", loss="mean_squared_error", metrics=["acc"], run_eagerly=True)
    

    This should do the trick.