use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class ImgBandBiasLayer method eval.
/**
* Eval nn result.
*
* @param input the input
* @return the nn result
*/
@Nonnull
public Result eval(@Nonnull final Result input) {
@Nullable final double[] bias = getBias();
input.addRef();
return new Result(TensorArray.wrap(input.getData().stream().parallel().map(r -> {
if (r.getDimensions().length != 3) {
throw new IllegalArgumentException(Arrays.toString(r.getDimensions()));
}
if (r.getDimensions()[2] != bias.length) {
throw new IllegalArgumentException(String.format("%s: %s does not have %s bands", getName(), Arrays.toString(r.getDimensions()), bias.length));
}
@Nonnull Tensor tensor = new Tensor(add(r.getData()), r.getDimensions());
r.freeRef();
return tensor;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (!isFrozen()) {
final Delta<Layer> deltaBuffer = buffer.get(ImgBandBiasLayer.this, bias);
data.stream().parallel().forEach(d -> {
final double[] array = RecycleBin.DOUBLES.obtain(bias.length);
@Nullable final double[] signal = d.getData();
final int size = signal.length / bias.length;
for (int i = 0; i < signal.length; i++) {
array[i / size] += signal[i];
if (!Double.isFinite(array[i / size])) {
array[i / size] = 0.0;
}
}
d.freeRef();
assert Arrays.stream(array).allMatch(v -> Double.isFinite(v));
deltaBuffer.addInPlace(array);
RecycleBin.DOUBLES.recycle(array, array.length);
});
deltaBuffer.freeRef();
}
if (input.isAlive()) {
data.addRef();
input.accumulate(buffer, data);
}
}) {
@Override
protected void _free() {
input.freeRef();
}
@Override
public boolean isAlive() {
return input.isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class ImgBandSelectLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
final Result input = inObj[0];
final TensorList batch = input.getData();
@Nonnull final int[] inputDims = batch.getDimensions();
assert 3 == inputDims.length;
@Nonnull final Tensor outputDims = new Tensor(inputDims[0], inputDims[1], bands.length);
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
@Nonnull TensorArray wrap = TensorArray.wrap(IntStream.range(0, batch.length()).parallel().mapToObj(dataIndex -> outputDims.mapCoords((c) -> {
int[] coords = c.getCoords();
@Nullable Tensor tensor = batch.get(dataIndex);
double v = tensor.get(coords[0], coords[1], bands[coords[2]]);
tensor.freeRef();
return v;
})).toArray(i -> new Tensor[i]));
outputDims.freeRef();
return new Result(wrap, (@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 -> {
@Nonnull final Tensor passback = new Tensor(inputDims);
@Nullable final Tensor err = error.get(dataIndex);
err.coordStream(false).forEach(c -> {
int[] coords = c.getCoords();
passback.set(coords[0], coords[1], bands[coords[2]], err.get(c));
});
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();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class ImgCropLayer 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;
return new Result(TensorArray.wrap(IntStream.range(0, batch.length()).parallel().mapToObj(dataIndex -> {
@Nonnull final Tensor outputData = new Tensor(sizeX, sizeY, inputDims[2]);
Tensor inputData = batch.get(dataIndex);
ImgCropLayer.copy(inputData, outputData);
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);
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();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class AvgReducerLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
Arrays.stream(inObj).forEach(x -> x.addRef());
Arrays.stream(inObj).forEach(x -> x.getData().addRef());
return new Result(TensorArray.wrap(IntStream.range(0, inObj[0].getData().length()).parallel().mapToDouble(dataIndex -> {
double sum = 0;
for (@Nonnull final Result element : inObj) {
Tensor tensor = element.getData().get(dataIndex);
@Nullable final double[] input = tensor.getData();
for (final double element2 : input) {
sum += element2 / input.length;
}
tensor.freeRef();
}
return sum;
}).mapToObj(x -> new Tensor(new double[] { x }, new int[] { 1 })).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
for (@Nonnull final Result in_l : inObj) {
if (in_l.isAlive()) {
TensorList inData = in_l.getData();
@Nonnull final TensorList tensorList = TensorArray.wrap(IntStream.range(0, inData.length()).parallel().mapToObj(dataIndex -> {
Tensor deltaTensor = delta.get(dataIndex);
final double deltaV = deltaTensor.get(0);
deltaTensor.freeRef();
@Nonnull final Tensor passback = new Tensor(inData.getDimensions());
final int dim = passback.length();
for (int i = 0; i < dim; i++) {
passback.set(i, deltaV / dim);
}
return passback;
}).toArray(i -> new Tensor[i]));
in_l.accumulate(buffer, tensorList);
}
}
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(ReferenceCounting::freeRef);
Arrays.stream(inObj).map(Result::getData).forEach(ReferenceCounting::freeRef);
}
@Override
public boolean isAlive() {
for (@Nonnull final Result element : inObj) if (element.isAlive()) {
return true;
}
return false;
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class BiasLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
TensorList input;
if (0 == inObj.length) {
input = TensorArray.create();
} else {
input = inObj[0].getData();
}
return new Result(TensorArray.wrap(input.stream().parallel().map(r -> {
@Nonnull Tensor tensor = new Tensor(add(r.getData()), r.getDimensions());
r.freeRef();
return tensor;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (!isFrozen()) {
final Delta<Layer> deltaBuffer = buffer.get(BiasLayer.this, bias);
if (1 == bias.length) {
delta.stream().parallel().forEach(d -> {
@Nullable final double[] array = d.getData();
deltaBuffer.addInPlace(1 == array.length ? array : new double[] { Arrays.stream(array).sum() });
d.freeRef();
});
} else {
delta.stream().parallel().forEach(d -> {
deltaBuffer.addInPlace(d.getData());
d.freeRef();
});
}
deltaBuffer.freeRef();
}
if (0 < inObj.length && inObj[0].isAlive()) {
delta.addRef();
inObj[0].accumulate(buffer, delta);
}
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
return 0 < inObj.length && inObj[0].isAlive() || !isFrozen();
}
};
}
Aggregations