Context:
This step covers the preparation of the train data and the test data.
Codes:
Min-Max Scaler
scaler=MinMaxScaler(feature_range=(0,1))
data.index=data.Date
data.drop('Date',axis=1,inplace=True)
final_data = data.values
train_data=final_data[0:200,:]
valid_data=final_data[200:,:]
scaler=MinMaxScaler(feature_range=(0,1))
scaled_data=scaler.fit_transform(final_data)
x_train_data,y_train_data=[],[]
for i in range(60,len(train_data)):
x_train_data.append(scaled_data[i-60:i,0])
y_train_data.append(scaled_data[i,0])
x_train_data = np.array(x_train_data)
y_train_data = np.array(y_train_data)
LSTM Model
We are defining the Long Short-Term Memory model in this stage.
lstm_model=Sequential()
lstm_model.add(LSTM(units=50,return_sequences=True,input_shape=(np.shape(x_train_data)[1],1)))
lstm_model.add(LSTM(units=50))
lstm_model.add(Dense(1))
model_data=data[len(data)-len(valid_data)-60:].values
model_data=model_data.reshape(-1,1)
model_data=scaler.transform(model_data)
Train and Test Data
This stage comprises the preparation of both the train and test data.
lstm_model.compile(loss='mean_squared_error',optimizer='adam')
lstm_model.fit(x_train_data,y_train_data,epochs=1,batch_size=1,verbose=2)
X_test=[]
for i in range(60,model_data.shape[0]):
X_test.append(model_data[i-60:i,0])
X_test=np.array(X_test)
X_test=np.reshape(X_test,(X_test.shape[0],X_test.shape[1],1))
The error! :(
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-4bebe72cad71> in <module>()
1 lstm_model.compile(loss='mean_squared_error',optimizer='adam')
----> 2 lstm_model.fit(x_train_data,y_train_data,epochs=1,batch_size=1,verbose=2)
3
4 X_test=[]
5 for i in range(60,model_data.shape[0]):
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
984 except Exception as e: # pylint:disable=broad-except
985 if hasattr(e, "ag_error_metadata"):
--> 986 raise e.ag_error_metadata.to_exception(e)
987 else:
988 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:830 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:813 run_step *
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:770 train_step *
y_pred = self(x, training=True)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:989 __call__ *
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py:212 assert_input_compatibility *
raise ValueError('Input ' + str(input_index) + ' of layer ' +
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (1, 60)
your x_train_data
needs to be 3 dimensional and in shape (number of observations, time steps, number of variables). So try:
x_train_data = np.array(x_train_data)
x_train_data = x_train_data.reshape(x_train_data.shape[0], x_train_data.shape[1], 1)
Same applies for x_test_data
. You might need to apply np.ravel(y_train_data)
before fitting. If other parts are ok then these changes should suffice