Search in sources :

Example 6 with Delta

use of com.simiacryptus.mindseye.lang.Delta in project MindsEye by SimiaCryptus.

the class ImgBandBiasLayer method eval.

/**
 * Eval nn result.
 *
 * @param input the input
 * @return the nn result
 */
@Nonnull
public Result eval(@Nonnull final Result input) {
    @Nullable final double[] bias = getBias();
    input.addRef();
    return new Result(TensorArray.wrap(input.getData().stream().parallel().map(r -> {
        if (r.getDimensions().length != 3) {
            throw new IllegalArgumentException(Arrays.toString(r.getDimensions()));
        }
        if (r.getDimensions()[2] != bias.length) {
            throw new IllegalArgumentException(String.format("%s: %s does not have %s bands", getName(), Arrays.toString(r.getDimensions()), bias.length));
        }
        @Nonnull Tensor tensor = new Tensor(add(r.getData()), r.getDimensions());
        r.freeRef();
        return tensor;
    }).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
        if (!isFrozen()) {
            final Delta<Layer> deltaBuffer = buffer.get(ImgBandBiasLayer.this, bias);
            data.stream().parallel().forEach(d -> {
                final double[] array = RecycleBin.DOUBLES.obtain(bias.length);
                @Nullable final double[] signal = d.getData();
                final int size = signal.length / bias.length;
                for (int i = 0; i < signal.length; i++) {
                    array[i / size] += signal[i];
                    if (!Double.isFinite(array[i / size])) {
                        array[i / size] = 0.0;
                    }
                }
                d.freeRef();
                assert Arrays.stream(array).allMatch(v -> Double.isFinite(v));
                deltaBuffer.addInPlace(array);
                RecycleBin.DOUBLES.recycle(array, array.length);
            });
            deltaBuffer.freeRef();
        }
        if (input.isAlive()) {
            data.addRef();
            input.accumulate(buffer, data);
        }
    }) {

        @Override
        protected void _free() {
            input.freeRef();
        }

        @Override
        public boolean isAlive() {
            return input.isAlive() || !isFrozen();
        }
    };
}
Also used : JsonObject(com.google.gson.JsonObject) Util(com.simiacryptus.util.Util) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) IntToDoubleFunction(java.util.function.IntToDoubleFunction) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) Result(com.simiacryptus.mindseye.lang.Result) FastRandom(com.simiacryptus.util.FastRandom) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) JsonUtil(com.simiacryptus.util.io.JsonUtil) Delta(com.simiacryptus.mindseye.lang.Delta) RecycleBin(com.simiacryptus.mindseye.lang.RecycleBin) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) TensorList(com.simiacryptus.mindseye.lang.TensorList) Map(java.util.Map) DoubleSupplier(java.util.function.DoubleSupplier) Layer(com.simiacryptus.mindseye.lang.Layer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) Layer(com.simiacryptus.mindseye.lang.Layer) Nullable(javax.annotation.Nullable) Result(com.simiacryptus.mindseye.lang.Result) Nonnull(javax.annotation.Nonnull)

Example 7 with Delta

use of com.simiacryptus.mindseye.lang.Delta in project MindsEye by SimiaCryptus.

the class BiasLayer method eval.

