use of org.deeplearning4j.nn.conf.ComputationGraphConfiguration in project deeplearning4j by deeplearning4j.
the class TestSparkComputationGraph method testSeedRepeatability.
@Test
public void testSeedRepeatability() throws Exception {
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).updater(Updater.RMSPROP).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).weightInit(WeightInit.XAVIER).graphBuilder().addInputs("in").addLayer("0", new org.deeplearning4j.nn.conf.layers.DenseLayer.Builder().nIn(4).nOut(4).activation(Activation.TANH).build(), "in").addLayer("1", new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(4).nOut(3).activation(Activation.SOFTMAX).build(), "0").setOutputs("1").pretrain(false).backprop(true).build();
Nd4j.getRandom().setSeed(12345);
ComputationGraph n1 = new ComputationGraph(conf);
n1.init();
Nd4j.getRandom().setSeed(12345);
ComputationGraph n2 = new ComputationGraph(conf);
n2.init();
Nd4j.getRandom().setSeed(12345);
ComputationGraph n3 = new ComputationGraph(conf);
n3.init();
SparkComputationGraph sparkNet1 = new SparkComputationGraph(sc, n1, new ParameterAveragingTrainingMaster.Builder(1).workerPrefetchNumBatches(5).batchSizePerWorker(5).averagingFrequency(1).repartionData(Repartition.Always).rngSeed(12345).build());
//Training master IDs are only unique if they are created at least 1 ms apart...
Thread.sleep(100);
SparkComputationGraph sparkNet2 = new SparkComputationGraph(sc, n2, new ParameterAveragingTrainingMaster.Builder(1).workerPrefetchNumBatches(5).batchSizePerWorker(5).averagingFrequency(1).repartionData(Repartition.Always).rngSeed(12345).build());
Thread.sleep(100);
SparkComputationGraph sparkNet3 = new SparkComputationGraph(sc, n3, new ParameterAveragingTrainingMaster.Builder(1).workerPrefetchNumBatches(5).batchSizePerWorker(5).averagingFrequency(1).repartionData(Repartition.Always).rngSeed(98765).build());
List<DataSet> data = new ArrayList<>();
DataSetIterator iter = new IrisDataSetIterator(1, 150);
while (iter.hasNext()) data.add(iter.next());
JavaRDD<DataSet> rdd = sc.parallelize(data);
sparkNet1.fit(rdd);
sparkNet2.fit(rdd);
sparkNet3.fit(rdd);
INDArray p1 = sparkNet1.getNetwork().params();
INDArray p2 = sparkNet2.getNetwork().params();
INDArray p3 = sparkNet3.getNetwork().params();
sparkNet1.getTrainingMaster().deleteTempFiles(sc);
sparkNet2.getTrainingMaster().deleteTempFiles(sc);
sparkNet3.getTrainingMaster().deleteTempFiles(sc);
assertEquals(p1, p2);
assertNotEquals(p1, p3);
}
use of org.deeplearning4j.nn.conf.ComputationGraphConfiguration in project deeplearning4j by deeplearning4j.
the class TestSparkComputationGraph method testBasic.
@Test
public void testBasic() throws Exception {
JavaSparkContext sc = this.sc;
RecordReader rr = new CSVRecordReader(0, ",");
rr.initialize(new FileSplit(new ClassPathResource("iris.txt").getTempFileFromArchive()));
MultiDataSetIterator iter = new RecordReaderMultiDataSetIterator.Builder(1).addReader("iris", rr).addInput("iris", 0, 3).addOutputOneHot("iris", 4, 3).build();
List<MultiDataSet> list = new ArrayList<>(150);
while (iter.hasNext()) list.add(iter.next());
ComputationGraphConfiguration config = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).learningRate(0.1).graphBuilder().addInputs("in").addLayer("dense", new DenseLayer.Builder().nIn(4).nOut(2).build(), "in").addLayer("out", new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(2).nOut(3).build(), "dense").setOutputs("out").pretrain(false).backprop(true).build();
ComputationGraph cg = new ComputationGraph(config);
cg.init();
TrainingMaster tm = new ParameterAveragingTrainingMaster(true, numExecutors(), 1, 10, 1, 0);
SparkComputationGraph scg = new SparkComputationGraph(sc, cg, tm);
scg.setListeners(Collections.singleton((IterationListener) new ScoreIterationListener(1)));
JavaRDD<MultiDataSet> rdd = sc.parallelize(list);
scg.fitMultiDataSet(rdd);
//Try: fitting using DataSet
DataSetIterator iris = new IrisDataSetIterator(1, 150);
List<DataSet> list2 = new ArrayList<>();
while (iris.hasNext()) list2.add(iris.next());
JavaRDD<DataSet> rddDS = sc.parallelize(list2);
scg.fit(rddDS);
}
use of org.deeplearning4j.nn.conf.ComputationGraphConfiguration in project deeplearning4j by deeplearning4j.
the class TestCompareParameterAveragingSparkVsSingleMachine method getGraphConfCNN.
private static ComputationGraphConfiguration getGraphConfCNN(int seed, Updater updater) {
Nd4j.getRandom().setSeed(seed);
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).learningRate(0.5).weightInit(WeightInit.XAVIER).updater(updater).iterations(1).seed(seed).graphBuilder().addInputs("in").addLayer("0", new ConvolutionLayer.Builder().nOut(3).kernelSize(2, 2).stride(1, 1).padding(0, 0).activation(Activation.TANH).build(), "in").addLayer("1", new ConvolutionLayer.Builder().nOut(3).kernelSize(2, 2).stride(1, 1).padding(0, 0).activation(Activation.TANH).build(), "0").addLayer("2", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MSE).nOut(10).build(), "1").setOutputs("2").setInputTypes(InputType.convolutional(10, 10, 3)).pretrain(false).backprop(true).build();
return conf;
}
use of org.deeplearning4j.nn.conf.ComputationGraphConfiguration in project deeplearning4j by deeplearning4j.
the class TestCompareParameterAveragingSparkVsSingleMachine method getGraphConf.
private static ComputationGraphConfiguration getGraphConf(int seed, Updater updater) {
Nd4j.getRandom().setSeed(seed);
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).learningRate(0.5).weightInit(WeightInit.XAVIER).updater(updater).iterations(1).seed(seed).graphBuilder().addInputs("in").addLayer("0", new DenseLayer.Builder().nIn(10).nOut(10).build(), "in").addLayer("1", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MSE).nIn(10).nOut(10).build(), "0").setOutputs("1").pretrain(false).backprop(true).build();
return conf;
}
use of org.deeplearning4j.nn.conf.ComputationGraphConfiguration in project deeplearning4j by deeplearning4j.
the class TestSparkMultiLayerParameterAveraging method testVaePretrainSimpleCG.
@Test
public void testVaePretrainSimpleCG() {
//Simple sanity check on pretraining
int nIn = 8;
Nd4j.getRandom().setSeed(12345);
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).updater(Updater.RMSPROP).weightInit(WeightInit.XAVIER).graphBuilder().addInputs("in").addLayer("0", new VariationalAutoencoder.Builder().nIn(8).nOut(10).encoderLayerSizes(12).decoderLayerSizes(13).reconstructionDistribution(new GaussianReconstructionDistribution("identity")).build(), "in").setOutputs("0").pretrain(true).backprop(false).build();
//Do training on Spark with one executor, for 3 separate minibatches
int rddDataSetNumExamples = 10;
int totalAveragings = 5;
int averagingFrequency = 3;
ParameterAveragingTrainingMaster tm = new ParameterAveragingTrainingMaster.Builder(rddDataSetNumExamples).averagingFrequency(averagingFrequency).batchSizePerWorker(rddDataSetNumExamples).saveUpdater(true).workerPrefetchNumBatches(0).build();
Nd4j.getRandom().setSeed(12345);
SparkComputationGraph sparkNet = new SparkComputationGraph(sc, conf.clone(), tm);
List<DataSet> trainData = new ArrayList<>();
int nDataSets = numExecutors() * totalAveragings * averagingFrequency;
for (int i = 0; i < nDataSets; i++) {
trainData.add(new DataSet(Nd4j.rand(rddDataSetNumExamples, nIn), null));
}
JavaRDD<DataSet> data = sc.parallelize(trainData);
sparkNet.fit(data);
}
Aggregations