0
$\begingroup$

Trying to use sigmoid as an activation function for the last dense layer of a LSTN, I get this error

ValueError: `logits` and `labels` must have the same shape, received ((None, 60, 1) vs (None,)).

The code is this

scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train) #scaled_train 
X_test_s = scaler.transform(X_test) #scaled_test     

length = 60
n_features=89

generator = TimeseriesGenerator(X_train_s, Y_train['TARGET_ENTRY_LONG'], length=length, batch_size=1)
validation_generator = TimeseriesGenerator(X_test_s, Y_test['TARGET_ENTRY_LONG'], length=length, batch_size=1)


# define model
model = Sequential()
model.add(LSTM(90, activation='relu', input_shape=(length, n_features), return_sequences=True, dropout = 0.3))
model.add(LSTM(30,activation='relu',return_sequences=True, dropout = 0.3))
model.add(Dense(1, activation = 'sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy')

model.summary()

# fit model

model.fit(generator,epochs=3,
                    validation_data=validation_generator)
                   #callbacks=[early_stop])

If I replace the last layer declaration with the following one

model.add(Dense(1))

I get no errors, but probably also not the expected result. Any idea?

$\endgroup$

1 Answer 1

0
$\begingroup$

Found the cause of the trouble after several attempts, it was in the layer before the last one: it shall have no "return_sequences=True" set, that is for all the layers before if the last one is a dense layer for binary classification using sigmoid as activation function. Therefore, this layer

model.add(LSTM(30,activation='relu',return_sequences=True, dropout = 0.3))

shall be written instead as following

model.add(LSTM(30,activation='relu', dropout = 0.3))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.