use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class CrossDotMetaLayer method eval.
@Nullable
@Override
public Result eval(@Nonnull final Result... inObj) {
final Result input = inObj[0];
final TensorList indata = input.getData();
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
indata.addRef();
final int itemCnt = indata.length();
final int dim = Tensor.length(indata.getDimensions());
@Nonnull final Tensor results = new Tensor(dim, dim);
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
if (i == j) {
continue;
}
double v = 0;
for (int k = 0; k < itemCnt; k++) {
Tensor tensor = indata.get(k);
@Nullable final double[] kk = tensor.getData();
v += kk[i] * kk[j];
tensor.freeRef();
}
results.set(new int[] { i, j }, v);
}
}
return new Result(TensorArray.wrap(results), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (input.isAlive()) {
@Nullable final Tensor deltaTensor = delta.get(0);
@Nonnull final Tensor[] feedback = new Tensor[itemCnt];
Arrays.parallelSetAll(feedback, i -> new Tensor(dim));
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
if (i == j) {
continue;
}
final double v = deltaTensor.get(i, j);
for (int k = 0; k < itemCnt; k++) {
Tensor tensor = indata.get(k);
@Nullable final double[] kk = tensor.getData();
feedback[k].add(i, v * kk[j]);
feedback[k].add(j, v * kk[i]);
tensor.freeRef();
}
}
}
deltaTensor.freeRef();
@Nonnull TensorArray tensorArray = TensorArray.wrap(feedback);
input.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
indata.freeRef();
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
return input.isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList in project MindsEye by SimiaCryptus.
the class DropoutNoiseLayer method eval.
@Nonnull
@Override
public Result eval(final Result... inObj) {
final Result inputResult = inObj[0];
inputResult.addRef();
final TensorList inputData = inputResult.getData();
final int itemCnt = inputData.length();
final Tensor[] mask = 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 -> {
if (seed == -1)
return 1;
return random.nextDouble() < getValue() ? 0 : (1.0 / getValue());
});
input.freeRef();
return output;
}).toArray(i -> new Tensor[i]);
return new Result(TensorArray.wrap(IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
Tensor inputTensor = inputData.get(dataIndex);
@Nullable final double[] input = inputTensor.getData();
@Nullable final double[] maskT = mask[dataIndex].getData();
@Nonnull final Tensor output = new Tensor(inputTensor.getDimensions());
@Nullable final double[] outputData = output.getData();
for (int i = 0; i < outputData.length; i++) {
outputData[i] = input[i] * maskT[i];
}
inputTensor.freeRef();
return output;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (inputResult.isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
Tensor deltaTensor = delta.get(dataIndex);
@Nullable final double[] deltaData = deltaTensor.getData();
@Nullable final double[] maskData = mask[dataIndex].getData();
@Nonnull final Tensor passback = new Tensor(deltaTensor.getDimensions());
for (int i = 0; i < passback.length(); i++) {
passback.set(i, maskData[i] * deltaData[i]);
}
deltaTensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
inputResult.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
inputResult.freeRef();
Arrays.stream(mask).forEach(ReferenceCounting::freeRef);
}
@Override
public boolean isAlive() {
return inputResult.isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorList 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.TensorList 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.TensorList 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);
}
Aggregations