use of org.deeplearning4j.spark.impl.multilayer.SparkDl4jMultiLayer in project deeplearning4j by deeplearning4j.
the class TestSparkMultiLayerParameterAveraging method testDistributedScoring.
@Test
public void testDistributedScoring() {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().regularization(true).l1(0.1).l2(0.1).seed(123).updater(Updater.NESTEROVS).learningRate(0.1).momentum(0.9).list().layer(0, new org.deeplearning4j.nn.conf.layers.DenseLayer.Builder().nIn(nIn).nOut(3).activation(Activation.TANH).build()).layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(3).nOut(nOut).activation(Activation.SOFTMAX).build()).backprop(true).pretrain(false).build();
SparkDl4jMultiLayer sparkNet = new SparkDl4jMultiLayer(sc, conf, new ParameterAveragingTrainingMaster(true, numExecutors(), 1, 10, 1, 0));
MultiLayerNetwork netCopy = sparkNet.getNetwork().clone();
int nRows = 100;
INDArray features = Nd4j.rand(nRows, nIn);
INDArray labels = Nd4j.zeros(nRows, nOut);
Random r = new Random(12345);
for (int i = 0; i < nRows; i++) {
labels.putScalar(new int[] { i, r.nextInt(nOut) }, 1.0);
}
INDArray localScoresWithReg = netCopy.scoreExamples(new DataSet(features, labels), true);
INDArray localScoresNoReg = netCopy.scoreExamples(new DataSet(features, labels), false);
List<Tuple2<String, DataSet>> dataWithKeys = new ArrayList<>();
for (int i = 0; i < nRows; i++) {
DataSet ds = new DataSet(features.getRow(i).dup(), labels.getRow(i).dup());
dataWithKeys.add(new Tuple2<>(String.valueOf(i), ds));
}
JavaPairRDD<String, DataSet> dataWithKeysRdd = sc.parallelizePairs(dataWithKeys);
JavaPairRDD<String, Double> sparkScoresWithReg = sparkNet.scoreExamples(dataWithKeysRdd, true, 4);
JavaPairRDD<String, Double> sparkScoresNoReg = sparkNet.scoreExamples(dataWithKeysRdd, false, 4);
Map<String, Double> sparkScoresWithRegMap = sparkScoresWithReg.collectAsMap();
Map<String, Double> sparkScoresNoRegMap = sparkScoresNoReg.collectAsMap();
for (int i = 0; i < nRows; i++) {
double scoreRegExp = localScoresWithReg.getDouble(i);
double scoreRegAct = sparkScoresWithRegMap.get(String.valueOf(i));
assertEquals(scoreRegExp, scoreRegAct, 1e-5);
double scoreNoRegExp = localScoresNoReg.getDouble(i);
double scoreNoRegAct = sparkScoresNoRegMap.get(String.valueOf(i));
assertEquals(scoreNoRegExp, scoreNoRegAct, 1e-5);
// System.out.println(scoreRegExp + "\t" + scoreRegAct + "\t" + scoreNoRegExp + "\t" + scoreNoRegAct);
}
List<DataSet> dataNoKeys = new ArrayList<>();
for (int i = 0; i < nRows; i++) {
dataNoKeys.add(new DataSet(features.getRow(i).dup(), labels.getRow(i).dup()));
}
JavaRDD<DataSet> dataNoKeysRdd = sc.parallelize(dataNoKeys);
List<Double> scoresWithReg = new ArrayList<>(sparkNet.scoreExamples(dataNoKeysRdd, true, 4).collect());
List<Double> scoresNoReg = new ArrayList<>(sparkNet.scoreExamples(dataNoKeysRdd, false, 4).collect());
Collections.sort(scoresWithReg);
Collections.sort(scoresNoReg);
double[] localScoresWithRegDouble = localScoresWithReg.data().asDouble();
double[] localScoresNoRegDouble = localScoresNoReg.data().asDouble();
Arrays.sort(localScoresWithRegDouble);
Arrays.sort(localScoresNoRegDouble);
for (int i = 0; i < localScoresWithRegDouble.length; i++) {
assertEquals(localScoresWithRegDouble[i], scoresWithReg.get(i), 1e-5);
assertEquals(localScoresNoRegDouble[i], scoresNoReg.get(i), 1e-5);
//System.out.println(localScoresWithRegDouble[i] + "\t" + scoresWithReg.get(i) + "\t" + localScoresNoRegDouble[i] + "\t" + scoresNoReg.get(i));
}
}
use of org.deeplearning4j.spark.impl.multilayer.SparkDl4jMultiLayer in project deeplearning4j by deeplearning4j.
the class TestSparkMultiLayerParameterAveraging method testSeedRepeatability.
@Test
public void testSeedRepeatability() throws Exception {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).updater(Updater.RMSPROP).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).weightInit(WeightInit.XAVIER).list().layer(0, new org.deeplearning4j.nn.conf.layers.DenseLayer.Builder().nIn(4).nOut(4).activation(Activation.TANH).build()).layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(4).nOut(3).activation(Activation.SOFTMAX).build()).pretrain(false).backprop(true).build();
Nd4j.getRandom().setSeed(12345);
MultiLayerNetwork n1 = new MultiLayerNetwork(conf);
n1.init();
Nd4j.getRandom().setSeed(12345);
MultiLayerNetwork n2 = new MultiLayerNetwork(conf);
n2.init();
Nd4j.getRandom().setSeed(12345);
MultiLayerNetwork n3 = new MultiLayerNetwork(conf);
n3.init();
SparkDl4jMultiLayer sparkNet1 = new SparkDl4jMultiLayer(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);
SparkDl4jMultiLayer sparkNet2 = new SparkDl4jMultiLayer(sc, n2, new ParameterAveragingTrainingMaster.Builder(1).workerPrefetchNumBatches(5).batchSizePerWorker(5).averagingFrequency(1).repartionData(Repartition.Always).rngSeed(12345).build());
Thread.sleep(100);
SparkDl4jMultiLayer sparkNet3 = new SparkDl4jMultiLayer(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.spark.impl.multilayer.SparkDl4jMultiLayer in project deeplearning4j by deeplearning4j.
the class TestSparkMultiLayerParameterAveraging method testRunIteration.
@Test
public void testRunIteration() {
DataSet dataSet = new IrisDataSetIterator(5, 5).next();
List<DataSet> list = dataSet.asList();
JavaRDD<DataSet> data = sc.parallelize(list);
SparkDl4jMultiLayer sparkNetCopy = new SparkDl4jMultiLayer(sc, getBasicConf(), new ParameterAveragingTrainingMaster(true, numExecutors(), 1, 5, 1, 0));
MultiLayerNetwork networkCopy = sparkNetCopy.fit(data);
INDArray expectedParams = networkCopy.params();
SparkDl4jMultiLayer sparkNet = getBasicNetwork();
MultiLayerNetwork network = sparkNet.fit(data);
INDArray actualParams = network.params();
assertEquals(expectedParams.size(1), actualParams.size(1));
}
use of org.deeplearning4j.spark.impl.multilayer.SparkDl4jMultiLayer in project deeplearning4j by deeplearning4j.
the class TestSparkMultiLayerParameterAveraging method testUpdaters.
@Test
public void testUpdaters() {
SparkDl4jMultiLayer sparkNet = getBasicNetwork();
MultiLayerNetwork netCopy = sparkNet.getNetwork().clone();
netCopy.fit(data);
Updater expectedUpdater = netCopy.conf().getLayer().getUpdater();
double expectedLR = netCopy.conf().getLayer().getLearningRate();
double expectedMomentum = netCopy.conf().getLayer().getMomentum();
Updater actualUpdater = sparkNet.getNetwork().conf().getLayer().getUpdater();
sparkNet.fit(sparkData);
double actualLR = sparkNet.getNetwork().conf().getLayer().getLearningRate();
double actualMomentum = sparkNet.getNetwork().conf().getLayer().getMomentum();
assertEquals(expectedUpdater, actualUpdater);
assertEquals(expectedLR, actualLR, 0.01);
assertEquals(expectedMomentum, actualMomentum, 0.01);
}
use of org.deeplearning4j.spark.impl.multilayer.SparkDl4jMultiLayer in project deeplearning4j by deeplearning4j.
the class TestTrainingStatsCollection method testStatsCollection.
@Test
public void testStatsCollection() throws Exception {
int nWorkers = 4;
SparkConf sparkConf = new SparkConf();
sparkConf.setMaster("local[" + nWorkers + "]");
sparkConf.setAppName("Test");
JavaSparkContext sc = new JavaSparkContext(sparkConf);
try {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).list().layer(0, new DenseLayer.Builder().nIn(10).nOut(10).build()).layer(1, new OutputLayer.Builder().nIn(10).nOut(10).build()).pretrain(false).backprop(true).build();
int miniBatchSizePerWorker = 10;
int averagingFrequency = 5;
int numberOfAveragings = 3;
int totalExamples = nWorkers * miniBatchSizePerWorker * averagingFrequency * numberOfAveragings;
Nd4j.getRandom().setSeed(12345);
List<DataSet> list = new ArrayList<>();
for (int i = 0; i < totalExamples; i++) {
INDArray f = Nd4j.rand(1, 10);
INDArray l = Nd4j.rand(1, 10);
DataSet ds = new DataSet(f, l);
list.add(ds);
}
JavaRDD<DataSet> rdd = sc.parallelize(list);
rdd.repartition(4);
ParameterAveragingTrainingMaster tm = new ParameterAveragingTrainingMaster.Builder(nWorkers, 1).averagingFrequency(averagingFrequency).batchSizePerWorker(miniBatchSizePerWorker).saveUpdater(true).workerPrefetchNumBatches(0).repartionData(Repartition.Always).build();
SparkDl4jMultiLayer sparkNet = new SparkDl4jMultiLayer(sc, conf, tm);
sparkNet.setCollectTrainingStats(true);
sparkNet.fit(rdd);
//Collect the expected keys:
List<String> expectedStatNames = new ArrayList<>();
Class<?>[] classes = new Class[] { CommonSparkTrainingStats.class, ParameterAveragingTrainingMasterStats.class, ParameterAveragingTrainingWorkerStats.class };
String[] fieldNames = new String[] { "columnNames", "columnNames", "columnNames" };
for (int i = 0; i < classes.length; i++) {
Field field = classes[i].getDeclaredField(fieldNames[i]);
field.setAccessible(true);
Object f = field.get(null);
Collection<String> c = (Collection<String>) f;
expectedStatNames.addAll(c);
}
System.out.println(expectedStatNames);
SparkTrainingStats stats = sparkNet.getSparkTrainingStats();
Set<String> actualKeySet = stats.getKeySet();
assertEquals(expectedStatNames.size(), actualKeySet.size());
for (String s : stats.getKeySet()) {
assertTrue(expectedStatNames.contains(s));
assertNotNull(stats.getValue(s));
}
String statsAsString = stats.statsAsString();
System.out.println(statsAsString);
//One line per stat
assertEquals(actualKeySet.size(), statsAsString.split("\n").length);
//Go through nested stats
//First: master stats
assertTrue(stats instanceof ParameterAveragingTrainingMasterStats);
ParameterAveragingTrainingMasterStats masterStats = (ParameterAveragingTrainingMasterStats) stats;
List<EventStats> exportTimeStats = masterStats.getParameterAveragingMasterExportTimesMs();
assertEquals(1, exportTimeStats.size());
assertDurationGreaterZero(exportTimeStats);
assertNonNullFields(exportTimeStats);
assertExpectedNumberMachineIdsJvmIdsThreadIds(exportTimeStats, 1, 1, 1);
List<EventStats> countRddTime = masterStats.getParameterAveragingMasterCountRddSizeTimesMs();
//occurs once per fit
assertEquals(1, countRddTime.size());
assertDurationGreaterEqZero(countRddTime);
assertNonNullFields(countRddTime);
//should occur only in master once
assertExpectedNumberMachineIdsJvmIdsThreadIds(countRddTime, 1, 1, 1);
List<EventStats> broadcastCreateTime = masterStats.getParameterAveragingMasterBroadcastCreateTimesMs();
assertEquals(numberOfAveragings, broadcastCreateTime.size());
assertDurationGreaterEqZero(broadcastCreateTime);
assertNonNullFields(broadcastCreateTime);
//only 1 thread for master
assertExpectedNumberMachineIdsJvmIdsThreadIds(broadcastCreateTime, 1, 1, 1);
List<EventStats> fitTimes = masterStats.getParameterAveragingMasterFitTimesMs();
//i.e., number of times fit(JavaRDD<DataSet>) was called
assertEquals(1, fitTimes.size());
assertDurationGreaterZero(fitTimes);
assertNonNullFields(fitTimes);
//only 1 thread for master
assertExpectedNumberMachineIdsJvmIdsThreadIds(fitTimes, 1, 1, 1);
List<EventStats> splitTimes = masterStats.getParameterAveragingMasterSplitTimesMs();
//Splitting of the data set is executed once only (i.e., one fit(JavaRDD<DataSet>) call)
assertEquals(1, splitTimes.size());
assertDurationGreaterEqZero(splitTimes);
assertNonNullFields(splitTimes);
//only 1 thread for master
assertExpectedNumberMachineIdsJvmIdsThreadIds(splitTimes, 1, 1, 1);
List<EventStats> aggregateTimesMs = masterStats.getParamaterAveragingMasterAggregateTimesMs();
assertEquals(numberOfAveragings, aggregateTimesMs.size());
assertDurationGreaterEqZero(aggregateTimesMs);
assertNonNullFields(aggregateTimesMs);
//only 1 thread for master
assertExpectedNumberMachineIdsJvmIdsThreadIds(aggregateTimesMs, 1, 1, 1);
List<EventStats> processParamsTimesMs = masterStats.getParameterAveragingMasterProcessParamsUpdaterTimesMs();
assertEquals(numberOfAveragings, processParamsTimesMs.size());
assertDurationGreaterEqZero(processParamsTimesMs);
assertNonNullFields(processParamsTimesMs);
//only 1 thread for master
assertExpectedNumberMachineIdsJvmIdsThreadIds(processParamsTimesMs, 1, 1, 1);
List<EventStats> repartitionTimesMs = masterStats.getParameterAveragingMasterRepartitionTimesMs();
assertEquals(numberOfAveragings, repartitionTimesMs.size());
assertDurationGreaterEqZero(repartitionTimesMs);
assertNonNullFields(repartitionTimesMs);
//only 1 thread for master
assertExpectedNumberMachineIdsJvmIdsThreadIds(repartitionTimesMs, 1, 1, 1);
//Second: Common spark training stats
SparkTrainingStats commonStats = masterStats.getNestedTrainingStats();
assertNotNull(commonStats);
assertTrue(commonStats instanceof CommonSparkTrainingStats);
CommonSparkTrainingStats cStats = (CommonSparkTrainingStats) commonStats;
List<EventStats> workerFlatMapTotalTimeMs = cStats.getWorkerFlatMapTotalTimeMs();
assertEquals(numberOfAveragings * nWorkers, workerFlatMapTotalTimeMs.size());
assertDurationGreaterZero(workerFlatMapTotalTimeMs);
assertNonNullFields(workerFlatMapTotalTimeMs);
assertExpectedNumberMachineIdsJvmIdsThreadIds(workerFlatMapTotalTimeMs, 1, 1, nWorkers);
List<EventStats> workerFlatMapGetInitialModelTimeMs = cStats.getWorkerFlatMapGetInitialModelTimeMs();
assertEquals(numberOfAveragings * nWorkers, workerFlatMapGetInitialModelTimeMs.size());
assertDurationGreaterEqZero(workerFlatMapGetInitialModelTimeMs);
assertNonNullFields(workerFlatMapGetInitialModelTimeMs);
assertExpectedNumberMachineIdsJvmIdsThreadIds(workerFlatMapGetInitialModelTimeMs, 1, 1, nWorkers);
List<EventStats> workerFlatMapDataSetGetTimesMs = cStats.getWorkerFlatMapDataSetGetTimesMs();
int numMinibatchesProcessed = workerFlatMapDataSetGetTimesMs.size();
//1 for every time we get a data set
int expectedNumMinibatchesProcessed = numberOfAveragings * nWorkers * averagingFrequency;
//Sometimes random split is just bad - some executors might miss out on getting the expected amount of data
assertTrue(numMinibatchesProcessed >= expectedNumMinibatchesProcessed - 5);
List<EventStats> workerFlatMapProcessMiniBatchTimesMs = cStats.getWorkerFlatMapProcessMiniBatchTimesMs();
assertTrue(workerFlatMapProcessMiniBatchTimesMs.size() >= numberOfAveragings * nWorkers * averagingFrequency - 5);
assertDurationGreaterEqZero(workerFlatMapProcessMiniBatchTimesMs);
assertNonNullFields(workerFlatMapDataSetGetTimesMs);
assertExpectedNumberMachineIdsJvmIdsThreadIds(workerFlatMapDataSetGetTimesMs, 1, 1, nWorkers);
//Third: ParameterAveragingTrainingWorker stats
SparkTrainingStats paramAvgStats = cStats.getNestedTrainingStats();
assertNotNull(paramAvgStats);
assertTrue(paramAvgStats instanceof ParameterAveragingTrainingWorkerStats);
ParameterAveragingTrainingWorkerStats pStats = (ParameterAveragingTrainingWorkerStats) paramAvgStats;
List<EventStats> parameterAveragingWorkerBroadcastGetValueTimeMs = pStats.getParameterAveragingWorkerBroadcastGetValueTimeMs();
assertEquals(numberOfAveragings * nWorkers, parameterAveragingWorkerBroadcastGetValueTimeMs.size());
assertDurationGreaterEqZero(parameterAveragingWorkerBroadcastGetValueTimeMs);
assertNonNullFields(parameterAveragingWorkerBroadcastGetValueTimeMs);
assertExpectedNumberMachineIdsJvmIdsThreadIds(parameterAveragingWorkerBroadcastGetValueTimeMs, 1, 1, nWorkers);
List<EventStats> parameterAveragingWorkerInitTimeMs = pStats.getParameterAveragingWorkerInitTimeMs();
assertEquals(numberOfAveragings * nWorkers, parameterAveragingWorkerInitTimeMs.size());
assertDurationGreaterEqZero(parameterAveragingWorkerInitTimeMs);
assertNonNullFields(parameterAveragingWorkerInitTimeMs);
assertExpectedNumberMachineIdsJvmIdsThreadIds(parameterAveragingWorkerInitTimeMs, 1, 1, nWorkers);
List<EventStats> parameterAveragingWorkerFitTimesMs = pStats.getParameterAveragingWorkerFitTimesMs();
assertTrue(parameterAveragingWorkerFitTimesMs.size() >= numberOfAveragings * nWorkers * averagingFrequency - 5);
assertDurationGreaterEqZero(parameterAveragingWorkerFitTimesMs);
assertNonNullFields(parameterAveragingWorkerFitTimesMs);
assertExpectedNumberMachineIdsJvmIdsThreadIds(parameterAveragingWorkerFitTimesMs, 1, 1, nWorkers);
assertNull(pStats.getNestedTrainingStats());
//Finally: try exporting stats
String tempDir = System.getProperty("java.io.tmpdir");
String outDir = FilenameUtils.concat(tempDir, "dl4j_testTrainingStatsCollection");
stats.exportStatFiles(outDir, sc.sc());
String htmlPlotsPath = FilenameUtils.concat(outDir, "AnalysisPlots.html");
StatsUtils.exportStatsAsHtml(stats, htmlPlotsPath, sc);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StatsUtils.exportStatsAsHTML(stats, baos);
baos.close();
byte[] bytes = baos.toByteArray();
String str = new String(bytes, "UTF-8");
// System.out.println(str);
} finally {
sc.stop();
}
}
Aggregations