InvalidArgumentError: Incompatible shapes with Keras LSTM Net

Viewed 5763

I want to predict the pressure of a machine. I have 18 input values and the pressure as output. So I have 19 columns and 7657 rows as the database consists of 7657 time steps and each counts for 1 sec.

I have a problem with the following code:

import tensorflow as tf
import pandas as pd
from matplotlib import pyplot
from sklearn.preprocessing import MinMaxScaler
from sklearn import linear_model  
from keras.models import Sequential
from keras.layers import Dense #Standard neural network layer
from keras.layers import LSTM
from keras.layers import Activation 
from keras.layers import Dropout

df = pd.read_csv('Testdaten_2_Test.csv',delimiter=';')

feature_col_names=['LSDI','LZT1I', ..... ,'LZT5I']
predicted_class_names = ['LMDI']

x = df[feature_col_names].values
y = df[predicted_class_names].values

x_train_size = 6400
x_train, x_test = x[0:x_train_size], x[x_train_size:len(x)]

y_train_size = 6400
y_train, y_test = y[0:y_train_size], y[y_train_size:len(y)]

nb_model = linear_model.LinearRegression()
nb_model.fit(X=x_train, y=y_train)

nb_predict_train = nb_model.predict(x_test)

from sklearn import metrics

def scale(x, y):
    # fit scaler
    x_scaler = MinMaxScaler(feature_range=(-1, 1))
    x_scaler = x_scaler.fit(x)
    x_scaled = x_scaler.transform(x)

    # fit scaler
    y_scaler = MinMaxScaler(feature_range=(-1, 1))
    y_scaler = y_scaler.fit(y)
    y_scaled = y_scaler.transform(y)
    return x_scaler, y_scaler, x_scaled, y_scaled

x_scaler, y_scaler, x_scaled, y_scaled = scale(x, y)
x_train, x_test = x_scaled[0:x_train_size], x_scaled[x_train_size:len(x)]
y_train, y_test = y_scaled[0:y_train_size], y_scaled[y_train_size:len(y)]

x_train=x_train.reshape(x_train_size,1,18)
y_train=y_train.reshape(y_train_size,1,1)

model = Sequential()

model.add(LSTM(10, return_sequences=True,batch_input_shape=(32,1,18)))  
model.add(LSTM(10,return_sequences=True))  
model.add(LSTM(1,return_sequences=True, activation='linear'))

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

model.fit(x_train, y_train, epochs=10,batch_size=32)

score = model.evaluate(x_test, y_test,batch_size=32)

predicted = model.predict(x_test)
predicted = y_scaler.inverse_transform(predicted)
predicted = [x if x > 0 else 0 for x in predicted]

correct_values = y_scaler.inverse_transform(y_test)
correct_values = [x if x > 0 else 0 for x in correct_values]
print(nb_predict_train)

I Get the Error:

ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (1257, 18)

After the last line of code.

I also tried to reshape the test data but then I get a very similar error.

I think, I'm missing something very easy or basic but I can't figure it out at the moment, as I'm just a beginner in coding neuronal networks. I need this for my master thesis so I would be very thank full if anyone could help me out.

1 Answers

The problem is that your model input batch_input_shape is fixed. The length of your test length is 1257 and cannot be divisible by 32. It should be changed as follows:

model.add(LSTM(10, return_sequences=True,batch_input_shape=(None,1,18)))

You should modify test shape before the model evaluate test.

x_test= x_test.reshape(len(x)-x_train_size,1,18)
y_test= y_test.reshape(len(y)-x_train_size,1,1)
score = model.evaluate(x_test, y_test,batch_size=32)

Of course, you have to reshape predicted and y_test before inverse_transform.

predicted = model.predict(x_test)
predicted= predicted.reshape(len(y)-x_train_size,1)
y_test= y_test.reshape(len(y)-x_train_size,1)
Related