【问题标题】:ValueError: Input 0 is incompatible with layer model error for 1-D time series classification modelValueError:输入 0 与一维时间序列分类模型的层模型错误不兼容
【发布时间】:2021-10-08 18:33:44
【问题描述】:

我正在尝试使用 Keras 教程来遵循 time series classification with transformers。这是我的代码的相关部分:

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)

这是我得到的输出:

(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)

我已经尝试按照herehere 给出的提示来解决这个问题,但没有成功。任何帮助将不胜感激。

【问题讨论】:

  • 你的物体是什么形状的?
  • 我在代码中添加了一些打印语句来说明形状。请参考输出。

标签: python tensorflow keras transformer


【解决方案1】:

我错误地省略了教程的以下部分:

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))

因此,输入的大小不正确。添加上面提到的行解决了这个问题。

【讨论】:

    猜你喜欢
    • 2021-07-04
    • 2021-05-31
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    相关资源
    最近更新 更多