Search in sources :

Example 1 with ModelAndGradient

use of org.deeplearning4j.ui.weights.ModelAndGradient in project deeplearning4j by deeplearning4j.

the class TestSerialization method testModelSerde.

@Test
public void testModelSerde() throws Exception {
    ObjectMapper mapper = getMapper();
    NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().momentum(0.9f).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1000).learningRate(1e-1f).layer(new org.deeplearning4j.nn.conf.layers.AutoEncoder.Builder().nIn(4).nOut(3).corruptionLevel(0.6).sparsity(0.5).lossFunction(LossFunctions.LossFunction.RECONSTRUCTION_CROSSENTROPY).build()).build();
    DataSet d2 = new IrisDataSetIterator(150, 150).next();
    INDArray input = d2.getFeatureMatrix();
    int numParams = conf.getLayer().initializer().numParams(conf);
    INDArray params = Nd4j.create(1, numParams);
    AutoEncoder da = (AutoEncoder) conf.getLayer().instantiate(conf, Arrays.asList(new ScoreIterationListener(1), new HistogramIterationListener(1)), 0, params, true);
    da.setInput(input);
    ModelAndGradient g = new ModelAndGradient(da);
    String json = mapper.writeValueAsString(g);
    ModelAndGradient read = mapper.readValue(json, ModelAndGradient.class);
    assertEquals(g, read);
}
Also used : IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSet(org.nd4j.linalg.dataset.DataSet) ModelAndGradient(org.deeplearning4j.ui.weights.ModelAndGradient) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) HistogramIterationListener(org.deeplearning4j.ui.weights.HistogramIterationListener) INDArray(org.nd4j.linalg.api.ndarray.INDArray) AutoEncoder(org.deeplearning4j.nn.layers.feedforward.autoencoder.AutoEncoder) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IrisDataSetIterator (org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator)1 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)1 AutoEncoder (org.deeplearning4j.nn.layers.feedforward.autoencoder.AutoEncoder)1 ScoreIterationListener (org.deeplearning4j.optimize.listeners.ScoreIterationListener)1 HistogramIterationListener (org.deeplearning4j.ui.weights.HistogramIterationListener)1 ModelAndGradient (org.deeplearning4j.ui.weights.ModelAndGradient)1 Test (org.junit.Test)1 INDArray (org.nd4j.linalg.api.ndarray.INDArray)1 DataSet (org.nd4j.linalg.dataset.DataSet)1