Search in sources :

Example 31 with PipelineNetwork

use of com.simiacryptus.mindseye.network.PipelineNetwork 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();
            }
        }
    }
}
Also used : PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) HashMap(java.util.HashMap) Random(java.util.Random) Result(com.simiacryptus.mindseye.lang.Result) ArmijoWolfeSearch(com.simiacryptus.mindseye.opt.line.ArmijoWolfeSearch) ArrayList(java.util.ArrayList) Trainable(com.simiacryptus.mindseye.eval.Trainable) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) QuadraticSearch(com.simiacryptus.mindseye.opt.line.QuadraticSearch) LBFGS(com.simiacryptus.mindseye.opt.orient.LBFGS) RecursiveSubspace(com.simiacryptus.mindseye.opt.orient.RecursiveSubspace) StepRecord(com.simiacryptus.mindseye.test.StepRecord) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) MeanSqLossLayer(com.simiacryptus.mindseye.layers.java.MeanSqLossLayer) Logger(org.slf4j.Logger) PlotCanvas(smile.plot.PlotCanvas) QQN(com.simiacryptus.mindseye.opt.orient.QQN) GradientDescent(com.simiacryptus.mindseye.opt.orient.GradientDescent) BasicTrainable(com.simiacryptus.mindseye.eval.BasicTrainable) StaticLearningRate(com.simiacryptus.mindseye.opt.line.StaticLearningRate) TestUtil(com.simiacryptus.mindseye.test.TestUtil) DAGNode(com.simiacryptus.mindseye.network.DAGNode) DoubleStream(java.util.stream.DoubleStream) java.awt(java.awt) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) TensorList(com.simiacryptus.mindseye.lang.TensorList) Step(com.simiacryptus.mindseye.opt.Step) ProblemRun(com.simiacryptus.mindseye.test.ProblemRun) javax.swing(javax.swing) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) TensorList(com.simiacryptus.mindseye.lang.TensorList) MeanSqLossLayer(com.simiacryptus.mindseye.layers.java.MeanSqLossLayer) Result(com.simiacryptus.mindseye.lang.Result) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) StepRecord(com.simiacryptus.mindseye.test.StepRecord) Arrays(java.util.Arrays) Nullable(javax.annotation.Nullable)

Example 32 with PipelineNetwork

use of com.simiacryptus.mindseye.network.PipelineNetwork in project MindsEye by SimiaCryptus.

the class SquareActivationLayerTest method getReferenceLayer.

@Nullable
@Override
public Layer getReferenceLayer() {
    PipelineNetwork network = new PipelineNetwork();
    network.wrap(new LinearActivationLayer().setScale(alpha), network.wrap(new NthPowerActivationLayer().setPower(2), network.getInput(0)));
    return network;
// return new NthPowerActivationLayer().setPower(2);
}
Also used : NthPowerActivationLayer(com.simiacryptus.mindseye.layers.java.NthPowerActivationLayer) PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) LinearActivationLayer(com.simiacryptus.mindseye.layers.java.LinearActivationLayer) Nullable(javax.annotation.Nullable)

Example 33 with PipelineNetwork

use of com.simiacryptus.mindseye.network.PipelineNetwork in project MindsEye by SimiaCryptus.

the class EncodingUtil method findUnitComponent.

/**
 * Find unit component tensor.
 *
 * @param decoder the decoder
 * @param band    the band
 * @param tensor  the tensor
 * @return the tensor
 */
@Nullable
public static Tensor findUnitComponent(final PipelineNetwork decoder, final int band, @Nonnull final Tensor tensor) {
    @Nonnull final PipelineNetwork decoderBand = new PipelineNetwork();
    @Nonnull final double[] gate = new double[tensor.getDimensions()[2]];
    gate[band] = 1;
    decoderBand.add(new ImgBandScaleLayer(gate));
    decoderBand.add(decoder);
    try {
        return decoderBand.eval(tensor).getData().get(0);
    // return log.image(t.toImage(), "");
    } catch (@Nonnull final RuntimeException e) {
        throw e;
    } catch (@Nonnull final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Nonnull(javax.annotation.Nonnull) PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) ImgBandScaleLayer(com.simiacryptus.mindseye.layers.java.ImgBandScaleLayer) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Aggregations

PipelineNetwork (com.simiacryptus.mindseye.network.PipelineNetwork)33 Nonnull (javax.annotation.Nonnull)29 Tensor (com.simiacryptus.mindseye.lang.Tensor)16 DAGNode (com.simiacryptus.mindseye.network.DAGNode)13 Nullable (javax.annotation.Nullable)12 ArrayList (java.util.ArrayList)11 StepRecord (com.simiacryptus.mindseye.test.StepRecord)10 Layer (com.simiacryptus.mindseye.lang.Layer)9 Arrays (java.util.Arrays)9 List (java.util.List)9 ArrayTrainable (com.simiacryptus.mindseye.eval.ArrayTrainable)8 DAGNetwork (com.simiacryptus.mindseye.network.DAGNetwork)8 IntStream (java.util.stream.IntStream)8 MeanSqLossLayer (com.simiacryptus.mindseye.layers.java.MeanSqLossLayer)7 TestUtil (com.simiacryptus.mindseye.test.TestUtil)7 NotebookOutput (com.simiacryptus.util.io.NotebookOutput)7 Map (java.util.Map)7 Trainable (com.simiacryptus.mindseye.eval.Trainable)6 IterativeTrainer (com.simiacryptus.mindseye.opt.IterativeTrainer)6 BufferedImage (java.awt.image.BufferedImage)6