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;
}
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();
}
};
}
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();
}
};
}
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();
}
};
}
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();
}
};
}
Aggregations