2

I am a beginner with Neural Networks and not very well aware with mathematics at the back end of scaling matrices using scaler.inverse_transform. I am using a tutorial to apply LSTM on my data and forecast time series for one of the variable. I am having this problem in prediction when I am scaling. The code is as below.

This is how I have trained Data.

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM

# split into train and test sets
values = reframed.values
n_train_sec = 5000
train = values[:n_train_sec, :]
test = values[n_train_sec:, :]
# split into input and outputs
train_X, train_y = train[:, :-1], train[:, -1]
test_X, test_y = test[:, :-1], test[:, -1]
# reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

This is how I designed model.

# design network
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
# fit network
history = model.fit(train_X, 
train_y,epochs=100,batch_size=160,validation_data=(test_X, test_y), 
verbose=2, shuffle=False)

and this is how I am trying to predict

from math import sqrt
from numpy import concatenate

# make a prediction
yhat = model.predict(test_X)
test_X = test_X.reshape((test_X.shape[0], test_X.shape[2]))
# invert scaling for forecast
inv_yhat = concatenate((yhat, test_X[:, 1:]), axis=1)
inv_yhat = scaler.inverse_transform(inv_yhat)
inv_yhat = inv_yhat[:,0]
# invert scaling for actual
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_X[:, 1:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:,0]
# calculate RMSE
rmse = sqrt(mean_squared_error(inv_y, inv_yhat))
print('Test RMSE: %.3f' % rmse)

I am getting.

ValueError: operands could not be broadcast together with shapes (4599,12) 
(11,) (4599,12) 

Initially the shape of test_X was (4599, 1, 12). If somebody is interested to know more about data I can send data and html of iPython file.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Can you include the code that trains `scaler`, and some code to generate example data for `test_X` and `test_y`? – Jeremy McGibbon Sep 19 '17 at 00:00
  • 1
    I have edited the post. let me know if you need something more. – Shan-E-Zehra Lashari Sep 19 '17 at 00:07
  • Where does `reframed` come from? Can you put in code that generates something like `values`, maybe using `np.random.randn(n_samples, n_features)`? And where does `scaler` come from? Ideally the code you've pasted here should run and give the same error you've gotten if I copy it into my own Python console. – Jeremy McGibbon Sep 19 '17 at 01:26
  • `scaler` is almost certainly something like `from sklearn.preprocessing import MinMaxScaler` followed by `scaler = MinMaxScaler(feature_range=(0,1))`. I'm agreeing with @Shan-E-ZehraLashari that LSTM's are non-trivial if you want to predict multiple variables. – Adrian Keister Jan 18 '19 at 02:58
  • Does this answer your question? [ValueError: operands could not be broadcast together with shapes - inverse\_transform- Python](https://stackoverflow.com/questions/45847006/valueerror-operands-could-not-be-broadcast-together-with-shapes-inverse-trans) – Hadij Jan 15 '20 at 19:00

0 Answers0