use of org.deeplearning4j.optimize.listeners.ScoreIterationListener in project deeplearning4j by deeplearning4j.
the class MultipleEpochsIteratorTest method testCifarDataSetIteratorReset.
// use when checking cifar dataset iterator
@Ignore
@Test
public void testCifarDataSetIteratorReset() {
int epochs = 2;
Nd4j.getRandom().setSeed(12345);
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().regularization(false).learningRate(1.0).weightInit(WeightInit.XAVIER).seed(12345L).list().layer(0, new DenseLayer.Builder().nIn(400).nOut(50).activation(Activation.RELU).build()).layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(50).nOut(10).build()).pretrain(false).backprop(true).inputPreProcessor(0, new CnnToFeedForwardPreProcessor(20, 20, 1)).build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
net.setListeners(new ScoreIterationListener(1));
MultipleEpochsIterator ds = new MultipleEpochsIterator(epochs, new CifarDataSetIterator(10, 20, new int[] { 20, 20, 1 }));
net.fit(ds);
assertEquals(epochs, ds.epochs);
assertEquals(2, ds.batch);
}
use of org.deeplearning4j.optimize.listeners.ScoreIterationListener in project deeplearning4j by deeplearning4j.
the class MultiLayerNeuralNetConfigurationTest method testIterationListener.
@Test
public void testIterationListener() {
MultiLayerNetwork model1 = new MultiLayerNetwork(getConf());
model1.init();
model1.setListeners(Collections.singletonList((IterationListener) new ScoreIterationListener(1)));
MultiLayerNetwork model2 = new MultiLayerNetwork(getConf());
model2.setListeners(Collections.singletonList((IterationListener) new ScoreIterationListener(1)));
model2.init();
Layer[] l1 = model1.getLayers();
for (int i = 0; i < l1.length; i++) assertTrue(l1[i].getListeners() != null && l1[i].getListeners().size() == 1);
Layer[] l2 = model2.getLayers();
for (int i = 0; i < l2.length; i++) assertTrue(l2[i].getListeners() != null && l2[i].getListeners().size() == 1);
}
use of org.deeplearning4j.optimize.listeners.ScoreIterationListener in project deeplearning4j by deeplearning4j.
the class OutputLayerTest method testWeightsDifferent.
@Test
public void testWeightsDifferent() {
Nd4j.MAX_ELEMENTS_PER_SLICE = Integer.MAX_VALUE;
Nd4j.MAX_SLICES_TO_PRINT = Integer.MAX_VALUE;
NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).miniBatch(false).seed(123).iterations(1000).learningRate(1e-1).layer(new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder().nIn(4).nOut(3).weightInit(WeightInit.XAVIER).updater(Updater.ADAGRAD).lossFunction(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).activation(Activation.SOFTMAX).build()).build();
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray params = Nd4j.create(1, numParams);
OutputLayer o = (OutputLayer) conf.getLayer().instantiate(conf, null, 0, params, true);
o.setBackpropGradientsViewArray(Nd4j.create(1, params.length()));
int numSamples = 150;
int batchSize = 150;
DataSetIterator iter = new IrisDataSetIterator(batchSize, numSamples);
// Loads data into generator and format consumable for NN
DataSet iris = iter.next();
iris.normalizeZeroMeanZeroUnitVariance();
o.setListeners(new ScoreIterationListener(1));
SplitTestAndTrain t = iris.splitTestAndTrain(0.8);
o.fit(t.getTrain());
log.info("Evaluate model....");
Evaluation eval = new Evaluation(3);
eval.eval(t.getTest().getLabels(), o.output(t.getTest().getFeatureMatrix(), true));
log.info(eval.stats());
}
use of org.deeplearning4j.optimize.listeners.ScoreIterationListener in project deeplearning4j by deeplearning4j.
the class AutoEncoderTest method testAutoEncoder.
@Test
public void testAutoEncoder() throws Exception {
MnistDataFetcher fetcher = new MnistDataFetcher(true);
NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().momentum(0.9f).optimizationAlgo(OptimizationAlgorithm.LINE_GRADIENT_DESCENT).iterations(1).learningRate(1e-1f).layer(new org.deeplearning4j.nn.conf.layers.AutoEncoder.Builder().nIn(784).nOut(600).corruptionLevel(0.6).lossFunction(LossFunctions.LossFunction.RECONSTRUCTION_CROSSENTROPY).build()).build();
fetcher.fetch(100);
DataSet d2 = fetcher.next();
INDArray input = d2.getFeatureMatrix();
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray params = Nd4j.create(1, numParams);
AutoEncoder da = (AutoEncoder) conf.getLayer().instantiate(conf, Arrays.<IterationListener>asList(new ScoreIterationListener(1)), 0, params, true);
assertEquals(da.params(), da.params());
assertEquals(471784, da.params().length());
da.setParams(da.params());
da.fit(input);
}
use of org.deeplearning4j.optimize.listeners.ScoreIterationListener in project deeplearning4j by deeplearning4j.
the class OutputLayerTest method testBinary.
@Test
public void testBinary() {
Nd4j.MAX_ELEMENTS_PER_SLICE = Integer.MAX_VALUE;
Nd4j.MAX_SLICES_TO_PRINT = Integer.MAX_VALUE;
DataBuffer.Type initialType = Nd4j.dataType();
DataTypeUtil.setDTypeForContext(DataBuffer.Type.DOUBLE);
INDArray data = Nd4j.create(new double[][] { { 1, 1, 1, 0, 0, 0 }, { 1, 0, 1, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0 }, { 0, 0, 1, 1, 1, 0 }, { 0, 0, 1, 1, 0, 0 }, { 0, 0, 1, 1, 1, 0 } });
INDArray data2 = Nd4j.create(new double[][] { { 1, 0 }, { 1, 0 }, { 1, 0 }, { 0, 1 }, { 0, 1 }, { 0, 1 } });
DataSet dataset = new DataSet(data, data2);
NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(123).iterations(200).learningRate(1e-2).layer(new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder().nIn(6).nOut(2).weightInit(WeightInit.ZERO).updater(Updater.SGD).activation(Activation.SOFTMAX).lossFunction(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).build()).build();
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray params = Nd4j.create(1, numParams);
OutputLayer o = (OutputLayer) conf.getLayer().instantiate(conf, null, 0, params, true);
o.setBackpropGradientsViewArray(Nd4j.create(1, params.length()));
o.setListeners(new ScoreIterationListener(1));
o.fit(dataset);
DataTypeUtil.setDTypeForContext(initialType);
}
Aggregations