【问题标题】:how to add SOS token to Keras tokenizer?如何将 SOS 令牌添加到 Keras 标记器?
【发布时间】:2022-01-17 15:32:47
【问题描述】:

我有一个 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')

【问题讨论】:

    标签: python tensorflow keras nlp tokenize


    【解决方案1】:

    根据您的用例(例如,解码器模型),您可以将 &lt;sos&gt;&lt;eos&gt; 添加到每个句子中,然后像这样对它们进行标记:

    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 的过滤器中删除了 &lt;&gt;,以便您可以在句子中使用这些字符。另外,请检查此tutorial

    【讨论】:

    • 谢谢你的回答@AloneTogether
    猜你喜欢
    • 2020-04-19
    • 2021-03-15
    • 2021-07-22
    • 2019-05-13
    • 2013-07-02
    • 2021-01-17
    • 2021-04-13
    • 2018-06-15
    • 1970-01-01
    相关资源
    最近更新 更多