Search in sources :

Example 91 with TensorList

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

the class SimpleGpuEval method getDelta.

/**
 * Gets delta.
 *
 * @param output the output
 * @return the delta
 */
@Nonnull
public TensorArray getDelta(final TensorList output) {
    return TensorArray.wrap(output.stream().map(t -> {
        @Nullable Tensor map = t.map(v -> 1.0);
        t.freeRef();
        return map;
    }).toArray(i -> new Tensor[i]));
}
Also used : Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) CudnnHandle(com.simiacryptus.mindseye.lang.cudnn.CudnnHandle) CudaTensor(com.simiacryptus.mindseye.lang.cudnn.CudaTensor) TensorList(com.simiacryptus.mindseye.lang.TensorList) MemoryType(com.simiacryptus.mindseye.lang.cudnn.MemoryType) Layer(com.simiacryptus.mindseye.lang.Layer) Tensor(com.simiacryptus.mindseye.lang.Tensor) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) CudaTensorList(com.simiacryptus.mindseye.lang.cudnn.CudaTensorList) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) CudaTensor(com.simiacryptus.mindseye.lang.cudnn.CudaTensor) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 92 with TensorList

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

the class ImgPixelSoftmaxLayer method eval.

/**
 * Eval nn result.
 *
 * @param input the input
 * @return the nn result
 */
@Nonnull
public Result eval(@Nonnull final Result input) {
    final TensorList inputData = input.getData();
    inputData.addRef();
    input.addRef();
    int[] inputDims = inputData.getDimensions();
    assert 3 == inputDims.length;
    final int inputBands = inputDims[2];
    final int width = inputDims[0];
    final int height = inputDims[1];
    TensorArray maxima = TensorArray.wrap(inputData.stream().map(inputTensor -> {
        try {
            return new Tensor(width, height, 1).setByCoord(c -> {
                return IntStream.range(0, inputBands).mapToDouble(band -> {
                    int[] coords = c.getCoords();
                    return inputTensor.get(coords[0], coords[1], band);
                }).max().getAsDouble();
            });
        } finally {
            inputTensor.freeRef();
        }
    }).toArray(i -> new Tensor[i]));
    TensorArray exps = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
        final Tensor inputTensor = inputData.get(index);
        Tensor maxTensor = maxima.get(index);
        try {
            return new Tensor(inputDims).setByCoord(c -> {
                int[] coords = c.getCoords();
                return Math.exp(inputTensor.get(c) - maxTensor.get(coords[0], coords[1], 0));
            });
        } finally {
            inputTensor.freeRef();
            maxTensor.freeRef();
        }
    }).toArray(i -> new Tensor[i]));
    maxima.freeRef();
    TensorArray sums = TensorArray.wrap(exps.stream().map(expTensor -> {
        try {
            return new Tensor(width, height, 1).setByCoord(c -> {
                return IntStream.range(0, inputBands).mapToDouble(band -> {
                    int[] coords = c.getCoords();
                    return expTensor.get(coords[0], coords[1], band);
                }).sum();
            });
        } finally {
            expTensor.freeRef();
        }
    }).toArray(i -> new Tensor[i]));
    TensorArray output = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
        Tensor sumTensor = sums.get(index);
        Tensor expTensor = exps.get(index);
        try {
            return new Tensor(inputDims).setByCoord(c -> {
                int[] coords = c.getCoords();
                return (expTensor.get(c) / sumTensor.get(coords[0], coords[1], 0));
            });
        } finally {
            sumTensor.freeRef();
            expTensor.freeRef();
        }
    }).toArray(i -> new Tensor[i]));
    return new Result(output, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
        if (input.isAlive()) {
            TensorArray dots = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
                final Tensor deltaTensor = delta.get(index);
                Tensor expTensor = exps.get(index);
                try {
                    return new Tensor(width, height, 1).setByCoord(c -> {
                        return IntStream.range(0, inputBands).mapToDouble(band -> {
                            int[] coords = c.getCoords();
                            return expTensor.get(coords[0], coords[1], band) * deltaTensor.get(coords[0], coords[1], band);
                        }).sum();
                    });
                } finally {
                    expTensor.freeRef();
                    deltaTensor.freeRef();
                }
            }).toArray(i -> new Tensor[i]));
            TensorArray passback = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
                final Tensor deltaTensor = delta.get(index);
                final Tensor expTensor = exps.get(index);
                Tensor sumTensor = sums.get(index);
                Tensor dotTensor = dots.get(index);
                try {
                    return new Tensor(inputDims).setByCoord(c -> {
                        int[] coords = c.getCoords();
                        double sum = sumTensor.get(coords[0], coords[1], 0);
                        double dot = dotTensor.get(coords[0], coords[1], 0);
                        double deltaValue = deltaTensor.get(c);
                        double expValue = expTensor.get(c);
                        return (sum * deltaValue - dot) * expValue / (sum * sum);
                    });
                } finally {
                    deltaTensor.freeRef();
                    expTensor.freeRef();
                    sumTensor.freeRef();
                    dotTensor.freeRef();
                }
            }).toArray(i -> new Tensor[i]));
            input.accumulate(buffer, passback);
            dots.freeRef();
        }
    }) {

        @Override
        protected void _free() {
            inputData.freeRef();
            input.freeRef();
            sums.freeRef();
            exps.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) 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) 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) Result(com.simiacryptus.mindseye.lang.Result) Nonnull(javax.annotation.Nonnull)

Example 93 with TensorList

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

