use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class SimpleGpuEval method getDelta.
/**
* Gets delta.
*
* @param output the output
* @return the delta
*/
@Nonnull
public TensorArray getDelta(final TensorList output) {
return TensorArray.wrap(output.stream().map(t -> {
@Nullable Tensor map = t.map(v -> 1.0);
t.freeRef();
return map;
}).toArray(i -> new Tensor[i]));
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class ImgPixelSoftmaxLayer method eval.
/**
* Eval nn result.
*
* @param input the input
* @return the nn result
*/
@Nonnull
public Result eval(@Nonnull final Result input) {
final TensorList inputData = input.getData();
inputData.addRef();
input.addRef();
int[] inputDims = inputData.getDimensions();
assert 3 == inputDims.length;
final int inputBands = inputDims[2];
final int width = inputDims[0];
final int height = inputDims[1];
TensorArray maxima = TensorArray.wrap(inputData.stream().map(inputTensor -> {
try {
return new Tensor(width, height, 1).setByCoord(c -> {
return IntStream.range(0, inputBands).mapToDouble(band -> {
int[] coords = c.getCoords();
return inputTensor.get(coords[0], coords[1], band);
}).max().getAsDouble();
});
} finally {
inputTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
TensorArray exps = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
final Tensor inputTensor = inputData.get(index);
Tensor maxTensor = maxima.get(index);
try {
return new Tensor(inputDims).setByCoord(c -> {
int[] coords = c.getCoords();
return Math.exp(inputTensor.get(c) - maxTensor.get(coords[0], coords[1], 0));
});
} finally {
inputTensor.freeRef();
maxTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
maxima.freeRef();
TensorArray sums = TensorArray.wrap(exps.stream().map(expTensor -> {
try {
return new Tensor(width, height, 1).setByCoord(c -> {
return IntStream.range(0, inputBands).mapToDouble(band -> {
int[] coords = c.getCoords();
return expTensor.get(coords[0], coords[1], band);
}).sum();
});
} finally {
expTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
TensorArray output = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
Tensor sumTensor = sums.get(index);
Tensor expTensor = exps.get(index);
try {
return new Tensor(inputDims).setByCoord(c -> {
int[] coords = c.getCoords();
return (expTensor.get(c) / sumTensor.get(coords[0], coords[1], 0));
});
} finally {
sumTensor.freeRef();
expTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
return new Result(output, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (input.isAlive()) {
TensorArray dots = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
final Tensor deltaTensor = delta.get(index);
Tensor expTensor = exps.get(index);
try {
return new Tensor(width, height, 1).setByCoord(c -> {
return IntStream.range(0, inputBands).mapToDouble(band -> {
int[] coords = c.getCoords();
return expTensor.get(coords[0], coords[1], band) * deltaTensor.get(coords[0], coords[1], band);
}).sum();
});
} finally {
expTensor.freeRef();
deltaTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
TensorArray passback = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
final Tensor deltaTensor = delta.get(index);
final Tensor expTensor = exps.get(index);
Tensor sumTensor = sums.get(index);
Tensor dotTensor = dots.get(index);
try {
return new Tensor(inputDims).setByCoord(c -> {
int[] coords = c.getCoords();
double sum = sumTensor.get(coords[0], coords[1], 0);
double dot = dotTensor.get(coords[0], coords[1], 0);
double deltaValue = deltaTensor.get(c);
double expValue = expTensor.get(c);
return (sum * deltaValue - dot) * expValue / (sum * sum);
});
} finally {
deltaTensor.freeRef();
expTensor.freeRef();
sumTensor.freeRef();
dotTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
input.accumulate(buffer, passback);
dots.freeRef();
}
}) {
@Override
protected void _free() {
inputData.freeRef();
input.freeRef();
sums.freeRef();
exps.freeRef();
}
@Override
public boolean isAlive() {
return input.isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class ImgPixelSumLayer method evalAndFree.
/**
* Eval nn result.
*
* @param input the input
* @return the nn result
*/
@Nonnull
public Result evalAndFree(@Nonnull final Result input) {
final TensorList inputData = input.getData();
int[] inputDims = inputData.getDimensions();
assert 3 == inputDims.length;
return new Result(TensorArray.wrap(inputData.stream().map(tensor -> {
Tensor result = new Tensor(inputDims[0], inputDims[1], 1).setByCoord(c -> {
return IntStream.range(0, inputDims[2]).mapToDouble(i -> {
int[] coords = c.getCoords();
return tensor.get(coords[0], coords[1], i);
}).sum();
});
tensor.freeRef();
return result;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (input.isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(delta.stream().map(deltaTensor -> {
int[] deltaDims = deltaTensor.getDimensions();
Tensor result = new Tensor(deltaDims[0], deltaDims[1], inputDims[2]).setByCoord(c -> {
int[] coords = c.getCoords();
return deltaTensor.get(coords[0], coords[1], 0);
});
deltaTensor.freeRef();
return result;
}).toArray(i -> new Tensor[i]));
input.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
inputData.freeRef();
input.freeRef();
}
@Override
public boolean isAlive() {
return input.isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class ImgReshapeLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
// assert Arrays.stream(inObj).flatMapToDouble(input-> input.getData().stream().flatMapToDouble(x-> Arrays.stream(x.getData()))).allMatch(v->Double.isFinite(v));
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;
assert expand || 0 == inputDims[0] % kernelSizeX : (inputDims[0] + " % " + kernelSizeX);
assert expand || 0 == inputDims[1] % kernelSizeY : (inputDims[1] + " % " + kernelSizeY);
assert !expand || 0 == inputDims[2] % (kernelSizeX * kernelSizeY);
// assert input.getData().stream().flatMapToDouble(x-> Arrays.stream(x.getData())).allMatch(v->Double.isFinite(v));
Tensor outputDims;
if (expand) {
outputDims = new Tensor(inputDims[0] * kernelSizeX, inputDims[1] * kernelSizeY, inputDims[2] / (kernelSizeX * kernelSizeY));
} else {
outputDims = new Tensor(inputDims[0] / kernelSizeX, inputDims[1] / kernelSizeY, inputDims[2] * kernelSizeX * kernelSizeY);
}
TensorArray data = TensorArray.wrap(IntStream.range(0, batch.length()).parallel().mapToObj(dataIndex -> {
Tensor inputData = batch.get(dataIndex);
Tensor tensor = expand ? ImgReshapeLayer.copyExpand(inputData, outputDims.copy()) : ImgReshapeLayer.copyCondense(inputData, outputDims.copy());
inputData.freeRef();
return tensor;
}).toArray(i -> new Tensor[i]));
outputDims.freeRef();
return new Result(data, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList error) -> {
// assert error.stream().flatMapToDouble(x-> Arrays.stream(x.getData())).allMatch(v->Double.isFinite(v));
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);
Tensor tensor = expand ? ImgReshapeLayer.copyCondense(err, passback) : ImgReshapeLayer.copyExpand(err, passback);
err.freeRef();
return tensor;
}).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.TensorArray in project MindsEye by SimiaCryptus.
the class ImgTileSelectLayer 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;
@Nonnull final int[] dimOut = getViewDimensions(inputDims, new int[] { sizeY, sizeX, inputDims[2] }, new int[] { positionX, positionY, 0 });
return new Result(TensorArray.wrap(IntStream.range(0, batch.length()).parallel().mapToObj(dataIndex -> {
@Nonnull final Tensor outputData = new Tensor(dimOut);
Tensor inputData = batch.get(dataIndex);
copy(inputData, outputData, positionX, positionY);
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, -positionX, -positionY);
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