Tensorflow或Keras中文本的单热编码字符是否可能?
tf.one_hot
似乎只采用整数。tf.keras.preprocessing.text.one_hot
似乎是一个热门的编码句子
单词,但不是字符。除此之外,tf.keras.preprocessing.text.one_hot
的工作非常奇怪,因为响应并不是真正的单热编码,因为以下代码:
text = "ab bba bbd"
res = tf.keras.preprocessing.text.one_hot(text=text,n=3)
print(res)
导致这个结果:
[1,2,2]
每次运行此程序时,输出都是不同的3d矢量,有时它是[1,1,1]
或[2,1,1]
。文件说,不能保证单一性,但这对我来说似乎毫无意义。
答案 0 :(得分:3)
我找到了一个基于纯python的很好的答案,遗憾的是我不再找到源代码了。它首先将每个char转换为int,然后使用one-hot数组替换int。如果字母表的长度和顺序相同,即使在所有程序中,整个程序也是如此。
# Is the alphabet of all possible chars you want to convert
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
def convert_to_onehot(data):
#Creates a dict, that maps to every char of alphabet an unique int based on position
char_to_int = dict((c,i) for i,c in enumerate(alphabet))
encoded_data = []
#Replaces every char in data with the mapped int
encoded_data.append([char_to_int[char] for char in data])
print(encoded_data) # Prints the int encoded array
#This part now replaces the int by an one-hot array with size alphabet
one_hot = []
for value in encoded_data:
#At first, the whole array is initialized with 0
letter = [0 for _ in range(len(alphabet))]
#Only at the number of the int, 1 is written
letter[value] = 1
one_hot.append(letter)
return one_hot
print(convert_to_onehot("hello world"))
答案 1 :(得分:2)
您可以使用keras to_categorical
import tensorflow as tf
# define the document
text = 'The quick brown fox jumped over the lazy dog.'
# estimate the size of the vocabulary
words = set(tf.keras.preprocessing.text.text_to_word_sequence(text))
vocab_size = len(words)
print(vocab_size)
# integer encode the document
result = tf.keras.utils.to_categorical(tf.keras.preprocessing
.text.one_hot(text, round(vocab_size*1.3)))
print(result)
结果
[[1, 2, 3, 4, 5, 6, 1, 7, 8]]