需要对 keras 中的一种热门或散列技巧有基本的了解。
from keras.preprocessing.text import hashing_trick
from keras.preprocessing.text import text_to_word_sequence
# define the document
text = 'The quick brown fox jumped over the lazy dog dog.'
# estimate the size of the vocabulary
words = set(text_to_word_sequence(text))
print(words)
vocab_size = len(words)
print(vocab_size)
# integer encode the document
result = hashing_trick(text, round(vocab_size*1.3), hash_function='md5')
print(text)
print(result)
输出:
{'over', 'the', 'lazy', 'dog', 'quick', 'brown', 'jumped', 'fox'} 8 敏捷的棕色狐狸跳过了懒狗狗。[6、4、1、2、7、5、6、2、6、6]
结论:这里为每个标记分配一个整数。例如。“快被分配4”--6
快--4棕--1狐--2跳--7超--5--6懒--2狗--6
我想了解如何为“the”和“dog”分配相同的整数 6。如果我错了,请纠正我,并请提供解释它是如何做到的?