Hagbard
Hagbard

Reputation: 3680

ValueError: Input 0 is incompatible with layer model error for 1-D time series classification model

I'm attempting to follow this time series classification with transformers with Keras tutorial. This is the relevant part of my code:

x_trainScaledNPArray = np.array(x_trainScaled)
x_testScaledNPArray = np.array(x_testScaled)
y_trainNPArray = np.array(y_train)
y_testNPArray = np.array(y_test)

print(x_trainScaledNPArray.shape)
print(x_testScaledNPArray.shape)
print(y_trainNPArray.shape)
print(y_testNPArray.shape)

n_classes = len(np.unique(y_train))
input_shapeIndex0 = x_trainScaledNPArray.shape[0:]
print(input_shapeIndex0)

model = build_model(n_classes,input_shapeIndex0,head_size=256,num_heads=4,ff_dim=4,num_transformer_blocks=4,mlp_units=[128],mlp_dropout=0.4,dropout=0.25)
model.compile(loss="sparse_categorical_crossentropy",optimizer=keras.optimizers.Adam(learning_rate=1e-4),metrics=["sparse_categorical_accuracy"])

callbacks = [keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True)]
model.fit(x_trainScaledNPArray,y_trainNPArray,validation_split=0.2,epochs=200,batch_size=64,callbacks=callbacks)

This is the output I get:

(18287, 2048)
(347, 2048)
(18287,)
(347,)
(18287, 2048)
[...]

ValueError: Input 0 is incompatible with layer model: expected shape=(None, 18287, 2048), found shape=(None, 2048)

I already tried to solve this by following the hints given here and here but without any success. Any help would be highly appreciated.

Upvotes: 0

Views: 54

Answers (1)

Hagbard
Hagbard

Reputation: 3680

I erroneously omitted the following part of the tutorial:

x_train = x_train.reshape((x_train.shape[0], x_train.shape[1], 1))
x_test = x_test.reshape((x_test.shape[0], x_test.shape[1], 1))

Thus, the input didn't have the correct size. Adding the lines mentioned above solved the issue.

Upvotes: 1

Related Questions