Search in sources :

Example 1 with PlaceholderLayer

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

the class TensorListTrainable method getNNContext.

/**
 * Get nn context nn result [ ].
 *
 * @param data the data
 * @param mask the mask
 * @return the nn result [ ]
 */
public static Result[] getNNContext(@Nullable final TensorList[] data, @Nullable final boolean[] mask) {
    if (null == data)
        throw new IllegalArgumentException();
    int inputs = data.length;
    assert 0 < inputs;
    int items = data[0].length();
    assert 0 < items;
    return IntStream.range(0, inputs).mapToObj(col -> {
        final Tensor[] tensors = IntStream.range(0, items).mapToObj(row -> data[col].get(row)).toArray(i -> new Tensor[i]);
        @Nonnull TensorArray tensorArray = TensorArray.create(tensors);
        if (null == mask || col >= mask.length || !mask[col]) {
            return new ConstantResult(tensorArray);
        } else {
            return new Result(tensorArray, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
                for (int index = 0; index < delta.length(); index++) {
                    final Tensor dt = delta.get(index);
                    @Nullable final double[] d = dt.getData();
                    final Tensor t = tensors[index];
                    @Nullable final double[] p = t.getData();
                    @Nonnull PlaceholderLayer<double[]> layer = new PlaceholderLayer<>(p);
                    buffer.get(layer, p).addInPlace(d).freeRef();
                    dt.freeRef();
                    layer.freeRef();
                }
            }) {

                @Override
                public boolean isAlive() {
                    return true;
                }
            };
        }
    }).toArray(x1 -> new Result[x1]);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Tensor(com.simiacryptus.mindseye.lang.Tensor) ReferenceCountingBase(com.simiacryptus.mindseye.lang.ReferenceCountingBase) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) Result(com.simiacryptus.mindseye.lang.Result) StateSet(com.simiacryptus.mindseye.lang.StateSet) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) TensorList(com.simiacryptus.mindseye.lang.TensorList) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) TimedResult(com.simiacryptus.util.lang.TimedResult) Layer(com.simiacryptus.mindseye.lang.Layer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) Nonnull(javax.annotation.Nonnull) PointSample(com.simiacryptus.mindseye.lang.PointSample) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) ConstantResult(com.simiacryptus.mindseye.lang.ConstantResult) 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) TimedResult(com.simiacryptus.util.lang.TimedResult) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer)

Example 2 with PlaceholderLayer

use of com.simiacryptus.mindseye.layers.java.PlaceholderLayer 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 3 with PlaceholderLayer

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

the class SingleDerivativeTester method getFeedbackGradient.

