0
votes

I'm trying to train an LSTM network to do an unsupervised binary classification.

I have a matrix of integers as input, every row is a different trace ad every column is a feature.

This is the model I used:

time_steps = 4000
features = 25
model = Sequential()
model.add(LSTM(128, input_shape=(time_steps, features), name='lstm'))
model.add(Dense(1, activation='relu'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, x_train, batch_size=batch_size, epochs=epochs, verbose=2)

And this is the error I get:

Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (1, 4000, 25)

It is generated when it tries to run model.fit

The input is formed like the following:

x_train = np.array([input_array[:4000]])

Every trace of the input has 25 features.

I'm new in the field and I can't figure out how to solve the problem. I have checked similar tickets but none of them helped me.

Here some of the tickets I analyzed:

Error when checking target: expected dense_1 to have 3 dimensions, but got array with shape (118, 1)

ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (68, 50, 50, 50, 1)

Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (1, 1226, 2)

ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (10000, 1)

2

2 Answers

1
votes

A couple of remarks:

  • x_train is given as input to a LSTM layer, which expects 3D input. The first dimension is the samples, second is the time steps and final dimension are the features.
  • When you call fit you pass x_train twice meaning you want the target to be the same as the input data. This makes sense if you are trying to do an auto-encoder, however it cannot work on this architecture. The output of the Dense layer will be a single value, there is no way it can match the shape of the input (which is 3D). Below I added a y_train so that your model instead predicts a single value.
time_steps = 40
features = 25
x_train = np.random.normal(size=(10, time_steps, features))
y_train =  np.random.normal(size=(10, ))
model = Sequential()
model.add(LSTM(128, input_shape=(time_steps, features), name='lstm'))
model.add(Dense(1, activation='relu'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=32, epochs=2, verbose=2)

Best of luck!

0
votes

You haven't shown the shape of input_array, but you might try reshaping it with np.reshape(input_array, (4000, 25)). Read more on reshape here