use of edu.neu.ccs.pyramid.eval.MAP in project pyramid by cheng-li.
the class CBMGB method reportGeneral.
private static void reportGeneral(Config config, CBM cbm, MultiLabelClfDataSet dataSet, String name) throws Exception {
System.out.println("============================================================");
System.out.println("computing other predictor-independent metrics");
System.out.println("label averaged MAP");
System.out.println(MAP.map(cbm, dataSet));
System.out.println("instance averaged MAP");
System.out.println(MAP.instanceMAP(cbm, dataSet));
System.out.println("global AP truncated at 30");
System.out.println(AveragePrecision.globalAveragePrecisionTruncated(cbm, dataSet, 30));
String output = config.getString("output.dir");
File labelProbFile = Paths.get(output, name + "_predictions", "label_probabilities.txt").toFile();
double labelProbThreshold = config.getDouble("report.labelProbThreshold");
try (BufferedWriter br = new BufferedWriter(new FileWriter(labelProbFile))) {
for (int i = 0; i < dataSet.getNumDataPoints(); i++) {
br.write(CBMInspector.topLabels(cbm, dataSet.getRow(i), labelProbThreshold));
br.newLine();
}
}
System.out.println("individual label probabilities are saved to " + labelProbFile.getAbsolutePath());
List<Integer> unobservedLabels = Arrays.stream(FileUtils.readFileToString(new File(output, "unobserved_labels.txt")).split(",")).map(s -> s.trim()).filter(s -> !s.isEmpty()).map(s -> Integer.parseInt(s)).collect(Collectors.toList());
// Here we do not use approximation
double[] logLikelihoods = IntStream.range(0, dataSet.getNumDataPoints()).parallel().mapToDouble(i -> cbm.predictLogAssignmentProb(dataSet.getRow(i), dataSet.getMultiLabels()[i])).toArray();
double average = IntStream.range(0, dataSet.getNumDataPoints()).filter(i -> !containsNovelClass(dataSet.getMultiLabels()[i], unobservedLabels)).mapToDouble(i -> logLikelihoods[i]).average().getAsDouble();
File logLikelihoodFile = Paths.get(output, name + "_predictions", "ground_truth_log_likelihood.txt").toFile();
FileUtils.writeStringToFile(logLikelihoodFile, PrintUtil.toMutipleLines(logLikelihoods));
System.out.println("individual log likelihood of the " + name + " ground truth label set is written to " + logLikelihoodFile.getAbsolutePath());
System.out.println("average log likelihood of the " + name + " ground truth label sets = " + average);
if (!unobservedLabels.isEmpty() && name.equals("test")) {
System.out.println("This is computed by ignoring test instances with new labels unobserved during training");
System.out.println("The following labels do not actually appear in the training set and therefore cannot be learned:");
System.out.println(ListUtil.toSimpleString(unobservedLabels));
}
}
use of edu.neu.ccs.pyramid.eval.MAP in project pyramid by cheng-li.
the class CBMLR method reportGeneral.
private static void reportGeneral(Config config, CBM cbm, MultiLabelClfDataSet dataSet, String name) throws Exception {
System.out.println("============================================================");
System.out.println("computing other predictor-independent metrics");
System.out.println("label averaged MAP");
System.out.println(MAP.map(cbm, dataSet));
System.out.println("instance averaged MAP");
System.out.println(MAP.instanceMAP(cbm, dataSet));
System.out.println("global AP truncated at 30");
System.out.println(AveragePrecision.globalAveragePrecisionTruncated(cbm, dataSet, 30));
String output = config.getString("output.dir");
File labelProbFile = Paths.get(output, name + "_predictions", "label_probabilities.txt").toFile();
double labelProbThreshold = config.getDouble("report.labelProbThreshold");
try (BufferedWriter br = new BufferedWriter(new FileWriter(labelProbFile))) {
for (int i = 0; i < dataSet.getNumDataPoints(); i++) {
br.write(CBMInspector.topLabels(cbm, dataSet.getRow(i), labelProbThreshold));
br.newLine();
}
}
System.out.println("individual label probabilities are saved to " + labelProbFile.getAbsolutePath());
List<Integer> unobservedLabels = Arrays.stream(FileUtils.readFileToString(new File(output, "unobserved_labels.txt")).split(",")).map(s -> s.trim()).filter(s -> !s.isEmpty()).map(s -> Integer.parseInt(s)).collect(Collectors.toList());
// Here we do not use approximation
double[] logLikelihoods = IntStream.range(0, dataSet.getNumDataPoints()).parallel().mapToDouble(i -> cbm.predictLogAssignmentProb(dataSet.getRow(i), dataSet.getMultiLabels()[i])).toArray();
double average = IntStream.range(0, dataSet.getNumDataPoints()).filter(i -> !containsNovelClass(dataSet.getMultiLabels()[i], unobservedLabels)).mapToDouble(i -> logLikelihoods[i]).average().getAsDouble();
File logLikelihoodFile = Paths.get(output, name + "_predictions", "ground_truth_log_likelihood.txt").toFile();
FileUtils.writeStringToFile(logLikelihoodFile, PrintUtil.toMutipleLines(logLikelihoods));
System.out.println("individual log likelihood of the " + name + " ground truth label set is written to " + logLikelihoodFile.getAbsolutePath());
System.out.println("average log likelihood of the " + name + " ground truth label sets = " + average);
if (!unobservedLabels.isEmpty() && name.equals("test")) {
System.out.println("This is computed by ignoring test instances with new labels unobserved during training");
System.out.println("The following labels do not actually appear in the training set and therefore cannot be learned:");
System.out.println(ListUtil.toSimpleString(unobservedLabels));
}
}
use of edu.neu.ccs.pyramid.eval.MAP in project pyramid by cheng-li.
the class CBMEN method reportGeneral.
private static void reportGeneral(Config config, CBM cbm, MultiLabelClfDataSet dataSet, String name) throws Exception {
System.out.println("============================================================");
System.out.println("computing other predictor-independent metrics");
System.out.println("label averaged MAP");
System.out.println(MAP.map(cbm, dataSet));
// todo
// System.out.println("instance averaged MAP");
// System.out.println(MAP.instanceMAP(cbm, dataSet));
// System.out.println("global AP truncated at 30");
// System.out.println(AveragePrecision.globalAveragePrecisionTruncated(cbm, dataSet, 30));
String output = config.getString("output.dir");
File labelProbFile = Paths.get(output, name + "_predictions", "label_probabilities.txt").toFile();
double labelProbThreshold = config.getDouble("report.labelProbThreshold");
try (BufferedWriter br = new BufferedWriter(new FileWriter(labelProbFile))) {
for (int i = 0; i < dataSet.getNumDataPoints(); i++) {
br.write(CBMInspector.topLabels(cbm, dataSet.getRow(i), labelProbThreshold));
br.newLine();
}
}
System.out.println("individual label probabilities are saved to " + labelProbFile.getAbsolutePath());
List<Integer> unobservedLabels = Arrays.stream(FileUtils.readFileToString(new File(output, "unobserved_labels.txt")).split(",")).map(s -> s.trim()).filter(s -> !s.isEmpty()).map(s -> Integer.parseInt(s)).collect(Collectors.toList());
// Here we do not use approximation
double[] logLikelihoods = IntStream.range(0, dataSet.getNumDataPoints()).parallel().mapToDouble(i -> cbm.predictLogAssignmentProb(dataSet.getRow(i), dataSet.getMultiLabels()[i])).toArray();
double average = IntStream.range(0, dataSet.getNumDataPoints()).filter(i -> !containsNovelClass(dataSet.getMultiLabels()[i], unobservedLabels)).mapToDouble(i -> logLikelihoods[i]).average().getAsDouble();
File logLikelihoodFile = Paths.get(output, name + "_predictions", "ground_truth_log_likelihood.txt").toFile();
FileUtils.writeStringToFile(logLikelihoodFile, PrintUtil.toMutipleLines(logLikelihoods));
System.out.println("individual log likelihood of the " + name + " ground truth label set is written to " + logLikelihoodFile.getAbsolutePath());
System.out.println("average log likelihood of the " + name + " ground truth label sets = " + average);
if (!unobservedLabels.isEmpty() && name.equals("test")) {
System.out.println("This is computed by ignoring test instances with new labels unobserved during training");
System.out.println("The following labels do not actually appear in the training set and therefore cannot be learned:");
System.out.println(ListUtil.toSimpleString(unobservedLabels));
}
}
Aggregations