0
votes

I have an X_train = [(4096, 18464),(4097, 43045),(4098, 38948),(4099, 2095),(4100, 59432),(4101, 55338),(4102, 51245),(4103, 26658),(4104, 30755),....] with a shape (3283, 2) and

y_train = [19189, 19189, 19189, ..., 1155085434105692417, 1155120620365152513,...] with a shape (3283, 1)

I reshaped the X_train using the code:

X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))

and got shape (3283, 1, 2)

now I build a lstm model:

data_dim= 2
timesteps=1
num_classes=2

model_pass = Sequential()
model_pass.add(LSTM(units=64,  return_sequences=True, 
                input_shape=(timesteps, data_dim)))
model_pass.add(Dense(2, activation='sigmoid'))
model_pass.compile(loss='binary_crossentropy', optimizer='adam',metrics=['accuracy'])
model_pass.summary()
model_pass.fit(X_train, y_train,batch_size=1, epochs = 1, verbose = 1)

But it's giving me an error: ValueError: Error when checking target: expected dense_24 to have 3 dimensions, but got array with shape (3283, 1)

Can anybody tell me what should I do?

1

1 Answers

0
votes

After the dense layer, the output shape is (number of samples, timesteps, 2). The number 2 is from Dense(2,...). But y_train_pass probably has shape (number of samples, 1). That gives an error.

Here is an example of possible codes where I changed Dense(2,...) to Dense(1,...) and reshape y_train

from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
import numpy as np

X_train = np.random.randn(3283,2)
X_test = np.random.randn(1000,2)
y_train = np.random.randint(2, size=(3283,1))
print(y_train.shape)
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
y_train = np.reshape(y_train, (y_train.shape[0],1, y_train.shape[1]))

data_dim= 2
timesteps=1
num_classes=2

model_pass = Sequential()
model_pass.add(LSTM(units=64,  return_sequences=True, 
                input_shape=(timesteps, data_dim)))
model_pass.add(Dense(1, activation='sigmoid'))
model_pass.compile(loss='binary_crossentropy', optimizer='adam',metrics=['accuracy'])
model_pass.summary()
model_pass.fit(X_train, y_train,batch_size=1, epochs = 1, verbose = 1)

By the way, just a comment. It is weird that your y_train values do not have 'binary' values and you use loss='binary_crossentropy'.