披露:我来这里是为了赏金,然后我尝试了 Colab,一切正常..
接下来我阅读了 cmets:“这个问题在当前状态下是个笑话。没有办法重现它。”在这一点上我同意。但由于我是 幸运的汉斯 并且显然有很多时间拖延,我按照 OP 提示启动了 Pycharm:“不,当我将它粘贴到我的 pycharm 时,我得到了上述错误"
但这也对我有用,这让我想知道你是否触摸过某些东西,所以我很高兴为你提供一个(n)(未触摸的)工作版本..
import numpy as np
def readucr(filename):
data = np.loadtxt(filename, delimiter="\t")
y = data[:, 0]
x = data[:, 1:]
return x, y.astype(int)
root_url = "https://raw.githubusercontent.com/hfawaz/cd-diagram/master/FordA/"
x_train, y_train = readucr(root_url + "FordA_TRAIN.tsv")
x_test, y_test = readucr(root_url + "FordA_TEST.tsv")
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))
n_classes = len(np.unique(y_train))
idx = np.random.permutation(len(x_train))
x_train = x_train[idx]
y_train = y_train[idx]
y_train[y_train == -1] = 0
y_test[y_test == -1] = 0
from tensorflow import keras
from tensorflow.keras import layers
def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0):
# Normalization and Attention
x = layers.LayerNormalization(epsilon=1e-6)(inputs)
x = layers.MultiHeadAttention(
key_dim=head_size, num_heads=num_heads, dropout=dropout
)(x, x)
x = layers.Dropout(dropout)(x)
res = x + inputs
# Feed Forward Part
x = layers.LayerNormalization(epsilon=1e-6)(res)
x = layers.Conv1D(filters=ff_dim, kernel_size=1, activation="relu")(x)
x = layers.Dropout(dropout)(x)
x = layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
return x + res
还要确保我们谈论的是相同的包版本
我使用了 numpy (1.21.2) 和 tensorflow (2.6.0) - 尝试使用这些版本,如果您使用不同的版本,请告诉我。