use of com.simiacryptus.mindseye.lang.Result in project MindsEye by SimiaCryptus.
the class ImgPixelGateLayer method eval.
/**
* Eval nn result.
*
* @param input the input
* @param gate the gate
* @return the nn result
*/
@Nonnull
public Result eval(@Nonnull final Result input, @Nonnull final Result gate) {
final TensorList inputData = input.getData();
final TensorList gateData = gate.getData();
inputData.addRef();
input.addRef();
gate.addRef();
gateData.addRef();
int[] inputDims = inputData.getDimensions();
assert 3 == inputDims.length;
return new Result(TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(i -> {
Tensor inputTensor = inputData.get(i);
Tensor gateTensor = gateData.get(i);
Tensor result = new Tensor(inputDims[0], inputDims[1], 1).setByCoord(c -> {
return IntStream.range(0, inputDims[2]).mapToDouble(b -> {
int[] coords = c.getCoords();
return inputTensor.get(coords[0], coords[1], b) * gateTensor.get(coords[0], coords[1], 0);
}).sum();
});
inputTensor.freeRef();
gateTensor.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(IntStream.range(0, delta.length()).mapToObj(i -> {
Tensor deltaTensor = delta.get(i);
Tensor gateTensor = gateData.get(i);
Tensor result = new Tensor(input.getData().getDimensions()).setByCoord(c -> {
int[] coords = c.getCoords();
return deltaTensor.get(coords[0], coords[1], 0) * gateTensor.get(coords[0], coords[1], 0);
});
deltaTensor.freeRef();
gateTensor.freeRef();
return result;
}).toArray(i -> new Tensor[i]));
input.accumulate(buffer, tensorArray);
}
if (gate.isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(i -> {
Tensor deltaTensor = delta.get(i);
Tensor inputTensor = inputData.get(i);
Tensor result = new Tensor(gateData.getDimensions()).setByCoord(c -> IntStream.range(0, inputDims[2]).mapToDouble(b -> {
int[] coords = c.getCoords();
return deltaTensor.get(coords[0], coords[1], 0) * inputTensor.get(coords[0], coords[1], b);
}).sum());
deltaTensor.freeRef();
inputTensor.freeRef();
return result;
}).toArray(i -> new Tensor[i]));
gate.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
inputData.freeRef();
input.freeRef();
gate.freeRef();
gateData.freeRef();
}
@Override
public boolean isAlive() {
return input.isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.Result in project MindsEye by SimiaCryptus.
the class ImgTileAssemblyLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
assert 3 == inObj[0].getData().getDimensions().length;
int[] outputDims = getOutputDims(inObj);
return new Result(TensorArray.wrap(IntStream.range(0, inObj[0].getData().length()).parallel().mapToObj(dataIndex -> {
@Nonnull final Tensor outputData = new Tensor(outputDims);
int totalWidth = 0;
int totalHeight = 0;
int inputIndex = 0;
for (int row = 0; row < rows; row++) {
int positionX = 0;
int rowHeight = 0;
for (int col = 0; col < columns; col++) {
TensorList tileTensor = inObj[inputIndex].getData();
int[] tileDimensions = tileTensor.getDimensions();
rowHeight = Math.max(rowHeight, tileDimensions[1]);
Tensor inputData = tileTensor.get(dataIndex);
ImgTileAssemblyLayer.copy(inputData, outputData, positionX, totalHeight);
inputData.freeRef();
positionX += tileDimensions[0];
inputIndex += 1;
}
totalHeight += rowHeight;
totalWidth = Math.max(totalWidth, positionX);
}
return outputData;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
int totalHeight = 0;
int inputIndex = 0;
for (int row = 0; row < rows; row++) {
int positionX = 0;
int rowHeight = 0;
for (int col = 0; col < columns; col++) {
Result in = inObj[inputIndex];
int[] inputDataDimensions = in.getData().getDimensions();
rowHeight = Math.max(rowHeight, inputDataDimensions[1]);
if (in.isAlive()) {
int _positionX = positionX;
int _totalHeight = totalHeight;
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).parallel().mapToObj(dataIndex -> {
@Nullable final Tensor deltaTensor = delta.get(dataIndex);
@Nonnull final Tensor passbackTensor = new Tensor(inputDataDimensions);
ImgTileAssemblyLayer.copy(deltaTensor, passbackTensor, -_positionX, -_totalHeight);
deltaTensor.freeRef();
return passbackTensor;
}).toArray(i -> new Tensor[i]));
in.accumulate(buffer, tensorArray);
}
positionX += inputDataDimensions[0];
inputIndex += 1;
}
totalHeight += rowHeight;
}
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
return inObj[0].isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.Result in project MindsEye by SimiaCryptus.
the class ImgTileSubnetLayer method evalAndFree.
@Nullable
@Override
public Result evalAndFree(@Nonnull final Result... inObj) {
assert 1 == inObj.length;
Result input = inObj[0];
@Nonnull final int[] inputDims = input.getData().getDimensions();
assert 3 == inputDims.length;
int cols = (int) (Math.ceil((inputDims[0] - width) * 1.0 / strideX) + 1);
int rows = (int) (Math.ceil((inputDims[1] - height) * 1.0 / strideY) + 1);
if (cols == 1 && rows == 1)
return getInner().eval(inObj);
Result[] results = new Result[rows * cols];
TensorList[] passback = new TensorList[rows * cols];
int index = 0;
AtomicInteger passbacks = new AtomicInteger(0);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int positionX = col * strideX;
int positionY = row * strideY;
assert positionX >= 0;
assert positionY >= 0;
assert positionX < inputDims[0];
assert positionY < inputDims[1];
final int finalIndex = index;
Result selectedTile = new ImgTileSelectLayer(width, height, positionX, positionY).eval(new Result(input.getData(), (ctx, delta) -> {
passback[finalIndex] = delta;
if (passbacks.incrementAndGet() == rows * cols) {
passbacks.set(0);
TensorList reassembled = new ImgTileAssemblyLayer(cols, rows).evalAndFree(Arrays.stream(passback).map(t -> new Result(t, (c2, d2) -> {
})).toArray(i -> new Result[i])).getDataAndFree();
inObj[0].accumulate(ctx, reassembled);
}
}) {
@Override
protected void _free() {
inObj[0].freeRef();
super._free();
}
});
results[index] = getInner().eval(selectedTile);
index = index + 1;
}
}
inObj[0].getData().freeRef();
return new ImgTileAssemblyLayer(cols, rows).eval(results);
}
use of com.simiacryptus.mindseye.lang.Result in project MindsEye by SimiaCryptus.
the class L1NormalizationLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... input) {
Arrays.stream(input).forEach(nnResult -> nnResult.addRef());
final Result in = input[0];
final TensorList inData = in.getData();
inData.addRef();
return new Result(TensorArray.wrap(IntStream.range(0, inData.length()).mapToObj(dataIndex -> {
@Nullable final Tensor value = inData.get(dataIndex);
try {
final double sum = value.sum();
if (!Double.isFinite(sum) || 0 == sum) {
value.addRef();
return value;
} else {
return value.scale(1.0 / sum);
}
} finally {
value.freeRef();
}
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList outDelta) -> {
if (in.isAlive()) {
final Tensor[] passbackArray = IntStream.range(0, outDelta.length()).mapToObj(dataIndex -> {
Tensor inputTensor = inData.get(dataIndex);
@Nullable final double[] value = inputTensor.getData();
Tensor outputTensor = outDelta.get(dataIndex);
@Nullable final double[] delta = outputTensor.getData();
final double dot = ArrayUtil.dot(value, delta);
final double sum = Arrays.stream(value).sum();
@Nonnull final Tensor passback = new Tensor(outputTensor.getDimensions());
@Nullable final double[] passbackData = passback.getData();
if (0 != sum || Double.isFinite(sum)) {
for (int i = 0; i < value.length; i++) {
passbackData[i] = (delta[i] - dot / sum) / sum;
}
}
outputTensor.freeRef();
inputTensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]);
assert Arrays.stream(passbackArray).flatMapToDouble(x -> Arrays.stream(x.getData())).allMatch(v -> Double.isFinite(v));
@Nonnull TensorArray tensorArray = TensorArray.wrap(passbackArray);
in.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
inData.freeRef();
Arrays.stream(input).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
return in.isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.Result in project MindsEye by SimiaCryptus.
the class DAGNetwork method buildExeCtx.
/**
* Build handler ctx graph evaluation context.
*
* @param inputs the inputs
* @return the graph evaluation context
*/
@Nonnull
public GraphEvaluationContext buildExeCtx(@Nonnull final Result... inputs) {
assert inputs.length == inputHandles.size() : inputs.length + " != " + inputHandles.size();
@Nonnull final GraphEvaluationContext context = new GraphEvaluationContext();
for (int i = 0; i < inputs.length; i++) {
UUID key = inputHandles.get(i);
Result input = inputs[i];
if (!context.calculated.containsKey(key)) {
input.getData().addRef();
context.calculated.put(key, new Singleton<CountingResult>().set(new CountingResult(input)));
}
}
context.expectedCounts.putAll(getNodes().stream().flatMap(t -> {
return Arrays.stream(t.getInputs()).map(n -> n.getId());
}).filter(x -> !inputHandles.contains(x)).collect(Collectors.groupingBy(x -> x, Collectors.counting())));
return context;
}
Aggregations