【问题标题】:How to make prediction on new text dataset using saved text classification model如何使用保存的文本分类模型对新的文本数据集进行预测
【发布时间】:2021-01-25 19:12:00
【问题描述】:

我根据本指南训练了一个文本分类器:https://developers.google.com/machine-learning/guides/text-classification/step-4

并将模型另存为

model.save('~./output/model.h5')

在这种情况下,我如何使用这个模型对另一个新数据集上的文本进行分类?

谢谢

【问题讨论】:

    标签: python keras nlp text-classification


    【解决方案1】:
    import tensorflow as tf
    
    # Recreate the exact same model, including its weights and the optimizer
    new_model = tf.keras.models.load_model('~./output/model.h5')
    
    # Show the model architecture
    new_model.summary()
    
    # Apply the same process of data preparation while training the model.
    # Lets say after Data preprocessing you have stored the processed data in test_data
    
    # check model accuracy from unseen/new dataset
    loss, acc = new_model.evaluate(test_data,  test_labels, verbose=2)
    print('Restored model, accuracy: {:5.2f}%'.format(100*acc))
    

    【讨论】:

    • 当我想使用保存的模型时,我对如何处理新测试数据的文本矢量化和嵌入部分有点困惑?
    • 通过与训练模型之前相同的过程重新迭代测试数据 - 就这么简单。
    • 它有错误:输入源操作连接到节点sequential_1/embedding_1/embedding_lookup:sequential_1/embedding_1/embedding_lookup/633(定义在/Library/Frameworks/Python.framework/Versions/3.7/lib/python3 .7/contextlib.py:112) 函数调用栈:predict_function
    • 分享正确的代码和细节以理解错误。
    • 我发了一个新帖子:stackoverflow.com/questions/64351963/…
    【解决方案2】:

    可以使用tensorflow的Text分词工具类(Tokenizer)来处理Test数据中的未知词。

    1. Num_words 是词汇量(它选择最常用的词)

    2. Assign oov_token = 'Some string',用于词汇量之外的所有标记/单词(基本上测试数据中的新单词将被视为 oov_token 字符串。

    3. 拟合训练数据,然后为训练和测试数据生成标记序列。

      tf.keras.preprocessing.text.Tokenizer( num_words=None, filters='!"#$%&()*+,-./:;?@[\]^_`{|}~\t\n', lower=True, split=' ', char_level=False, oov_token=None, document_count=0, **kwargs )

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-26
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      相关资源
      最近更新 更多