Search in sources :

Example 6 with TensorArray

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

the class HyperbolicActivationLayer method eval.

@Nonnull
@Override
public Result eval(final Result... inObj) {
    final TensorList indata = inObj[0].getData();
    indata.addRef();
    inObj[0].addRef();
    weights.addRef();
    HyperbolicActivationLayer.this.addRef();
    final int itemCnt = indata.length();
    return new Result(TensorArray.wrap(IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
        @Nullable final Tensor input = indata.get(dataIndex);
        @Nullable Tensor map = input.map(v -> {
            final int sign = v < 0 ? negativeMode : 1;
            final double a = Math.max(0, weights.get(v < 0 ? 1 : 0));
            return sign * (Math.sqrt(Math.pow(a * v, 2) + 1) - a) / a;
        });
        input.freeRef();
        return map;
    }).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
        if (!isFrozen()) {
            IntStream.range(0, delta.length()).forEach(dataIndex -> {
                @Nullable Tensor deltaI = delta.get(dataIndex);
                @Nullable Tensor inputI = indata.get(dataIndex);
                @Nullable final double[] deltaData = deltaI.getData();
                @Nullable final double[] inputData = inputI.getData();
                @Nonnull final Tensor weightDelta = new Tensor(weights.getDimensions());
                for (int i = 0; i < deltaData.length; i++) {
                    final double d = deltaData[i];
                    final double x = inputData[i];
                    final int sign = x < 0 ? negativeMode : 1;
                    final double a = Math.max(0, weights.getData()[x < 0 ? 1 : 0]);
                    weightDelta.add(x < 0 ? 1 : 0, -sign * d / (a * a * Math.sqrt(1 + Math.pow(a * x, 2))));
                }
                deltaI.freeRef();
                inputI.freeRef();
                buffer.get(HyperbolicActivationLayer.this, weights.getData()).addInPlace(weightDelta.getData()).freeRef();
                weightDelta.freeRef();
            });
        }
        if (inObj[0].isAlive()) {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
                @Nullable Tensor inputTensor = indata.get(dataIndex);
                Tensor deltaTensor = delta.get(dataIndex);
                @Nullable final double[] deltaData = deltaTensor.getData();
                @Nonnull final int[] dims = indata.getDimensions();
                @Nonnull final Tensor passback = new Tensor(dims);
                for (int i = 0; i < passback.length(); i++) {
                    final double x = inputTensor.getData()[i];
                    final double d = deltaData[i];
                    final int sign = x < 0 ? negativeMode : 1;
                    final double a = Math.max(0, weights.getData()[x < 0 ? 1 : 0]);
                    passback.set(i, sign * d * a * x / Math.sqrt(1 + a * x * a * x));
                }
                deltaTensor.freeRef();
                inputTensor.freeRef();
                return passback;
            }).toArray(i -> new Tensor[i]));
            inObj[0].accumulate(buffer, tensorArray);
        }
    }) {

        @Override
        protected void _free() {
            indata.freeRef();
            inObj[0].freeRef();
            weights.freeRef();
            HyperbolicActivationLayer.this.freeRef();
        }

        @Override
        public boolean isAlive() {
            return inObj[0].isAlive() || !isFrozen();
        }
    };
}
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) 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 7 with TensorArray

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

the class ImgBandScaleLayer method eval.

/**
 * Eval nn result.
 *
 * @param input the input
 * @return the nn result
 */
