use of com.simiacryptus.mindseye.lang.Layer in project MindsEye by SimiaCryptus.
the class AvgPoolingLayer method eval.
@Nonnull
@SuppressWarnings("unchecked")
@Override
public Result eval(@Nonnull final Result... inObj) {
final int kernelSize = Tensor.length(kernelDims);
final TensorList data = inObj[0].getData();
@Nonnull final int[] inputDims = data.getDimensions();
final int[] newDims = IntStream.range(0, inputDims.length).map(i -> {
assert 0 == inputDims[i] % kernelDims[i] : inputDims[i] + ":" + kernelDims[i];
return inputDims[i] / kernelDims[i];
}).toArray();
final Map<Coordinate, List<int[]>> coordMap = AvgPoolingLayer.getCoordMap(kernelDims, newDims);
final Tensor[] outputValues = IntStream.range(0, data.length()).mapToObj(dataIndex -> {
@Nullable final Tensor input = data.get(dataIndex);
@Nonnull final Tensor output = new Tensor(newDims);
for (@Nonnull final Entry<Coordinate, List<int[]>> entry : coordMap.entrySet()) {
double sum = entry.getValue().stream().mapToDouble(inputCoord -> input.get(inputCoord)).sum();
if (Double.isFinite(sum)) {
output.add(entry.getKey(), sum / kernelSize);
}
}
input.freeRef();
return output;
}).toArray(i -> new Tensor[i]);
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
return new Result(TensorArray.wrap(outputValues), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (inObj[0].isAlive()) {
final Tensor[] passback = IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
@Nullable Tensor tensor = delta.get(dataIndex);
@Nonnull final Tensor backSignal = new Tensor(inputDims);
for (@Nonnull final Entry<Coordinate, List<int[]>> outputMapping : coordMap.entrySet()) {
final double outputValue = tensor.get(outputMapping.getKey());
for (@Nonnull final int[] inputCoord : outputMapping.getValue()) {
backSignal.add(inputCoord, outputValue / kernelSize);
}
}
tensor.freeRef();
return backSignal;
}).toArray(i -> new Tensor[i]);
@Nonnull TensorArray tensorArray = TensorArray.wrap(passback);
inObj[0].accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
return inObj[0].isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.Layer in project MindsEye by SimiaCryptus.
the class FullyConnectedReferenceLayer method eval.
@Nonnull
@Override
public Result eval(final Result... inObj) {
final Result inputResult = inObj[0];
final TensorList indata = inputResult.getData();
inputResult.addRef();
indata.addRef();
@Nonnull int[] inputDimensions = indata.getDimensions();
assert Tensor.length(inputDimensions) == Tensor.length(this.inputDims) : Arrays.toString(inputDimensions) + " == " + Arrays.toString(this.inputDims);
weights.addRef();
return new Result(TensorArray.wrap(IntStream.range(0, indata.length()).mapToObj(index -> {
@Nullable final Tensor input = indata.get(index);
@Nullable final Tensor output = new Tensor(outputDims);
weights.coordStream(false).forEach(c -> {
int[] coords = c.getCoords();
double prev = output.get(coords[1]);
double w = weights.get(c);
double i = input.get(coords[0]);
double value = prev + w * i;
output.set(coords[1], value);
});
input.freeRef();
return output;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (!isFrozen()) {
final Delta<Layer> deltaBuffer = buffer.get(FullyConnectedReferenceLayer.this, getWeights().getData());
Tensor[] array = IntStream.range(0, indata.length()).mapToObj(i -> {
@Nullable final Tensor inputTensor = indata.get(i);
@Nullable final Tensor deltaTensor = delta.get(i);
@Nonnull Tensor weights = new Tensor(FullyConnectedReferenceLayer.this.weights.getDimensions());
weights.coordStream(false).forEach(c -> {
int[] coords = c.getCoords();
weights.set(c, inputTensor.get(coords[0]) * deltaTensor.get(coords[1]));
});
inputTensor.freeRef();
deltaTensor.freeRef();
return weights;
}).toArray(i -> new Tensor[i]);
Tensor tensor = Arrays.stream(array).reduce((a, b) -> {
Tensor c = a.addAndFree(b);
b.freeRef();
return c;
}).get();
deltaBuffer.addInPlace(tensor.getData()).freeRef();
tensor.freeRef();
}
if (inputResult.isAlive()) {
@Nonnull final TensorList tensorList = TensorArray.wrap(IntStream.range(0, indata.length()).mapToObj(i -> {
@Nullable final Tensor inputTensor = new Tensor(inputDims);
@Nullable final Tensor deltaTensor = delta.get(i);
weights.coordStream(false).forEach(c -> {
int[] coords = c.getCoords();
inputTensor.set(coords[0], inputTensor.get(coords[0]) + weights.get(c) * deltaTensor.get(coords[1]));
});
deltaTensor.freeRef();
return inputTensor;
}).toArray(i -> new Tensor[i]));
inputResult.accumulate(buffer, tensorList);
}
}) {
@Override
protected void _free() {
indata.freeRef();
inputResult.freeRef();
weights.freeRef();
}
@Override
public boolean isAlive() {
return inputResult.isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.Layer in project MindsEye by SimiaCryptus.
the class GaussianNoiseLayer method eval.
@Nonnull
@Override
public Result eval(final Result... inObj) {
final Result in0 = inObj[0];
final TensorList inputData = in0.getData();
final int itemCnt = inputData.length();
in0.addRef();
inputData.addRef();
final Tensor[] outputA = IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
@Nonnull final Random random = new Random(seed);
@Nullable final Tensor input = inputData.get(dataIndex);
@Nullable final Tensor output = input.map(x -> {
return x + random.nextGaussian() * getValue();
});
input.freeRef();
return output;
}).toArray(i -> new Tensor[i]);
return new Result(TensorArray.wrap(outputA), (@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 tensor = delta.get(dataIndex);
@Nullable final double[] deltaData = tensor.getData();
@Nonnull final Tensor passback = new Tensor(inputData.getDimensions());
for (int i = 0; i < passback.length(); i++) {
passback.set(i, deltaData[i]);
}
tensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
in0.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
inputData.freeRef();
in0.freeRef();
}
@Override
public boolean isAlive() {
return in0.isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.Layer 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.Layer 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();
}
};
}
Aggregations