@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    TensorList input;
    if (0 == inObj.length) {
        input = TensorArray.create();
    } else {
        input = inObj[0].getData();
    }
    return new Result(TensorArray.wrap(input.stream().parallel().map(r -> {
        @Nonnull Tensor tensor = new Tensor(add(r.getData()), r.getDimensions());
        r.freeRef();
        return tensor;
    }).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
        if (!isFrozen()) {
            final Delta<Layer> deltaBuffer = buffer.get(BiasLayer.this, bias);
            if (1 == bias.length) {
                delta.stream().parallel().forEach(d -> {
                    @Nullable final double[] array = d.getData();
                    deltaBuffer.addInPlace(1 == array.length ? array : new double[] { Arrays.stream(array).sum() });
                    d.freeRef();
                });
            } else {
                delta.stream().parallel().forEach(d -> {
                    deltaBuffer.addInPlace(d.getData());
                    d.freeRef();
                });
            }
            deltaBuffer.freeRef();
        }
        if (0 < inObj.length && inObj[0].isAlive()) {
            delta.addRef();
            inObj[0].accumulate(buffer, delta);
        }
    }) {

        @Override
        protected void _free() {
            Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
        }

        @Override
        public boolean isAlive() {
            return 0 < inObj.length && inObj[0].isAlive() || !isFrozen();
        }
    };
}
Also used : JsonObject(com.google.gson.JsonObject) Util(com.simiacryptus.util.Util) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) IntToDoubleFunction(java.util.function.IntToDoubleFunction) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) Result(com.simiacryptus.mindseye.lang.Result) FastRandom(com.simiacryptus.util.FastRandom) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) JsonUtil(com.simiacryptus.util.io.JsonUtil) Delta(com.simiacryptus.mindseye.lang.Delta) RecycleBin(com.simiacryptus.mindseye.lang.RecycleBin) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) TensorList(com.simiacryptus.mindseye.lang.TensorList) Map(java.util.Map) DoubleSupplier(java.util.function.DoubleSupplier) Layer(com.simiacryptus.mindseye.lang.Layer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) Layer(com.simiacryptus.mindseye.lang.Layer) Nullable(javax.annotation.Nullable) Result(com.simiacryptus.mindseye.lang.Result) Nonnull(javax.annotation.Nonnull)

Example 8 with Delta

use of com.simiacryptus.mindseye.lang.Delta in project MindsEye by SimiaCryptus.

the class BatchDerivativeTester method getFeedbackGradient.