@Nonnull
public Result eval(@Nonnull final Result input) {
    @Nullable final double[] weights = getWeights();
    final TensorList inData = input.getData();
    inData.addRef();
    input.addRef();
    @Nullable Function<Tensor, Tensor> tensorTensorFunction = tensor -> {
        if (tensor.getDimensions().length != 3) {
            throw new IllegalArgumentException(Arrays.toString(tensor.getDimensions()));
        }
        if (tensor.getDimensions()[2] != weights.length) {
            throw new IllegalArgumentException(String.format("%s: %s does not have %s bands", getName(), Arrays.toString(tensor.getDimensions()), weights.length));
        }
        @Nullable Tensor tensor1 = tensor.mapCoords(c -> tensor.get(c) * weights[c.getCoords()[2]]);
        tensor.freeRef();
        return tensor1;
    };
    Tensor[] data = inData.stream().parallel().map(tensorTensorFunction).toArray(i -> new Tensor[i]);
    return new Result(TensorArray.wrap(data), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
        if (!isFrozen()) {
            final Delta<Layer> deltaBuffer = buffer.get(ImgBandScaleLayer.this, weights);
            IntStream.range(0, delta.length()).forEach(index -> {
                @Nonnull int[] dimensions = delta.getDimensions();
                int z = dimensions[2];
                int y = dimensions[1];
                int x = dimensions[0];
                final double[] array = RecycleBin.DOUBLES.obtain(z);
                Tensor deltaTensor = delta.get(index);
                @Nullable final double[] deltaArray = deltaTensor.getData();
                Tensor inputTensor = inData.get(index);
                @Nullable final double[] inputData = inputTensor.getData();
                for (int i = 0; i < z; i++) {
                    for (int j = 0; j < y * x; j++) {
                        // array[i] += deltaArray[i + z * j];
                        array[i] += deltaArray[i * x * y + j] * inputData[i * x * y + j];
                    }
                }
                inputTensor.freeRef();
                deltaTensor.freeRef();
                assert Arrays.stream(array).allMatch(v -> Double.isFinite(v));
                deltaBuffer.addInPlace(array);
                RecycleBin.DOUBLES.recycle(array, array.length);
            });
            deltaBuffer.freeRef();
        }
        if (input.isAlive()) {
            Tensor[] tensors = delta.stream().map(t -> {
                @Nullable Tensor tensor = t.mapCoords((c) -> t.get(c) * weights[c.getCoords()[2]]);
                t.freeRef();
                return tensor;
            }).toArray(i -> new Tensor[i]);
            @Nonnull TensorArray tensorArray = TensorArray.wrap(tensors);
            input.accumulate(buffer, tensorArray);
        }
    }) {

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

        @Override
        public boolean isAlive() {
            return input.isAlive() || !isFrozen();
        }
    };
}
Also used : IntStream(java.util.stream.IntStream) 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) Function(java.util.function.Function) 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) Result(com.simiacryptus.mindseye.lang.Result) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 8 with TensorArray

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

the class ImgConcatLayer method eval.

