Search in sources :

Example 1 with StochasticComponent

use of com.simiacryptus.mindseye.layers.java.StochasticComponent in project MindsEye by SimiaCryptus.

the class ValidatingTrainer method reset.

@Nonnull
private ValidatingTrainer reset(@Nonnull final TrainingPhase phase, final long seed) {
    if (!phase.trainingSubject.reseed(seed))
        throw new IterativeStopException();
    phase.orientation.reset();
    phase.trainingSubject.reseed(seed);
    if (phase.trainingSubject.getLayer() instanceof DAGNetwork) {
        ((DAGNetwork) phase.trainingSubject.getLayer()).visitLayers(layer -> {
            if (layer instanceof StochasticComponent)
                ((StochasticComponent) layer).shuffle(StochasticComponent.random.get().nextLong());
        });
    }
    return this;
}
Also used : StochasticComponent(com.simiacryptus.mindseye.layers.java.StochasticComponent) IterativeStopException(com.simiacryptus.mindseye.lang.IterativeStopException) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) Nonnull(javax.annotation.Nonnull)

Example 2 with StochasticComponent

use of com.simiacryptus.mindseye.layers.java.StochasticComponent in project MindsEye by SimiaCryptus.

the class ValidatingTrainer method run.

/**
 * Run double.
 *
 * @return the double
 */
