use of com.simiacryptus.mindseye.lang.DeltaSet in project MindsEye by SimiaCryptus.
the class RecursiveSubspace method buildSubspace.
/**
* Build subspace nn layer.
*
* @param subject the subject
* @param measurement the measurement
* @param monitor the monitor
* @return the nn layer
*/
@Nullable
public Layer buildSubspace(@Nonnull Trainable subject, @Nonnull PointSample measurement, @Nonnull TrainingMonitor monitor) {
@Nonnull PointSample origin = measurement.copyFull().backup();
@Nonnull final DeltaSet<Layer> direction = measurement.delta.scale(-1);
final double magnitude = direction.getMagnitude();
if (Math.abs(magnitude) < 1e-10) {
monitor.log(String.format("Zero gradient: %s", magnitude));
} else if (Math.abs(magnitude) < 1e-5) {
monitor.log(String.format("Low gradient: %s", magnitude));
}
boolean hasPlaceholders = direction.getMap().entrySet().stream().filter(x -> x.getKey() instanceof PlaceholderLayer).findAny().isPresent();
List<Layer> deltaLayers = direction.getMap().entrySet().stream().map(x -> x.getKey()).filter(x -> !(x instanceof PlaceholderLayer)).collect(Collectors.toList());
int size = deltaLayers.size() + (hasPlaceholders ? 1 : 0);
if (null == weights || weights.length != size)
weights = new double[size];
return new LayerBase() {
@Nonnull
Layer self = this;
@Nonnull
@Override
public Result eval(Result... array) {
assertAlive();
origin.restore();
IntStream.range(0, deltaLayers.size()).forEach(i -> {
direction.getMap().get(deltaLayers.get(i)).accumulate(weights[hasPlaceholders ? (i + 1) : i]);
});
if (hasPlaceholders) {
direction.getMap().entrySet().stream().filter(x -> x.getKey() instanceof PlaceholderLayer).distinct().forEach(entry -> entry.getValue().accumulate(weights[0]));
}
PointSample measure = subject.measure(monitor);
double mean = measure.getMean();
monitor.log(String.format("RecursiveSubspace: %s <- %s", mean, Arrays.toString(weights)));
direction.addRef();
return new Result(TensorArray.wrap(new Tensor(mean)), (DeltaSet<Layer> buffer, TensorList data) -> {
DoubleStream deltaStream = deltaLayers.stream().mapToDouble(layer -> {
Delta<Layer> a = direction.getMap().get(layer);
Delta<Layer> b = measure.delta.getMap().get(layer);
return b.dot(a) / Math.max(Math.sqrt(a.dot(a)), 1e-8);
});
if (hasPlaceholders) {
deltaStream = DoubleStream.concat(DoubleStream.of(direction.getMap().keySet().stream().filter(x -> x instanceof PlaceholderLayer).distinct().mapToDouble(layer -> {
Delta<Layer> a = direction.getMap().get(layer);
Delta<Layer> b = measure.delta.getMap().get(layer);
return b.dot(a) / Math.max(Math.sqrt(a.dot(a)), 1e-8);
}).sum()), deltaStream);
}
buffer.get(self, weights).addInPlace(deltaStream.toArray()).freeRef();
}) {
@Override
protected void _free() {
measure.freeRef();
direction.freeRef();
}
@Override
public boolean isAlive() {
return true;
}
};
}
@Override
protected void _free() {
direction.freeRef();
origin.freeRef();
super._free();
}
@Nonnull
@Override
public JsonObject getJson(Map<CharSequence, byte[]> resources, DataSerializer dataSerializer) {
throw new IllegalStateException();
}
@Nullable
@Override
public List<double[]> state() {
return null;
}
};
}
use of com.simiacryptus.mindseye.lang.DeltaSet in project MindsEye by SimiaCryptus.
the class SimpleEval method call.
@Nonnull
@Override
public SimpleEval call() {
Tensor[] inputCopy = Arrays.stream(input).map(x -> x.copy()).toArray(i -> new Tensor[i]);
derivative = Arrays.stream(inputCopy).map(input -> new Tensor(input.getDimensions())).toArray(i -> new Tensor[i]);
Result[] input = IntStream.range(0, inputCopy.length).mapToObj(i -> {
return new Result(TensorArray.create(inputCopy[i]), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
data.stream().forEach(t -> {
derivative[i].addInPlace(t);
t.freeRef();
});
}) {
@Override
protected void _free() {
}
@Override
public boolean isAlive() {
return true;
}
};
}).toArray(i -> new Result[i]);
@Nullable final Result eval;
try {
eval = layer.eval(input);
} finally {
for (@Nonnull Result result : input) {
result.getData().freeRef();
result.freeRef();
}
for (@Nonnull Tensor tensor : inputCopy) {
tensor.freeRef();
}
}
TensorList evalData = eval.getData();
TensorList outputTensorList = evalData.copy();
@Nullable Tensor outputTensor = outputTensorList.get(0);
@Nonnull DeltaSet<Layer> deltaSet = new DeltaSet<>();
try {
synchronized (this) {
if (null != output) {
output.freeRef();
output = null;
}
}
output = outputTensor.copy();
@Nonnull TensorList tensorList = getFeedback(outputTensorList);
eval.accumulate(deltaSet, tensorList);
return this;
} finally {
outputTensor.freeRef();
evalData.freeRef();
outputTensorList.freeRef();
eval.freeRef();
deltaSet.freeRef();
}
}
use of com.simiacryptus.mindseye.lang.DeltaSet in project MindsEye by SimiaCryptus.
the class SimpleListEval method call.
@Nonnull
@Override
public SimpleResult call() {
TensorList[] inputCopy = Arrays.stream(input).map(x -> x.copy()).toArray(i -> new TensorList[i]);
inputDerivative = Arrays.stream(inputCopy).map(tensorList -> TensorArray.wrap(tensorList.stream().map(i -> {
@Nonnull Tensor tensor = new Tensor(i.getDimensions());
i.freeRef();
return tensor;
}).toArray(i -> new Tensor[i]))).toArray(i -> new TensorList[i]);
Result[] inputs = IntStream.range(0, inputCopy.length).mapToObj(i -> {
return new Result(inputCopy[i], (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
SimpleListEval.accumulate(inputDerivative[i], data);
}) {
@Override
public boolean isAlive() {
return true;
}
};
}).toArray(i -> new Result[i]);
@Nullable final Result eval = layer.eval(inputs);
for (@Nonnull Result result : inputs) {
result.freeRef();
}
TensorList outputData = eval.getData().copy();
for (@Nonnull TensorList tensorList : inputCopy) {
tensorList.freeRef();
}
eval.getData().freeRef();
@Nonnull TensorList tensorList = getFeedback(outputData);
this.layerDerivative.freeRef();
this.layerDerivative = new DeltaSet<>();
eval.accumulate(layerDerivative, tensorList);
eval.freeRef();
output = outputData;
return this;
}
Aggregations