use of org.deeplearning4j.nn.api.Updater in project deeplearning4j by deeplearning4j.
the class TestUpdaters method testNoOpUpdater.
@Test
public void testNoOpUpdater() {
Random r = new Random(12345L);
double lr = 0.5;
NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().learningRate(lr).layer(new DenseLayer.Builder().nIn(nIn).nOut(nOut).updater(org.deeplearning4j.nn.conf.Updater.NONE).build()).build();
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray params = Nd4j.create(1, numParams);
Layer layer = conf.getLayer().instantiate(conf, null, 0, params, true);
Updater updater = UpdaterCreator.getUpdater(layer);
for (int i = 0; i < weightGradient.length(); i++) weightGradient.putScalar(i, r.nextDouble());
for (int i = 0; i < biasGradient.length(); i++) biasGradient.putScalar(i, r.nextDouble());
gradient.gradientForVariable().put(DefaultParamInitializer.WEIGHT_KEY, weightGradient);
gradient.gradientForVariable().put(DefaultParamInitializer.BIAS_KEY, biasGradient);
updater.update(layer, gradient, -1, 1);
INDArray weightGradActual = gradient.getGradientFor(DefaultParamInitializer.WEIGHT_KEY);
INDArray biasGradActual = gradient.getGradientFor(DefaultParamInitializer.BIAS_KEY);
assertEquals(weightGradient, weightGradActual);
assertEquals(biasGradient, biasGradActual);
}
use of org.deeplearning4j.nn.api.Updater in project deeplearning4j by deeplearning4j.
the class VariationalAutoencoder method fit.
@Override
public void fit() {
if (input == null) {
throw new IllegalStateException("Cannot fit layer: layer input is null (not set)");
}
if (solver == null) {
solver = new Solver.Builder().model(this).configure(conf()).listeners(getListeners()).build();
//Set the updater state view array. For MLN and CG, this is done by MultiLayerUpdater and ComputationGraphUpdater respectively
Updater updater = solver.getOptimizer().getUpdater();
int updaterStateSize = updater.stateSizeForLayer(this);
if (updaterStateSize > 0) {
updater.setStateViewArray(this, Nd4j.createUninitialized(new int[] { 1, updaterStateSize }, Nd4j.order()), true);
}
}
this.optimizer = solver.getOptimizer();
solver.optimize();
}
use of org.deeplearning4j.nn.api.Updater in project deeplearning4j by deeplearning4j.
the class BaseOutputLayer method fit.
/**
* Fit the model
*
* @param input the examples to classify (one example in each row)
* @param labels the example labels(a binary outcome matrix)
*/
@Override
public void fit(INDArray input, INDArray labels) {
setInput(input);
setLabels(labels);
applyDropOutIfNecessary(true);
if (solver == null) {
solver = new Solver.Builder().configure(conf()).listeners(getListeners()).model(this).build();
//Set the updater state view array. For MLN and CG, this is done by MultiLayerUpdater and ComputationGraphUpdater respectively
Updater updater = solver.getOptimizer().getUpdater();
int updaterStateSize = updater.stateSizeForLayer(this);
if (updaterStateSize > 0)
updater.setStateViewArray(this, Nd4j.createUninitialized(new int[] { 1, updaterStateSize }, Nd4j.order()), true);
}
solver.optimize();
}
use of org.deeplearning4j.nn.api.Updater in project deeplearning4j by deeplearning4j.
the class BaseLayer method fit.
@Override
public void fit(INDArray input) {
if (input != null) {
setInput(input.dup());
applyDropOutIfNecessary(true);
}
if (solver == null) {
solver = new Solver.Builder().model(this).configure(conf()).listeners(getListeners()).build();
//Set the updater state view array. For MLN and CG, this is done by MultiLayerUpdater and ComputationGraphUpdater respectively
Updater updater = solver.getOptimizer().getUpdater();
int updaterStateSize = updater.stateSizeForLayer(this);
if (updaterStateSize > 0)
updater.setStateViewArray(this, Nd4j.createUninitialized(new int[] { 1, updaterStateSize }, Nd4j.order()), true);
}
this.optimizer = solver.getOptimizer();
solver.optimize();
}
use of org.deeplearning4j.nn.api.Updater in project deeplearning4j by deeplearning4j.
the class TestDecayPolicies method testMomentumScheduleSingleLayer.
@Test
public void testMomentumScheduleSingleLayer() {
double lr = 1e-2;
double mu = 0.6;
Map<Integer, Double> momentumAfter = new HashMap<>();
momentumAfter.put(1, 0.2);
int iterations = 2;
NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().learningRate(lr).momentum(mu).momentumAfter(momentumAfter).iterations(iterations).layer(new DenseLayer.Builder().nIn(nIn).nOut(nOut).updater(org.deeplearning4j.nn.conf.Updater.NESTEROVS).build()).build();
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray params = Nd4j.create(1, numParams);
Layer layer = conf.getLayer().instantiate(conf, null, 0, params, true);
Updater updater = UpdaterCreator.getUpdater(layer);
int stateSize = updater.stateSizeForLayer(layer);
updater.setStateViewArray(layer, Nd4j.create(1, stateSize), true);
Gradient gradientExpected = new DefaultGradient();
gradientExpected.setGradientFor(DefaultParamInitializer.WEIGHT_KEY, weightGradient.dup());
gradientExpected.setGradientFor(DefaultParamInitializer.BIAS_KEY, biasGradient.dup());
for (int i = 0; i < 2; i++) {
updater.update(layer, gradientSingle, i, 1);
mu = testNesterovsComputation(gradientSingle, gradientExpected, lr, mu, momentumAfter, i);
assertEquals(mu, layer.conf().getLayer().getMomentum(), 1e-4);
}
}
Aggregations