Search in sources :

Example 1 with Coordinate

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

the class MaxDropoutNoiseLayer method getCellMap.

private List<List<Coordinate>> getCellMap(@Nonnull final IntArray dims) {
    Tensor tensor = new Tensor(dims.data);
    ArrayList<List<Coordinate>> lists = new ArrayList<>(tensor.coordStream(true).collect(Collectors.groupingBy((@Nonnull final Coordinate c) -> {
        int cellId = 0;
        int max = 0;
        for (int dim = 0; dim < dims.size(); dim++) {
            final int pos = c.getCoords()[dim] / kernelSize[dim];
            cellId = cellId * max + pos;
            max = dims.get(dim) / kernelSize[dim];
        }
        return cellId;
    })).values());
    tensor.freeRef();
    return lists;
}
Also used : Tensor(com.simiacryptus.mindseye.lang.Tensor) Coordinate(com.simiacryptus.mindseye.lang.Coordinate) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TensorList(com.simiacryptus.mindseye.lang.TensorList)

Example 2 with Coordinate

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

the class MaxImageBandLayer method eval.

@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
    assert 1 == inObj.length;
    final TensorList inputData = inObj[0].getData();
    inputData.addRef();
    inputData.length();
    @Nonnull final int[] inputDims = inputData.getDimensions();
    assert 3 == inputDims.length;
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    final Coordinate[][] maxCoords = inputData.stream().map(data -> {
        Coordinate[] coordinates = IntStream.range(0, inputDims[2]).mapToObj(band -> {
            return data.coordStream(true).filter(e -> e.getCoords()[2] == band).max(Comparator.comparing(c -> data.get(c))).get();
        }).toArray(i -> new Coordinate[i]);
        data.freeRef();
        return coordinates;
    }).toArray(i -> new Coordinate[i][]);
    return new Result(TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(dataIndex -> {
        Tensor tensor = inputData.get(dataIndex);
        final DoubleStream doubleStream = IntStream.range(0, inputDims[2]).mapToDouble(band -> {
            final int[] maxCoord = maxCoords[dataIndex][band].getCoords();
            double v = tensor.get(maxCoord[0], maxCoord[1], band);
            return v;
        });
        Tensor tensor1 = new Tensor(1, 1, inputDims[2]).set(Tensor.getDoubles(doubleStream, inputDims[2]));
        tensor.freeRef();
        return tensor1;
    }).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
        if (inObj[0].isAlive()) {
            @Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).parallel().mapToObj(dataIndex -> {
                Tensor deltaTensor = delta.get(dataIndex);
                @Nonnull final Tensor passback = new Tensor(inputData.getDimensions());
                IntStream.range(0, inputDims[2]).forEach(b -> {
                    final int[] maxCoord = maxCoords[dataIndex][b].getCoords();
                    passback.set(new int[] { maxCoord[0], maxCoord[1], b }, deltaTensor.get(0, 0, b));
                });
                deltaTensor.freeRef();
                return passback;
            }).toArray(i -> new Tensor[i]));
            inObj[0].accumulate(buffer, tensorArray);
        }
    }) {

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

        @Override
        public boolean isAlive() {
            return inObj[0].isAlive();
        }
    };
}
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) DoubleStream(java.util.stream.DoubleStream) JsonUtil(com.simiacryptus.util.io.JsonUtil) 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) Comparator(java.util.Comparator) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tensor(com.simiacryptus.mindseye.lang.Tensor) Nonnull(javax.annotation.Nonnull) Coordinate(com.simiacryptus.mindseye.lang.Coordinate) TensorArray(com.simiacryptus.mindseye.lang.TensorArray) DoubleStream(java.util.stream.DoubleStream) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) TensorList(com.simiacryptus.mindseye.lang.TensorList) Result(com.simiacryptus.mindseye.lang.Result) Nonnull(javax.annotation.Nonnull)

Example 3 with Coordinate

use of com.simiacryptus.mindseye.lang.Coordinate 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 4 with Coordinate

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

the class BiasMetaLayer method eval.