public double run() {
    try {
        final long timeoutAt = System.currentTimeMillis() + timeout.toMillis();
        if (validationSubject.getLayer() instanceof DAGNetwork) {
            ((DAGNetwork) validationSubject.getLayer()).visitLayers(layer -> {
                if (layer instanceof StochasticComponent)
                    ((StochasticComponent) layer).clearNoise();
            });
        }
        @Nonnull final EpochParams epochParams = new EpochParams(timeoutAt, epochIterations, getTrainingSize(), validationSubject.measure(monitor));
        int epochNumber = 0;
        int iterationNumber = 0;
        int lastImprovement = 0;
        double lowestValidation = Double.POSITIVE_INFINITY;
        while (true) {
            if (shouldHalt(monitor, timeoutAt)) {
                monitor.log("Training halted");
                break;
            }
            monitor.log(String.format("Epoch parameters: %s, %s", epochParams.trainingSize, epochParams.iterations));
            @Nonnull final List<TrainingPhase> regimen = getRegimen();
            final long seed = System.nanoTime();
            final List<EpochResult> epochResults = IntStream.range(0, regimen.size()).mapToObj(i -> {
                final TrainingPhase phase = getRegimen().get(i);
                return runPhase(epochParams, phase, i, seed);
            }).collect(Collectors.toList());
            final EpochResult primaryPhase = epochResults.get(0);
            iterationNumber += primaryPhase.iterations;
            final double trainingDelta = primaryPhase.currentPoint.getMean() / primaryPhase.priorMean;
            if (validationSubject.getLayer() instanceof DAGNetwork) {
                ((DAGNetwork) validationSubject.getLayer()).visitLayers(layer -> {
                    if (layer instanceof StochasticComponent)
                        ((StochasticComponent) layer).clearNoise();
                });
            }
            final PointSample currentValidation = validationSubject.measure(monitor);
            final double overtraining = Math.log(trainingDelta) / Math.log(currentValidation.getMean() / epochParams.validation.getMean());
            final double validationDelta = currentValidation.getMean() / epochParams.validation.getMean();
            final double adj1 = Math.pow(Math.log(getTrainingTarget()) / Math.log(validationDelta), adjustmentFactor);
            final double adj2 = Math.pow(overtraining / getOvertrainingTarget(), adjustmentFactor);
            final double validationMean = currentValidation.getMean();
            if (validationMean < lowestValidation) {
                lowestValidation = validationMean;
                lastImprovement = iterationNumber;
            }
            monitor.log(String.format("Epoch %d result apply %s iterations, %s/%s samples: {validation *= 2^%.5f; training *= 2^%.3f; Overtraining = %.2f}, {itr*=%.2f, len*=%.2f} %s since improvement; %.4f validation time", ++epochNumber, primaryPhase.iterations, epochParams.trainingSize, getMaxTrainingSize(), Math.log(validationDelta) / Math.log(2), Math.log(trainingDelta) / Math.log(2), overtraining, adj1, adj2, iterationNumber - lastImprovement, validatingMeasurementTime.getAndSet(0) / 1e9));
            if (!primaryPhase.continueTraining) {
                monitor.log(String.format("Training %d runPhase halted", epochNumber));
                break;
            }
            if (epochParams.trainingSize >= getMaxTrainingSize()) {
                final double roll = FastRandom.INSTANCE.random();
                if (roll > Math.pow(2 - validationDelta, pessimism)) {
                    monitor.log(String.format("Training randomly converged: %3f", roll));
                    break;
                } else {
                    if (iterationNumber - lastImprovement > improvmentStaleThreshold) {
                        if (disappointments.incrementAndGet() > getDisappointmentThreshold()) {
                            monitor.log(String.format("Training converged after %s iterations", iterationNumber - lastImprovement));
                            break;
                        } else {
                            monitor.log(String.format("Training failed to converged on %s attempt after %s iterations", disappointments.get(), iterationNumber - lastImprovement));
                        }
                    } else {
                        disappointments.set(0);
                    }
                }
            }
            if (validationDelta < 1.0 && trainingDelta < 1.0) {
                if (adj1 < 1 - adjustmentTolerance || adj1 > 1 + adjustmentTolerance) {
                    epochParams.iterations = Math.max(getMinEpochIterations(), Math.min(getMaxEpochIterations(), (int) (primaryPhase.iterations * adj1)));
                }
                if (adj2 < 1 + adjustmentTolerance || adj2 > 1 - adjustmentTolerance) {
                    epochParams.trainingSize = Math.max(0, Math.min(Math.max(getMinTrainingSize(), Math.min(getMaxTrainingSize(), (int) (epochParams.trainingSize * adj2))), epochParams.trainingSize));
                }
            } else {
                epochParams.trainingSize = Math.max(0, Math.min(Math.max(getMinTrainingSize(), Math.min(getMaxTrainingSize(), epochParams.trainingSize * 5)), epochParams.trainingSize));
                epochParams.iterations = 1;
            }
            epochParams.validation = currentValidation;
        }
        if (validationSubject.getLayer() instanceof DAGNetwork) {
            ((DAGNetwork) validationSubject.getLayer()).visitLayers(layer -> {
                if (layer instanceof StochasticComponent)
                    ((StochasticComponent) layer).clearNoise();
            });
        }
        return epochParams.validation.getMean();
    } catch (@Nonnull final Throwable e) {
        throw new RuntimeException(e);
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) DoubleStatistics(com.simiacryptus.util.data.DoubleStatistics) TemporalUnit(java.time.temporal.TemporalUnit) LineSearchStrategy(com.simiacryptus.mindseye.opt.line.LineSearchStrategy) SampledTrainable(com.simiacryptus.mindseye.eval.SampledTrainable) TrainableBase(com.simiacryptus.mindseye.eval.TrainableBase) DoubleBuffer(com.simiacryptus.mindseye.lang.DoubleBuffer) HashMap(java.util.HashMap) SampledCachedTrainable(com.simiacryptus.mindseye.eval.SampledCachedTrainable) ArmijoWolfeSearch(com.simiacryptus.mindseye.opt.line.ArmijoWolfeSearch) Function(java.util.function.Function) StateSet(com.simiacryptus.mindseye.lang.StateSet) ArrayList(java.util.ArrayList) TrainableWrapper(com.simiacryptus.mindseye.eval.TrainableWrapper) Trainable(com.simiacryptus.mindseye.eval.Trainable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) ManagementFactory(java.lang.management.ManagementFactory) FailsafeLineSearchCursor(com.simiacryptus.mindseye.opt.line.FailsafeLineSearchCursor) LineSearchCursor(com.simiacryptus.mindseye.opt.line.LineSearchCursor) Nonnull(javax.annotation.Nonnull) IterativeStopException(com.simiacryptus.mindseye.lang.IterativeStopException) Util(com.simiacryptus.util.Util) StochasticComponent(com.simiacryptus.mindseye.layers.java.StochasticComponent) QQN(com.simiacryptus.mindseye.opt.orient.QQN) OrientationStrategy(com.simiacryptus.mindseye.opt.orient.OrientationStrategy) FastRandom(com.simiacryptus.util.FastRandom) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) TimedResult(com.simiacryptus.util.lang.TimedResult) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) PointSample(com.simiacryptus.mindseye.lang.PointSample) Nonnull(javax.annotation.Nonnull) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) StochasticComponent(com.simiacryptus.mindseye.layers.java.StochasticComponent) PointSample(com.simiacryptus.mindseye.lang.PointSample)

Example 3 with StochasticComponent

use of com.simiacryptus.mindseye.layers.java.StochasticComponent in project MindsEye by SimiaCryptus.

the class StochasticSamplingSubnetLayer method eval.

