use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class ConvolutionLayer 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();
batch.addRef();
@Nonnull final int[] inputDims = batch.get(0).getDimensions();
@Nonnull final int[] kernelDims = kernel.getDimensions();
@Nullable final double[] kernelData = ConvolutionLayer.this.kernel.getData();
@Nonnull final ConvolutionController convolutionController = new ConvolutionController(inputDims, kernelDims, paddingX, paddingY);
final Tensor[] output = IntStream.range(0, batch.length()).mapToObj(dataIndex -> new Tensor(convolutionController.getOutputDims())).toArray(i -> new Tensor[i]);
try {
final double[][] inputBuffers = batch.stream().map(x -> {
@Nullable double[] data = x.getData();
x.detach();
return data;
}).toArray(i -> new double[i][]);
final double[][] outputBuffers = Arrays.stream(output).map(x -> x.getData()).toArray(i -> new double[i][]);
convolutionController.convolve(inputBuffers, kernelData, outputBuffers);
} catch (@Nonnull final Throwable e) {
throw new RuntimeException("Error mapCoords image res " + Arrays.toString(inputDims), e);
}
int outputLength = output.length;
return new Result(TensorArray.wrap(output), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList error) -> {
if (!isFrozen()) {
final double[][] inputBuffers = batch.stream().map(x -> {
@Nullable double[] data = x.getData();
x.freeRef();
return data;
}).toArray(i -> new double[i][]);
final double[][] outputBuffers = error.stream().map(x -> {
@Nullable double[] data = x.getData();
x.freeRef();
return data;
}).toArray(i -> new double[i][]);
@Nonnull final Tensor weightGradient = new Tensor(kernelDims);
convolutionController.gradient(inputBuffers, weightGradient.getData(), outputBuffers);
buffer.get(ConvolutionLayer.this, kernelData).addInPlace(weightGradient.getData()).freeRef();
weightGradient.freeRef();
}
if (input.isAlive()) {
final Tensor[] inputBufferTensors = IntStream.range(0, outputLength).mapToObj(dataIndex -> new Tensor(inputDims)).toArray(i -> new Tensor[i]);
final double[][] inputBuffers = Arrays.stream(inputBufferTensors).map(x -> {
@Nullable double[] data = x.getData();
return data;
}).toArray(i -> new double[i][]);
final double[][] outputBuffers = error.stream().map(x -> {
@Nullable double[] data = x.getData();
x.freeRef();
return data;
}).toArray(i -> new double[i][]);
convolutionController.backprop(inputBuffers, kernelData, outputBuffers);
@Nonnull TensorArray tensorArray = TensorArray.wrap(inputBufferTensors);
input.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
batch.freeRef();
}
@Override
public boolean isAlive() {
return input.isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorArray 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.TensorArray in project MindsEye by SimiaCryptus.
the class MaxPoolingLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
final Result in = inObj[0];
in.getData().length();
@Nonnull final int[] inputDims = in.getData().getDimensions();
final List<Tuple2<Integer, int[]>> regions = MaxPoolingLayer.calcRegionsCache.apply(new MaxPoolingLayer.CalcRegionsParameter(inputDims, kernelDims));
final Tensor[] outputA = IntStream.range(0, in.getData().length()).mapToObj(dataIndex -> {
final int[] newDims = IntStream.range(0, inputDims.length).map(i -> {
return (int) Math.ceil(inputDims[i] * 1.0 / kernelDims[i]);
}).toArray();
@Nonnull final Tensor output = new Tensor(newDims);
return output;
}).toArray(i -> new Tensor[i]);
Arrays.stream(outputA).mapToInt(x -> x.length()).sum();
@Nonnull final int[][] gradientMapA = new int[in.getData().length()][];
IntStream.range(0, in.getData().length()).forEach(dataIndex -> {
@Nullable final Tensor input = in.getData().get(dataIndex);
final Tensor output = outputA[dataIndex];
@Nonnull final IntToDoubleFunction keyExtractor = inputCoords -> input.get(inputCoords);
@Nonnull final int[] gradientMap = new int[input.length()];
regions.parallelStream().forEach(tuple -> {
final Integer from = tuple.getFirst();
final int[] toList = tuple.getSecond();
int toMax = -1;
double bestValue = Double.NEGATIVE_INFINITY;
for (final int c : toList) {
final double value = keyExtractor.applyAsDouble(c);
if (-1 == toMax || bestValue < value) {
bestValue = value;
toMax = c;
}
}
gradientMap[from] = toMax;
output.set(from, input.get(toMax));
});
input.freeRef();
gradientMapA[dataIndex] = gradientMap;
});
return new Result(TensorArray.wrap(outputA), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (in.isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, in.getData().length()).parallel().mapToObj(dataIndex -> {
@Nonnull final Tensor backSignal = new Tensor(inputDims);
final int[] ints = gradientMapA[dataIndex];
@Nullable final Tensor datum = data.get(dataIndex);
for (int i = 0; i < datum.length(); i++) {
backSignal.add(ints[i], datum.get(i));
}
datum.freeRef();
return backSignal;
}).toArray(i -> new Tensor[i]));
in.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
return in.isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class MaxMetaLayer method eval.
@Nonnull
@Override
public Result eval(final Result... inObj) {
final Result input = inObj[0];
input.addRef();
final int itemCnt = input.getData().length();
final Tensor input0Tensor = input.getData().get(0);
final int vectorSize = input0Tensor.length();
@Nonnull final int[] indicies = new int[vectorSize];
for (int i = 0; i < vectorSize; i++) {
final int itemNumber = i;
indicies[i] = IntStream.range(0, itemCnt).mapToObj(x -> x).max(Comparator.comparing(dataIndex -> {
Tensor tensor = input.getData().get(dataIndex);
double v = tensor.getData()[itemNumber];
tensor.freeRef();
return v;
})).get();
}
return new Result(TensorArray.wrap(input0Tensor.mapIndex((v, c) -> {
Tensor tensor = input.getData().get(indicies[c]);
double v1 = tensor.getData()[c];
tensor.freeRef();
return v1;
})), (@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()));
input0Tensor.coordStream(true).forEach((inputCoord) -> {
feedback[indicies[inputCoord.getIndex()]].add(inputCoord, delta.get(inputCoord));
});
@Nonnull TensorArray tensorArray = TensorArray.wrap(feedback);
input.accumulate(buffer, tensorArray);
delta.freeRef();
}
}) {
@Override
public boolean isAlive() {
return input.isAlive();
}
@Override
protected void _free() {
input.freeRef();
input0Tensor.freeRef();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class FullyConnectedLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
final TensorList indata = inObj[0].getData();
indata.addRef();
for (@Nonnull Result result : inObj) {
result.addRef();
}
FullyConnectedLayer.this.addRef();
assert Tensor.length(indata.getDimensions()) == Tensor.length(this.inputDims) : Arrays.toString(indata.getDimensions()) + " == " + Arrays.toString(this.inputDims);
@Nonnull DoubleMatrix doubleMatrix = new DoubleMatrix(Tensor.length(indata.getDimensions()), Tensor.length(outputDims), this.weights.getData());
@Nonnull final DoubleMatrix matrixObj = FullyConnectedLayer.transpose(doubleMatrix);
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, indata.length()).parallel().mapToObj(dataIndex -> {
@Nullable final Tensor input = indata.get(dataIndex);
@Nullable final Tensor output = new Tensor(outputDims);
matrixObj.mmuli(new DoubleMatrix(input.length(), 1, input.getData()), new DoubleMatrix(output.length(), 1, output.getData()));
input.freeRef();
return output;
}).toArray(i -> new Tensor[i]));
RecycleBin.DOUBLES.recycle(matrixObj.data, matrixObj.data.length);
this.weights.addRef();
return new Result(tensorArray, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (!isFrozen()) {
final Delta<Layer> deltaBuffer = buffer.get(FullyConnectedLayer.this, this.weights.getData());
final int threads = 4;
IntStream.range(0, threads).parallel().mapToObj(x -> x).flatMap(thread -> {
@Nullable Stream<Tensor> stream = IntStream.range(0, indata.length()).filter(i -> thread == i % threads).mapToObj(dataIndex -> {
@Nonnull final Tensor weightDelta = new Tensor(Tensor.length(inputDims), Tensor.length(outputDims));
Tensor deltaTensor = delta.get(dataIndex);
Tensor inputTensor = indata.get(dataIndex);
FullyConnectedLayer.crossMultiplyT(deltaTensor.getData(), inputTensor.getData(), weightDelta.getData());
inputTensor.freeRef();
deltaTensor.freeRef();
return weightDelta;
});
return stream;
}).reduce((a, b) -> {
@Nullable Tensor c = a.addAndFree(b);
b.freeRef();
return c;
}).map(data -> {
@Nonnull Delta<Layer> layerDelta = deltaBuffer.addInPlace(data.getData());
data.freeRef();
return layerDelta;
});
deltaBuffer.freeRef();
}
if (inObj[0].isAlive()) {
@Nonnull final TensorList tensorList = TensorArray.wrap(IntStream.range(0, indata.length()).parallel().mapToObj(dataIndex -> {
Tensor deltaTensor = delta.get(dataIndex);
@Nonnull final Tensor passback = new Tensor(indata.getDimensions());
FullyConnectedLayer.multiply(this.weights.getData(), deltaTensor.getData(), passback.getData());
deltaTensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
inObj[0].accumulate(buffer, tensorList);
}
}) {
@Override
protected void _free() {
indata.freeRef();
FullyConnectedLayer.this.freeRef();
for (@Nonnull Result result : inObj) {
result.freeRef();
}
FullyConnectedLayer.this.weights.freeRef();
}
@Override
public boolean isAlive() {
return !isFrozen() || Arrays.stream(inObj).anyMatch(x -> x.isAlive());
}
};
}
Aggregations