use of com.simiacryptus.mindseye.lang.ReferenceCounting in project MindsEye by SimiaCryptus.
the class EntropyLossLayer method eval.
@Nonnull
@Override
public Result eval(@Nonnull final Result... inObj) {
Arrays.stream(inObj).forEach(nnResult -> nnResult.addRef());
final double zero_tol = 1e-12;
final Result in0 = inObj[0];
TensorList indata = in0.getData();
indata.addRef();
@Nonnull final Tensor[] gradient = new Tensor[indata.length()];
final double max_prob = 1.;
return new Result(TensorArray.wrap(IntStream.range(0, indata.length()).mapToObj(dataIndex -> {
@Nullable final Tensor l = indata.get(dataIndex);
@Nullable final Tensor r = inObj[1].getData().get(dataIndex);
assert l.length() == r.length() : l.length() + " != " + r.length();
@Nonnull final Tensor gradientTensor = new Tensor(l.getDimensions());
@Nullable final double[] gradientData = gradientTensor.getData();
double total = 0;
@Nullable final double[] ld = l.getData();
@Nullable final double[] rd = r.getData();
for (int i = 0; i < l.length(); i++) {
final double lv = Math.max(Math.min(ld[i], max_prob), zero_tol);
final double rv = rd[i];
if (rv > 0) {
gradientData[i] = -rv / lv;
total += -rv * Math.log(lv);
} else {
gradientData[i] = 0;
}
}
l.freeRef();
r.freeRef();
assert total >= 0;
gradient[dataIndex] = gradientTensor;
@Nonnull final Tensor outValue = new Tensor(new double[] { total }, 1);
return outValue;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (inObj[1].isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
Tensor deltaTensor = delta.get(dataIndex);
@Nullable final Tensor inputTensor = indata.get(dataIndex);
@Nonnull final Tensor passback = new Tensor(gradient[dataIndex].getDimensions());
for (int i = 0; i < passback.length(); i++) {
final double lv = Math.max(Math.min(inputTensor.get(i), max_prob), zero_tol);
passback.set(i, -deltaTensor.get(0) * Math.log(lv));
}
inputTensor.freeRef();
deltaTensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
inObj[1].accumulate(buffer, tensorArray);
}
if (in0.isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
Tensor tensor = delta.get(dataIndex);
@Nonnull final Tensor passback = new Tensor(gradient[dataIndex].getDimensions());
for (int i = 0; i < passback.length(); i++) {
passback.set(i, tensor.get(0) * gradient[dataIndex].get(i));
}
tensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
in0.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
indata.freeRef();
Arrays.stream(gradient).forEach(ReferenceCounting::freeRef);
Arrays.stream(inObj).forEach(ReferenceCounting::freeRef);
}
@Override
public boolean isAlive() {
return in0.isAlive() || in0.isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.ReferenceCounting in project MindsEye by SimiaCryptus.
the class TrainingTester method testModelLearning.
/**
* Test model learning.
*
* @param log the log
* @param component the component
* @param random the randomize
* @param inputPrototype the input prototype
* @return the apply result
*/
public TestResult testModelLearning(@Nonnull final NotebookOutput log, @Nonnull final Layer component, final Random random, final Tensor[] inputPrototype) {
@Nonnull final Layer network_target = shuffle(random, component.copy()).freeze();
final Tensor[][] input_target = shuffleCopy(random, inputPrototype);
log.p("In this apply, attempt to train a network to emulate a randomized network given an example input/output. The target state is:");
log.code(() -> {
return network_target.state().stream().map(Arrays::toString).reduce((a, b) -> a + "\n" + b).orElse("");
});
Result[] array = ConstantResult.batchResultArray(input_target);
Result eval = network_target.eval(array);
Arrays.stream(array).forEach(ReferenceCounting::freeRef);
TensorList result = eval.getData();
eval.freeRef();
final Tensor[] output_target = result.stream().toArray(i -> new Tensor[i]);
result.freeRef();
if (output_target.length != input_target.length) {
logger.info("Batch layers not supported");
return null;
}
return trainAll("Model Convergence", log, append(input_target, output_target), shuffle(random, component.copy()));
}
use of com.simiacryptus.mindseye.lang.ReferenceCounting in project MindsEye by SimiaCryptus.
the class CudaLayerTester method testNonstandardBounds.
/**
* Test nonstandard bounds tolerance statistics.
*
* @param log the log
* @param reference the reference
* @param inputPrototype the input prototype
* @return the tolerance statistics
*/
@Nonnull
public ToleranceStatistics testNonstandardBounds(final NotebookOutput log, @Nullable final Layer reference, @Nonnull final Tensor[] inputPrototype) {
log.h2("Irregular Input");
log.p("This layer should be able to accept non-dense inputs.");
return log.code(() -> {
Tensor[] randomized = Arrays.stream(inputPrototype).map(x -> x.map(v -> getRandom())).toArray(i -> new Tensor[i]);
logger.info("Input: " + Arrays.stream(randomized).map(Tensor::prettyPrint).collect(Collectors.toList()));
Precision precision = Precision.Double;
TensorList[] controlInput = CudaSystem.run(gpu -> {
return Arrays.stream(randomized).map(original -> {
TensorArray data = TensorArray.create(original);
CudaTensorList wrap = CudaTensorList.wrap(gpu.getTensor(data, precision, MemoryType.Managed, false), 1, original.getDimensions(), precision);
data.freeRef();
return wrap;
}).toArray(i -> new TensorList[i]);
}, 0);
@Nonnull final SimpleResult controlResult = CudaSystem.run(gpu -> {
return SimpleGpuEval.run(reference, gpu, controlInput);
}, 1);
final TensorList[] irregularInput = CudaSystem.run(gpu -> {
return Arrays.stream(randomized).map(original -> {
return buildIrregularCudaTensor(gpu, precision, original);
}).toArray(i -> new TensorList[i]);
}, 0);
@Nonnull final SimpleResult testResult = CudaSystem.run(gpu -> {
return SimpleGpuEval.run(reference, gpu, irregularInput);
}, 1);
try {
ToleranceStatistics compareOutput = compareOutput(controlResult, testResult);
ToleranceStatistics compareDerivatives = compareDerivatives(controlResult, testResult);
return compareDerivatives.combine(compareOutput);
} finally {
Arrays.stream(randomized).forEach(ReferenceCountingBase::freeRef);
Arrays.stream(controlInput).forEach(ReferenceCounting::freeRef);
Arrays.stream(irregularInput).forEach(x -> x.freeRef());
controlResult.freeRef();
testResult.freeRef();
}
});
}
use of com.simiacryptus.mindseye.lang.ReferenceCounting in project MindsEye by SimiaCryptus.
the class LoggingWrapperLayer method eval.
@Override
public Result eval(@Nonnull final Result... inObj) {
final Result[] wrappedInput = IntStream.range(0, inObj.length).mapToObj(i -> {
final Result inputToWrap = inObj[i];
inputToWrap.addRef();
return new Result(inputToWrap.getData(), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
@Nonnull final String formatted = data.stream().map(x -> {
String str = x.prettyPrint();
x.freeRef();
return str;
}).reduce((a, b) -> a + "\n" + b).get();
log.info(String.format("Feedback Output %s for layer %s: \n\t%s", i, getInner().getName(), formatted.replaceAll("\n", "\n\t")));
data.addRef();
inputToWrap.accumulate(buffer, data);
}) {
@Override
protected void _free() {
inputToWrap.freeRef();
}
@Override
public boolean isAlive() {
return inputToWrap.isAlive();
}
};
}).toArray(i -> new Result[i]);
for (int i = 0; i < inObj.length; i++) {
final TensorList tensorList = inObj[i].getData();
@Nonnull final String formatted = tensorList.stream().map(x -> {
String str = x.prettyPrint();
x.freeRef();
return str;
}).reduce((a, b) -> a + "\n" + b).get();
log.info(String.format("Input %s for layer %s: \n\t%s", i, getInner().getName(), formatted.replaceAll("\n", "\n\t")));
}
@Nullable final Result output = getInner().eval(wrappedInput);
Arrays.stream(wrappedInput).forEach(ReferenceCounting::freeRef);
{
final TensorList tensorList = output.getData();
@Nonnull final String formatted = tensorList.stream().map(x -> {
String str = x.prettyPrint();
x.freeRef();
return str;
}).reduce((a, b) -> a + "\n" + b).get();
log.info(String.format("Output for layer %s: \n\t%s", getInner().getName(), formatted.replaceAll("\n", "\n\t")));
}
return new Result(output.getData(), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList data) -> {
@Nonnull final String formatted = data.stream().map(x -> {
String str = x.prettyPrint();
x.freeRef();
return str;
}).reduce((a, b) -> a + "\n" + b).get();
log.info(String.format("Feedback Input for layer %s: \n\t%s", getInner().getName(), formatted.replaceAll("\n", "\n\t")));
data.addRef();
output.accumulate(buffer, data);
}) {
@Override
protected void _free() {
output.freeRef();
}
@Override
public boolean isAlive() {
return output.isAlive();
}
};
}
use of com.simiacryptus.mindseye.lang.ReferenceCounting in project MindsEye by SimiaCryptus.
the class MaxDropoutNoiseLayer method eval.
@Nonnull
@Override
public Result eval(final Result... inObj) {
final Result in0 = inObj[0];
final TensorList data0 = in0.getData();
final int itemCnt = data0.length();
in0.addRef();
data0.addRef();
final Tensor[] mask = IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
@Nullable final Tensor input = data0.get(dataIndex);
@Nullable final Tensor output = input.map(x -> 0);
final List<List<Coordinate>> cells = getCellMap_cached.apply(new IntArray(output.getDimensions()));
cells.forEach(cell -> {
output.set(cell.stream().max(Comparator.comparingDouble(c -> input.get(c))).get(), 1);
});
input.freeRef();
return output;
}).toArray(i -> new Tensor[i]);
return new Result(TensorArray.wrap(IntStream.range(0, itemCnt).mapToObj(dataIndex -> {
Tensor inputData = data0.get(dataIndex);
@Nullable final double[] input = inputData.getData();
@Nullable final double[] maskT = mask[dataIndex].getData();
@Nonnull final Tensor output = new Tensor(inputData.getDimensions());
@Nullable final double[] outputData = output.getData();
for (int i = 0; i < outputData.length; i++) {
outputData[i] = input[i] * maskT[i];
}
inputData.freeRef();
return output;
}).toArray(i -> new Tensor[i])), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
if (in0.isAlive()) {
@Nonnull TensorArray tensorArray = TensorArray.wrap(IntStream.range(0, delta.length()).mapToObj(dataIndex -> {
Tensor deltaTensor = delta.get(dataIndex);
@Nullable final double[] deltaData = deltaTensor.getData();
@Nonnull final int[] dims = data0.getDimensions();
@Nullable final double[] maskData = mask[dataIndex].getData();
@Nonnull final Tensor passback = new Tensor(dims);
for (int i = 0; i < passback.length(); i++) {
passback.set(i, maskData[i] * deltaData[i]);
}
deltaTensor.freeRef();
return passback;
}).toArray(i -> new Tensor[i]));
in0.accumulate(buffer, tensorArray);
}
}) {
@Override
protected void _free() {
in0.freeRef();
data0.freeRef();
Arrays.stream(mask).forEach(ReferenceCounting::freeRef);
}
@Override
public boolean isAlive() {
return in0.isAlive() || !isFrozen();
}
};
}
Aggregations