其他分享
首页 > 其他分享> > task04:卷积情感分析

task04:卷积情感分析

作者:互联网

task04:卷积情感分析

一、数据预处理:

二、构建模型:

三、训练模型:

四、验证模型:

import spacy
nlp = spacy.load('en_core_web_sm')

def predict_sentiment(model, sentence, min_len = 5):
    model.eval()
    tokenized = [tok.text for tok in nlp.tokenizer(sentence)]
    if len(tokenized) < min_len:
        tokenized += ['<pad>'] * (min_len - len(tokenized))
    indexed = [TEXT.vocab.stoi[t] for t in tokenized]
    tensor = torch.LongTensor(indexed).to(device)
    tensor = tensor.unsqueeze(0)
    prediction = torch.sigmoid(model(tensor))
    return prediction.item()

参考资料:

DataWhale开源资料

标签:dim,卷积,self,batch,len,filter,情感,task04,size
来源: https://blog.csdn.net/maozixiang/article/details/120462087