Home > Enterprise >  Failing to match the required number of dimensions for Keras LSTM model
Failing to match the required number of dimensions for Keras LSTM model

Time:11-02

I have tried to set up a bare minimum example for building a neural network. I got 5 prices for a car over 5 different dates. No matter how I rearrange my data, I get 1 out of 2 types of errors.

Either

ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (1, 1)

or

ValueError: Data cardinality is ambiguous:
x sizes: 5
y sizes: 1
Make sure all arrays contain the same number of samples.

I've begun to suspect that no matter how I arrange this data, it will never work. Do I need to add another dimension (e.g. both prices and amount of tax)?

Full code:

import numpy as np
from keras.models import Sequential #, LSTM
from keras.layers.core import  Dense;
from keras.layers import LSTM
import tensorflow as tf

time_list = [ 1296000.0, 19350000.0, 29635200.0, 48294000.0, 45961200.0] # my sample data
price_list = [ 0.05260218,0.05260218,0.0,0.96769388,1.0 ]

these_dates = np.array(time_list) 
prices = np.array(price_list)

#these_dates = these_dates.reshape(-1, 1)  # ive tried every variery of dimensions, nothing works.  
#prices = prices.reshape(-1, 1)

model = Sequential()
model.add(LSTM(10 , return_sequences = True , input_shape =(len(prices) , 1) ,input_dim=2))
model.compile(optimizer = 'adam' , loss = 'mean_squared_error')
model.fit( prices ,these_dates , batch_size = 1 , epochs =1)

Specifying the input_ndim doesn't seem to help. What do I need to do to get these dimensions to match? Will it ever work?

CodePudding user response:

As explained in the keras documentation the required input shape is (batch, timesteps, features). In your case this is (5, 1, 1) since batch=5, timesteps=1 and features=1, see the example below.

import numpy as np
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, LSTM
tf.random.set_seed(0)

# generate the features
X = np.array([1296000.0, 19350000.0, 29635200.0, 48294000.0, 45961200.0])

# generate the target
y = np.array([0.05260218, 0.05260218, 0.0, 0.96769388, 1.0])

# rescale the features
X = (X - np.min(X)) / (np.max(X) - np.min(X))

# reshape the features
X = X.reshape(len(X), 1, 1)
print(X.shape)
# (5, 1, 1)

# define the model
model = Sequential()
model.add(LSTM(10, return_sequences=False, input_shape=(X.shape[0], X.shape[1])))
model.add(Dense(1))

# compile the model
model.compile(optimizer='adam', loss='mse')

# fit the model
model.fit(X, y, batch_size=1, epochs=10)

# generate the model predictions
model.predict(X)
# array([[0.05585098],
#        [0.0940358 ],
#        [0.11524458],
#        [0.152216  ],
#        [0.14772224]], dtype=float32)
  • Related