@Nonnull
private Tensor getFeedbackGradient(@Nonnull final Layer component, final int inputIndex, @Nonnull final Tensor outputPrototype, @Nonnull 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(1));
        inputKey.getKey().freeRef();
        final Result[] copyInput = Arrays.stream(inputPrototype).map(x -> new Result(TensorArray.create(x), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
        }) {

            @Override
            public boolean isAlive() {
                return false;
            }
        }).toArray(i -> new Result[i]);
        copyInput[inputIndex].getData().freeRef();
        copyInput[inputIndex].freeRef();
        double[] target = new double[inputDims * outputPrototype.length()];
        copyInput[inputIndex] = new Result(TensorArray.create(inputTensor), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
            if (1 != data.length())
                throw new AssertionError();
            if (data.length() != 1)
                throw new AssertionError();
            @Nonnull final Tensor gradientBuffer = new Tensor(inputDims, outputPrototype.length());
            if (!Arrays.equals(inputTensor.getDimensions(), data.getDimensions())) {
                throw new AssertionError();
            }
            IntStream.range(0, data.length()).forEach(dataIndex -> {
                for (int i = 0; i < inputDims; i++) {
                    @Nullable Tensor tensor = data.get(dataIndex);
                    gradientBuffer.set(new int[] { i, j_ }, tensor.getData()[i]);
                    tensor.freeRef();
                }
            });
            buffer.get(inputKey, target).addInPlace(gradientBuffer.getData()).freeRef();
            gradientBuffer.freeRef();
        }) {

            @Override
            public boolean isAlive() {
                return true;
            }
        };
        @Nullable final Result eval;
        try {
            eval = component.eval(copyInput);
        } finally {
            for (@Nonnull Result nnResult : copyInput) {
                nnResult.freeRef();
                nnResult.getData().freeRef();
            }
        }
        @Nonnull final DeltaSet<Layer> deltaSet = new DeltaSet<Layer>();
        @Nonnull TensorArray tensorArray = TensorArray.wrap(new Tensor(outputPrototype.getDimensions()).set(j, 1));
        try {
            eval.accumulate(deltaSet, tensorArray);
            final Delta<Layer> inputDelta = deltaSet.getMap().get(inputKey);
            if (null != inputDelta) {
                @Nonnull Tensor tensor = new Tensor(inputDelta.getDelta(), result.getDimensions());
                result.addInPlace(tensor);
                tensor.freeRef();
            }
        } finally {
            eval.getData().freeRef();
            eval.freeRef();
            deltaSet.freeRef();
            inputKey.freeRef();
        }
    }
    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) Optional(java.util.Optional) 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 4 with PlaceholderLayer

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

the class RecursiveSubspace method buildSubspace.

/**
 * Build subspace nn layer.
 *
 * @param subject     the subject
 * @param measurement the measurement
 * @param monitor     the monitor
 * @return the nn layer
 */
