Search in sources :

Example 31 with ReferenceCounting

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

the class SoftmaxActivationLayer method eval.

@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
    final int itemCnt = inObj[0].getData().length();
    @Nonnull final double[] sumA = new double[itemCnt];
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    @Nonnull final Tensor[] expA = new Tensor[itemCnt];
    final Tensor[] outputA = IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
        @Nullable final Tensor input = inObj[0].getData().get(dataIndex);
        assert 1 < input.length() : "input.length() = " + input.length();
        @Nullable final Tensor exp;
        final DoubleSummaryStatistics summaryStatistics = DoubleStream.of(input.getData()).filter(x -> Double.isFinite(x)).summaryStatistics();
        final double max = summaryStatistics.getMax();
        // final double min = summaryStatistics.getMin();
        exp = input.map(x -> {
            double xx = Math.exp(x - max);
            return Double.isFinite(xx) ? xx : 0;
        });
        input.freeRef();
        assert Arrays.stream(exp.getData()).allMatch(Double::isFinite);
        assert Arrays.stream(exp.getData()).allMatch(v -> v >= 0);
        // assert exp.sum() > 0;
        final double sum = 0 < exp.sum() ? exp.sum() : 1;
        assert Double.isFinite(sum);
        expA[dataIndex] = exp;
        sumA[dataIndex] = sum;
        @Nullable Tensor result = exp.map(x -> x / sum);
        return result;
    }).toArray(i -> new Tensor[i]);
    assert Arrays.stream(outputA).flatMapToDouble(x -> Arrays.stream(x.getData())).allMatch(v -> Double.isFinite(v));
    return new Result(TensorArray.wrap(outputA), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
        if (inObj[0].isAlive()) {
            final Tensor[] passbackA = IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
                Tensor deltaTensor = data.get(dataIndex);
                @Nullable final double[] delta = deltaTensor.getData();
                @Nullable final double[] expdata = expA[dataIndex].getData();
                @Nonnull final Tensor passback = new Tensor(data.getDimensions());
                final int dim = expdata.length;
                double dot = 0;
                for (int i = 0; i < expdata.length; i++) {
                    dot += delta[i] * expdata[i];
                }
                final double sum = sumA[dataIndex];
                for (int i = 0; i < dim; i++) {
                    double value = 0;
                    value = (sum * delta[i] - dot) * expdata[i] / (sum * sum);
                    passback.set(i, value);
                }
                deltaTensor.freeRef();
                return passback;
            }).toArray(i -> new Tensor[i]);
            assert Arrays.stream(passbackA).flatMapToDouble(x -> Arrays.stream(x.getData())).allMatch(v -> Double.isFinite(v));
            @Nonnull TensorArray tensorArray = TensorArray.wrap(passbackA);
            inObj[0].accumulate(buffer, tensorArray);
        }
    }) {

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

        @Override
        public boolean isAlive() {
            return inObj[0].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) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) Result(com.simiacryptus.mindseye.lang.Result) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) DoubleStream(java.util.stream.DoubleStream) 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) Nonnull(javax.annotation.Nonnull) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) TensorList(com.simiacryptus.mindseye.lang.TensorList) Result(com.simiacryptus.mindseye.lang.Result) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nonnull(javax.annotation.Nonnull)

Example 32 with ReferenceCounting

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

the class NthPowerActivationLayer method eval.

@Override
public Result eval(@Nonnull final Result... inObj) {
    final int itemCnt = inObj[0].getData().length();
    assert 0 < itemCnt;
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    @Nonnull final Tensor[] inputGradientA = new Tensor[itemCnt];
    return new Result(TensorArray.wrap(IntStream.range(0, itemCnt).parallel().mapToObj(dataIndex -> {
        @Nullable final Tensor input = inObj[0].getData().get(dataIndex);
        @Nonnull final Tensor output = new Tensor(inObj[0].getData().getDimensions());
        @Nonnull final Tensor gradient = new Tensor(input.length());
        @Nullable final double[] inputData = input.getData();
        @Nullable final double[] gradientData = gradient.getData();
        @Nullable final double[] outputData = output.getData();
        inputGradientA[dataIndex] = gradient;
        if (power == 2) {
            NthPowerActivationLayer.square(input, inputData, gradientData, outputData);
        } else if (power == 0.5) {
            NthPowerActivationLayer.squareRoot(input, inputData, gradientData, outputData);
        } else if (power == 0.0) {
            NthPowerActivationLayer.unity(input, inputData, gradientData, outputData);
        } else {
            NthPowerActivationLayer.nthPower(power, input, inputData, gradientData, outputData);
        }
        input.freeRef();
        return output;
    }).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
        if (inObj[0].isAlive()) {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, itemCnt).parallel().mapToObj(dataIndex -> {
                @Nonnull final Tensor passback = new Tensor(data.getDimensions());
                @Nullable final Tensor tensor = data.get(dataIndex);
                @Nullable double[] tensorData = tensor.getData();
                @Nullable final double[] gradientData = inputGradientA[dataIndex].getData();
                IntStream.range(0, passback.length()).forEach(i -> {
                    final double v = gradientData[i];
                    if (Double.isFinite(v)) {
                        passback.set(i, tensorData[i] * v);
                    }
                });
                tensor.freeRef();
                return passback;
            }).toArray(i -> new Tensor[i]));
            inObj[0].accumulate(buffer, tensorArray);
        }
    }) {

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

        @Override
        public boolean isAlive() {
            return 0.0 != power && inObj[0].isAlive();
        }
    };
}
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) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) 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) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) Nullable(javax.annotation.Nullable)

Aggregations

ReferenceCounting (com.simiacryptus.mindseye.lang.ReferenceCounting)32 Nonnull (javax.annotation.Nonnull)32 Nullable (javax.annotation.Nullable)30 TensorList (com.simiacryptus.mindseye.lang.TensorList)28 Layer (com.simiacryptus.mindseye.lang.Layer)27 Result (com.simiacryptus.mindseye.lang.Result)26 Arrays (java.util.Arrays)26 Map (java.util.Map)24 DeltaSet (com.simiacryptus.mindseye.lang.DeltaSet)23 List (java.util.List)23 JsonObject (com.google.gson.JsonObject)22 DataSerializer (com.simiacryptus.mindseye.lang.DataSerializer)21 LayerBase (com.simiacryptus.mindseye.lang.LayerBase)21 Tensor (com.simiacryptus.mindseye.lang.Tensor)20 CudaDevice (com.simiacryptus.mindseye.lang.cudnn.CudaDevice)20 CudaMemory (com.simiacryptus.mindseye.lang.cudnn.CudaMemory)20 IntStream (java.util.stream.IntStream)20 CudaTensor (com.simiacryptus.mindseye.lang.cudnn.CudaTensor)19 CudaTensorList (com.simiacryptus.mindseye.lang.cudnn.CudaTensorList)18 Stream (java.util.stream.Stream)18