use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.
the class ImageDecompositionLab method run.
/**
* Run.
*
* @param log the log
*/
public void run(@Nonnull final NotebookOutput log) {
final int pretrainMinutes = 30;
final int timeoutMinutes = 30;
final int images = 10;
final int size = 400;
@Nonnull String source = "H:\\SimiaCryptus\\photos";
displayImage = images;
final Tensor[][] trainingImages = null == source ? EncodingUtil.getImages(log, size, images, "kangaroo") : Arrays.stream(new File(source).listFiles()).map(input -> {
try {
return ImageIO.read(input);
} catch (IOException e) {
throw new RuntimeException(e);
}
}).map(img -> new Tensor[] { new Tensor(1.0), Tensor.fromRGB(TestUtil.resize(img, size)) }).toArray(i -> new Tensor[i][]);
Arrays.stream(trainingImages).map(x -> x[1]).map(x -> x.toImage()).map(x -> {
return log.image(x, "example");
}).forEach(str -> log.p(str));
log.h1("First LayerBase");
@Nonnull final InitializationStep step0 = log.code(() -> {
return new InitializationStep(log, trainingImages, size, pretrainMinutes, timeoutMinutes, 3, 9, 5);
}).invoke();
log.h1("Second LayerBase");
@Nonnull final AddLayerStep step1 = log.code(() -> {
return new AddLayerStep(log, step0.trainingData, step0.model, 2, step0.toSize, pretrainMinutes * 2, timeoutMinutes, step0.band1, 18, 3, 4);
}).invoke();
log.h1("Third LayerBase");
@Nonnull final AddLayerStep step2 = log.code(() -> {
return new AddLayerStep(log, step1.trainingData, step1.integrationModel, 3, step1.toSize, pretrainMinutes * 3, timeoutMinutes, step1.band2, 48, 3, 1);
}).invoke();
log.h1("Fourth LayerBase");
@Nonnull final AddLayerStep step3 = log.code(() -> {
return new AddLayerStep(log, step2.trainingData, step2.integrationModel, 4, step2.toSize, pretrainMinutes * 4, timeoutMinutes, step2.band2, 48, 5, 4);
}).invoke();
log.h1("Transcoding Different Category");
log.code(() -> {
return new TranscodeStep(log, "yin_yang", images, size, timeoutMinutes * 5, step3.integrationModel, step3.toSize, step3.toSize, step3.band2);
}).invoke();
}
use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.
the class TestUtil method printDataStatistics.
/**
* Print data statistics.
*
* @param log the log
* @param data the data
*/
public static void printDataStatistics(@Nonnull final NotebookOutput log, @Nonnull final Tensor[][] data) {
for (int col = 1; col < data[0].length; col++) {
final int c = col;
log.out("Learned Representation Statistics for Column " + col + " (all bands)");
log.code(() -> {
@Nonnull final ScalarStatistics scalarStatistics = new ScalarStatistics();
Arrays.stream(data).flatMapToDouble(row -> Arrays.stream(row[c].getData())).forEach(v -> scalarStatistics.add(v));
return scalarStatistics.getMetrics();
});
final int _col = col;
log.out("Learned Representation Statistics for Column " + col + " (by band)");
log.code(() -> {
@Nonnull final int[] dimensions = data[0][_col].getDimensions();
return IntStream.range(0, dimensions[2]).mapToObj(x -> x).flatMap(b -> {
return Arrays.stream(data).map(r -> r[_col]).map(tensor -> {
@Nonnull final ScalarStatistics scalarStatistics = new ScalarStatistics();
scalarStatistics.add(new Tensor(dimensions[0], dimensions[1]).setByCoord(coord -> tensor.get(coord.getCoords()[0], coord.getCoords()[1], b)).getData());
return scalarStatistics;
});
}).map(x -> x.getMetrics().toString()).reduce((a, b) -> a + "\n" + b).get();
});
}
}
use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.
the class TestUtil method extractPerformance.
/**
* Remove performance wrappers.
*
* @param log the log
* @param network the network
*/
public static void extractPerformance(@Nonnull final NotebookOutput log, @Nonnull final DAGNetwork network) {
log.p("Per-layer Performance Metrics:");
log.code(() -> {
@Nonnull final Map<CharSequence, MonitoringWrapperLayer> metrics = new HashMap<>();
network.visitNodes(node -> {
if (node.getLayer() instanceof MonitoringWrapperLayer) {
@Nullable final MonitoringWrapperLayer layer = node.getLayer();
Layer inner = layer.getInner();
String str = inner.toString();
str += " class=" + inner.getClass().getName();
// if(inner instanceof MultiPrecision<?>) {
// str += "; precision=" + ((MultiPrecision) inner).getPrecision().name();
// }
metrics.put(str, layer);
}
});
TestUtil.log.info("Performance: \n\t" + metrics.entrySet().stream().sorted(Comparator.comparing(x -> -x.getValue().getForwardPerformance().getMean())).map(e -> {
@Nonnull final PercentileStatistics performanceF = e.getValue().getForwardPerformance();
@Nonnull final PercentileStatistics performanceB = e.getValue().getBackwardPerformance();
return String.format("%.6fs +- %.6fs (%d) <- %s", performanceF.getMean(), performanceF.getStdDev(), performanceF.getCount(), e.getKey()) + (performanceB.getCount() == 0 ? "" : String.format("%n\tBack: %.6fs +- %.6fs (%s)", performanceB.getMean(), performanceB.getStdDev(), performanceB.getCount()));
}).reduce((a, b) -> a + "\n\t" + b).get());
});
removeInstrumentation(network);
}
use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.
the class AutoencodingProblem method run.
@Nonnull
@Override
public AutoencodingProblem run(@Nonnull final NotebookOutput log) {
@Nonnull final DAGNetwork fwdNetwork = fwdFactory.imageToVector(log, features);
@Nonnull final DAGNetwork revNetwork = revFactory.vectorToImage(log, features);
@Nonnull final PipelineNetwork echoNetwork = new PipelineNetwork(1);
echoNetwork.add(fwdNetwork);
echoNetwork.add(revNetwork);
@Nonnull final PipelineNetwork supervisedNetwork = new PipelineNetwork(1);
supervisedNetwork.add(fwdNetwork);
@Nonnull final DropoutNoiseLayer dropoutNoiseLayer = new DropoutNoiseLayer().setValue(dropout);
supervisedNetwork.add(dropoutNoiseLayer);
supervisedNetwork.add(revNetwork);
supervisedNetwork.add(new MeanSqLossLayer(), supervisedNetwork.getHead(), supervisedNetwork.getInput(0));
log.h3("Network Diagrams");
log.code(() -> {
return Graphviz.fromGraph(TestUtil.toGraph(fwdNetwork)).height(400).width(600).render(Format.PNG).toImage();
});
log.code(() -> {
return Graphviz.fromGraph(TestUtil.toGraph(revNetwork)).height(400).width(600).render(Format.PNG).toImage();
});
log.code(() -> {
return Graphviz.fromGraph(TestUtil.toGraph(supervisedNetwork)).height(400).width(600).render(Format.PNG).toImage();
});
@Nonnull final TrainingMonitor monitor = new TrainingMonitor() {
@Nonnull
TrainingMonitor inner = TestUtil.getMonitor(history);
@Override
public void log(final String msg) {
inner.log(msg);
}
@Override
public void onStepComplete(final Step currentPoint) {
dropoutNoiseLayer.shuffle(StochasticComponent.random.get().nextLong());
inner.onStepComplete(currentPoint);
}
};
final Tensor[][] trainingData = getTrainingData(log);
// MonitoredObject monitoringRoot = new MonitoredObject();
// TestUtil.addMonitoring(supervisedNetwork, monitoringRoot);
log.h3("Training");
TestUtil.instrumentPerformance(supervisedNetwork);
@Nonnull final ValidatingTrainer trainer = optimizer.train(log, new SampledArrayTrainable(trainingData, supervisedNetwork, trainingData.length / 2, batchSize), new ArrayTrainable(trainingData, supervisedNetwork, batchSize), monitor);
log.code(() -> {
trainer.setTimeout(timeoutMinutes, TimeUnit.MINUTES).setMaxIterations(10000).run();
});
if (!history.isEmpty()) {
log.code(() -> {
return TestUtil.plot(history);
});
log.code(() -> {
return TestUtil.plotTime(history);
});
}
TestUtil.extractPerformance(log, supervisedNetwork);
{
@Nonnull final String modelName = "encoder_model" + AutoencodingProblem.modelNo++ + ".json";
log.p("Saved model as " + log.file(fwdNetwork.getJson().toString(), modelName, modelName));
}
@Nonnull final String modelName = "decoder_model" + AutoencodingProblem.modelNo++ + ".json";
log.p("Saved model as " + log.file(revNetwork.getJson().toString(), modelName, modelName));
// log.h3("Metrics");
// log.code(() -> {
// return TestUtil.toFormattedJson(monitoringRoot.getMetrics());
// });
log.h3("Validation");
log.p("Here are some re-encoded examples:");
log.code(() -> {
@Nonnull final TableOutput table = new TableOutput();
data.validationData().map(labeledObject -> {
return toRow(log, labeledObject, echoNetwork.eval(labeledObject.data).getData().get(0).getData());
}).filter(x -> null != x).limit(10).forEach(table::putRow);
return table;
});
log.p("Some rendered unit vectors:");
for (int featureNumber = 0; featureNumber < features; featureNumber++) {
@Nonnull final Tensor input = new Tensor(features).set(featureNumber, 1);
@Nullable final Tensor tensor = revNetwork.eval(input).getData().get(0);
log.out(log.image(tensor.toImage(), ""));
}
return this;
}
use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.
the class EncodingProblem method run.
@Nonnull
@Override
public EncodingProblem run(@Nonnull final NotebookOutput log) {
@Nonnull final TrainingMonitor monitor = TestUtil.getMonitor(history);
Tensor[][] trainingData;
try {
trainingData = data.trainingData().map(labeledObject -> {
return new Tensor[] { new Tensor(features).set(this::random), labeledObject.data };
}).toArray(i -> new Tensor[i][]);
} catch (@Nonnull final IOException e) {
throw new RuntimeException(e);
}
@Nonnull final DAGNetwork imageNetwork = revFactory.vectorToImage(log, features);
log.h3("Network Diagram");
log.code(() -> {
return Graphviz.fromGraph(TestUtil.toGraph(imageNetwork)).height(400).width(600).render(Format.PNG).toImage();
});
@Nonnull final PipelineNetwork trainingNetwork = new PipelineNetwork(2);
@Nullable final DAGNode image = trainingNetwork.add(imageNetwork, trainingNetwork.getInput(0));
@Nullable final DAGNode softmax = trainingNetwork.add(new SoftmaxActivationLayer(), trainingNetwork.getInput(0));
trainingNetwork.add(new SumInputsLayer(), trainingNetwork.add(new EntropyLossLayer(), softmax, softmax), trainingNetwork.add(new NthPowerActivationLayer().setPower(1.0 / 2.0), trainingNetwork.add(new MeanSqLossLayer(), image, trainingNetwork.getInput(1))));
log.h3("Training");
log.p("We start by training apply a very small population to improve initial convergence performance:");
TestUtil.instrumentPerformance(trainingNetwork);
@Nonnull final Tensor[][] primingData = Arrays.copyOfRange(trainingData, 0, 1000);
@Nonnull final ValidatingTrainer preTrainer = optimizer.train(log, (SampledTrainable) new SampledArrayTrainable(primingData, trainingNetwork, trainingSize, batchSize).setMinSamples(trainingSize).setMask(true, false), new ArrayTrainable(primingData, trainingNetwork, batchSize), monitor);
log.code(() -> {
preTrainer.setTimeout(timeoutMinutes / 2, TimeUnit.MINUTES).setMaxIterations(batchSize).run();
});
TestUtil.extractPerformance(log, trainingNetwork);
log.p("Then our main training phase:");
TestUtil.instrumentPerformance(trainingNetwork);
@Nonnull final ValidatingTrainer mainTrainer = optimizer.train(log, (SampledTrainable) new SampledArrayTrainable(trainingData, trainingNetwork, trainingSize, batchSize).setMinSamples(trainingSize).setMask(true, false), new ArrayTrainable(trainingData, trainingNetwork, batchSize), monitor);
log.code(() -> {
mainTrainer.setTimeout(timeoutMinutes, TimeUnit.MINUTES).setMaxIterations(batchSize).run();
});
TestUtil.extractPerformance(log, trainingNetwork);
if (!history.isEmpty()) {
log.code(() -> {
return TestUtil.plot(history);
});
log.code(() -> {
return TestUtil.plotTime(history);
});
}
try {
@Nonnull String filename = log.getName().toString() + EncodingProblem.modelNo++ + "_plot.png";
ImageIO.write(Util.toImage(TestUtil.plot(history)), "png", log.file(filename));
log.appendFrontMatterProperty("result_plot", filename, ";");
} catch (IOException e) {
throw new RuntimeException(e);
}
// log.file()
@Nonnull final String modelName = "encoding_model_" + EncodingProblem.modelNo++ + ".json";
log.appendFrontMatterProperty("result_model", modelName, ";");
log.p("Saved model as " + log.file(trainingNetwork.getJson().toString(), modelName, modelName));
log.h3("Results");
@Nonnull final PipelineNetwork testNetwork = new PipelineNetwork(2);
testNetwork.add(imageNetwork, testNetwork.getInput(0));
log.code(() -> {
@Nonnull final TableOutput table = new TableOutput();
Arrays.stream(trainingData).map(tensorArray -> {
@Nullable final Tensor predictionSignal = testNetwork.eval(tensorArray).getData().get(0);
@Nonnull final LinkedHashMap<CharSequence, Object> row = new LinkedHashMap<>();
row.put("Source", log.image(tensorArray[1].toImage(), ""));
row.put("Echo", log.image(predictionSignal.toImage(), ""));
return row;
}).filter(x -> null != x).limit(10).forEach(table::putRow);
return table;
});
log.p("Learned Model Statistics:");
log.code(() -> {
@Nonnull final ScalarStatistics scalarStatistics = new ScalarStatistics();
trainingNetwork.state().stream().flatMapToDouble(x -> Arrays.stream(x)).forEach(v -> scalarStatistics.add(v));
return scalarStatistics.getMetrics();
});
log.p("Learned Representation Statistics:");
log.code(() -> {
@Nonnull final ScalarStatistics scalarStatistics = new ScalarStatistics();
Arrays.stream(trainingData).flatMapToDouble(row -> Arrays.stream(row[0].getData())).forEach(v -> scalarStatistics.add(v));
return scalarStatistics.getMetrics();
});
log.p("Some rendered unit vectors:");
for (int featureNumber = 0; featureNumber < features; featureNumber++) {
@Nonnull final Tensor input = new Tensor(features).set(featureNumber, 1);
@Nullable final Tensor tensor = imageNetwork.eval(input).getData().get(0);
TestUtil.renderToImages(tensor, true).forEach(img -> {
log.out(log.image(img, ""));
});
}
return this;
}
Aggregations