I am trying to predict stock closing price using news sentiment analysis with the help LSTM neural network but after I do model.fit(). I am encountered with the error message: ValueError: Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 3, 3), found shape=(1, 3, 11)
. I am a newbie in the field of data science. I have attached the screenshot of the dataset.
Independent and dependent feature selection
X = df.drop('Close',axis=1).values
y= df['Close'].values
Feature scaling
scaler = MinMaxScaler()
scalerFeatures = MinMaxScaler(feature_range=(0, 1))
scalerTarget = MinMaxScaler(feature_range=(0, 1))
featuresScaled = scalerFeatures.fit_transform(X)
targetScaled = scalerTarget.fit_transform(y.reshape(-1, 1))
Python function to convert dataset into numpy array
def createDataset(dataset, target, lookBack):
dataX, dataY = [], []
for i in range(len(dataset) - lookBack):
a = dataset[i:(i + lookBack), :]
dataX.append(a)
dataY.append(target[i + lookBack])
return np.array(dataX), np.array(dataY)
**Code to reshape **
lookBack = 3
X, y = createDataset(featuresScaled, targetScaled, lookBack)
print(X[:2])
print(y[:2])
trainSize = int(len(X) * 0.8)
testSize = len(X) - trainSize
trainX, testX = X[0:trainSize, :], X[trainSize:len(X), :]
trainY, testY = y[0:trainSize], y[trainSize:len(y)]
trainX = np.reshape(trainX, (trainX.shape[0], lookBack, trainX.shape[2]))
testX = np.reshape(testX, (testX.shape[0], lookBack, testX.shape[2]))
Neural network layer
batchSize = 1
epoch = 20
neurons = 100
dropout = 0.6
model = Sequential()
model.add(LSTM(neurons, return_sequences=True, activation='tanh', input_shape=(lookBack, X.shape[1])))
model.add(Dropout(dropout))
model.add(LSTM(neurons, return_sequences=True, activation='tanh'))
model.add(Dropout(dropout))
model.add(LSTM(neurons, activation='tanh'))
model.add(Dropout(dropout))
model.add(Dense(units=1, activation='linear', activity_regularizer=regularizers.l1(0.00001)))
model.add(Activation('tanh'))
model.summary()
Model compilation and fit
model.compile(loss='mean_squared_error' , optimizer='RMSprop')
model.fit(trainX, trainY, epochs=epoch, batch_size=batchSize, verbose=1, validation_split=0.2)
Error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[91], line 3
1 model.compile(loss='mean_squared_error' , optimizer='RMSprop')
----> 3 model.fit(trainX, trainY, epochs=epoch, batch_size=batchSize, verbose=1, validation_split=0.2)
File /opt/conda/lib/python3.10/site-packages/keras/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File /tmp/__autograph_generated_filegi2fwtkf.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/opt/conda/lib/python3.10/site-packages/keras/engine/training.py", line 1284, in train_function *
return step_function(self, iterator)
File "/opt/conda/lib/python3.10/site-packages/keras/engine/training.py", line 1268, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/opt/conda/lib/python3.10/site-packages/keras/engine/training.py", line 1249, in run_step **
outputs = model.train_step(data)
File "/opt/conda/lib/python3.10/site-packages/keras/engine/training.py", line 1050, in train_step
y_pred = self(x, training=True)
File "/opt/conda/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/opt/conda/lib/python3.10/site-packages/keras/engine/input_spec.py", line 298, in assert_input_compatibility
raise ValueError(
ValueError: Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 3, 3), found shape=(1, 3, 11)
With the help of code and dataset provided, how can I resolve the error ValueError: Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 3, 3), found shape=(1, 3, 11)