@Nonnull
private Tensor getFeedbackGradient(@Nonnull final Layer component, final int inputIndex, @Nonnull final Tensor outputPrototype, final Tensor... inputPrototype) {
    final Tensor inputTensor = inputPrototype[inputIndex];
    final int inputDims = inputTensor.length();
    @Nonnull final Tensor result = new Tensor(inputDims, outputPrototype.length());
    for (int j = 0; j < outputPrototype.length(); j++) {
        final int j_ = j;
        @Nonnull final PlaceholderLayer<Tensor> inputKey = new PlaceholderLayer<Tensor>(new Tensor());
        @Nonnull final Result copyInput = new Result(TensorArray.create(inputPrototype), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
            @Nonnull final Tensor gradientBuffer = new Tensor(inputDims, outputPrototype.length());
            if (!Arrays.equals(inputTensor.getDimensions(), data.get(inputIndex).getDimensions())) {
                throw new AssertionError();
            }
            for (int i = 0; i < inputDims; i++) {
                gradientBuffer.set(new int[] { i, j_ }, data.get(inputIndex).getData()[i]);
            }
            buffer.get(inputKey, new double[gradientBuffer.length()]).addInPlace(gradientBuffer.getData());
        }) {

            @Override
            public boolean isAlive() {
                return true;
            }
        };
        @Nullable final Result eval = component.eval(copyInput);
        @Nonnull final DeltaSet<Layer> xxx = new DeltaSet<Layer>();
        @Nonnull TensorArray tensorArray = TensorArray.wrap(eval.getData().stream().map(x -> {
            @Nonnull Tensor set = x.set(j_, 1);
            x.freeRef();
            return set;
        }).toArray(i -> new Tensor[i]));
        eval.accumulate(xxx, tensorArray);
        final Delta<Layer> inputDelta = xxx.getMap().get(inputKey);
        if (null != inputDelta) {
            result.addInPlace(new Tensor(inputDelta.getDelta(), result.getDimensions()));
        }
    }
    return result;
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DoubleBuffer(com.simiacryptus.mindseye.lang.DoubleBuffer) Result(com.simiacryptus.mindseye.lang.Result) Collectors(java.util.stream.Collectors) Delta(com.simiacryptus.mindseye.lang.Delta) List(java.util.List) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) TensorList(com.simiacryptus.mindseye.lang.TensorList) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Layer(com.simiacryptus.mindseye.lang.Layer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Layer(com.simiacryptus.mindseye.lang.Layer) Result(com.simiacryptus.mindseye.lang.Result) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nullable(javax.annotation.Nullable) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Nonnull(javax.annotation.Nonnull)

Example 9 with Delta

use of com.simiacryptus.mindseye.lang.Delta in project MindsEye by SimiaCryptus.

the class BatchDerivativeTester method testFrozen.

/**
 * Test frozen.
 *
 * @param component      the component
 * @param inputPrototype the input prototype
 */
public void testFrozen(@Nonnull final Layer component, @Nonnull final Tensor[] inputPrototype) {
    @Nonnull final AtomicBoolean reachedInputFeedback = new AtomicBoolean(false);
    @Nonnull final Layer frozen = component.copy().freeze();
    @Nullable final Result eval = frozen.eval(new Result(TensorArray.create(inputPrototype), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
        reachedInputFeedback.set(true);
    }) {

        @Override
        public boolean isAlive() {
            return true;
        }
    });
    @Nonnull final DeltaSet<Layer> buffer = new DeltaSet<Layer>();
    TensorList tensorList = eval.getData().copy();
    eval.accumulate(buffer, tensorList);
    final List<Delta<Layer>> deltas = component.state().stream().map(doubles -> {
        return buffer.stream().filter(x -> x.target == doubles).findFirst().orElse(null);
    }).filter(x -> x != null).collect(Collectors.toList());
    if (!deltas.isEmpty() && !component.state().isEmpty()) {
        throw new AssertionError("Frozen component listed in delta. Deltas: " + deltas);
    }
    final int inElements = Arrays.stream(inputPrototype).mapToInt(x -> x.length()).sum();
    if (!reachedInputFeedback.get() && 0 < inElements) {
        throw new RuntimeException("Frozen component did not pass input backwards");
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DoubleBuffer(com.simiacryptus.mindseye.lang.DoubleBuffer) Result(com.simiacryptus.mindseye.lang.Result) Collectors(java.util.stream.Collectors) Delta(com.simiacryptus.mindseye.lang.Delta) List(java.util.List) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) TensorList(com.simiacryptus.mindseye.lang.TensorList) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Layer(com.simiacryptus.mindseye.lang.Layer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) SimpleEval(com.simiacryptus.mindseye.test.SimpleEval) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Layer(com.simiacryptus.mindseye.lang.Layer) Result(com.simiacryptus.mindseye.lang.Result) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Delta(com.simiacryptus.mindseye.lang.Delta) Nullable(javax.annotation.Nullable)

Example 10 with Delta

use of com.simiacryptus.mindseye.lang.Delta in project MindsEye by SimiaCryptus.

the class LBFGS method lbfgs.

private boolean lbfgs(@Nonnull PointSample measurement, @Nonnull TrainingMonitor monitor, @Nonnull List<PointSample> history, @Nonnull DeltaSet<Layer> direction) {
    try {
        @Nonnull DeltaSet<Layer> p = measurement.delta.copy();
        if (!p.stream().parallel().allMatch(y -> Arrays.stream(y.getDelta()).allMatch(d -> Double.isFinite(d)))) {
            throw new IllegalStateException("Non-finite value");
        }
        @Nonnull final double[] alphas = new double[history.size()];
        for (int i = history.size() - 2; i >= 0; i--) {
            @Nonnull final DeltaSet<Layer> sd = history.get(i + 1).weights.subtract(history.get(i).weights);
            @Nonnull final DeltaSet<Layer> yd = history.get(i + 1).delta.subtract(history.get(i).delta);
            final double denominator = sd.dot(yd);
            if (0 == denominator) {
                throw new IllegalStateException("Orientation vanished.");
            }
            alphas[i] = p.dot(sd) / denominator;
            p = p.subtract(yd.scale(alphas[i]));
            if ((!p.stream().parallel().allMatch(y -> Arrays.stream(y.getDelta()).allMatch(d -> Double.isFinite(d))))) {
                throw new IllegalStateException("Non-finite value");
            }
        }
        @Nonnull final DeltaSet<Layer> sk = history.get(history.size() - 1).weights.subtract(history.get(history.size() - 2).weights);
        @Nonnull final DeltaSet<Layer> yk = history.get(history.size() - 1).delta.subtract(history.get(history.size() - 2).delta);
        p = p.scale(sk.dot(yk) / yk.dot(yk));
        if (!p.stream().parallel().allMatch(y -> Arrays.stream(y.getDelta()).allMatch(d -> Double.isFinite(d)))) {
            throw new IllegalStateException("Non-finite value");
        }
        for (int i = 0; i < history.size() - 1; i++) {
            @Nonnull final DeltaSet<Layer> sd = history.get(i + 1).weights.subtract(history.get(i).weights);
            @Nonnull final DeltaSet<Layer> yd = history.get(i + 1).delta.subtract(history.get(i).delta);
            final double beta = p.dot(yd) / sd.dot(yd);
            p = p.add(sd.scale(alphas[i] - beta));
            if (!p.stream().parallel().allMatch(y -> Arrays.stream(y.getDelta()).allMatch(d -> Double.isFinite(d)))) {
                throw new IllegalStateException("Non-finite value");
            }
        }
        boolean accept = measurement.delta.dot(p) < 0;
        if (accept) {
            monitor.log("Accepted: " + new Stats(direction, p));
            copy(p, direction);
        } else {
            monitor.log("Rejected: " + new Stats(direction, p));
        }
        return accept;
    } catch (Throwable e) {
        monitor.log(String.format("LBFGS Orientation Error: %s", e.getMessage()));
        return false;
    }
}
Also used : Arrays(java.util.Arrays) LineSearchPoint(com.simiacryptus.mindseye.opt.line.LineSearchPoint) Collectors(java.util.stream.Collectors) ArrayUtil(com.simiacryptus.util.ArrayUtil) TreeSet(java.util.TreeSet) Trainable(com.simiacryptus.mindseye.eval.Trainable) Delta(com.simiacryptus.mindseye.lang.Delta) List(java.util.List) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) Map(java.util.Map) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Layer(com.simiacryptus.mindseye.lang.Layer) SimpleLineSearchCursor(com.simiacryptus.mindseye.opt.line.SimpleLineSearchCursor) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) DoubleBufferSet(com.simiacryptus.mindseye.lang.DoubleBufferSet) Comparator(java.util.Comparator) Nonnull(javax.annotation.Nonnull) PointSample(com.simiacryptus.mindseye.lang.PointSample) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) Layer(com.simiacryptus.mindseye.lang.Layer) LineSearchPoint(com.simiacryptus.mindseye.opt.line.LineSearchPoint)

Aggregations

Delta (com.simiacryptus.mindseye.lang.Delta)13 DeltaSet (com.simiacryptus.mindseye.lang.DeltaSet)13 Layer (com.simiacryptus.mindseye.lang.Layer)13 Arrays (java.util.Arrays)13 List (java.util.List)13 Nonnull (javax.annotation.Nonnull)13 Nullable (javax.annotation.Nullable)13 Result (com.simiacryptus.mindseye.lang.Result)12 Tensor (com.simiacryptus.mindseye.lang.Tensor)12 TensorArray (com.simiacryptus.mindseye.lang.TensorArray)12 TensorList (com.simiacryptus.mindseye.lang.TensorList)12 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 IntStream (java.util.stream.IntStream)10 PlaceholderLayer (com.simiacryptus.mindseye.layers.java.PlaceholderLayer)8 Collectors (java.util.stream.Collectors)8 Map (java.util.Map)7 JsonObject (com.google.gson.JsonObject)6 ConstantResult (com.simiacryptus.mindseye.lang.ConstantResult)6 DataSerializer (com.simiacryptus.mindseye.lang.DataSerializer)6