use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class ImgBandSelectLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
final Result input = inObj[0];
final TensorList batch = input.getData();
@Nonnull final int[] inputDims = batch.getDimensions();
assert 3 == inputDims.length;
@Nonnull final Tensor outputDims = new Tensor(inputDims[0], inputDims[1], bands.length);
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
@Nonnull TensorArray wrap = TensorArray.wrap(IntStream.range(0, batch.length()).parallel().mapToObj(dataIndex -> outputDims.mapCoords((c) -> {
int[] coords = c.getCoords();
@Nullable Tensor tensor = batch.get(dataIndex);
double v = tensor.get(coords[0], coords[1], bands[coords[2]]);
tensor.freeRef();
return v;
})).toArray(i -> new Tensor[i]));
outputDims.freeRef();
return new Result(wrap, (@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 -> {
@Nonnull final Tensor passback = new Tensor(inputDims);
@Nullable final Tensor err = error.get(dataIndex);
err.coordStream(false).forEach(c -> {
int[] coords = c.getCoords();
passback.set(coords[0], coords[1], bands[coords[2]], err.get(c));
});
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();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class ImgCropLayer 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;
return new Result(TensorArray.wrap(IntStream.range(0, batch.length()).parallel().mapToObj(dataIndex -> {
@Nonnull final Tensor outputData = new Tensor(sizeX, sizeY, inputDims[2]);
Tensor inputData = batch.get(dataIndex);
ImgCropLayer.copy(inputData, outputData);
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);
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();
}
};
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class CrossDifferenceLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
assert 1 == inObj.length;
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
return new Result(TensorArray.wrap(inObj[0].getData().stream().parallel().map(tensor -> {
final int inputDim = tensor.length();
final int outputDim = (inputDim * inputDim - inputDim) / 2;
@Nonnull final Tensor result = new Tensor(outputDim);
@Nullable final double[] inputData = tensor.getData();
@Nullable final double[] resultData = result.getData();
IntStream.range(0, inputDim).forEach(x -> {
IntStream.range(x + 1, inputDim).forEach(y -> {
resultData[CrossDifferenceLayer.index(x, y, inputDim)] = inputData[x] - inputData[y];
});
});
tensor.freeRef();
return result;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
final Result input = inObj[0];
if (input.isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(data.stream().parallel().map(tensor -> {
final int outputDim = tensor.length();
final int inputDim = (1 + (int) Math.sqrt(1 + 8 * outputDim)) / 2;
@Nonnull final Tensor passback = new Tensor(inputDim);
@Nullable final double[] passbackData = passback.getData();
@Nullable final double[] tensorData = tensor.getData();
IntStream.range(0, inputDim).forEach(x -> {
IntStream.range(x + 1, inputDim).forEach(y -> {
passbackData[x] += tensorData[CrossDifferenceLayer.index(x, y, inputDim)];
passbackData[y] += -tensorData[CrossDifferenceLayer.index(x, y, inputDim)];
});
});
tensor.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() {
for (@Nonnull final Result element : inObj) if (element.isAlive()) {
return true;
}
return false;
}
};
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class CrossProductLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
assert 1 == inObj.length;
final Result in = inObj[0];
TensorList indata = in.getData();
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
indata.addRef();
return new Result(TensorArray.wrap(indata.stream().parallel().map(tensor -> {
final int inputDim = tensor.length();
final int outputDim = (inputDim * inputDim - inputDim) / 2;
@Nonnull final Tensor result = new Tensor(outputDim);
@Nullable final double[] inputData = tensor.getData();
@Nullable final double[] resultData = result.getData();
IntStream.range(0, inputDim).forEach(x -> {
IntStream.range(x + 1, inputDim).forEach(y -> {
resultData[CrossProductLayer.index(x, y, inputDim)] = inputData[x] * inputData[y];
});
});
tensor.freeRef();
return result;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (in.isAlive()) {
assert delta.length() == delta.length();
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).parallel().mapToObj(batchIndex -> {
@Nullable final Tensor deltaTensor = delta.get(batchIndex);
final int outputDim = deltaTensor.length();
final int inputDim = (1 + (int) Math.sqrt(1 + 8 * outputDim)) / 2;
@Nonnull final Tensor passback = new Tensor(inputDim);
@Nullable final double[] passbackData = passback.getData();
@Nullable final double[] tensorData = deltaTensor.getData();
Tensor inputTensor = indata.get(batchIndex);
@Nullable final double[] inputData = inputTensor.getData();
IntStream.range(0, inputDim).forEach(x -> {
IntStream.range(x + 1, inputDim).forEach(y -> {
passbackData[x] += tensorData[CrossProductLayer.index(x, y, inputDim)] * inputData[y];
passbackData[y] += tensorData[CrossProductLayer.index(x, y, inputDim)] * inputData[x];
});
});
deltaTensor.freeRef();
inputTensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
in.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
indata.freeRef();
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
for (@Nonnull final Result element : inObj) if (element.isAlive()) {
return true;
}
return false;
}
};
}
use of com.simiacryptus.mindseye.lang.TensorArray in project MindsEye by SimiaCryptus.
the class EntropyLossLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
final double zero_tol = 1e-12;
final Result in0 = inObj[0];
TensorList indata = in0.getData();
indata.addRef();
@Nonnull final Tensor[] gradient = new Tensor[indata.length()];
final double max_prob = 1.;
return new Result(TensorArray.wrap(IntStream.range(0, indata.length()).mapToObj(dataIndex -> {
@Nullable final Tensor l = indata.get(dataIndex);
@Nullable final Tensor r = inObj[1].getData().get(dataIndex);
assert l.length() == r.length() : l.length() + " != " + r.length();
@Nonnull final Tensor gradientTensor = new Tensor(l.getDimensions());
@Nullable final double[] gradientData = gradientTensor.getData();
double total = 0;
@Nullable final double[] ld = l.getData();
@Nullable final double[] rd = r.getData();
for (int i = 0; i < l.length(); i++) {
final double lv = Math.max(Math.min(ld[i], max_prob), zero_tol);
final double rv = rd[i];
if (rv > 0) {
gradientData[i] = -rv / lv;
total += -rv * Math.log(lv);
} else {
gradientData[i] = 0;
}
}
l.freeRef();
r.freeRef();
assert total >= 0;
gradient[dataIndex] = gradientTensor;
@Nonnull final Tensor outValue = new Tensor(new double[] { total }, 1);
return outValue;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (inObj[1].isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
Tensor deltaTensor = delta.get(dataIndex);
@Nullable final Tensor inputTensor = indata.get(dataIndex);
@Nonnull final Tensor passback = new Tensor(gradient[dataIndex].getDimensions());
for (int i = 0; i < passback.length(); i++) {
final double lv = Math.max(Math.min(inputTensor.get(i), max_prob), zero_tol);
passback.set(i, -deltaTensor.get(0) * Math.log(lv));
}
inputTensor.freeRef();
deltaTensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
inObj[1].accumulate(buffer, tensorArray);
}
if (in0.isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
Tensor tensor = delta.get(dataIndex);
@Nonnull final Tensor passback = new Tensor(gradient[dataIndex].getDimensions());
for (int i = 0; i < passback.length(); i++) {
passback.set(i, tensor.get(0) * gradient[dataIndex].get(i));
}
tensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
in0.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
indata.freeRef();
Arrays.stream(gradient).forEach(ReferenceCounting::freeRef);
Arrays.stream(inObj).forEach(ReferenceCounting::freeRef);
}
@Override
public boolean isAlive() {
return in0.isAlive() || in0.isAlive();
}
};
}
Aggregations