我有一個 Keras 分詞器,我想在我的序列中添加一個句子開頭標記,但我找不到任何關于它的資訊來說明我該怎么做?
tokenizer = Tokenizer(split=' ')
tokenizer.fit_on_texts(data)
tokenizer.word_index['<pad>'] = 0
tokenizer.index_word[0] = '<pad>'
text_tokenized = tokenizer.texts_to_sequences(data)
text_corpus_padded = pad_sequences(text_tokenized, padding='post', maxlen=100, dtype='int32')
uj5u.com熱心網友回復:
根據您的用例(例如,解碼器模型),您可以將<sos>和添加<eos>到每個句子,然后像這樣標記它們:
import tensorflow as tf
data = ['Hello World', 'Hello New World']
data = ['<sos> ' x ' <eos>' for x in data]
tokenizer = tf.keras.preprocessing.text.Tokenizer(split=' ', filters='!"#$%&()* ,-./:;=?@[\\]^_`{|}~\t\n')
tokenizer.fit_on_texts(data)
tokenizer.word_index['<pad>'] = 0
tokenizer.index_word[0] = '<pad>'
text_tokenized = tokenizer.texts_to_sequences(data)
print(text_tokenized)
print(tokenizer.word_index)
[[1, 2, 3, 4], [1, 2, 5, 3, 4]]
{'<sos>': 1, 'hello': 2, 'world': 3, '<eos>': 4, 'new': 5, '<pad>': 0}
請注意,我已從 中的過濾器中洗掉<和>,Tokenizer以便您可以在句子中使用這些字符。另外,請查看本教程。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381029.html
上一篇:從.txt中洗掉引號
下一篇:loss<0是什么意思