@Nullable
@Override
public Result eval(@Nonnull final Result... inObj) {
    Result[] counting = Arrays.stream(inObj).map(r -> {
        return new CountingResult(r, samples);
    }).toArray(i -> new Result[i]);
    return average(Arrays.stream(getSeeds()).mapToObj(seed -> {
        Layer inner = getInner();
        if (inner instanceof DAGNetwork) {
            ((DAGNetwork) inner).visitNodes(node -> {
                Layer layer = node.getLayer();
                if (layer instanceof StochasticComponent) {
                    ((StochasticComponent) layer).shuffle(seed);
                }
                if (layer instanceof MultiPrecision<?>) {
                    ((MultiPrecision) layer).setPrecision(precision);
                }
            });
        }
        if (inner instanceof MultiPrecision<?>) {
            ((MultiPrecision) inner).setPrecision(precision);
        }
        if (inner instanceof StochasticComponent) {
            ((StochasticComponent) inner).shuffle(seed);
        }
        inner.setFrozen(isFrozen());
        return inner.eval(counting);
    }).toArray(i -> new Result[i]), precision);
}
Also used : PipelineNetwork(com.simiacryptus.mindseye.network.PipelineNetwork) IntStream(java.util.stream.IntStream) JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) StochasticComponent(com.simiacryptus.mindseye.layers.java.StochasticComponent) CountingResult(com.simiacryptus.mindseye.network.CountingResult) Tensor(com.simiacryptus.mindseye.lang.Tensor) Random(java.util.Random) WrapperLayer(com.simiacryptus.mindseye.layers.java.WrapperLayer) Result(com.simiacryptus.mindseye.lang.Result) ValueLayer(com.simiacryptus.mindseye.layers.java.ValueLayer) DAGNode(com.simiacryptus.mindseye.network.DAGNode) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) StochasticComponent(com.simiacryptus.mindseye.layers.java.StochasticComponent) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) WrapperLayer(com.simiacryptus.mindseye.layers.java.WrapperLayer) ValueLayer(com.simiacryptus.mindseye.layers.java.ValueLayer) Layer(com.simiacryptus.mindseye.lang.Layer) CountingResult(com.simiacryptus.mindseye.network.CountingResult) Result(com.simiacryptus.mindseye.lang.Result) CountingResult(com.simiacryptus.mindseye.network.CountingResult) Nullable(javax.annotation.Nullable)

Example 4 with StochasticComponent

use of com.simiacryptus.mindseye.layers.java.StochasticComponent in project MindsEye by SimiaCryptus.

the class IterativeTrainer method measure.

/**
 * Measure point sample.
 *
 * @param reset the reset
 * @return the point sample
 */
@Nullable
public PointSample measure(boolean reset) {
    @Nullable PointSample currentPoint = null;
    int retries = 0;
    do {
        if (reset) {
            orientation.reset();
            if (subject.getLayer() instanceof DAGNetwork) {
                ((DAGNetwork) subject.getLayer()).visitLayers(layer -> {
                    if (layer instanceof StochasticComponent)
                        ((StochasticComponent) layer).shuffle(StochasticComponent.random.get().nextLong());
                });
            }
            if (!subject.reseed(System.nanoTime())) {
                if (retries > 0)
                    throw new IterativeStopException("Failed to reset training subject");
            } else {
                monitor.log(String.format("Reset training subject"));
            }
        }
        if (null != currentPoint) {
            currentPoint.freeRef();
        }
        currentPoint = subject.measure(monitor);
    } while (!Double.isFinite(currentPoint.getMean()) && 10 < retries++);
    if (!Double.isFinite(currentPoint.getMean())) {
        currentPoint.freeRef();
        throw new IterativeStopException();
    }
    return currentPoint;
}
Also used : StochasticComponent(com.simiacryptus.mindseye.layers.java.StochasticComponent) IterativeStopException(com.simiacryptus.mindseye.lang.IterativeStopException) PointSample(com.simiacryptus.mindseye.lang.PointSample) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) Nullable(javax.annotation.Nullable) Nullable(javax.annotation.Nullable)

Example 5 with StochasticComponent

use of com.simiacryptus.mindseye.layers.java.StochasticComponent in project MindsEye by SimiaCryptus.

the class IterativeTrainer method run.

/**
 * Run double.
 *
 * @return the double
 */
