use of com.simiacryptus.util.data.IntArray in project MindsEye by SimiaCryptus.
the class MaxDropoutNoiseLayer method eval.
@Nonnull
@Override
public Result eval(final Result... inObj) {
final Result in0 = inObj[0];
final TensorList data0 = in0.getData();
final int itemCnt = data0.length();
in0.addRef();
data0.addRef();
final Tensor[] mask = IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
@Nullable final Tensor input = data0.get(dataIndex);
@Nullable final Tensor output = input.map(x -> 0);
final List<List<Coordinate>> cells = getCellMap_cached.apply(new IntArray(output.getDimensions()));
cells.forEach(cell -> {
output.set(cell.stream().max(Comparator.comparingDouble(c -> input.get(c))).get(), 1);
});
input.freeRef();
return output;
}).toArray(i -> new Tensor[i]);
return new Result(TensorArray.wrap(IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
Tensor inputData = data0.get(dataIndex);
@Nullable final double[] input = inputData.getData();
@Nullable final double[] maskT = mask[dataIndex].getData();
@Nonnull final Tensor output = new Tensor(inputData.getDimensions());
@Nullable final double[] outputData = output.getData();
for (int i = 0; i < outputData.length; i++) {
outputData[i] = input[i] * maskT[i];
}
inputData.freeRef();
return output;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (in0.isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
Tensor deltaTensor = delta.get(dataIndex);
@Nullable final double[] deltaData = deltaTensor.getData();
@Nonnull final int[] dims = data0.getDimensions();
@Nullable final double[] maskData = mask[dataIndex].getData();
@Nonnull final Tensor passback = new Tensor(dims);
for (int i = 0; i < passback.length(); i++) {
passback.set(i, maskData[i] * deltaData[i]);
}
deltaTensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
in0.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
in0.freeRef();
data0.freeRef();
Arrays.stream(mask).forEach(ReferenceCounting::freeRef);
}
@Override
public boolean isAlive() {
return in0.isAlive() || !isFrozen();
}
};
}
Aggregations