tokenizer.texts_to_sequences Keras Tokenizer 给出几乎所有的零
tokenizer.texts_to_sequences Keras Tokenizer gives almost all zeros
我正在努力创建一个文本分类代码,但我在使用分词器对文档进行编码时遇到了问题。
1) 我首先在我的文档中安装了一个分词器,如下所示:
vocabulary_size = 20000
tokenizer = Tokenizer(num_words= vocabulary_size, filters='')
tokenizer.fit_on_texts(df['data'])
2) 然后我想检查我的数据是否正确拟合所以我转换成序列如下:
sequences = tokenizer.texts_to_sequences(df['data'])
data = pad_sequences(sequences, maxlen= num_words)
print(data)
这给了我很好的输出。即把单词编码成数字
[[ 9628 1743 29 ... 161 52 250]
[14948 1 70 ... 31 108 78]
[ 2207 1071 155 ... 37607 37608 215]
...
[ 145 74 947 ... 1 76 21]
[ 95 11045 1244 ... 693 693 144]
[ 11 133 61 ... 87 57 24]]
现在,我想使用相同的方法将文本转换为序列。
像这样:
sequences = tokenizer.texts_to_sequences("physics is nice ")
text = pad_sequences(sequences, maxlen=num_words)
print(text)
它给了我奇怪的输出:
[[ 0 0 0 0 0 0 0 0 0 394]
[ 0 0 0 0 0 0 0 0 0 3136]
[ 0 0 0 0 0 0 0 0 0 1383]
[ 0 0 0 0 0 0 0 0 0 507]
[ 0 0 0 0 0 0 0 0 0 1]
[ 0 0 0 0 0 0 0 0 0 1261]
[ 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 1114]
[ 0 0 0 0 0 0 0 0 0 1]
[ 0 0 0 0 0 0 0 0 0 1261]
[ 0 0 0 0 0 0 0 0 0 753]]
根据 Keras 文档 (Keras):
texts_to_sequences(texts)
Arguments: texts: list of texts to turn to sequences.
Return: list of
sequences (one per text input).
不是要把每个单词都编码成对应的数字吗?如果文本短于 50 到 50,则填充文本?
错误在哪里?
错误是你填充序列的地方。 maxlen 的值应该是您想要的最大标记,例如50. 因此,将行更改为:
maxlen = 50
data = pad_sequences(sequences, maxlen=maxlen)
sequences = tokenizer.texts_to_sequences("physics is nice ")
text = pad_sequences(sequences, maxlen=maxlen)
这会将序列削减为 50 个标记,并用零填充较短的标记。注意 padding
选项。默认值为 pre
,这意味着如果句子短于 maxlen
,则填充序列将从零开始填充它。如果你想要序列末尾的零添加到 pad_sequences
选项 padding='post'
.
我想你应该这样称呼:
sequences = tokenizer.texts_to_sequences(["physics is nice "])
你应该尝试这样调用:
sequences = tokenizer.texts_to_sequences(["physics is nice"])
当你使用时,Pads 序列长度相同,即在你的情况下为 num_words=vocabulary_size,这就是你得到输出的原因,
试试 : tokenizer.texts_to_sequences ,这会给你一个单词序列。
阅读有关填充的更多信息,它仅用于匹配数据的每一行,小岛最多使用 2 个句子。
第 1 句和第 2 句,
sentanec1 的长度为 5,而句子 2 的长度为 8。
现在,当我们发送数据进行训练时,如果我们不使用 3 填充 sentence1,那么我们将无法执行批量 Wiese 训练。
希望对你有帮助
你应该这样调用方法:
new_sample = ['A new sample to be classified']
seq = tokenizer.texts_to_sequences(new_sample )
padded = pad_sequences(seq, maxlen=MAX_SEQUENCE_LENGTH)
pred = model.predict(padded)
您可以像下面这样传递以获得输出。
twt = ['He is a lazy person.']
twt = tokenizer.texts_to_sequences(twt)
print (twt)
或
twt = tokenizer.texts_to_sequences(['He is a lazy person.'])
print (twt)
我正在努力创建一个文本分类代码,但我在使用分词器对文档进行编码时遇到了问题。
1) 我首先在我的文档中安装了一个分词器,如下所示:
vocabulary_size = 20000
tokenizer = Tokenizer(num_words= vocabulary_size, filters='')
tokenizer.fit_on_texts(df['data'])
2) 然后我想检查我的数据是否正确拟合所以我转换成序列如下:
sequences = tokenizer.texts_to_sequences(df['data'])
data = pad_sequences(sequences, maxlen= num_words)
print(data)
这给了我很好的输出。即把单词编码成数字
[[ 9628 1743 29 ... 161 52 250]
[14948 1 70 ... 31 108 78]
[ 2207 1071 155 ... 37607 37608 215]
...
[ 145 74 947 ... 1 76 21]
[ 95 11045 1244 ... 693 693 144]
[ 11 133 61 ... 87 57 24]]
现在,我想使用相同的方法将文本转换为序列。 像这样:
sequences = tokenizer.texts_to_sequences("physics is nice ")
text = pad_sequences(sequences, maxlen=num_words)
print(text)
它给了我奇怪的输出:
[[ 0 0 0 0 0 0 0 0 0 394]
[ 0 0 0 0 0 0 0 0 0 3136]
[ 0 0 0 0 0 0 0 0 0 1383]
[ 0 0 0 0 0 0 0 0 0 507]
[ 0 0 0 0 0 0 0 0 0 1]
[ 0 0 0 0 0 0 0 0 0 1261]
[ 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 1114]
[ 0 0 0 0 0 0 0 0 0 1]
[ 0 0 0 0 0 0 0 0 0 1261]
[ 0 0 0 0 0 0 0 0 0 753]]
根据 Keras 文档 (Keras):
texts_to_sequences(texts)
Arguments: texts: list of texts to turn to sequences.
Return: list of sequences (one per text input).
不是要把每个单词都编码成对应的数字吗?如果文本短于 50 到 50,则填充文本? 错误在哪里?
错误是你填充序列的地方。 maxlen 的值应该是您想要的最大标记,例如50. 因此,将行更改为:
maxlen = 50
data = pad_sequences(sequences, maxlen=maxlen)
sequences = tokenizer.texts_to_sequences("physics is nice ")
text = pad_sequences(sequences, maxlen=maxlen)
这会将序列削减为 50 个标记,并用零填充较短的标记。注意 padding
选项。默认值为 pre
,这意味着如果句子短于 maxlen
,则填充序列将从零开始填充它。如果你想要序列末尾的零添加到 pad_sequences
选项 padding='post'
.
我想你应该这样称呼:
sequences = tokenizer.texts_to_sequences(["physics is nice "])
你应该尝试这样调用:
sequences = tokenizer.texts_to_sequences(["physics is nice"])
当你使用时,Pads 序列长度相同,即在你的情况下为 num_words=vocabulary_size,这就是你得到输出的原因, 试试 : tokenizer.texts_to_sequences ,这会给你一个单词序列。 阅读有关填充的更多信息,它仅用于匹配数据的每一行,小岛最多使用 2 个句子。 第 1 句和第 2 句, sentanec1 的长度为 5,而句子 2 的长度为 8。 现在,当我们发送数据进行训练时,如果我们不使用 3 填充 sentence1,那么我们将无法执行批量 Wiese 训练。 希望对你有帮助
你应该这样调用方法:
new_sample = ['A new sample to be classified']
seq = tokenizer.texts_to_sequences(new_sample )
padded = pad_sequences(seq, maxlen=MAX_SEQUENCE_LENGTH)
pred = model.predict(padded)
您可以像下面这样传递以获得输出。
twt = ['He is a lazy person.']
twt = tokenizer.texts_to_sequences(twt)
print (twt)
或
twt = tokenizer.texts_to_sequences(['He is a lazy person.'])
print (twt)