use of com.simiacryptus.mindseye.lang.Result 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.Result 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.Result in project MindsEye by SimiaCryptus.
the class DAGNetwork method eval.
@Nullable
@Override
public Result eval(final Result... input) {
assertAlive();
@Nonnull GraphEvaluationContext buildExeCtx = buildExeCtx(input);
@Nullable Result result;
try {
result = getHead().get(buildExeCtx);
} finally {
buildExeCtx.freeRef();
}
return result;
}
use of com.simiacryptus.mindseye.lang.Result 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.Result 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();
}
};
}
Aggregations