@Nullable
@Override
public Result eval(@Nonnull final Result... inObj) {
    final int itemCnt = inObj[0].getData().length();
    Tensor tensor1 = inObj[1].getData().get(0);
    final Tensor[] tensors = IntStream.range(0, itemCnt).parallel().mapToObj(dataIndex -> {
        Tensor tensor = inObj[0].getData().get(dataIndex);
        Tensor mapIndex = tensor.mapIndex((v, c) -> {
            return v + tensor1.get(c);
        });
        tensor.freeRef();
        return mapIndex;
    }).toArray(i -> new Tensor[i]);
    tensor1.freeRef();
    Tensor tensor0 = tensors[0];
    tensor0.addRef();
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    return new Result(TensorArray.wrap(tensors), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
        if (inObj[0].isAlive()) {
            data.addRef();
            inObj[0].accumulate(buffer, data);
        }
        if (inObj[1].isAlive()) {
            @Nonnull final ToDoubleFunction<Coordinate> f = (c) -> {
                return IntStream.range(0, itemCnt).mapToDouble(i -> {
                    Tensor tensor = data.get(i);
                    double v = tensor.get(c);
                    tensor.freeRef();
                    return v;
                }).sum();
            };
            @Nullable final Tensor passback = tensor0.mapCoords(f);
            @Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, inObj[1].getData().length()).mapToObj(i -> {
                if (i == 0)
                    return passback;
                else {
                    @Nullable Tensor map = passback.map(v -> 0);
                    passback.freeRef();
                    return map;
                }
            }).toArray(i -> new Tensor[i]));
            inObj[1].accumulate(buffer, tensorArray);
        }
    }) {

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

        @Override
        public boolean isAlive() {
            return inObj[0].isAlive() || inObj[1].isAlive();
        }
    };
}
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) Nullable(javax.annotation.Nullable)

Example 5 with Coordinate

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

the class SumMetaLayer method eval.

@Nullable
@Override
public Result eval(@Nonnull final Result... inObj) {
    final Result input = inObj[0];
    Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
    final int itemCnt = input.getData().length();
    if (null == lastResult || minBatches < itemCnt) {
        @Nonnull final ToDoubleFunction<Coordinate> f = (c) -> IntStream.range(0, itemCnt).mapToDouble(dataIndex -> input.getData().get(dataIndex).get(c)).sum();
        lastResult = input.getData().get(0).mapCoords(f);
    }
    return new Result(TensorArray.wrap(lastResult), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
        if (input.isAlive()) {
            @Nullable final Tensor delta = data.get(0);
            @Nonnull final Tensor[] feedback = new Tensor[itemCnt];
            Arrays.parallelSetAll(feedback, i -> new Tensor(delta.getDimensions()));
            @Nonnull final ToDoubleFunction<Coordinate> f = (inputCoord) -> {
                for (int inputItem = 0; inputItem < itemCnt; inputItem++) {
                    feedback[inputItem].add(inputCoord, delta.get(inputCoord));
                }
                return 0;
            };
            delta.mapCoords(f);
            @Nonnull TensorArray tensorArray = TensorArray.wrap(feedback);
            input.accumulate(buffer, tensorArray);
        }
    }) {

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

        @Override
        public boolean isAlive() {
            return input.isAlive();
        }
    };
}
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) Coordinate(com.simiacryptus.mindseye.lang.Coordinate) 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) Nullable(javax.annotation.Nullable)

Aggregations

Coordinate (com.simiacryptus.mindseye.lang.Coordinate)7 Tensor (com.simiacryptus.mindseye.lang.Tensor)7 TensorList (com.simiacryptus.mindseye.lang.TensorList)7 List (java.util.List)7 JsonObject (com.google.gson.JsonObject)6 DataSerializer (com.simiacryptus.mindseye.lang.DataSerializer)6 DeltaSet (com.simiacryptus.mindseye.lang.DeltaSet)6 Layer (com.simiacryptus.mindseye.lang.Layer)6 LayerBase (com.simiacryptus.mindseye.lang.LayerBase)6 Result (com.simiacryptus.mindseye.lang.Result)6 TensorArray (com.simiacryptus.mindseye.lang.TensorArray)6 Arrays (java.util.Arrays)6 Map (java.util.Map)6 IntStream (java.util.stream.IntStream)6 Nonnull (javax.annotation.Nonnull)6 Nullable (javax.annotation.Nullable)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 JsonUtil (com.simiacryptus.util.io.JsonUtil)3 ToDoubleFunction (java.util.function.ToDoubleFunction)3