前沿追踪|基于TorchText的PyTorch文本分类( 三 )


前沿追踪|基于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)')
前沿追踪|基于TorchText的PyTorch文本分类现在 , 我们将在单个新闻文本字符串上测试我们的模型 , 并预测给定新闻文本的类标签 。
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)])
前沿追踪|基于TorchText的PyTorch文本分类第二个预测:
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)])
前沿追踪|基于TorchText的PyTorch文本分类第三个预测:
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)])


推荐阅读