【问题标题】:Should <EOS> and <BOS> tags be explictly added to vocabulary after using keras.preprocessing.text Tokenizer?使用 keras.preprocessing.text Tokenizer 后是否应该将 <EOS> 和 <BOS> 标签显式添加到词汇表中?
【发布时间】:2020-09-13 04:00:41
【问题描述】:

在 Keras 中,我们有 keras.preprocessing.text 来标记我们要求的文本并生成词汇表。

tokenizer = tf.keras.preprocessing.text.Tokenizer(split=' ',  oov_token=1)
tokenizer.fit_on_texts(["Hello world"])
seqs = tokenizer.texts_to_sequences(["Hello world"])

如果我们在填充后将生成的 seqs 馈送到像 RNN 这样的神经网络,我不确定是否要显式添加序列结束 (EOS) 标记和序列开始 (BOS) 标记seq 为固定长度。或者,Keras 是为我们做的吗? (我没有看到任何使用 Keras 分词器时明确添加 EOS 和 BOS 的示例)

【问题讨论】:

    标签: python tensorflow keras recurrent-neural-network vocabulary


    【解决方案1】:

    不,不需要为tf.keras.preprocessing.text.Tokenizer添加&lt;EOS&gt;&lt;BOS&gt;
    由于index_word 映射以oov_token 开头的顺序工作,下一个优先级是频率最高的单词,然后是与输入顺序相同的单词。 这有助于 Keras API 在内部处理映射,这与其他使用 &lt;START&gt;&lt;END&gt; 标签的文本预处理 API 不同。

    下面是带有示例句子的示例,以显示index_word 映射。

    text_data = ["this is the sample sentence",
                "one more sentence"]
    
    lang_tokenizer = tf.keras.preprocessing.text.Tokenizer(oov_token="<UNK>")
    lang_tokenizer.fit_on_texts(text_data)
    lang_tokenizer.index_word
    

    index_word:

    {1: '<UNK>',
     2: 'sentence',
     3: 'this',
     4: 'is',
     5: 'the',
     6: 'sample',
     7: 'one',
     8: 'more'}
    

    测试:

    res = lang_tokenizer.texts_to_sequences(["testing with sample sentence"]) 
    

    [[1, 1, 6, 2]]

    希望这能回答您的问题,祝您学习愉快!

    【讨论】:

      猜你喜欢
      • 2018-06-04
      • 2023-01-26
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 2017-01-31
      • 1970-01-01
      相关资源
      最近更新 更多