2

我是一名学生,我正在尝试在 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.

更新代码

import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.layers import Input, Bidirectional, Lambda, Dropout, Dense, LSTM
from tensorflow.keras.models import Model
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,))
emb_layer = Lambda(ELMoEmbedding, output_shape=(max_len,1024))(inpt)  
bdlstm1 = Bidirectional(LSTM(1024))
drp = Dropout(0.5)(bdlstm1)
dns1 = Dense(2, activation='softmax')(drp)
model = Model(inpt, dns1)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x, y, batch_size = 100, epochs=10)

新错误

TypeError: Failed to convert object of type <class 'tensorflow.python.keras.layers.wrappers.Bidirectional'> to Tensor. Contents: <tensorflow.python.keras.layers.wrappers.Bidirectional object at 0x7fc724d042d0>. Consider casting elements to a supported type.
4

0 回答 0