@Nullable
public Layer buildSubspace(@Nonnull Trainable subject, @Nonnull PointSample measurement, @Nonnull TrainingMonitor monitor) {
    @Nonnull PointSample origin = measurement.copyFull().backup();
    @Nonnull final DeltaSet<Layer> direction = measurement.delta.scale(-1);
    final double magnitude = direction.getMagnitude();
    if (Math.abs(magnitude) < 1e-10) {
        monitor.log(String.format("Zero gradient: %s", magnitude));
    } else if (Math.abs(magnitude) < 1e-5) {
        monitor.log(String.format("Low gradient: %s", magnitude));
    }
    boolean hasPlaceholders = direction.getMap().entrySet().stream().filter(x -> x.getKey() instanceof PlaceholderLayer).findAny().isPresent();
    List<Layer> deltaLayers = direction.getMap().entrySet().stream().map(x -> x.getKey()).filter(x -> !(x instanceof PlaceholderLayer)).collect(Collectors.toList());
    int size = deltaLayers.size() + (hasPlaceholders ? 1 : 0);
    if (null == weights || weights.length != size)
        weights = new double[size];
    return new LayerBase() {

        @Nonnull
        Layer self = this;

        @Nonnull
        @Override
        public Result eval(Result... array) {
            assertAlive();
            origin.restore();
            IntStream.range(0, deltaLayers.size()).forEach(i -> {
                direction.getMap().get(deltaLayers.get(i)).accumulate(weights[hasPlaceholders ? (i + 1) : i]);
            });
            if (hasPlaceholders) {
                direction.getMap().entrySet().stream().filter(x -> x.getKey() instanceof PlaceholderLayer).distinct().forEach(entry -> entry.getValue().accumulate(weights[0]));
            }
            PointSample measure = subject.measure(monitor);
            double mean = measure.getMean();
            monitor.log(String.format("RecursiveSubspace: %s <- %s", mean, Arrays.toString(weights)));
            direction.addRef();
            return new Result(TensorArray.wrap(new Tensor(mean)), (DeltaSet<Layer> buffer, TensorList data) -> {
                DoubleStream deltaStream = deltaLayers.stream().mapToDouble(layer -> {
                    Delta<Layer> a = direction.getMap().get(layer);
                    Delta<Layer> b = measure.delta.getMap().get(layer);
                    return b.dot(a) / Math.max(Math.sqrt(a.dot(a)), 1e-8);
                });
                if (hasPlaceholders) {
                    deltaStream = DoubleStream.concat(DoubleStream.of(direction.getMap().keySet().stream().filter(x -> x instanceof PlaceholderLayer).distinct().mapToDouble(layer -> {
                        Delta<Layer> a = direction.getMap().get(layer);
                        Delta<Layer> b = measure.delta.getMap().get(layer);
                        return b.dot(a) / Math.max(Math.sqrt(a.dot(a)), 1e-8);
                    }).sum()), deltaStream);
                }
                buffer.get(self, weights).addInPlace(deltaStream.toArray()).freeRef();
            }) {

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

                @Override
                public boolean isAlive() {
                    return true;
                }
            };
        }

        @Override
        protected void _free() {
            direction.freeRef();
            origin.freeRef();
            super._free();
        }

        @Nonnull
        @Override
        public JsonObject getJson(Map<CharSequence, byte[]> resources, DataSerializer dataSerializer) {
            throw new IllegalStateException();
        }

        @Nullable
        @Override
        public List<double[]> state() {
            return null;
        }
    };
}
Also used : IntStream(java.util.stream.IntStream) JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) Tensor(com.simiacryptus.mindseye.lang.Tensor) Result(com.simiacryptus.mindseye.lang.Result) ArmijoWolfeSearch(com.simiacryptus.mindseye.opt.line.ArmijoWolfeSearch) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) StateSet(com.simiacryptus.mindseye.lang.StateSet) Trainable(com.simiacryptus.mindseye.eval.Trainable) Delta(com.simiacryptus.mindseye.lang.Delta) 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) IterativeTrainer(com.simiacryptus.mindseye.opt.IterativeTrainer) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BasicTrainable(com.simiacryptus.mindseye.eval.BasicTrainable) Collectors(java.util.stream.Collectors) DoubleStream(java.util.stream.DoubleStream) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) ArrayTrainable(com.simiacryptus.mindseye.eval.ArrayTrainable) TensorList(com.simiacryptus.mindseye.lang.TensorList) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) PointSample(com.simiacryptus.mindseye.lang.PointSample) 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) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) DoubleStream(java.util.stream.DoubleStream) PointSample(com.simiacryptus.mindseye.lang.PointSample) Map(java.util.Map) PlaceholderLayer(com.simiacryptus.mindseye.layers.java.PlaceholderLayer) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) Nullable(javax.annotation.Nullable)

Aggregations

DeltaSet (com.simiacryptus.mindseye.lang.DeltaSet)4 Layer (com.simiacryptus.mindseye.lang.Layer)4 Result (com.simiacryptus.mindseye.lang.Result)4 Tensor (com.simiacryptus.mindseye.lang.Tensor)4 TensorArray (com.simiacryptus.mindseye.lang.TensorArray)4 TensorList (com.simiacryptus.mindseye.lang.TensorList)4 PlaceholderLayer (com.simiacryptus.mindseye.layers.java.PlaceholderLayer)4 Arrays (java.util.Arrays)4 IntStream (java.util.stream.IntStream)4 Nonnull (javax.annotation.Nonnull)4 Nullable (javax.annotation.Nullable)4 ConstantResult (com.simiacryptus.mindseye.lang.ConstantResult)3 Delta (com.simiacryptus.mindseye.lang.Delta)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 DoubleBuffer (com.simiacryptus.mindseye.lang.DoubleBuffer)2 PointSample (com.simiacryptus.mindseye.lang.PointSample)2 StateSet (com.simiacryptus.mindseye.lang.StateSet)2 TrainingMonitor (com.simiacryptus.mindseye.opt.TrainingMonitor)2 SimpleEval (com.simiacryptus.mindseye.test.SimpleEval)2