使用 ONNX 转换模型

Using ONNX converted model

伙计们,我正在做一个情感分析项目,我将一个 BERT 模型转换为一个 ONNX 模型,因为当我想给它一个巨大的数据来预测时,原始模型有一个巨大的运行时间。但是现在我不知道如何使用这个ONNX模型。我将以正常方式运行模型时粘贴我的原始代码。

顺便说一句,如果有人有任何建议可以优化代码或模型而无需使用 ONNX 或 Openvino,我将不胜感激。

Link到模特抱脸网站

cosining = spatial.distance.cosine
X_pos_test = model.encode(pos_test)
X_neg_test = model.encode(neg_test)
sentence_tokenize = hazm.sent_tokenize # It's a tokenizer to finding Farsi sentences in texts

def predicting(string, api_value, model):

cs = {'Sentence': [], 'Negative Score': [], 'Positive Score': [] }

 for i in range(len(api_value)):
    
    api_value[i] = cleaning_text(api_value[i]) # Normalizing texts
    sentence_tokenized = np.array(sentence_tokenize(api_value[i]))
    
    for sentence in range(len(sentence_tokenized)):
        
        encoded_sentence_tokenized = model.encode(sentence_tokenized[sentence])
        
        neg_result = 1 - cosining(X_neg_test, encoded_sentence_tokenized) # for negative
        pos_result = 1 - cosining(X_pos_test, encoded_sentence_tokenized) # for positive
        
        cs['Sentence'].append(sentence_tokenized[sentence])
        cs['Negative Score'].append(neg_result)
        cs['Positive Score'].append(pos_result)

cs_finall = pd.DataFrame(cs)
cs_finall.to_excel(string + " score.xlsx", index=False)
return cs_finall

我在 this article 中找到了答案,它来自 huggingface 网站。我希望它可以帮助别人