use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.
the class TrainingTester method train.
private List<StepRecord> train(@Nonnull NotebookOutput log, @Nonnull BiFunction<NotebookOutput, Trainable, List<StepRecord>> opt, @Nonnull Layer layer, @Nonnull Tensor[][] data, @Nonnull boolean... mask) {
try {
int inputs = data[0].length;
@Nonnull final PipelineNetwork network = new PipelineNetwork(inputs);
network.wrap(new MeanSqLossLayer(), network.add(layer, IntStream.range(0, inputs - 1).mapToObj(i -> network.getInput(i)).toArray(i -> new DAGNode[i])), network.getInput(inputs - 1));
@Nonnull ArrayTrainable trainable = new ArrayTrainable(data, network);
if (0 < mask.length)
trainable.setMask(mask);
List<StepRecord> history;
try {
history = opt.apply(log, trainable);
if (history.stream().mapToDouble(x -> x.fitness).min().orElse(1) > 1e-5) {
if (!network.isFrozen()) {
log.p("This training apply resulted in the following configuration:");
log.code(() -> {
return network.state().stream().map(Arrays::toString).reduce((a, b) -> a + "\n" + b).orElse("");
});
}
if (0 < mask.length) {
log.p("And regressed input:");
log.code(() -> {
return Arrays.stream(data).flatMap(x -> Arrays.stream(x)).limit(1).map(x -> x.prettyPrint()).reduce((a, b) -> a + "\n" + b).orElse("");
});
}
log.p("To produce the following output:");
log.code(() -> {
Result[] array = ConstantResult.batchResultArray(pop(data));
@Nullable Result eval = layer.eval(array);
for (@Nonnull Result result : array) {
result.freeRef();
result.getData().freeRef();
}
TensorList tensorList = eval.getData();
eval.freeRef();
String str = tensorList.stream().limit(1).map(x -> {
String s = x.prettyPrint();
x.freeRef();
return s;
}).reduce((a, b) -> a + "\n" + b).orElse("");
tensorList.freeRef();
return str;
});
} else {
log.p("Training Converged");
}
} finally {
trainable.freeRef();
network.freeRef();
}
return history;
} finally {
layer.freeRef();
for (@Nonnull Tensor[] tensors : data) {
for (@Nonnull Tensor tensor : tensors) {
tensor.freeRef();
}
}
}
}
use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.
the class CudaLayerTester method testNonstandardBounds.
/**
* Test nonstandard bounds tolerance statistics.
*
* @param log the log
* @param reference the reference
* @param inputPrototype the input prototype
* @return the tolerance statistics
*/
@Nonnull
public ToleranceStatistics testNonstandardBounds(final NotebookOutput log, @Nullable final Layer reference, @Nonnull final Tensor[] inputPrototype) {
log.h2("Irregular Input");
log.p("This layer should be able to accept non-dense inputs.");
return log.code(() -> {
Tensor[] randomized = Arrays.stream(inputPrototype).map(x -> x.map(v -> getRandom())).toArray(i -> new Tensor[i]);
logger.info("Input: " + Arrays.stream(randomized).map(Tensor::prettyPrint).collect(Collectors.toList()));
Precision precision = Precision.Double;
TensorList[] controlInput = CudaSystem.run(gpu -> {
return Arrays.stream(randomized).map(original -> {
TensorArray data = TensorArray.create(original);
CudaTensorList wrap = CudaTensorList.wrap(gpu.getTensor(data, precision, MemoryType.Managed, false), 1, original.getDimensions(), precision);
data.freeRef();
return wrap;
}).toArray(i -> new TensorList[i]);
}, 0);
@Nonnull final SimpleResult controlResult = CudaSystem.run(gpu -> {
return SimpleGpuEval.run(reference, gpu, controlInput);
}, 1);
final TensorList[] irregularInput = CudaSystem.run(gpu -> {
return Arrays.stream(randomized).map(original -> {
return buildIrregularCudaTensor(gpu, precision, original);
}).toArray(i -> new TensorList[i]);
}, 0);
@Nonnull final SimpleResult testResult = CudaSystem.run(gpu -> {
return SimpleGpuEval.run(reference, gpu, irregularInput);
}, 1);
try {
ToleranceStatistics compareOutput = compareOutput(controlResult, testResult);
ToleranceStatistics compareDerivatives = compareDerivatives(controlResult, testResult);
return compareDerivatives.combine(compareOutput);
} finally {
Arrays.stream(randomized).forEach(ReferenceCountingBase::freeRef);
Arrays.stream(controlInput).forEach(ReferenceCounting::freeRef);
Arrays.stream(irregularInput).forEach(x -> x.freeRef());
controlResult.freeRef();
testResult.freeRef();
}
});
}
use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.
the class NotebookReportBase method run.
/**
* Run.
*
* @param fn the fn
* @param logPath the log path
*/
public void run(@Nonnull Consumer<NotebookOutput> fn, @Nonnull CharSequence... logPath) {
try (@Nonnull NotebookOutput log = getLog(logPath.length == 0 ? new String[] { getClass().getSimpleName() } : logPath)) {
printHeader(log);
@Nonnull TimedResult<Void> time = TimedResult.time(() -> {
try {
fn.accept(log);
log.setFrontMatterProperty("result", "OK");
} catch (Throwable e) {
log.setFrontMatterProperty("result", getExceptionString(e).toString().replaceAll("\n", "<br/>").trim());
throw (RuntimeException) (e instanceof RuntimeException ? e : new RuntimeException(e));
}
});
log.setFrontMatterProperty("execution_time", String.format("%.6f", time.timeNanos / 1e9));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.
the class TestUtil method printHistory.
/**
* Print history.
*
* @param log the log
* @param history the history
*/
public static void printHistory(@Nonnull final NotebookOutput log, @Nonnull final List<StepRecord> history) {
if (!history.isEmpty()) {
log.out("Convergence Plot: ");
log.code(() -> {
final DoubleSummaryStatistics valueStats = history.stream().mapToDouble(x -> x.fitness).filter(x -> x > 0).summaryStatistics();
@Nonnull final PlotCanvas plot = ScatterPlot.plot(history.stream().map(step -> new double[] { step.iteration, Math.log10(Math.max(valueStats.getMin(), step.fitness)) }).toArray(i -> new double[i][]));
plot.setTitle("Convergence Plot");
plot.setAxisLabels("Iteration", "log10(Fitness)");
plot.setSize(600, 400);
return plot;
});
}
}
use of com.simiacryptus.util.io.NotebookOutput in project MindsEye by SimiaCryptus.
the class ReferenceIO method test.
@Nullable
@Override
public ToleranceStatistics test(@Nonnull final NotebookOutput log, @Nonnull final Layer layer, @Nonnull final Tensor... inputPrototype) {
if (!referenceIO.isEmpty()) {
log.h1("Reference Input/Output Pairs");
log.p("Display pre-setBytes input/output example pairs:");
referenceIO.forEach((input, output) -> {
log.code(() -> {
@Nonnull final SimpleEval eval = SimpleEval.run(layer, input);
Tensor add = output.scale(-1).addAndFree(eval.getOutput());
@Nonnull final DoubleStatistics error = new DoubleStatistics().accept(add.getData());
add.freeRef();
String format = String.format("--------------------\nInput: \n[%s]\n--------------------\nOutput: \n%s\nError: %s\n--------------------\nDerivative: \n%s", Arrays.stream(input).map(t -> t.prettyPrint()).reduce((a, b) -> a + ",\n" + b).get(), eval.getOutput().prettyPrint(), error, Arrays.stream(eval.getDerivative()).map(t -> t.prettyPrint()).reduce((a, b) -> a + ",\n" + b).get());
eval.freeRef();
return format;
});
});
} else {
log.h1("Example Input/Output Pair");
log.p("Display input/output pairs from random executions:");
log.code(() -> {
@Nonnull final SimpleEval eval = SimpleEval.run(layer, inputPrototype);
String format = String.format("--------------------\nInput: \n[%s]\n--------------------\nOutput: \n%s\n--------------------\nDerivative: \n%s", Arrays.stream(inputPrototype).map(t -> t.prettyPrint()).reduce((a, b) -> a + ",\n" + b).orElse(""), eval.getOutput().prettyPrint(), Arrays.stream(eval.getDerivative()).map(t -> t.prettyPrint()).reduce((a, b) -> a + ",\n" + b).orElse(""));
eval.freeRef();
return format;
});
}
return null;
}
Aggregations