Home > database >  LSTM Data Cardinality ambiguous issue regarding number of samples
LSTM Data Cardinality ambiguous issue regarding number of samples

Time:03-01

My attempt at solving: Previously I had: ValueError: cannot reshape array of size 8244 into shape (5,1) for my LSTM model which then I changed xtrain shape to reshape(-1,1374,1).

|Column | Count | Dtype | |:---- |:------:| -----:| | somedata | 1718 | Float64 | | somedata | 1718 | Float64 | | somedata | 1718 | Float64 | | somedata | 1718 | Float64 | ValueError: Data cardinality is ambiguous: x sizes: 1 y sizes: 1374 Make sure all arrays contain the same number of samples.

new.shape <- (1718,7)

# create the variables for prediction and split into training and test sets

y = np.log(new['Close'].astype(int)) # we want to predict the adjusted close price
X = new.drop('Close', axis=1) # predictive variables (removing Adj close from it)

#split the data into training and test sets
xtrain, xtest, ytrain, ytest = train_test_split(X, y, test_size=0.20, random_state=42)

# Build the LSTM model
model2 = Sequential()
model2.add(LSTM(128, return_sequences=True, input_shape= (xtrain.shape[1], 1)))
model2.add(LSTM(64, return_sequences=False))
model2.add(Dense(25))
model2.add(Dense(1))
# view model summary
model2.summary()
# Compile the model
model2.compile(optimizer='adam', loss='mean_squared_error')
xtrain.shape <-(1374,6)
#predictions 
predictions2 = model2.predict(np.array(xtest).reshape(-1,5,1))

CodePudding user response:

You just have to make sure that x and y have the same number of samples, meaning their first dimensions are the same. Here is a working example:

import tensorflow as tf

x = tf.random.normal((100, 50, 1))
y = tf.random.normal((100, 1))
model2 = tf.keras.Sequential()
model2.add(tf.keras.layers.LSTM(128, return_sequences=True, input_shape= (x.shape[1], 1)))
model2.add(tf.keras.layers.LSTM(64, return_sequences=False))
model2.add(tf.keras.layers.Dense(25))
model2.add(tf.keras.layers.Dense(1))
model2.summary()
model2.compile(optimizer='adam', loss='mean_squared_error')
model2.fit(x, y, epochs=2)

A bidirectional-LSTM would look like this:

import tensorflow as tf

x = tf.random.normal((100, 50, 1))
y = tf.random.normal((100, 1))
model2 = tf.keras.Sequential()
model2.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128, return_sequences=True), input_shape= (x.shape[1], 1)))
model2.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, return_sequences=False)))
model2.add(tf.keras.layers.Dense(25))
model2.add(tf.keras.layers.Dense(1))
model2.summary()
model2.compile(optimizer='adam', loss='mean_squared_error')
model2.fit(x, y, epochs=2)
  • Related