use of org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator in project deeplearning4j by deeplearning4j.
the class LocalResponseTest method testMultiCNNLayer.
@Test
public void testMultiCNNLayer() throws Exception {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.LINE_GRADIENT_DESCENT).iterations(1).seed(123).list().layer(0, new ConvolutionLayer.Builder().nIn(1).nOut(6).weightInit(WeightInit.XAVIER).activation(Activation.RELU).build()).layer(1, new LocalResponseNormalization.Builder().build()).layer(2, new DenseLayer.Builder().nOut(2).build()).layer(3, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).nIn(2).nOut(10).build()).backprop(true).pretrain(false).setInputType(InputType.convolutionalFlat(28, 28, 1)).build();
MultiLayerNetwork network = new MultiLayerNetwork(conf);
network.init();
DataSetIterator iter = new MnistDataSetIterator(2, 2);
DataSet next = iter.next();
network.fit(next);
}
use of org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator in project deeplearning4j by deeplearning4j.
the class MultiLayerTest method testOutput.
@Test
public void testOutput() throws Exception {
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(784).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).setInputType(InputType.convolutional(28, 28, 1)).build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
DataSetIterator fullData = new MnistDataSetIterator(1, 2);
net.fit(fullData);
fullData.reset();
DataSet expectedSet = fullData.next(2);
INDArray expectedOut = net.output(expectedSet.getFeatureMatrix(), false);
fullData.reset();
INDArray actualOut = net.output(fullData);
assertEquals(expectedOut, actualOut);
}
use of org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator in project deeplearning4j by deeplearning4j.
the class RBMTests method testBackprop.
@Test
public void testBackprop() throws Exception {
int numSamples = 10;
int batchSize = 10;
DataSetIterator mnistIter = new MnistDataSetIterator(batchSize, numSamples, true);
DataSet input = mnistIter.next();
INDArray features = input.getFeatures();
mnistIter.reset();
MultiLayerNetwork rbm = getRBMMLNNet(true, true, features, 50, 10, WeightInit.UNIFORM);
rbm.fit(mnistIter);
MultiLayerNetwork rbm2 = getRBMMLNNet(true, true, features, 50, 10, WeightInit.UNIFORM);
rbm2.fit(mnistIter);
DataSet test = mnistIter.next();
Evaluation eval = new Evaluation();
INDArray output = rbm.output(test.getFeatureMatrix());
eval.eval(test.getLabels(), output);
double f1Score = eval.f1();
Evaluation eval2 = new Evaluation();
INDArray output2 = rbm2.output(test.getFeatureMatrix());
eval2.eval(test.getLabels(), output2);
double f1Score2 = eval2.f1();
assertEquals(f1Score, f1Score2, 1e-4);
}
use of org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator in project deeplearning4j by deeplearning4j.
the class TestDecayPolicies method testUpdatingInConf.
@Test
public void testUpdatingInConf() throws Exception {
OptimizationAlgorithm[] optimizationAlgorithms = new OptimizationAlgorithm[] { OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT, OptimizationAlgorithm.LINE_GRADIENT_DESCENT, OptimizationAlgorithm.CONJUGATE_GRADIENT, OptimizationAlgorithm.LBFGS };
for (OptimizationAlgorithm oa : optimizationAlgorithms) {
Map<Integer, Double> momentumSchedule = new HashMap<>();
double m = 0.001;
for (int i = 0; i <= 100; i++) {
momentumSchedule.put(i, Math.min(m, 0.9999));
m += 0.001;
}
Map<Integer, Double> learningRateSchedule = new HashMap<>();
double lr = 0.1;
for (int i = 0; i <= 100; i++) {
learningRateSchedule.put(i, lr);
lr *= 0.96;
}
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(oa).iterations(1).learningRateDecayPolicy(LearningRatePolicy.Schedule).learningRateSchedule(learningRateSchedule).updater(org.deeplearning4j.nn.conf.Updater.NESTEROVS).weightInit(WeightInit.XAVIER).momentum(0.9).momentumAfter(momentumSchedule).regularization(true).l2(0.0001).list().layer(0, new DenseLayer.Builder().nIn(784).nOut(10).build()).layer(1, new OutputLayer.Builder().nIn(10).nOut(10).build()).pretrain(false).backprop(true).build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
int last_layer_index = 1;
DataSetIterator trainIter = new MnistDataSetIterator(64, true, 12345);
int count = 0;
while (trainIter.hasNext()) {
net.fit(trainIter.next());
// always print the same number (0.1 and 0.9)
double lrLastLayer = (net.getLayer(last_layer_index)).conf().getLearningRateByParam("W");
double mLastLayer = (net.getLayer(last_layer_index)).conf().getLayer().getMomentum();
assertEquals(learningRateSchedule.get(count), lrLastLayer, 1e-6);
assertEquals(momentumSchedule.get(count), mLastLayer, 1e-6);
if (count++ >= 100)
break;
}
}
}
use of org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator in project deeplearning4j by deeplearning4j.
the class TestSparkMultiLayerParameterAveraging method testFitViaStringPathsSize1.
@Test
public void testFitViaStringPathsSize1() throws Exception {
Path tempDir = Files.createTempDirectory("DL4J-testFitViaStringPathsSize1");
File tempDirF = tempDir.toFile();
tempDirF.deleteOnExit();
int dataSetObjSize = 1;
int batchSizePerExecutor = 25;
int numSplits = 10;
int averagingFrequency = 3;
int totalExamples = numExecutors() * batchSizePerExecutor * numSplits * averagingFrequency;
DataSetIterator iter = new MnistDataSetIterator(dataSetObjSize, totalExamples, false);
int i = 0;
while (iter.hasNext()) {
File nextFile = new File(tempDirF, i + ".bin");
DataSet ds = iter.next();
ds.save(nextFile);
i++;
}
System.out.println("Saved to: " + tempDirF.getAbsolutePath());
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().updater(Updater.RMSPROP).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).list().layer(0, new org.deeplearning4j.nn.conf.layers.DenseLayer.Builder().nIn(28 * 28).nOut(50).activation(Activation.TANH).build()).layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(50).nOut(10).activation(Activation.SOFTMAX).build()).pretrain(false).backprop(true).build();
SparkDl4jMultiLayer sparkNet = new SparkDl4jMultiLayer(sc, conf, new ParameterAveragingTrainingMaster.Builder(numExecutors(), dataSetObjSize).workerPrefetchNumBatches(5).batchSizePerWorker(batchSizePerExecutor).averagingFrequency(averagingFrequency).repartionData(Repartition.Always).build());
sparkNet.setCollectTrainingStats(true);
//List files:
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(tempDir.toUri(), config);
RemoteIterator<LocatedFileStatus> fileIter = hdfs.listFiles(new org.apache.hadoop.fs.Path(tempDir.toString()), false);
List<String> paths = new ArrayList<>();
while (fileIter.hasNext()) {
String path = fileIter.next().getPath().toString();
paths.add(path);
}
INDArray paramsBefore = sparkNet.getNetwork().params().dup();
JavaRDD<String> pathRdd = sc.parallelize(paths);
sparkNet.fitPaths(pathRdd);
INDArray paramsAfter = sparkNet.getNetwork().params().dup();
assertNotEquals(paramsBefore, paramsAfter);
Thread.sleep(2000);
SparkTrainingStats stats = sparkNet.getSparkTrainingStats();
//Expect
System.out.println(stats.statsAsString());
assertEquals(numSplits, stats.getValue("ParameterAveragingMasterRepartitionTimesMs").size());
List<EventStats> list = stats.getValue("ParameterAveragingWorkerFitTimesMs");
assertEquals(numSplits * numExecutors() * averagingFrequency, list.size());
for (EventStats es : list) {
ExampleCountEventStats e = (ExampleCountEventStats) es;
assertTrue(batchSizePerExecutor * averagingFrequency - 10 >= e.getTotalExampleCount());
}
sparkNet.getTrainingMaster().deleteTempFiles(sc);
}
Aggregations