use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class BiasMetaLayer method eval.
@Nullable
@Override
public Result eval(@Nonnull final Result... inObj) {
final int itemCnt = inObj[0].getData().length();
Tensor tensor1 = inObj[1].getData().get(0);
final Tensor[] tensors = IntStream.range(0, itemCnt).parallel().mapToObj(dataIndex -> {
Tensor tensor = inObj[0].getData().get(dataIndex);
Tensor mapIndex = tensor.mapIndex((v, c) -> {
return v + tensor1.get(c);
});
tensor.freeRef();
return mapIndex;
}).toArray(i -> new Tensor[i]);
tensor1.freeRef();
Tensor tensor0 = tensors[0];
tensor0.addRef();
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
return new Result(TensorArray.wrap(tensors), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (inObj[0].isAlive()) {
data.addRef();
inObj[0].accumulate(buffer, data);
}
if (inObj[1].isAlive()) {
@Nonnull final ToDoubleFunction<Coordinate> f = (c) -> {
return IntStream.range(0, itemCnt).mapToDouble(i -> {
Tensor tensor = data.get(i);
double v = tensor.get(c);
tensor.freeRef();
return v;
}).sum();
};
@Nullable final Tensor passback = tensor0.mapCoords(f);
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, inObj[1].getData().length()).mapToObj(i -> {
if (i == 0)
return passback;
else {
@Nullable Tensor map = passback.map(v -> 0);
passback.freeRef();
return map;
}
}).toArray(i -> new Tensor[i]));
inObj[1].accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
tensor0.freeRef();
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
return inObj[0].isAlive() || inObj[1].isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorArray 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.TensorArray 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.TensorArray 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.TensorArray 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();
}
};
}
Aggregations