use of com.simiacryptus.mindseye.lang.Delta in project MindsEye by SimiaCryptus.
the class ImgBandBiasLayer method eval.
/**
* Eval nn result.
*
* @param input the input
* @return the nn result
*/
@Nonnull
public Result eval(@Nonnull final Result input) {
@Nullable final double[] bias = getBias();
input.addRef();
return new Result(TensorArray.wrap(input.getData().stream().parallel().map(r -> {
if (r.getDimensions().length != 3) {
throw new IllegalArgumentException(Arrays.toString(r.getDimensions()));
}
if (r.getDimensions()[2] != bias.length) {
throw new IllegalArgumentException(String.format("%s: %s does not have %s bands", getName(), Arrays.toString(r.getDimensions()), bias.length));
}
@Nonnull Tensor tensor = new Tensor(add(r.getData()), r.getDimensions());
r.freeRef();
return tensor;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
if (!isFrozen()) {
final Delta<Layer> deltaBuffer = buffer.get(ImgBandBiasLayer.this, bias);
data.stream().parallel().forEach(d -> {
final double[] array = RecycleBin.DOUBLES.obtain(bias.length);
@Nullable final double[] signal = d.getData();
final int size = signal.length / bias.length;
for (int i = 0; i < signal.length; i++) {
array[i / size] += signal[i];
if (!Double.isFinite(array[i / size])) {
array[i / size] = 0.0;
}
}
d.freeRef();
assert Arrays.stream(array).allMatch(v -> Double.isFinite(v));
deltaBuffer.addInPlace(array);
RecycleBin.DOUBLES.recycle(array, array.length);
});
deltaBuffer.freeRef();
}
if (input.isAlive()) {
data.addRef();
input.accumulate(buffer, data);
}
}) {
@Override
protected void _free() {
input.freeRef();
}
@Override
public boolean isAlive() {
return input.isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.Delta in project MindsEye by SimiaCryptus.
the class BiasLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
TensorList input;
if (0 == inObj.length) {
input = TensorArray.create();
} else {
input = inObj[0].getData();
}
return new Result(TensorArray.wrap(input.stream().parallel().map(r -> {
@Nonnull Tensor tensor = new Tensor(add(r.getData()), r.getDimensions());
r.freeRef();
return tensor;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (!isFrozen()) {
final Delta<Layer> deltaBuffer = buffer.get(BiasLayer.this, bias);
if (1 == bias.length) {
delta.stream().parallel().forEach(d -> {
@Nullable final double[] array = d.getData();
deltaBuffer.addInPlace(1 == array.length ? array : new double[] { Arrays.stream(array).sum() });
d.freeRef();
});
} else {
delta.stream().parallel().forEach(d -> {
deltaBuffer.addInPlace(d.getData());
d.freeRef();
});
}
deltaBuffer.freeRef();
}
if (0 < inObj.length && inObj[0].isAlive()) {
delta.addRef();
inObj[0].accumulate(buffer, delta);
}
}) {
@Override
protected void _free() {
Arrays.stream(inObj).forEach(nnResult -> nnResult.freeRef());
}
@Override
public boolean isAlive() {
return 0 < inObj.length && inObj[0].isAlive() || !isFrozen();
}
};
}
use of com.simiacryptus.mindseye.lang.Delta in project MindsEye by SimiaCryptus.
the class BatchDerivativeTester method getFeedbackGradient.
@Nonnull
private Tensor getFeedbackGradient(@Nonnull final Layer component, final int inputIndex, @Nonnull final Tensor outputPrototype, final Tensor... inputPrototype) {
final Tensor inputTensor = inputPrototype[inputIndex];
final int inputDims = inputTensor.length();
@Nonnull final Tensor result = new Tensor(inputDims, outputPrototype.length());
for (int j = 0; j < outputPrototype.length(); j++) {
final int j_ = j;
@Nonnull final PlaceholderLayer<Tensor> inputKey = new PlaceholderLayer<Tensor>(new Tensor());
@Nonnull final Result copyInput = new Result(TensorArray.create(inputPrototype), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
@Nonnull final Tensor gradientBuffer = new Tensor(inputDims, outputPrototype.length());
if (!Arrays.equals(inputTensor.getDimensions(), data.get(inputIndex).getDimensions())) {
throw new AssertionError();
}
for (int i = 0; i < inputDims; i++) {
gradientBuffer.set(new int[] { i, j_ }, data.get(inputIndex).getData()[i]);
}
buffer.get(inputKey, new double[gradientBuffer.length()]).addInPlace(gradientBuffer.getData());
}) {
@Override
public boolean isAlive() {
return true;
}
};
@Nullable final Result eval = component.eval(copyInput);
@Nonnull final DeltaSet<Layer> xxx = new DeltaSet<Layer>();
@Nonnull TensorArray tensorArray = TensorArray.wrap(eval.getData().stream().map(x -> {
@Nonnull Tensor set = x.set(j_, 1);
x.freeRef();
return set;
}).toArray(i -> new Tensor[i]));
eval.accumulate(xxx, tensorArray);
final Delta<Layer> inputDelta = xxx.getMap().get(inputKey);
if (null != inputDelta) {
result.addInPlace(new Tensor(inputDelta.getDelta(), result.getDimensions()));
}
}
return result;
}
use of com.simiacryptus.mindseye.lang.Delta 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.Delta 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;
}
}
Aggregations