Search in sources :

Example 31 with TensorArray

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

the class ImgBandSelectLayer method eval.

@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
    final Result input = inObj[0];
    final TensorList batch = input.getData();
    @Nonnull final int[] inputDims = batch.getDimensions();
    assert 3 == inputDims.length;
    @Nonnull final Tensor outputDims = new Tensor(inputDims[0], inputDims[1], bands.length);
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    @Nonnull TensorArray wrap = TensorArray.wrap(IntStream.range(0, batch.length()).parallel().mapToObj(dataIndex -> outputDims.mapCoords((c) -> {
        int[] coords = c.getCoords();
        @Nullable Tensor tensor = batch.get(dataIndex);
        double v = tensor.get(coords[0], coords[1], bands[coords[2]]);
        tensor.freeRef();
        return v;
    })).toArray(i -> new Tensor[i]));
    outputDims.freeRef();
    return new Result(wrap, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList error) -> {
        if (input.isAlive()) {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, error.length()).parallel().mapToObj(dataIndex -> {
                @Nonnull final Tensor passback = new Tensor(inputDims);
                @Nullable final Tensor err = error.get(dataIndex);
                err.coordStream(false).forEach(c -> {
                    int[] coords = c.getCoords();
                    passback.set(coords[0], coords[1], bands[coords[2]], err.get(c));
                });
                err.freeRef();
                return passback;
            }).toArray(i -> new Tensor[i]));
            input.accumulate(buffer, tensorArray);
        }
    }) {

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

        @Override
        public boolean isAlive() {
            return input.isAlive() || !isFrozen();
        }
    };
}
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) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) ArrayList(java.util.ArrayList) JsonArray(com.google.gson.JsonArray) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) TensorList(com.simiacryptus.mindseye.lang.TensorList) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) JsonPrimitive(com.google.gson.JsonPrimitive) 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) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) Nullable(javax.annotation.Nullable) Result(com.simiacryptus.mindseye.lang.Result) Nonnull(javax.annotation.Nonnull)

Example 32 with TensorArray

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

the class ImgCropLayer method eval.

@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    final Result input = inObj[0];
    final TensorList batch = input.getData();
    @Nonnull final int[] inputDims = batch.getDimensions();
    assert 3 == inputDims.length;
    return new Result(TensorArray.wrap(IntStream.range(0, batch.length()).parallel().mapToObj(dataIndex -> {
        @Nonnull final Tensor outputData = new Tensor(sizeX, sizeY, inputDims[2]);
        Tensor inputData = batch.get(dataIndex);
        ImgCropLayer.copy(inputData, outputData);
        inputData.freeRef();
        return outputData;
    }).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList error) -> {
        if (input.isAlive()) {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, error.length()).parallel().mapToObj(dataIndex -> {
                @Nullable final Tensor err = error.get(dataIndex);
                @Nonnull final Tensor passback = new Tensor(inputDims);
                copy(err, passback);
                err.freeRef();
                return passback;
            }).toArray(i -> new Tensor[i]));
            input.accumulate(buffer, tensorArray);
        }
    }) {

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

        @Override
        public boolean isAlive() {
            return input.isAlive() || !isFrozen();
        }
    };
}
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) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) ArrayList(java.util.ArrayList) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) TensorList(com.simiacryptus.mindseye.lang.TensorList) Map(java.util.Map) 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) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) Nullable(javax.annotation.Nullable) Result(com.simiacryptus.mindseye.lang.Result) Nonnull(javax.annotation.Nonnull)

Example 33 with TensorArray

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

the class CrossDifferenceLayer method eval.

