MCPcopy Create free account
hub / github.com/NJUNLP/GTS / eval

Function eval

code/NNModel/main.py:81–113  ·  view source on GitHub ↗
(model, dataset, args)

Source from the content-addressed store, hash-verified

79
80
81def eval(model, dataset, args):
82 model.eval()
83 with torch.no_grad():
84 predictions=[]
85 labels=[]
86 all_ids = []
87 all_lengths = []
88 for i in range(dataset.batch_count):
89 sentence_ids, sentence_tokens, lengths, mask, aspect_tags, _, tags = dataset.get_batch(i)
90 prediction = model.forward(sentence_tokens,lengths, mask)
91 prediction = prediction[-1]
92 prediction = torch.argmax(prediction, dim=3)
93 prediction_padded = torch.zeros(prediction.shape[0], args.max_sequence_len, args.max_sequence_len)
94 prediction_padded[:, :prediction.shape[1], :prediction.shape[1]] = prediction
95 predictions.append(prediction_padded)
96
97 all_ids.extend(sentence_ids)
98 labels.append(tags)
99 all_lengths.append(lengths)
100
101 predictions = torch.cat(predictions,dim=0).cpu().tolist()
102 labels = torch.cat(labels,dim=0).cpu().tolist()
103 all_lengths = torch.cat(all_lengths, dim=0).cpu().tolist()
104 precision, recall, f1 = utils.score_uniontags(args, predictions, labels, all_lengths, ignore_index=-1)
105
106 aspect_results = utils.score_aspect(predictions, labels, all_lengths, ignore_index=-1)
107 opinion_results = utils.score_opinion(predictions, labels, all_lengths, ignore_index=-1)
108 print('Aspect term\tP:{:.5f}\tR:{:.5f}\tF1:{:.5f}'.format(aspect_results[0], aspect_results[1], aspect_results[2]))
109 print('Opinion term\tP:{:.5f}\tR:{:.5f}\tF1:{:.5f}'.format(opinion_results[0], opinion_results[1], opinion_results[2]))
110 print(args.task+'\tP:{:.5f}\tR:{:.5f}\tF1:{:.5f}\n'.format(precision, recall, f1))
111
112 model.train()
113 return precision, recall, f1
114
115
116def test(args):

Callers 2

trainFunction · 0.70
testFunction · 0.70

Calls 5

score_uniontagsMethod · 0.80
score_aspectMethod · 0.80
score_opinionMethod · 0.80
get_batchMethod · 0.45
forwardMethod · 0.45

Tested by

no test coverage detected