the class ImgPixelSumLayer method evalAndFree.

/**
 * Eval nn result.
 *
 * @param input the input
 * @return the nn result
 */
@Nonnull
public Result evalAndFree(@Nonnull final Result input) {
    final TensorList inputData = input.getData();
    int[] inputDims = inputData.getDimensions();
    assert 3 == inputDims.length;
    return new Result(TensorArray.wrap(inputData.stream().map(tensor -> {
        Tensor result = new Tensor(inputDims[0], inputDims[1], 1).setByCoord(c -> {
            return IntStream.range(0, inputDims[2]).mapToDouble(i -> {
                int[] coords = c.getCoords();
                return tensor.get(coords[0], coords[1], i);
            }).sum();
        });
        tensor.freeRef();
        return result;
    }).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
        if (input.isAlive()) {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(delta.stream().map(deltaTensor -> {
                int[] deltaDims = deltaTensor.getDimensions();
                Tensor result = new Tensor(deltaDims[0], deltaDims[1], inputDims[2]).setByCoord(c -> {
                    int[] coords = c.getCoords();
                    return deltaTensor.get(coords[0], coords[1], 0);
                });
                deltaTensor.freeRef();
                return result;
            }).toArray(i -> new Tensor[i]));
            input.accumulate(buffer, tensorArray);
        }
    }) {

        @Override
        protected void _free() {
            inputData.freeRef();
            input.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) 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) 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) Result(com.simiacryptus.mindseye.lang.Result) Nonnull(javax.annotation.Nonnull)

Example 94 with TensorList

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

the class ImgReshapeLayer method eval.

@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
    // assert Arrays.stream(inObj).flatMapToDouble(input-> input.getData().stream().flatMapToDouble(x-> Arrays.stream(x.getData()))).allMatch(v->Double.isFinite(v));
    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;
    assert expand || 0 == inputDims[0] % kernelSizeX : (inputDims[0] + " % " + kernelSizeX);
    assert expand || 0 == inputDims[1] % kernelSizeY : (inputDims[1] + " % " + kernelSizeY);
    assert !expand || 0 == inputDims[2] % (kernelSizeX * kernelSizeY);
    // assert input.getData().stream().flatMapToDouble(x-> Arrays.stream(x.getData())).allMatch(v->Double.isFinite(v));
    Tensor outputDims;
    if (expand) {
        outputDims = new Tensor(inputDims[0] * kernelSizeX, inputDims[1] * kernelSizeY, inputDims[2] / (kernelSizeX * kernelSizeY));
    } else {
        outputDims = new Tensor(inputDims[0] / kernelSizeX, inputDims[1] / kernelSizeY, inputDims[2] * kernelSizeX * kernelSizeY);
    }
    TensorArray data = TensorArray.wrap(IntStream.range(0, batch.length()).parallel().mapToObj(dataIndex -> {
        Tensor inputData = batch.get(dataIndex);
        Tensor tensor = expand ? ImgReshapeLayer.copyExpand(inputData, outputDims.copy()) : ImgReshapeLayer.copyCondense(inputData, outputDims.copy());
        inputData.freeRef();
        return tensor;
    }).toArray(i -> new Tensor[i]));
    outputDims.freeRef();
    return new Result(data, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList error) -> {
        // assert error.stream().flatMapToDouble(x-> Arrays.stream(x.getData())).allMatch(v->Double.isFinite(v));
        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);
                Tensor tensor = expand ? ImgReshapeLayer.copyCondense(err, passback) : ImgReshapeLayer.copyExpand(err, passback);
                err.freeRef();
                return tensor;
            }).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 95 with TensorList

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

the class ImgTileSelectLayer 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;
    @Nonnull final int[] dimOut = getViewDimensions(inputDims, new int[] { sizeY, sizeX, inputDims[2] }, new int[] { positionX, positionY, 0 });
    return new Result(TensorArray.wrap(IntStream.range(0, batch.length()).parallel().mapToObj(dataIndex -> {
        @Nonnull final Tensor outputData = new Tensor(dimOut);
        Tensor inputData = batch.get(dataIndex);
        copy(inputData, outputData, positionX, positionY);
        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, -positionX, -positionY);
                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)

Aggregations

TensorList (com.simiacryptus.mindseye.lang.TensorList)110 Nonnull (javax.annotation.Nonnull)109 Nullable (javax.annotation.Nullable)103 Result (com.simiacryptus.mindseye.lang.Result)95 Arrays (java.util.Arrays)93 Layer (com.simiacryptus.mindseye.lang.Layer)91 Tensor (com.simiacryptus.mindseye.lang.Tensor)88 DeltaSet (com.simiacryptus.mindseye.lang.DeltaSet)87 IntStream (java.util.stream.IntStream)82 List (java.util.List)80 TensorArray (com.simiacryptus.mindseye.lang.TensorArray)76 Map (java.util.Map)68 JsonObject (com.google.gson.JsonObject)64 DataSerializer (com.simiacryptus.mindseye.lang.DataSerializer)63 LayerBase (com.simiacryptus.mindseye.lang.LayerBase)61 Logger (org.slf4j.Logger)57 LoggerFactory (org.slf4j.LoggerFactory)57 ReferenceCounting (com.simiacryptus.mindseye.lang.ReferenceCounting)33 CudaTensor (com.simiacryptus.mindseye.lang.cudnn.CudaTensor)30 CudaTensorList (com.simiacryptus.mindseye.lang.cudnn.CudaTensorList)30