@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
    assert 1 == inObj.length;
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    return new Result(TensorArray.wrap(inObj[0].getData().stream().parallel().map(tensor -> {
        final int inputDim = tensor.length();
        final int outputDim = (inputDim * inputDim - inputDim) / 2;
        @Nonnull final Tensor result = new Tensor(outputDim);
        @Nullable final double[] inputData = tensor.getData();
        @Nullable final double[] resultData = result.getData();
        IntStream.range(0, inputDim).forEach(x -> {
            IntStream.range(x + 1, inputDim).forEach(y -> {
                resultData[CrossDifferenceLayer.index(x, y, inputDim)] = inputData[x] - inputData[y];
            });
        });
        tensor.freeRef();
        return result;
    }).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
        final Result input = inObj[0];
        if (input.isAlive()) {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(data.stream().parallel().map(tensor -> {
                final int outputDim = tensor.length();
                final int inputDim = (1 + (int) Math.sqrt(1 + 8 * outputDim)) / 2;
                @Nonnull final Tensor passback = new Tensor(inputDim);
                @Nullable final double[] passbackData = passback.getData();
                @Nullable final double[] tensorData = tensor.getData();
                IntStream.range(0, inputDim).forEach(x -> {
                    IntStream.range(x + 1, inputDim).forEach(y -> {
                        passbackData[x] += tensorData[CrossDifferenceLayer.index(x, y, inputDim)];
                        passbackData[y] += -tensorData[CrossDifferenceLayer.index(x, y, inputDim)];
                    });
                });
                tensor.freeRef();
                return passback;
            }).toArray(i -> new Tensor[i]));
            input.accumulate(buffer, tensorArray);
        }
    }) {

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

        @Override
        public boolean isAlive() {
            for (@Nonnull final Result element : inObj) if (element.isAlive()) {
                return true;
            }
            return false;
        }
    };
}
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) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) TensorList(com.simiacryptus.mindseye.lang.TensorList) Map(java.util.Map) 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) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) Nullable(javax.annotation.Nullable) Result(com.simiacryptus.mindseye.lang.Result) Nonnull(javax.annotation.Nonnull)

Example 34 with TensorArray

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

the class CrossProductLayer method eval.

@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
    assert 1 == inObj.length;
    final Result in = inObj[0];
    TensorList indata = in.getData();
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    indata.addRef();
    return new Result(TensorArray.wrap(indata.stream().parallel().map(tensor -> {
        final int inputDim = tensor.length();
        final int outputDim = (inputDim * inputDim - inputDim) / 2;
        @Nonnull final Tensor result = new Tensor(outputDim);
        @Nullable final double[] inputData = tensor.getData();
        @Nullable final double[] resultData = result.getData();
        IntStream.range(0, inputDim).forEach(x -> {
            IntStream.range(x + 1, inputDim).forEach(y -> {
                resultData[CrossProductLayer.index(x, y, inputDim)] = inputData[x] * inputData[y];
            });
        });
        tensor.freeRef();
        return result;
    }).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
        if (in.isAlive()) {
            assert delta.length() == delta.length();
            @Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).parallel().mapToObj(batchIndex -> {
                @Nullable final Tensor deltaTensor = delta.get(batchIndex);
                final int outputDim = deltaTensor.length();
                final int inputDim = (1 + (int) Math.sqrt(1 + 8 * outputDim)) / 2;
                @Nonnull final Tensor passback = new Tensor(inputDim);
                @Nullable final double[] passbackData = passback.getData();
                @Nullable final double[] tensorData = deltaTensor.getData();
                Tensor inputTensor = indata.get(batchIndex);
                @Nullable final double[] inputData = inputTensor.getData();
                IntStream.range(0, inputDim).forEach(x -> {
                    IntStream.range(x + 1, inputDim).forEach(y -> {
                        passbackData[x] += tensorData[CrossProductLayer.index(x, y, inputDim)] * inputData[y];
                        passbackData[y] += tensorData[CrossProductLayer.index(x, y, inputDim)] * inputData[x];
                    });
                });
                deltaTensor.freeRef();
                inputTensor.freeRef();
                return passback;
            }).toArray(i -> new Tensor[i]));
            in.accumulate(buffer, tensorArray);
        }
    }) {

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

        @Override
        public boolean isAlive() {
            for (@Nonnull final Result element : inObj) if (element.isAlive()) {
                return true;
            }
            return false;
        }
    };
}
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) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) TensorList(com.simiacryptus.mindseye.lang.TensorList) Map(java.util.Map) 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) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) Nullable(javax.annotation.Nullable) Result(com.simiacryptus.mindseye.lang.Result) Nonnull(javax.annotation.Nonnull)

Example 35 with TensorArray

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

the class EntropyLossLayer method eval.

