前沿追踪|基于TorchText的PyTorch文本分类( 三 )
下一步 , 我们将在测试数据集上测试我们的模型 , 并检查模型的准确性 。
print('Checking the results of test dataset...')test_loss, test_acc = test(test_dataset)print(f'\tLoss: {test_loss:.4f}(test)\t|\tAcc: {test_acc * 100:.1f}%(test)')
现在 , 我们将在单个新闻文本字符串上测试我们的模型 , 并预测给定新闻文本的类标签 。
DBpedia_label = {0: 'Company',1: 'EducationalInstitution',2: 'Artist',3: 'Athlete',4: 'OfficeHolder',5: 'MeanOfTransportation',6: 'Building',7: 'NaturalPlace',8: 'Village',9: 'Animal',10: 'Plant',11: 'Album',12: 'Film',13: 'WrittenWork'}def predict(text, model, vocab, ngrams):tokenizer = get_tokenizer("basic_english")with torch.no_grad():text = torch.tensor([vocab[token]for token in ngrams_iterator(tokenizer(text), ngrams)])output = model(text, torch.tensor([0]))return output.argmax(1).item() + 1vocab = train_dataset.get_vocab()model = model.to("cpu")
现在 , 我们将从测试数据中随机抽取一些文本并检查预测的类标签 。
第一个预测:
ex_text_str = "Brekke Church (Norwegian: Brekke kyrkje) is a parish church in Gulen Municipality in Sogn og Fjordane county, Norway. It is located in the village of Brekke. The church is part of the Brekke parish in the Nordhordland deanery in the Diocese of Bj??rgvin. The white, wooden church, which has 390 seats, was consecrated on 19 November 1862 by the local Dean Thomas Erichsen. The architect Christian Henrik Grosch made the designs for the church, which is the third church on the site."print("This is a %s news" %DBpedia_label[predict(ex_text_str, model, vocab, 2)])
第二个预测:
ex_text_str2 = "Cerithiella superba is a species of very small sea snail, a marine gastropod mollusk in the family Newtoniellidae. This species is known from European waters. It was described by Thiele, 1912."print("This text belongs to %s class" %DBpedia_label[predict(ex_text_str2, model, vocab, 2)])
第三个预测:
ex_text_str3 = "Nithari is a village in the western part of the state of Uttar Pradesh India bordering on New Delhi. Nithari forms part of the New Okhla Industrial Development Authority's planned industrial city Noida falling in Sector 31. Nithari made international news headlines in December 2006 when the skeletons of a number of apparently murdered women and children were unearthed in the village."print("This text belongs to %s class" %DBpedia_label[predict(ex_text_str3, model, vocab, 2)])
推荐阅读
- 兵器前沿观察|不知醒悟只知变本加厉,欧盟咋视而不见?,乌克兰一次又一次被坑
- 央视新闻客户端|新加坡要求部分入境者佩戴电子追踪器
- 前沿时刻|同时中国的好消息令人鼓舞,特朗普败局已定?24小时接3条坏消息
- 军事的第一线|基于77GHz毫米波FMCW雷达的2D-SAR成像(Matlab算法与实测数据)
- 一点前沿关科技|Y,据报道,特斯拉即将生产更便宜的Model
- 大国前沿|印度拉拢美日澳联合出兵,彻底封锁马六甲海峡?,无视多次警告
- 大国前沿|俄称这次动作超乎想象,布局初见成效!大量新型坦克紧急交付巴军
- 大国前沿|用错手段,农民苦不堪言,化肥价格连续暴涨!印度学白宫制裁对手
- 环球时报|中国驻法使馆:不接受基于谎言的所谓“国际独立调查”
- 大国前沿|美媒替白宫说出了真相,美军最大威胁是什么?并不是俄罗斯和伊朗