我想从一系列标记中生成n-grams:
bigram:: "1 3 4 5" --> { (1,3), (3,4), (4,5) }
经过搜索,我找到了这个讨论串,它使用了以下代码:
def find_ngrams(input_list, n): return zip(*[input_list[i:] for i in range(n)])
如果我在训练过程中使用这段代码,我认为它会严重影响性能。所以我在寻找更好的选项。
回答:
如果你需要以字符串格式生成bigram:
import tensorflow as tftf.enable_eager_execution()sentence = ['this is example sentence']x = tf.string_split(sentence).values[:-1] + ' ' + tf.string_split(sentence).values[1:]# tf.Tensor([b'this is' b'is example' b'example sentence'], shape=(3,), dtype=string)
你也可以使用tensorflow-transform
来生成ngrams。
import tensorflow_transform as tfttft.ngrams(tensor, (1,2), " ")
注意:截至2019年1月22日,tensorflow-transform仅支持Python 2。