我想使用Tensorflow Dataset api通过tensorflow Hub初始化我的数据集。我想使用dataset.map函数将我的文本数据转换为嵌入数据。我的Tensorflow版本是1.14。
由于我使用了elmo v2模块,它将一串句子数组转换为它们的单词嵌入,所以我使用了以下代码:
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."怎样才能得到预期的结果?
发布于 2019-07-05 19:59:14
遗憾的是,这在TensorFlow 1.x中不受支持
但是,TensorFlow 2.0支持它,所以如果您可以升级到tensorflow 2,并从tf 2 (current list here)的可用文本嵌入模块中进行选择,那么您就可以在您的dataset管道中使用它。如下所示:
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 (我不认为在新的格式中是可用的),那么我能看到的在预处理阶段嵌入的唯一选择是首先通过一个简单的嵌入模型运行你的数据集,然后保存结果,然后将嵌入的向量单独用于下游任务。(我知道这不太理想)。
https://stackoverflow.com/questions/56898069
复制相似问题