MCPcopy
hub / github.com/dmlc/dgl / compute_acc

Function compute_acc

examples/distributed/graphsage/node_classification_unsupervised.py:154–183  ·  view source on GitHub ↗

Compute the accuracy of prediction given the labels. We will fist train a LogisticRegression model using the trained embeddings, the training set, validation set and test set is provided as the arguments. The final result is predicted by the lr model. emb: The pretrained embe

(emb, labels, train_nids, val_nids, test_nids)

Source from the content-addressed store, hash-verified

152
153
154def compute_acc(emb, labels, train_nids, val_nids, test_nids):
155 """
156 Compute the accuracy of prediction given the labels.
157
158 We will fist train a LogisticRegression model using the trained embeddings,
159 the training set, validation set and test set is provided as the arguments.
160
161 The final result is predicted by the lr model.
162
163 emb: The pretrained embeddings
164 labels: The ground truth
165 train_nids: The training set node ids
166 val_nids: The validation set node ids
167 test_nids: The test set node ids
168 """
169
170 emb = emb[np.arange(labels.shape[0])].cpu().numpy()
171 train_nids = train_nids.cpu().numpy()
172 val_nids = val_nids.cpu().numpy()
173 test_nids = test_nids.cpu().numpy()
174 labels = labels.cpu().numpy()
175
176 emb = (emb - emb.mean(0, keepdims=True)) / emb.std(0, keepdims=True)
177 lr = lm.LogisticRegression(multi_class="multinomial", max_iter=10000)
178 lr.fit(emb[train_nids], labels[train_nids])
179
180 pred = lr.predict(emb)
181 eval_acc = skm.accuracy_score(labels[val_nids], pred[val_nids])
182 test_acc = skm.accuracy_score(labels[test_nids], pred[test_nids])
183 return eval_acc, test_acc
184
185
186def run(args, device, data):

Callers 1

runFunction · 0.70

Calls 4

stdMethod · 0.80
cpuMethod · 0.45
fitMethod · 0.45
predictMethod · 0.45

Tested by

no test coverage detected