@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    final double zero_tol = 1e-12;
    final Result in0 = inObj[0];
    TensorList indata = in0.getData();
    indata.addRef();
    @Nonnull final Tensor[] gradient = new Tensor[indata.length()];
    final double max_prob = 1.;
    return new Result(TensorArray.wrap(IntStream.range(0, indata.length()).mapToObj(dataIndex -> {
        @Nullable final Tensor l = indata.get(dataIndex);
        @Nullable final Tensor r = inObj[1].getData().get(dataIndex);
        assert l.length() == r.length() : l.length() + " != " + r.length();
        @Nonnull final Tensor gradientTensor = new Tensor(l.getDimensions());
        @Nullable final double[] gradientData = gradientTensor.getData();
        double total = 0;
        @Nullable final double[] ld = l.getData();
        @Nullable final double[] rd = r.getData();
        for (int i = 0; i < l.length(); i++) {
            final double lv = Math.max(Math.min(ld[i], max_prob), zero_tol);
            final double rv = rd[i];
            if (rv > 0) {
                gradientData[i] = -rv / lv;
                total += -rv * Math.log(lv);
            } else {
                gradientData[i] = 0;
            }
        }
        l.freeRef();
        r.freeRef();
        assert total >= 0;
        gradient[dataIndex] = gradientTensor;
        @Nonnull final Tensor outValue = new Tensor(new double[] { total }, 1);
        return outValue;
    }).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
        if (inObj[1].isAlive()) {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
                Tensor deltaTensor = delta.get(dataIndex);
                @Nullable final Tensor inputTensor = indata.get(dataIndex);
                @Nonnull final Tensor passback = new Tensor(gradient[dataIndex].getDimensions());
                for (int i = 0; i < passback.length(); i++) {
                    final double lv = Math.max(Math.min(inputTensor.get(i), max_prob), zero_tol);
                    passback.set(i, -deltaTensor.get(0) * Math.log(lv));
                }
                inputTensor.freeRef();
                deltaTensor.freeRef();
                return passback;
            }).toArray(i -> new Tensor[i]));
            inObj[1].accumulate(buffer, tensorArray);
        }
        if (in0.isAlive()) {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
                Tensor tensor = delta.get(dataIndex);
                @Nonnull final Tensor passback = new Tensor(gradient[dataIndex].getDimensions());
                for (int i = 0; i < passback.length(); i++) {
                    passback.set(i, tensor.get(0) * gradient[dataIndex].get(i));
                }
                tensor.freeRef();
                return passback;
            }).toArray(i -> new Tensor[i]));
            in0.accumulate(buffer, tensorArray);
        }
    }) {

        @Override
        protected void _free() {
            indata.freeRef();
            Arrays.stream(gradient).forEach(ReferenceCounting::freeRef);
            Arrays.stream(inObj).forEach(ReferenceCounting::freeRef);
        }

        @Override
        public boolean isAlive() {
            return in0.isAlive() || in0.isAlive();
        }
    };
}
Also used : IntStream(java.util.stream.IntStream) JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Tensor(com.simiacryptus.mindseye.lang.Tensor) Result(com.simiacryptus.mindseye.lang.Result) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) TensorList(com.simiacryptus.mindseye.lang.TensorList) Map(java.util.Map) Layer(com.simiacryptus.mindseye.lang.Layer) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Nonnull(javax.annotation.Nonnull) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) Nullable(javax.annotation.Nullable) Result(com.simiacryptus.mindseye.lang.Result) Nonnull(javax.annotation.Nonnull)

Aggregations

TensorArray (com.simiacryptus.mindseye.lang.TensorArray)54 Nonnull (javax.annotation.Nonnull)52 Tensor (com.simiacryptus.mindseye.lang.Tensor)51 Nullable (javax.annotation.Nullable)49 DeltaSet (com.simiacryptus.mindseye.lang.DeltaSet)47 Layer (com.simiacryptus.mindseye.lang.Layer)47 Result (com.simiacryptus.mindseye.lang.Result)47 TensorList (com.simiacryptus.mindseye.lang.TensorList)47 Arrays (java.util.Arrays)46 IntStream (java.util.stream.IntStream)46 List (java.util.List)43 JsonObject (com.google.gson.JsonObject)36 LayerBase (com.simiacryptus.mindseye.lang.LayerBase)36 Map (java.util.Map)36 DataSerializer (com.simiacryptus.mindseye.lang.DataSerializer)35 Logger (org.slf4j.Logger)34 LoggerFactory (org.slf4j.LoggerFactory)34 Collectors (java.util.stream.Collectors)11 ConstantResult (com.simiacryptus.mindseye.lang.ConstantResult)8 Coordinate (com.simiacryptus.mindseye.lang.Coordinate)8