use of org.nd4j.linalg.dataset.api.iterator.DataSetIterator in project deeplearning4j by deeplearning4j.
the class ParameterServerParallelWrapperTest method testWrapper.
@Test
public void testWrapper() throws Exception {
int nChannels = 1;
int outputNum = 10;
// for GPU you usually want to have higher batchSize
int batchSize = 128;
int nEpochs = 10;
int iterations = 1;
int seed = 123;
log.info("Load data....");
DataSetIterator mnistTrain = new MnistDataSetIterator(batchSize, 1000);
DataSetIterator mnistTest = new MnistDataSetIterator(batchSize, false, 12345);
log.info("Build model....");
MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().seed(seed).iterations(iterations).regularization(true).l2(0.0005).learningRate(//.biasLearningRate(0.02)
0.01).weightInit(WeightInit.XAVIER).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).updater(Updater.NESTEROVS).momentum(0.9).list().layer(0, new ConvolutionLayer.Builder(5, 5).nIn(nChannels).stride(1, 1).nOut(20).activation(Activation.IDENTITY).build()).layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2).stride(2, 2).build()).layer(2, new ConvolutionLayer.Builder(5, 5).stride(1, 1).nOut(50).activation(Activation.IDENTITY).build()).layer(3, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2).stride(2, 2).build()).layer(4, new DenseLayer.Builder().activation(Activation.RELU).nOut(500).build()).layer(5, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nOut(outputNum).activation(Activation.SOFTMAX).build()).setInputType(InputType.convolutionalFlat(28, 28, 1)).backprop(true).pretrain(false);
MultiLayerConfiguration conf = builder.build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
ParameterServerParallelWrapper parameterServerParallelWrapper = ParameterServerParallelWrapper.builder().model(model).multiLayerNetwork(model).numEpochs(10).numWorkers(Runtime.getRuntime().availableProcessors()).statusServerPort(33000).preFetchSize(3).build();
parameterServerParallelWrapper.fit(mnistTrain);
parameterServerParallelWrapper.close();
Thread.sleep(30000);
}
use of org.nd4j.linalg.dataset.api.iterator.DataSetIterator in project deeplearning4j by deeplearning4j.
the class TestComputationGraphNetwork method testForwardBasicIris.
@Test
public void testForwardBasicIris() {
ComputationGraphConfiguration configuration = getIrisGraphConfiguration();
ComputationGraph graph = new ComputationGraph(configuration);
graph.init();
MultiLayerConfiguration mlc = getIrisMLNConfiguration();
MultiLayerNetwork net = new MultiLayerNetwork(mlc);
net.init();
DataSetIterator iris = new IrisDataSetIterator(150, 150);
DataSet ds = iris.next();
graph.setInput(0, ds.getFeatureMatrix());
Map<String, INDArray> activations = graph.feedForward(false);
//2 layers + 1 input node
assertEquals(3, activations.size());
assertTrue(activations.containsKey("input"));
assertTrue(activations.containsKey("firstLayer"));
assertTrue(activations.containsKey("outputLayer"));
//Now: set parameters of both networks to be identical. Then feedforward, and check we get the same outputs
Nd4j.getRandom().setSeed(12345);
int nParams = getNumParams();
INDArray params = Nd4j.rand(1, nParams);
graph.setParams(params.dup());
net.setParams(params.dup());
List<INDArray> mlnAct = net.feedForward(ds.getFeatureMatrix(), false);
activations = graph.feedForward(ds.getFeatureMatrix(), false);
assertEquals(mlnAct.get(0), activations.get("input"));
assertEquals(mlnAct.get(1), activations.get("firstLayer"));
assertEquals(mlnAct.get(2), activations.get("outputLayer"));
}
use of org.nd4j.linalg.dataset.api.iterator.DataSetIterator in project deeplearning4j by deeplearning4j.
the class TestComputationGraphNetwork method testIterationCountAndPresistence.
@Test
public void testIterationCountAndPresistence() throws IOException {
Nd4j.getRandom().setSeed(123);
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).seed(123).graphBuilder().addInputs("in").addLayer("0", new DenseLayer.Builder().nIn(4).nOut(3).weightInit(WeightInit.XAVIER).activation(Activation.TANH).build(), "in").addLayer("1", new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3).build(), "0").setOutputs("1").backprop(true).pretrain(false).build();
ComputationGraph network = new ComputationGraph(conf);
network.init();
DataSetIterator iter = new IrisDataSetIterator(50, 150);
assertEquals(0, network.getConfiguration().getIterationCount());
network.fit(iter);
assertEquals(3, network.getConfiguration().getIterationCount());
iter.reset();
network.fit(iter);
assertEquals(6, network.getConfiguration().getIterationCount());
iter.reset();
network.fit(iter.next());
assertEquals(7, network.getConfiguration().getIterationCount());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ModelSerializer.writeModel(network, baos, true);
byte[] asBytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(asBytes);
ComputationGraph net = ModelSerializer.restoreComputationGraph(bais, true);
assertEquals(7, net.getConfiguration().getIterationCount());
}
use of org.nd4j.linalg.dataset.api.iterator.DataSetIterator in project deeplearning4j by deeplearning4j.
the class TestComputationGraphNetwork method testIrisFit.
@Test
public void testIrisFit() {
ComputationGraphConfiguration configuration = getIrisGraphConfiguration();
ComputationGraph graph = new ComputationGraph(configuration);
graph.init();
MultiLayerConfiguration mlnConfig = getIrisMLNConfiguration();
MultiLayerNetwork net = new MultiLayerNetwork(mlnConfig);
net.init();
Nd4j.getRandom().setSeed(12345);
int nParams = getNumParams();
INDArray params = Nd4j.rand(1, nParams);
graph.setParams(params.dup());
net.setParams(params.dup());
DataSetIterator iris = new IrisDataSetIterator(75, 150);
net.fit(iris);
iris.reset();
graph.fit(iris);
//Check that parameters are equal for both models after fitting:
INDArray paramsMLN = net.params();
INDArray paramsGraph = graph.params();
assertNotEquals(params, paramsGraph);
assertEquals(paramsMLN, paramsGraph);
}
use of org.nd4j.linalg.dataset.api.iterator.DataSetIterator in project deeplearning4j by deeplearning4j.
the class TestComputationGraphNetwork method testPreTraining.
@Test
public void testPreTraining() {
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().iterations(100).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).updater(Updater.SGD).learningRate(1e-6).regularization(true).l2(2e-4).graphBuilder().addInputs("in").addLayer("layer0", new RBM.Builder(RBM.HiddenUnit.GAUSSIAN, RBM.VisibleUnit.GAUSSIAN).nIn(4).nOut(3).weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(0, 1)).activation(Activation.TANH).lossFunction(LossFunctions.LossFunction.KL_DIVERGENCE).build(), "in").addLayer("layer1", new RBM.Builder(RBM.HiddenUnit.GAUSSIAN, RBM.VisibleUnit.GAUSSIAN).nIn(4).nOut(3).weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(0, 1)).activation(Activation.TANH).lossFunction(LossFunctions.LossFunction.KL_DIVERGENCE).build(), "in").addLayer("layer2", new RBM.Builder(RBM.HiddenUnit.GAUSSIAN, RBM.VisibleUnit.GAUSSIAN).nIn(3).nOut(3).weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(0, 1)).activation(Activation.TANH).lossFunction(LossFunctions.LossFunction.KL_DIVERGENCE).build(), "layer1").addLayer("out", new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(3 + 3).nOut(3).weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(0, 1)).activation(Activation.SOFTMAX).build(), "layer0", "layer2").setOutputs("out").pretrain(true).backprop(false).build();
ComputationGraph net = new ComputationGraph(conf);
net.init();
net.setListeners(new ScoreIterationListener(1));
DataSetIterator iter = new IrisDataSetIterator(10, 150);
net.fit(iter);
}
Aggregations