gpt4 book ai didi

tensorflow - 使用Tensorflow和预训练的FastText获得看不见的单词的嵌入

转载 作者:行者123 更新时间:2023-12-04 12:51:37 27 4
gpt4 key购买 nike

我正在使用预训练的快速文本模型https://github.com/facebookresearch/fastText/blob/master/pretrained-vectors.md)。

我使用Gensim加载快速文本模型。它可以为任何单词输出一个矢量,无论它是可见的还是不可见的(词汇之外)。

from gensim.models.wrappers import FastText
en_model = FastText.load_fasttext_format('../wiki.en/wiki.en')
print(en_model['car'])
print(en_model['carcaryou'])

在tensorflow中,我知道我可以使用以下代码来获取可见单词的可训练嵌入:
# Embedding layer
embeddings = tf.get_variable('embedding_matrix', [vocab_size, state_size], Trainable=True)
rnn_inputs = tf.nn.embedding_lookup(embeddings, x)

已知单词的索引很容易获得。但是,对于那些看不见的单词,FastText根据子单词模式“预测”它们的潜在向量。看不见的单词没有任何索引。

在这种情况下,我该如何使用tensorflow使用fasttext来处理已知单词和看不见单词?

最佳答案

我发现了使用tf.py_func的解决方法:

def lookup(arr):
global model
global decode

decoded_arr = decode(arr)
new_arr = np.zeros((*arr.shape, 300))
for s, sent in enumerate(decoded_arr):
for w, word in enumerate(sent):
try:
new_arr[s, w] = model.wv[word]
except Exception as e:
print(e)
new_arr[s, w] = np.zeros(300)
return new_arr.astype(np.float32)

z = tf.py_func(lookup, [x], tf.float32, stateful=True, name=None)

这段代码有效,(使用法语,抱歉,没关系)
import tensorflow as tf
import numpy as np
from gensim.models.wrappers import FastText

model = FastText.load_fasttext_format("../../Tracfin/dev/han/data/embeddings/cc.fr.300.bin")
decode = np.vectorize(lambda x: x.decode("utf-8"))

def lookup(arr):
global model
global decode

decoded_arr = decode(arr)
new_arr = np.zeros((*arr.shape, 300))
for s, sent in enumerate(decoded_arr):
for w, word in enumerate(sent):
try:
new_arr[s, w] = model.wv[word]
except Exception as e:
print(e)
new_arr[s, w] = np.zeros(300)
return new_arr.astype(np.float32)

def extract_words(token):
# Split characters
out = tf.string_split([token], delimiter=" ")
# Convert to Dense tensor, filling with default value
out = tf.reshape(tf.sparse_tensor_to_dense(out, default_value="<pad>"), [-1])
return out


textfile = "text.txt"
words = [
"ceci est un texte hexabromocyclododécanes intéressant qui mentionne des",
"mots connus et des mots inconnus commeceluici ou celui-là polybromobiphényle",
]

with open(textfile, "w") as f:
f.write("\n".join(words))

tf.reset_default_graph()
padded_shapes = tf.TensorShape([None])
padding_values = "<pad>"

dataset = tf.data.TextLineDataset(textfile)
dataset = dataset.map(extract_words, 2)
dataset = dataset.shuffle(10000, reshuffle_each_iteration=True)
dataset = dataset.repeat()
dataset = dataset.padded_batch(3, padded_shapes, padding_values)
iterator = tf.data.Iterator.from_structure(
dataset.output_types, dataset.output_shapes
)
dataset_init_op = iterator.make_initializer(dataset, name="dataset_init_op")
x = iterator.get_next()
z = tf.py_func(lookup, [x], tf.float32, stateful=True, name=None)
sess = tf.InteractiveSession()
sess.run(dataset_init_op)
y, w = sess.run([x, z])
y = decode(y)

print(
"\nWords out of vocabulary: ",
np.sum(1 for word in y.reshape(-1) if word not in model.wv.vocab),
)
print("Lookup worked: ", all(model.wv[y[0][0][0]] == w[0][0][0]))

打印:
Words out of vocabulary:  6
Lookup worked: True

我没有尝试优化事物,尤其是查找循环,欢迎发表评论

关于tensorflow - 使用Tensorflow和预训练的FastText获得看不见的单词的嵌入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47022591/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com