MCPcopy Create free account
hub / github.com/649453932/Bert-Chinese-Text-Classification-Pytorch / evaluate

Function evaluate

train_eval.py:101–121  ·  view source on GitHub ↗
(config, model, data_iter, test=False)

Source from the content-addressed store, hash-verified

99
100
101def evaluate(config, model, data_iter, test=False):
102 model.eval()
103 loss_total = 0
104 predict_all = np.array([], dtype=int)
105 labels_all = np.array([], dtype=int)
106 with torch.no_grad():
107 for texts, labels in data_iter:
108 outputs = model(texts)
109 loss = F.cross_entropy(outputs, labels)
110 loss_total += loss
111 labels = labels.data.cpu().numpy()
112 predic = torch.max(outputs.data, 1)[1].cpu().numpy()
113 labels_all = np.append(labels_all, labels)
114 predict_all = np.append(predict_all, predic)
115
116 acc = metrics.accuracy_score(labels_all, predict_all)
117 if test:
118 report = metrics.classification_report(labels_all, predict_all, target_names=config.class_list, digits=4)
119 confusion = metrics.confusion_matrix(labels_all, predict_all)
120 return acc, loss_total / len(data_iter), report, confusion
121 return acc, loss_total / len(data_iter)

Callers 2

trainFunction · 0.85
testFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected