use of com.simiacryptus.mindseye.layers.java.LinearActivationLayer in project MindsEye by SimiaCryptus.
the class BinarySumLayer method getCompatibilityLayer.
/**
* Gets compatibility layer.
*
* @return the compatibility layer
*/
@Nonnull
public Layer getCompatibilityLayer() {
@Nonnull PipelineNetwork network = new PipelineNetwork(2);
network.wrap(new SumInputsLayer(), network.wrap(new LinearActivationLayer().setScale(this.leftFactor).freeze(), network.getInput(0)), network.wrap(new LinearActivationLayer().setScale(this.rightFactor).freeze(), network.getInput(1)));
return network;
}
use of com.simiacryptus.mindseye.layers.java.LinearActivationLayer in project MindsEye by SimiaCryptus.
the class SigmoidTreeNetwork method getHead.
@Nullable
@Override
public synchronized DAGNode getHead() {
if (null == head) {
synchronized (this) {
if (null == head) {
reset();
final DAGNode input = getInput(0);
switch(getMode()) {
case Linear:
head = add(alpha.setFrozen(false), add(alphaBias.setFrozen(false), input));
break;
case Fuzzy:
{
final DAGNode gateNode = add(gate.setFrozen(false), null != gateBias ? add(gateBias.setFrozen(false), input) : input);
head = add(new ProductInputsLayer(), add(alpha.setFrozen(false), add(alphaBias.setFrozen(false), input)), add(new LinearActivationLayer().setScale(2).freeze(), add(new SigmoidActivationLayer().setBalanced(false), gateNode)));
break;
}
case Bilinear:
{
final DAGNode gateNode = add(gate.setFrozen(false), null != gateBias ? add(gateBias.setFrozen(false), input) : input);
head = add(new SumInputsLayer(), add(new ProductInputsLayer(), add(alpha.setFrozen(false), add(alphaBias.setFrozen(false), input)), add(new SigmoidActivationLayer().setBalanced(false), gateNode)), add(new ProductInputsLayer(), add(beta.setFrozen(false), add(betaBias.setFrozen(false), input)), add(new SigmoidActivationLayer().setBalanced(false), add(new LinearActivationLayer().setScale(-1).freeze(), gateNode))));
break;
}
case Final:
final DAGNode gateNode = add(gate.setFrozen(false), null != gateBias ? add(gateBias.setFrozen(false), input) : input);
head = add(new SumInputsLayer(), add(new ProductInputsLayer(), add(alpha, input), add(new SigmoidActivationLayer().setBalanced(false), gateNode)), add(new ProductInputsLayer(), add(beta, input), add(new SigmoidActivationLayer().setBalanced(false), add(new LinearActivationLayer().setScale(-1).freeze(), gateNode))));
break;
}
}
}
}
return head;
}
use of com.simiacryptus.mindseye.layers.java.LinearActivationLayer in project MindsEye by SimiaCryptus.
the class ImageClassifier method deepDream.
/**
* Deep dream.
*
* @param log the log
* @param image the image
*/
public void deepDream(@Nonnull final NotebookOutput log, final Tensor image) {
log.code(() -> {
@Nonnull ArrayList<StepRecord> history = new ArrayList<>();
@Nonnull PipelineNetwork clamp = new PipelineNetwork(1);
clamp.add(new ActivationLayer(ActivationLayer.Mode.RELU));
clamp.add(new LinearActivationLayer().setBias(255).setScale(-1).freeze());
clamp.add(new ActivationLayer(ActivationLayer.Mode.RELU));
clamp.add(new LinearActivationLayer().setBias(255).setScale(-1).freeze());
@Nonnull PipelineNetwork supervised = new PipelineNetwork(1);
supervised.add(getNetwork().freeze(), supervised.wrap(clamp, supervised.getInput(0)));
// CudaTensorList gpuInput = CudnnHandle.apply(gpu -> {
// Precision precision = Precision.Float;
// return CudaTensorList.wrap(gpu.getPtr(TensorArray.wrap(image), precision, MemoryType.Managed), 1, image.getDimensions(), precision);
// });
// @Nonnull Trainable trainable = new TensorListTrainable(supervised, gpuInput).setVerbosity(1).setMask(true);
@Nonnull Trainable trainable = new ArrayTrainable(supervised, 1).setVerbose(true).setMask(true, false).setData(Arrays.<Tensor[]>asList(new Tensor[] { image }));
new IterativeTrainer(trainable).setMonitor(getTrainingMonitor(history, supervised)).setOrientation(new QQN()).setLineSearchFactory(name -> new ArmijoWolfeSearch()).setTimeout(60, TimeUnit.MINUTES).runAndFree();
return TestUtil.plot(history);
});
}
use of com.simiacryptus.mindseye.layers.java.LinearActivationLayer in project MindsEye by SimiaCryptus.
the class ImageClassifier method deepDream.
/**
* Deep dream.
*
* @param log the log
* @param image the image
* @param targetCategoryIndex the target category index
* @param totalCategories the total categories
* @param config the config
* @param network the network
* @param lossLayer the loss layer
* @param targetValue the target value
*/
public void deepDream(@Nonnull final NotebookOutput log, final Tensor image, final int targetCategoryIndex, final int totalCategories, Function<IterativeTrainer, IterativeTrainer> config, final Layer network, final Layer lossLayer, final double targetValue) {
@Nonnull List<Tensor[]> data = Arrays.<Tensor[]>asList(new Tensor[] { image, new Tensor(1, 1, totalCategories).set(targetCategoryIndex, targetValue) });
log.code(() -> {
for (Tensor[] tensors : data) {
ImageClassifier.log.info(log.image(tensors[0].toImage(), "") + tensors[1]);
}
});
log.code(() -> {
@Nonnull ArrayList<StepRecord> history = new ArrayList<>();
@Nonnull PipelineNetwork clamp = new PipelineNetwork(1);
clamp.add(new ActivationLayer(ActivationLayer.Mode.RELU));
clamp.add(new LinearActivationLayer().setBias(255).setScale(-1).freeze());
clamp.add(new ActivationLayer(ActivationLayer.Mode.RELU));
clamp.add(new LinearActivationLayer().setBias(255).setScale(-1).freeze());
@Nonnull PipelineNetwork supervised = new PipelineNetwork(2);
supervised.wrap(lossLayer, supervised.add(network.freeze(), supervised.wrap(clamp, supervised.getInput(0))), supervised.getInput(1));
// TensorList[] gpuInput = data.stream().map(data1 -> {
// return CudnnHandle.apply(gpu -> {
// Precision precision = Precision.Float;
// return CudaTensorList.wrap(gpu.getPtr(TensorArray.wrap(data1), precision, MemoryType.Managed), 1, image.getDimensions(), precision);
// });
// }).toArray(i -> new TensorList[i]);
// @Nonnull Trainable trainable = new TensorListTrainable(supervised, gpuInput).setVerbosity(1).setMask(true);
@Nonnull Trainable trainable = new ArrayTrainable(supervised, 1).setVerbose(true).setMask(true, false).setData(data);
config.apply(new IterativeTrainer(trainable).setMonitor(getTrainingMonitor(history, supervised)).setOrientation(new QQN()).setLineSearchFactory(name -> new ArmijoWolfeSearch()).setTimeout(60, TimeUnit.MINUTES)).setTerminateThreshold(Double.NEGATIVE_INFINITY).runAndFree();
return TestUtil.plot(history);
});
}
use of com.simiacryptus.mindseye.layers.java.LinearActivationLayer in project MindsEye by SimiaCryptus.
the class ArtistryUtil method getClamp.
/**
* Gets clamp.
*
* @param max the max
* @return the clamp
*/
@Nonnull
public static PipelineNetwork getClamp(final int max) {
@Nonnull PipelineNetwork clamp = new PipelineNetwork(1);
clamp.add(new ActivationLayer(ActivationLayer.Mode.RELU));
clamp.add(new LinearActivationLayer().setBias(max).setScale(-1).freeze());
clamp.add(new ActivationLayer(ActivationLayer.Mode.RELU));
clamp.add(new LinearActivationLayer().setBias(max).setScale(-1).freeze());
return clamp;
}
Aggregations