@Nullable
@Override
public Result eval(@Nonnull final Result... inObj) {
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    assert Arrays.stream(inObj).allMatch(x -> x.getData().getDimensions().length == 3) : "This component is for use mapCoords 3d image tensors only";
    final int numBatches = inObj[0].getData().length();
    assert Arrays.stream(inObj).allMatch(x -> x.getData().length() == numBatches) : "All inputs must use same batch size";
    @Nonnull final int[] outputDims = Arrays.copyOf(inObj[0].getData().getDimensions(), 3);
    outputDims[2] = Arrays.stream(inObj).mapToInt(x -> x.getData().getDimensions()[2]).sum();
    if (maxBands > 0)
        outputDims[2] = Math.min(maxBands, outputDims[2]);
    assert Arrays.stream(inObj).allMatch(x -> x.getData().getDimensions()[0] == outputDims[0]) : "Inputs must be same size";
    assert Arrays.stream(inObj).allMatch(x -> x.getData().getDimensions()[1] == outputDims[1]) : "Inputs must be same size";
    @Nonnull final List<Tensor> outputTensors = new ArrayList<>();
    for (int b = 0; b < numBatches; b++) {
        @Nonnull final Tensor outputTensor = new Tensor(outputDims);
        int pos = 0;
        @Nullable final double[] outputTensorData = outputTensor.getData();
        for (int i = 0; i < inObj.length; i++) {
            @Nullable Tensor tensor = inObj[i].getData().get(b);
            @Nullable final double[] data = tensor.getData();
            System.arraycopy(data, 0, outputTensorData, pos, Math.min(data.length, outputTensorData.length - pos));
            pos += data.length;
            tensor.freeRef();
        }
        outputTensors.add(outputTensor);
    }
    return new Result(TensorArray.wrap(outputTensors.toArray(new Tensor[] {})), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
        assert numBatches == data.length();
        @Nonnull final List<Tensor[]> splitBatches = new ArrayList<>();
        for (int b = 0; b < numBatches; b++) {
            @Nullable final Tensor tensor = data.get(b);
            @Nonnull final Tensor[] outputTensors2 = new Tensor[inObj.length];
            int pos = 0;
            for (int i = 0; i < inObj.length; i++) {
                @Nonnull final Tensor dest = new Tensor(inObj[i].getData().getDimensions());
                @Nullable double[] tensorData = tensor.getData();
                System.arraycopy(tensorData, pos, dest.getData(), 0, Math.min(dest.length(), tensorData.length - pos));
                pos += dest.length();
                outputTensors2[i] = dest;
            }
            tensor.freeRef();
            splitBatches.add(outputTensors2);
        }
        @Nonnull final Tensor[][] splitData = new Tensor[inObj.length][];
        for (int i = 0; i < splitData.length; i++) {
            splitData[i] = new Tensor[numBatches];
        }
        for (int i = 0; i < inObj.length; i++) {
            for (int b = 0; b < numBatches; b++) {
                splitData[i][b] = splitBatches.get(b)[i];
            }
        }
        for (int i = 0; i < inObj.length; i++) {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(splitData[i]);
            inObj[i].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 : Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) Result(com.simiacryptus.mindseye.lang.Result) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nullable(javax.annotation.Nullable) Nullable(javax.annotation.Nullable)

Example 9 with TensorArray

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

the class AvgMetaLayer method eval.

@Nonnull
@Override
public Result eval(final Result... inObj) {
    final Result input = inObj[0];
    input.addRef();
    TensorList inputData = input.getData();
    final int itemCnt = inputData.length();
    @Nullable Tensor thisResult;
    boolean passback;
    if (null == lastResult || inputData.length() > minBatchCount) {
        @Nonnull final ToDoubleFunction<Coordinate> f = (c) -> IntStream.range(0, itemCnt).mapToDouble(dataIndex -> {
            Tensor tensor = inputData.get(dataIndex);
            double v = tensor.get(c);
            tensor.freeRef();
            return v;
        }).sum() / itemCnt;
        Tensor tensor = inputData.get(0);
        thisResult = tensor.mapCoords(f);
        tensor.freeRef();
        passback = true;
        if (null != lastResult)
            lastResult.freeRef();
        lastResult = thisResult;
        lastResult.addRef();
    } else {
        passback = false;
        thisResult = lastResult;
        thisResult.freeRef();
    }
    return new Result(TensorArray.create(thisResult), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
        if (passback && input.isAlive()) {
            @Nullable final Tensor delta = data.get(0);
            @Nonnull final Tensor[] feedback = new Tensor[itemCnt];
            Arrays.parallelSetAll(feedback, i -> new Tensor(delta.getDimensions()));
            thisResult.coordStream(true).forEach((inputCoord) -> {
                for (int inputItem = 0; inputItem < itemCnt; inputItem++) {
                    feedback[inputItem].add(inputCoord, delta.get(inputCoord) / itemCnt);
                }
            });
            delta.freeRef();
            @Nonnull TensorArray tensorArray = TensorArray.wrap(feedback);
            input.accumulate(buffer, tensorArray);
        }
    }) {

        @Override
        public boolean isAlive() {
            return input.isAlive();
        }

        @Override
        protected void _free() {
            thisResult.freeRef();
            input.freeRef();
        }
    };
}
Also used : IntStream(java.util.stream.IntStream) JsonObject(com.google.gson.JsonObject) Coordinate(com.simiacryptus.mindseye.lang.Coordinate) 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) ToDoubleFunction(java.util.function.ToDoubleFunction) 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) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) Result(com.simiacryptus.mindseye.lang.Result) Coordinate(com.simiacryptus.mindseye.lang.Coordinate) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 10 with TensorArray

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

the class CudaTensorList method getHeapCopy.

/**
 * Gets heap copy.
 *
 * @return the heap copy
 */
@Nullable
public TensorArray getHeapCopy() {
    @Nullable TensorArray tensorList = heapCopy();
    tensorList.addRef();
    return tensorList;
}
Also used : TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nullable(javax.annotation.Nullable) Nullable(javax.annotation.Nullable)

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