我想使用TensorFlow数据集API使用TensorFlow Hub初始化数据集。我想使用dataset.map函数将文本数据转换为嵌入。我的TensorFlow版本是1.14。
由于我使用Elmo V2 Modlule,将一束句子数组转换为其单词嵌入式,因此我使用以下代码:
import tensorflow as tf
import tensorflow_hub as hub
...
sentences_array = load_sentences()
#Sentence_array=["I love Python", "python is a good PL"]
def parse(sentences):
elmo = hub.Module("./ELMO")
embeddings = elmo([sentences], signature="default", as_dict=True)
["word_emb"]
return embeddings
dataset = tf.data.TextLineDataset(sentences_array)
dataset = dataset.apply(tf.data.experimental.map_and_batch(map_func =
parse, batch_size=batch_size))
我想要嵌入文本数组,例如[batch_size,max_words_in_batch,embedding_size],但是我收到了一个错误消息,如:
"NotImplementedError: Using TF-Hub module within a TensorFlow defined
function is currently not supported."
如何获得预期结果?
不幸的是,这在Tensorflow 1.x
中不支持。但是,在Tensorflow 2.0中支持它,因此,如果您可以升级到Tensorflow 2并从可用的TF 2(当前列表(中选择可在dataset
Pipeline中使用它。这样的东西:
embedder = hub.load("https://tfhub.dev/google/tf2-preview/nnlm-en-dim128/1")
def parse(sentences):
embeddings = embedder([sentences])
return embeddings
dataset = tf.data.TextLineDataset("text.txt")
dataset = dataset.map(parse)
如果您与1.X绑定或与Elmo绑定(我认为尚不认为以新格式可用(,那么我可以在预处理阶段嵌入的唯一选项是首先通过数据集运行您的数据集一个简单的嵌入模型并保存结果,然后将嵌入式向量分别用于下游任务。(我很欣赏这不是理想的(。