use of com.simiacryptus.mindseye.lang.DeltaSet 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.DeltaSet 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.DeltaSet in project MindsEye by SimiaCryptus.
the class MomentumStrategy method orient.
@Nonnull
@Override
public SimpleLineSearchCursor orient(final Trainable subject, @Nonnull final PointSample measurement, final TrainingMonitor monitor) {
final LineSearchCursor orient = inner.orient(subject, measurement, monitor);
final DeltaSet<Layer> direction = ((SimpleLineSearchCursor) orient).direction;
@Nonnull final DeltaSet<Layer> newDelta = new DeltaSet<Layer>();
direction.getMap().forEach((layer, delta) -> {
final DoubleBuffer<Layer> prevBuffer = prevDelta.get(layer, delta.target);
newDelta.get(layer, delta.target).addInPlace(ArrayUtil.add(ArrayUtil.multiply(prevBuffer.getDelta(), carryOver), delta.getDelta()));
});
prevDelta = newDelta;
return new SimpleLineSearchCursor(subject, measurement, newDelta);
}
use of com.simiacryptus.mindseye.lang.DeltaSet in project MindsEye by SimiaCryptus.
the class SingleDerivativeTester method testUnFrozen.
/**
* Test un frozen.
*
* @param component the component
* @param inputPrototype the input prototype
*/
public void testUnFrozen(@Nonnull final Layer component, Tensor[] inputPrototype) {
inputPrototype = Arrays.stream(inputPrototype).map(tensor -> tensor.copy()).toArray(i -> new Tensor[i]);
@Nonnull final AtomicBoolean reachedInputFeedback = new AtomicBoolean(false);
@Nonnull final Layer frozen = component.copy().setFrozen(false);
List<TensorArray> inputCopies = Arrays.stream(inputPrototype).map(TensorArray::wrap).collect(Collectors.toList());
Result[] inputs = inputCopies.stream().map(tensor -> new Result(tensor, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
reachedInputFeedback.set(true);
}) {
@Override
public boolean isAlive() {
return true;
}
}).toArray(i -> new Result[i]);
@Nullable final Result eval;
try {
eval = frozen.eval(inputs);
} finally {
for (@Nonnull Result result : inputs) {
result.freeRef();
}
for (@Nonnull TensorArray tensorArray : inputCopies) {
tensorArray.freeRef();
}
}
@Nonnull final DeltaSet<Layer> buffer = new DeltaSet<Layer>();
TensorList tensorList = eval.getData();
eval.accumulate(buffer, tensorList);
eval.freeRef();
@Nullable final List<double[]> stateList = frozen.state();
final List<Delta<Layer>> deltas = stateList.stream().map(doubles -> {
return buffer.stream().filter(x -> x.target == doubles).findFirst().orElse(null);
}).filter(x -> x != null).collect(Collectors.toList());
if (deltas.isEmpty() && !stateList.isEmpty()) {
throw new AssertionError("Nonfrozen component not listed in delta. Deltas: " + deltas);
}
frozen.freeRef();
buffer.freeRef();
if (!reachedInputFeedback.get() && inputPrototype.length != 0) {
throw new RuntimeException("Nonfrozen component did not pass input backwards");
}
}
use of com.simiacryptus.mindseye.lang.DeltaSet in project MindsEye by SimiaCryptus.
the class ObjectLocation method renderAlpha.
/**
* Render alpha tensor.
*
* @param alphaPower the alpha power
* @param img the img
* @param locationResult the location result
* @param classification the classification
* @param category the category
* @return the tensor
*/
public Tensor renderAlpha(final double alphaPower, final Tensor img, final Result locationResult, final Tensor classification, final int category) {
TensorArray tensorArray = TensorArray.wrap(new Tensor(classification.getDimensions()).set(category, 1));
DeltaSet<Layer> deltaSet = new DeltaSet<>();
locationResult.accumulate(deltaSet, tensorArray);
double[] rawDelta = deltaSet.getMap().entrySet().stream().filter(x -> x.getValue().target == img.getData()).findAny().get().getValue().getDelta();
Tensor deltaColor = new Tensor(rawDelta, img.getDimensions()).mapAndFree(x -> Math.abs(x));
Tensor delta1d = blur(reduce(deltaColor), 3);
return TestUtil.normalizeBands(TestUtil.normalizeBands(delta1d, 1).mapAndFree(x -> Math.pow(x, alphaPower)));
}
Aggregations