我们如何修复以下错误“InternalError:无法将元素作为字节获取”?

2024-06-02 07:28:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一名学生,我正在尝试在keras上使用elmo实现文本分类。我从tensorflow hub导入了elmo层。你知道吗

def ELMoEmbedding(x):
    return embed(inputs={ "tokens": tf.squeeze(tf.cast(x, tf.string)), "sequence_len": tf.constant(100*[max_len])}, signature="tokens", as_dict=True)["elmo"]

url = "https://tfhub.dev/google/elmo/2"
embed = hub.Module(url)

型号:


inpt = Input(shape=(max_len,), dtype = tf.string)
emb_layer = Lambda(ELMoEmbedding, output_shape=(max_len,1024))(inpt)  
bdlstm1 = Bidirectional(LSTM(1024))
drp = Dropout(0.5)(bdlstm1)
dns1 = Dense(2, activation='relu')(drp)
dns2 = Dense(no_labels, activation='softmax')(dns1)
model = Model(inpt, dns2)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x, y, batch_size = 100, epochs=10)

x和y是int32 numpy数组

x = [[0,0,1,2,3],[0,0,4,5,6]]
y = [[0,1],[1,0]]

在实现上述代码时,我遇到了以下错误

c_api.TF_GetCode(self.status.status))

InternalError: Unable to get element as bytes.

Tags: urlstringmodellentfasembedmax