【发布时间】: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)
【问题讨论】:
-
你的物体是什么形状的?
-
我在代码中添加了一些打印语句来说明形状。请参考输出。
标签: python tensorflow keras transformer