use of com.simiacryptus.mindseye.lang.TensorList 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.TensorList in project MindsEye by SimiaCryptus.
the class TrainingTester method train.
private List<StepRecord> train(@Nonnull NotebookOutput log, @Nonnull BiFunction<NotebookOutput, Trainable, List<StepRecord>> opt, @Nonnull Layer layer, @Nonnull Tensor[][] data, @Nonnull boolean... mask) {
try {
int inputs = data[0].length;
@Nonnull final PipelineNetwork network = new PipelineNetwork(inputs);
network.wrap(new MeanSqLossLayer(), network.add(layer, IntStream.range(0, inputs - 1).mapToObj(i -> network.getInput(i)).toArray(i -> new DAGNode[i])), network.getInput(inputs - 1));
@Nonnull ArrayTrainable trainable = new ArrayTrainable(data, network);
if (0 < mask.length)
trainable.setMask(mask);
List<StepRecord> history;
try {
history = opt.apply(log, trainable);
if (history.stream().mapToDouble(x -> x.fitness).min().orElse(1) > 1e-5) {
if (!network.isFrozen()) {
log.p("This training apply resulted in the following configuration:");
log.code(() -> {
return network.state().stream().map(Arrays::toString).reduce((a, b) -> a + "\n" + b).orElse("");
});
}
if (0 < mask.length) {
log.p("And regressed input:");
log.code(() -> {
return Arrays.stream(data).flatMap(x -> Arrays.stream(x)).limit(1).map(x -> x.prettyPrint()).reduce((a, b) -> a + "\n" + b).orElse("");
});
}
log.p("To produce the following output:");
log.code(() -> {
Result[] array = ConstantResult.batchResultArray(pop(data));
@Nullable Result eval = layer.eval(array);
for (@Nonnull Result result : array) {
result.freeRef();
result.getData().freeRef();
}
TensorList tensorList = eval.getData();
eval.freeRef();
String str = tensorList.stream().limit(1).map(x -> {
String s = x.prettyPrint();
x.freeRef();
return s;
}).reduce((a, b) -> a + "\n" + b).orElse("");
tensorList.freeRef();
return str;
});
} else {
log.p("Training Converged");
}
} finally {
trainable.freeRef();
network.freeRef();
}
return history;
} finally {
layer.freeRef();
for (@Nonnull Tensor[] tensors : data) {
for (@Nonnull Tensor tensor : tensors) {
tensor.freeRef();
}
}
}
}
use of com.simiacryptus.mindseye.lang.TensorList 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.TensorList 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.TensorList 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();
}
});
}
Aggregations