def classification_report(data, model, session, sample=False): _, _, _, a, x, _, f, _, _ = data.next_validation_batch() n, e = session.run([model.nodes_gumbel_argmax, model.edges_gumbel_argmax] if sample else [ model.nodes_argmax, model.edges_argmax], feed_dict={model.edges_labels: a, model.nodes_labels: x, model.node_features: f, model.training: False, model.variational: False}) n, e = np.argmax(n, axis=-1), np.argmax(e, axis=-1) y_true = e.flatten() y_pred = a.flatten() target_names = [str(Chem.rdchem.BondType.values[int(e)]) for e in data.bond_decoder_m.values()] print('######## Classification Report ########\n') print(sk_classification_report(y_true, y_pred, labels=list(range(len(target_names))), target_names=target_names)) print('######## Confusion Matrix ########\n') print(confusion_matrix(y_true, y_pred, labels=list(range(len(target_names))))) y_true = n.flatten() y_pred = x.flatten() target_names = [Chem.Atom(e).GetSymbol() for e in data.atom_decoder_m.values()] print('######## Classification Report ########\n') print(sk_classification_report(y_true, y_pred, labels=list(range(len(target_names))), target_names=target_names)) print('\n######## Confusion Matrix ########\n') print(confusion_matrix(y_true, y_pred, labels=list(range(len(target_names)))))
def classification_report(y_true: List, y_pred: List) -> None: """ Calculates and logs the classification report """ report = sk_classification_report(y_true, y_pred, digits=4) logger.info("Classification Report") logger.info(report)
def classification_report(y_true, y_pred, target_names, normalize=False): if any((val is None for val in (y_true, y_pred))): raise ValueError("y_true and y_pred are needed to plot confusion " "matrix") # calculate how many names you expect values = set(y_true).union(set(y_pred)) expected_len = len(values) if target_names is not None: len_target = len(target_names) if target_names is not None and (expected_len != len_target): raise ValueError( ('Data cointains {} different values, but target' ' names contains {} values.'.format(expected_len, len(target_names)))) # if the user didn't pass target_names, create generic ones if target_names is None: values = list(values) values.sort() target_names = values cr = sk_classification_report(y_true, y_pred, target_names=target_names, output_dict=True) return cr
def sequence_compute_metrics(pairs, label_list): """Sequence metrics based on sklearn""" raw_predictions, labels = pairs predictions = np.argmax(raw_predictions, axis=1) metrics = { "accuracy": sk_accuracy_score(labels, predictions), "precision_micro": sk_precision_score(labels, predictions, average="micro"), "recall_micro": sk_recall_score(labels, predictions, average="micro"), "f1_micro": sk_f1_score(labels, predictions, average="micro"), "precision_macro": sk_precision_score(labels, predictions, average="macro"), "recall_macro": sk_recall_score(labels, predictions, average="macro"), "f1_macro": sk_f1_score(labels, predictions, average="macro"), # "report": sk_classification_report(labels, predictions, digits=4) } reports = sk_classification_report( labels, predictions, target_names=label_list, output_dict=True, ) for label, report in reports.items(): if not isinstance(report, dict): report = {"": report} for metric_key, metric_value in report.items(): metric_title = metric_key.replace(" avg", "_avg", 1) metrics.update({ f"label_{label}_{metric_title}": metric_value, }) wandb.log({ "roc": wandb.plot.roc_curve(labels, raw_predictions, labels=label_list), "matrix": wandb.sklearn.plot_confusion_matrix(labels, predictions, label_list) }) return metrics
def compute_token_metrics(agg_chunk_labels, agg_chunk_preds): report = sk_classification_report(sum(agg_chunk_labels, []), sum(agg_chunk_preds, []), target_names=label_names) results = sk_classification_metrics(sum(agg_chunk_labels, []), sum(agg_chunk_preds, [])) return results, report