MCPcopy Create free account
hub / github.com/huggingface/transformers / align_predictions

Function align_predictions

examples/token-classification/run_ner.py:192–206  ·  view source on GitHub ↗
(predictions: np.ndarray, label_ids: np.ndarray)

Source from the content-addressed store, hash-verified

190 )
191
192 def align_predictions(predictions: np.ndarray, label_ids: np.ndarray) -> Tuple[List[int], List[int]]:
193 preds = np.argmax(predictions, axis=2)
194
195 batch_size, seq_len = preds.shape
196
197 out_label_list = [[] for _ in range(batch_size)]
198 preds_list = [[] for _ in range(batch_size)]
199
200 for i in range(batch_size):
201 for j in range(seq_len):
202 if label_ids[i, j] != nn.CrossEntropyLoss().ignore_index:
203 out_label_list[i].append(label_map[label_ids[i][j]])
204 preds_list[i].append(label_map[preds[i][j]])
205
206 return preds_list, out_label_list
207
208 def compute_metrics(p: EvalPrediction) -> Dict:
209 preds_list, out_label_list = align_predictions(p.predictions, p.label_ids)

Callers 2

compute_metricsFunction · 0.70
mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected