use of com.simiacryptus.mindseye.lang.DeltaSet in project MindsEye by SimiaCryptus.
the class BatchDerivativeTester method testFrozen.
/**
* Test frozen.
*
* @param component the component
* @param inputPrototype the input prototype
*/
public void testFrozen(@Nonnull final Layer component, @Nonnull final Tensor[] inputPrototype) {
@Nonnull final AtomicBoolean reachedInputFeedback = new AtomicBoolean(false);
@Nonnull final Layer frozen = component.copy().freeze();
@Nullable final Result eval = frozen.eval(new Result(TensorArray.create(inputPrototype), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
reachedInputFeedback.set(true);
}) {
@Override
public boolean isAlive() {
return true;
}
});
@Nonnull final DeltaSet<Layer> buffer = new DeltaSet<Layer>();
TensorList tensorList = eval.getData().copy();
eval.accumulate(buffer, tensorList);
final List<Delta<Layer>> deltas = component.state().stream().map(doubles -> {
return buffer.stream().filter(x -> x.target == doubles).findFirst().orElse(null);
}).filter(x -> x != null).collect(Collectors.toList());
if (!deltas.isEmpty() && !component.state().isEmpty()) {
throw new AssertionError("Frozen component listed in delta. Deltas: " + deltas);
}
final int inElements = Arrays.stream(inputPrototype).mapToInt(x -> x.length()).sum();
if (!reachedInputFeedback.get() && 0 < inElements) {
throw new RuntimeException("Frozen component did not pass input backwards");
}
}
use of com.simiacryptus.mindseye.lang.DeltaSet in project MindsEye by SimiaCryptus.
the class LBFGS method lbfgs.
private boolean lbfgs(@Nonnull PointSample measurement, @Nonnull TrainingMonitor monitor, @Nonnull List<PointSample> history, @Nonnull DeltaSet<Layer> direction) {
try {
@Nonnull DeltaSet<Layer> p = measurement.delta.copy();
if (!p.stream().parallel().allMatch(y -> Arrays.stream(y.getDelta()).allMatch(d -> Double.isFinite(d)))) {
throw new IllegalStateException("Non-finite value");
}
@Nonnull final double[] alphas = new double[history.size()];
for (int i = history.size() - 2; i >= 0; i--) {
@Nonnull final DeltaSet<Layer> sd = history.get(i + 1).weights.subtract(history.get(i).weights);
@Nonnull final DeltaSet<Layer> yd = history.get(i + 1).delta.subtract(history.get(i).delta);
final double denominator = sd.dot(yd);
if (0 == denominator) {
throw new IllegalStateException("Orientation vanished.");
}
alphas[i] = p.dot(sd) / denominator;
p = p.subtract(yd.scale(alphas[i]));
if ((!p.stream().parallel().allMatch(y -> Arrays.stream(y.getDelta()).allMatch(d -> Double.isFinite(d))))) {
throw new IllegalStateException("Non-finite value");
}
}
@Nonnull final DeltaSet<Layer> sk = history.get(history.size() - 1).weights.subtract(history.get(history.size() - 2).weights);
@Nonnull final DeltaSet<Layer> yk = history.get(history.size() - 1).delta.subtract(history.get(history.size() - 2).delta);
p = p.scale(sk.dot(yk) / yk.dot(yk));
if (!p.stream().parallel().allMatch(y -> Arrays.stream(y.getDelta()).allMatch(d -> Double.isFinite(d)))) {
throw new IllegalStateException("Non-finite value");
}
for (int i = 0; i < history.size() - 1; i++) {
@Nonnull final DeltaSet<Layer> sd = history.get(i + 1).weights.subtract(history.get(i).weights);
@Nonnull final DeltaSet<Layer> yd = history.get(i + 1).delta.subtract(history.get(i).delta);
final double beta = p.dot(yd) / sd.dot(yd);
p = p.add(sd.scale(alphas[i] - beta));
if (!p.stream().parallel().allMatch(y -> Arrays.stream(y.getDelta()).allMatch(d -> Double.isFinite(d)))) {
throw new IllegalStateException("Non-finite value");
}
}
boolean accept = measurement.delta.dot(p) < 0;
if (accept) {
monitor.log("Accepted: " + new Stats(direction, p));
copy(p, direction);
} else {
monitor.log("Rejected: " + new Stats(direction, p));
}
return accept;
} catch (Throwable e) {
monitor.log(String.format("LBFGS Orientation Error: %s", e.getMessage()));
return false;
}
}
use of com.simiacryptus.mindseye.lang.DeltaSet in project MindsEye by SimiaCryptus.
the class ImgPixelSoftmaxLayer method eval.
/**
* Eval nn result.
*
* @param input the input
* @return the nn result
*/
@Nonnull
public Result eval(@Nonnull final Result input) {
final TensorList inputData = input.getData();
inputData.addRef();
input.addRef();
int[] inputDims = inputData.getDimensions();
assert 3 == inputDims.length;
final int inputBands = inputDims[2];
final int width = inputDims[0];
final int height = inputDims[1];
TensorArray maxima = TensorArray.wrap(inputData.stream().map(inputTensor -> {
try {
return new Tensor(width, height, 1).setByCoord(c -> {
return IntStream.range(0, inputBands).mapToDouble(band -> {
int[] coords = c.getCoords();
return inputTensor.get(coords[0], coords[1], band);
}).max().getAsDouble();
});
} finally {
inputTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
TensorArray exps = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
final Tensor inputTensor = inputData.get(index);
Tensor maxTensor = maxima.get(index);
try {
return new Tensor(inputDims).setByCoord(c -> {
int[] coords = c.getCoords();
return Math.exp(inputTensor.get(c) - maxTensor.get(coords[0], coords[1], 0));
});
} finally {
inputTensor.freeRef();
maxTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
maxima.freeRef();
TensorArray sums = TensorArray.wrap(exps.stream().map(expTensor -> {
try {
return new Tensor(width, height, 1).setByCoord(c -> {
return IntStream.range(0, inputBands).mapToDouble(band -> {
int[] coords = c.getCoords();
return expTensor.get(coords[0], coords[1], band);
}).sum();
});
} finally {
expTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
TensorArray output = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
Tensor sumTensor = sums.get(index);
Tensor expTensor = exps.get(index);
try {
return new Tensor(inputDims).setByCoord(c -> {
int[] coords = c.getCoords();
return (expTensor.get(c) / sumTensor.get(coords[0], coords[1], 0));
});
} finally {
sumTensor.freeRef();
expTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
return new Result(output, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (input.isAlive()) {
TensorArray dots = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
final Tensor deltaTensor = delta.get(index);
Tensor expTensor = exps.get(index);
try {
return new Tensor(width, height, 1).setByCoord(c -> {
return IntStream.range(0, inputBands).mapToDouble(band -> {
int[] coords = c.getCoords();
return expTensor.get(coords[0], coords[1], band) * deltaTensor.get(coords[0], coords[1], band);
}).sum();
});
} finally {
expTensor.freeRef();
deltaTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
TensorArray passback = TensorArray.wrap(IntStream.range(0, inputData.length()).mapToObj(index -> {
final Tensor deltaTensor = delta.get(index);
final Tensor expTensor = exps.get(index);
Tensor sumTensor = sums.get(index);
Tensor dotTensor = dots.get(index);
try {
return new Tensor(inputDims).setByCoord(c -> {
int[] coords = c.getCoords();
double sum = sumTensor.get(coords[0], coords[1], 0);
double dot = dotTensor.get(coords[0], coords[1], 0);
double deltaValue = deltaTensor.get(c);
double expValue = expTensor.get(c);
return (sum * deltaValue - dot) * expValue / (sum * sum);
});
} finally {
deltaTensor.freeRef();
expTensor.freeRef();
sumTensor.freeRef();
dotTensor.freeRef();
}
}).toArray(i -> new Tensor[i]));
input.accumulate(buffer, passback);
dots.freeRef();
}
}) {
@Override
protected void _free() {
inputData.freeRef();
input.freeRef();
sums.freeRef();
exps.freeRef();
}
@Override
public boolean isAlive() {
return input.isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.DeltaSet in project MindsEye by SimiaCryptus.
the class ImgPixelSumLayer method evalAndFree.
/**
* Eval nn result.
*
* @param input the input
* @return the nn result
*/
@Nonnull
public Result evalAndFree(@Nonnull final Result input) {
final TensorList inputData = input.getData();
int[] inputDims = inputData.getDimensions();
assert 3 == inputDims.length;
return new Result(TensorArray.wrap(inputData.stream().map(tensor -> {
Tensor result = new Tensor(inputDims[0], inputDims[1], 1).setByCoord(c -> {
return IntStream.range(0, inputDims[2]).mapToDouble(i -> {
int[] coords = c.getCoords();
return tensor.get(coords[0], coords[1], i);
}).sum();
});
tensor.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(delta.stream().map(deltaTensor -> {
int[] deltaDims = deltaTensor.getDimensions();
Tensor result = new Tensor(deltaDims[0], deltaDims[1], inputDims[2]).setByCoord(c -> {
int[] coords = c.getCoords();
return deltaTensor.get(coords[0], coords[1], 0);
});
deltaTensor.freeRef();
return result;
}).toArray(i -> new Tensor[i]));
input.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
inputData.freeRef();
input.freeRef();
}
@Override
public boolean isAlive() {
return input.isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.DeltaSet in project MindsEye by SimiaCryptus.
the class ImgReshapeLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
// assert Arrays.stream(inObj).flatMapToDouble(input-> input.getData().stream().flatMapToDouble(x-> Arrays.stream(x.getData()))).allMatch(v->Double.isFinite(v));
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;
assert expand || 0 == inputDims[0] % kernelSizeX : (inputDims[0] + " % " + kernelSizeX);
assert expand || 0 == inputDims[1] % kernelSizeY : (inputDims[1] + " % " + kernelSizeY);
assert !expand || 0 == inputDims[2] % (kernelSizeX * kernelSizeY);
// assert input.getData().stream().flatMapToDouble(x-> Arrays.stream(x.getData())).allMatch(v->Double.isFinite(v));
Tensor outputDims;
if (expand) {
outputDims = new Tensor(inputDims[0] * kernelSizeX, inputDims[1] * kernelSizeY, inputDims[2] / (kernelSizeX * kernelSizeY));
} else {
outputDims = new Tensor(inputDims[0] / kernelSizeX, inputDims[1] / kernelSizeY, inputDims[2] * kernelSizeX * kernelSizeY);
}
TensorArray data = TensorArray.wrap(IntStream.range(0, batch.length()).parallel().mapToObj(dataIndex -> {
Tensor inputData = batch.get(dataIndex);
Tensor tensor = expand ? ImgReshapeLayer.copyExpand(inputData, outputDims.copy()) : ImgReshapeLayer.copyCondense(inputData, outputDims.copy());
inputData.freeRef();
return tensor;
}).toArray(i -> new Tensor[i]));
outputDims.freeRef();
return new Result(data, (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList error) -> {
// assert error.stream().flatMapToDouble(x-> Arrays.stream(x.getData())).allMatch(v->Double.isFinite(v));
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);
Tensor tensor = expand ? ImgReshapeLayer.copyCondense(err, passback) : ImgReshapeLayer.copyExpand(err, passback);
err.freeRef();
return tensor;
}).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();
}
};
}
Aggregations