public double run() {
    final long timeoutMs = System.currentTimeMillis() + timeout.toMillis();
    long lastIterationTime = System.nanoTime();
    @Nullable PointSample currentPoint = measure(true);
    mainLoop: while (timeoutMs > System.currentTimeMillis() && currentPoint.getMean() > terminateThreshold) {
        if (currentIteration.get() > maxIterations) {
            break;
        }
        currentPoint.freeRef();
        currentPoint = measure(true);
        assert 0 < currentPoint.delta.getMap().size() : "Nothing to optimize";
        subiterationLoop: for (int subiteration = 0; subiteration < iterationsPerSample || iterationsPerSample <= 0; subiteration++) {
            if (timeoutMs < System.currentTimeMillis()) {
                break mainLoop;
            }
            if (currentIteration.incrementAndGet() > maxIterations) {
                break mainLoop;
            }
            currentPoint.freeRef();
            currentPoint = measure(true);
            @Nullable final PointSample _currentPoint = currentPoint;
            @Nonnull final TimedResult<LineSearchCursor> timedOrientation = TimedResult.time(() -> orientation.orient(subject, _currentPoint, monitor));
            final LineSearchCursor direction = timedOrientation.result;
            final CharSequence directionType = direction.getDirectionType();
            @Nullable final PointSample previous = currentPoint;
            previous.addRef();
            try {
                @Nonnull final TimedResult<PointSample> timedLineSearch = TimedResult.time(() -> step(direction, directionType, previous));
                currentPoint.freeRef();
                currentPoint = timedLineSearch.result;
                final long now = System.nanoTime();
                final CharSequence perfString = String.format("Total: %.4f; Orientation: %.4f; Line Search: %.4f", (now - lastIterationTime) / 1e9, timedOrientation.timeNanos / 1e9, timedLineSearch.timeNanos / 1e9);
                lastIterationTime = now;
                monitor.log(String.format("Fitness changed from %s to %s", previous.getMean(), currentPoint.getMean()));
                if (previous.getMean() <= currentPoint.getMean()) {
                    if (previous.getMean() < currentPoint.getMean()) {
                        monitor.log(String.format("Resetting Iteration %s", perfString));
                        currentPoint.freeRef();
                        currentPoint = direction.step(0, monitor).point;
                    } else {
                        monitor.log(String.format("Static Iteration %s", perfString));
                    }
                    if (subject.reseed(System.nanoTime())) {
                        monitor.log(String.format("Iteration %s failed, retrying. Error: %s", currentIteration.get(), currentPoint.getMean()));
                        monitor.log(String.format("Previous Error: %s -> %s", previous.getRate(), previous.getMean()));
                        break subiterationLoop;
                    } else {
                        monitor.log(String.format("Iteration %s failed, aborting. Error: %s", currentIteration.get(), currentPoint.getMean()));
                        monitor.log(String.format("Previous Error: %s -> %s", previous.getRate(), previous.getMean()));
                        break mainLoop;
                    }
                } else {
                    monitor.log(String.format("Iteration %s complete. Error: %s " + perfString, currentIteration.get(), currentPoint.getMean()));
                }
                monitor.onStepComplete(new Step(currentPoint, currentIteration.get()));
            } finally {
                previous.freeRef();
                direction.freeRef();
            }
        }
    }
    if (subject.getLayer() instanceof DAGNetwork) {
        ((DAGNetwork) subject.getLayer()).visitLayers(layer -> {
            if (layer instanceof StochasticComponent)
                ((StochasticComponent) layer).clearNoise();
        });
    }
    double mean = null == currentPoint ? Double.NaN : currentPoint.getMean();
    currentPoint.freeRef();
    return mean;
}
Also used : Nonnull(javax.annotation.Nonnull) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) FailsafeLineSearchCursor(com.simiacryptus.mindseye.opt.line.FailsafeLineSearchCursor) LineSearchCursor(com.simiacryptus.mindseye.opt.line.LineSearchCursor) StochasticComponent(com.simiacryptus.mindseye.layers.java.StochasticComponent) PointSample(com.simiacryptus.mindseye.lang.PointSample) Nullable(javax.annotation.Nullable)

Aggregations

StochasticComponent (com.simiacryptus.mindseye.layers.java.StochasticComponent)5 DAGNetwork (com.simiacryptus.mindseye.network.DAGNetwork)5 Nonnull (javax.annotation.Nonnull)4 IterativeStopException (com.simiacryptus.mindseye.lang.IterativeStopException)3 PointSample (com.simiacryptus.mindseye.lang.PointSample)3 Nullable (javax.annotation.Nullable)3 Layer (com.simiacryptus.mindseye.lang.Layer)2 FailsafeLineSearchCursor (com.simiacryptus.mindseye.opt.line.FailsafeLineSearchCursor)2 LineSearchCursor (com.simiacryptus.mindseye.opt.line.LineSearchCursor)2 Arrays (java.util.Arrays)2 Map (java.util.Map)2 IntStream (java.util.stream.IntStream)2 JsonObject (com.google.gson.JsonObject)1 SampledCachedTrainable (com.simiacryptus.mindseye.eval.SampledCachedTrainable)1 SampledTrainable (com.simiacryptus.mindseye.eval.SampledTrainable)1 Trainable (com.simiacryptus.mindseye.eval.Trainable)1 TrainableBase (com.simiacryptus.mindseye.eval.TrainableBase)1 TrainableWrapper (com.simiacryptus.mindseye.eval.TrainableWrapper)1 DataSerializer (com.simiacryptus.mindseye.lang.DataSerializer)1 DoubleBuffer (com.simiacryptus.mindseye.lang.DoubleBuffer)1