use of org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator in project deeplearning4j by deeplearning4j.
the class TestEarlyStoppingSparkCompGraph method getIris.
private JavaRDD<DataSet> getIris() {
JavaSparkContext sc = getContext();
IrisDataSetIterator iter = new IrisDataSetIterator(1, 150);
List<DataSet> list = new ArrayList<>(150);
while (iter.hasNext()) list.add(iter.next());
return sc.parallelize(list);
}
use of org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator in project deeplearning4j by deeplearning4j.
the class TestMiscFunctions method testFeedForwardWithKey.
@Test
public void testFeedForwardWithKey() {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).list().layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build()).layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(3).nOut(3).activation(Activation.SOFTMAX).build()).build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
DataSetIterator iter = new IrisDataSetIterator(150, 150);
DataSet ds = iter.next();
List<INDArray> expected = new ArrayList<>();
List<Tuple2<Integer, INDArray>> mapFeatures = new ArrayList<>();
int count = 0;
int arrayCount = 0;
Random r = new Random(12345);
while (count < 150) {
//1 to 5 inclusive examples
int exampleCount = r.nextInt(5) + 1;
if (count + exampleCount > 150)
exampleCount = 150 - count;
INDArray subset = ds.getFeatures().get(NDArrayIndex.interval(count, count + exampleCount), NDArrayIndex.all());
expected.add(net.output(subset, false));
mapFeatures.add(new Tuple2<>(arrayCount, subset));
arrayCount++;
count += exampleCount;
}
JavaPairRDD<Integer, INDArray> rdd = sc.parallelizePairs(mapFeatures);
SparkDl4jMultiLayer multiLayer = new SparkDl4jMultiLayer(sc, net, null);
Map<Integer, INDArray> map = multiLayer.feedForwardWithKey(rdd, 16).collectAsMap();
for (int i = 0; i < expected.size(); i++) {
INDArray exp = expected.get(i);
INDArray act = map.get(i);
assertEquals(exp, act);
}
}
use of org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator in project deeplearning4j by deeplearning4j.
the class TestMiscFunctions method testFeedForwardWithKeyGraph.
@Test
public void testFeedForwardWithKeyGraph() {
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).graphBuilder().addInputs("in1", "in2").addLayer("0", new DenseLayer.Builder().nIn(4).nOut(3).build(), "in1").addLayer("1", new DenseLayer.Builder().nIn(4).nOut(3).build(), "in2").addLayer("2", new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(6).nOut(3).activation(Activation.SOFTMAX).build(), "0", "1").setOutputs("2").build();
ComputationGraph net = new ComputationGraph(conf);
net.init();
DataSetIterator iter = new IrisDataSetIterator(150, 150);
DataSet ds = iter.next();
List<INDArray> expected = new ArrayList<>();
List<Tuple2<Integer, INDArray[]>> mapFeatures = new ArrayList<>();
int count = 0;
int arrayCount = 0;
Random r = new Random(12345);
while (count < 150) {
//1 to 5 inclusive examples
int exampleCount = r.nextInt(5) + 1;
if (count + exampleCount > 150)
exampleCount = 150 - count;
INDArray subset = ds.getFeatures().get(NDArrayIndex.interval(count, count + exampleCount), NDArrayIndex.all());
expected.add(net.outputSingle(false, subset, subset));
mapFeatures.add(new Tuple2<>(arrayCount, new INDArray[] { subset, subset }));
arrayCount++;
count += exampleCount;
}
JavaPairRDD<Integer, INDArray[]> rdd = sc.parallelizePairs(mapFeatures);
SparkComputationGraph graph = new SparkComputationGraph(sc, net, null);
Map<Integer, INDArray[]> map = graph.feedForwardWithKey(rdd, 16).collectAsMap();
for (int i = 0; i < expected.size(); i++) {
INDArray exp = expected.get(i);
INDArray act = map.get(i)[0];
assertEquals(exp, act);
}
}
use of org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator in project deeplearning4j by deeplearning4j.
the class TestSparkMultiLayerParameterAveraging method testFromSvmLight.
@Test
public void testFromSvmLight() throws Exception {
JavaRDD<LabeledPoint> data = MLUtils.loadLibSVMFile(sc.sc(), new ClassPathResource("svmLight/iris_svmLight_0.txt").getTempFileFromArchive().getAbsolutePath()).toJavaRDD().map(new Function<LabeledPoint, LabeledPoint>() {
@Override
public LabeledPoint call(LabeledPoint v1) throws Exception {
return new LabeledPoint(v1.label(), Vectors.dense(v1.features().toArray()));
}
});
DataSet d = new IrisDataSetIterator(150, 150).next();
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(123).optimizationAlgo(OptimizationAlgorithm.LINE_GRADIENT_DESCENT).iterations(100).miniBatch(true).maxNumLineSearchIterations(10).list().layer(0, new RBM.Builder(RBM.HiddenUnit.RECTIFIED, RBM.VisibleUnit.GAUSSIAN).nIn(4).nOut(100).weightInit(WeightInit.XAVIER).activation(Activation.RELU).lossFunction(LossFunctions.LossFunction.RMSE_XENT).build()).layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(100).nOut(3).activation(Activation.SOFTMAX).weightInit(WeightInit.XAVIER).build()).backprop(false).build();
MultiLayerNetwork network = new MultiLayerNetwork(conf);
network.init();
System.out.println("Initializing network");
SparkDl4jMultiLayer master = new SparkDl4jMultiLayer(sc, getBasicConf(), new ParameterAveragingTrainingMaster(true, numExecutors(), 1, 5, 1, 0));
MultiLayerNetwork network2 = master.fitLabeledPoint(data);
Evaluation evaluation = new Evaluation();
evaluation.eval(d.getLabels(), network2.output(d.getFeatureMatrix()));
System.out.println(evaluation.stats());
}
use of org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator in project deeplearning4j by deeplearning4j.
the class TestListeners method testStatsCollection.
@Test
public void testStatsCollection() {
JavaSparkContext sc = getContext();
int nExecutors = numExecutors();
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(123).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).list().layer(0, new DenseLayer.Builder().nIn(4).nOut(100).weightInit(WeightInit.XAVIER).activation(Activation.RELU).build()).layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(100).nOut(3).activation(Activation.SOFTMAX).weightInit(WeightInit.XAVIER).build()).pretrain(false).backprop(true).build();
MultiLayerNetwork network = new MultiLayerNetwork(conf);
network.init();
TrainingMaster tm = new ParameterAveragingTrainingMaster.Builder(1).batchSizePerWorker(5).averagingFrequency(6).build();
SparkDl4jMultiLayer net = new SparkDl4jMultiLayer(sc, conf, tm);
//In-memory
StatsStorage ss = new MapDBStatsStorage();
net.setListeners(ss, Collections.singletonList(new StatsListener(null)));
List<DataSet> list = new IrisDataSetIterator(120, 150).next().asList();
//120 examples, 4 executors, 30 examples per executor -> 6 updates of size 5 per executor
JavaRDD<DataSet> rdd = sc.parallelize(list);
net.fit(rdd);
List<String> sessions = ss.listSessionIDs();
System.out.println("Sessions: " + sessions);
assertEquals(1, sessions.size());
String sid = sessions.get(0);
List<String> typeIDs = ss.listTypeIDsForSession(sid);
List<String> workers = ss.listWorkerIDsForSession(sid);
System.out.println(sid + "\t" + typeIDs + "\t" + workers);
List<Persistable> lastUpdates = ss.getLatestUpdateAllWorkers(sid, StatsListener.TYPE_ID);
System.out.println(lastUpdates);
System.out.println("Static info:");
for (String wid : workers) {
Persistable staticInfo = ss.getStaticInfo(sid, StatsListener.TYPE_ID, wid);
System.out.println(sid + "\t" + wid);
}
assertEquals(1, typeIDs.size());
assertEquals(numExecutors(), workers.size());
String firstWorker = workers.get(0);
String firstWorkerSubstring = workers.get(0).substring(0, firstWorker.length() - 1);
for (String wid : workers) {
String widSubstring = wid.substring(0, wid.length() - 1);
assertEquals(firstWorkerSubstring, widSubstring);
String counterVal = wid.substring(wid.length() - 1, wid.length());
int cv = Integer.parseInt(counterVal);
assertTrue(0 <= cv && cv < numExecutors());
}
}
Aggregations