use of org.nd4j.linalg.lossfunctions.impl.LossMSE in project deeplearning4j by deeplearning4j.
the class RegressionTest050 method regressionTestMLP2.
@Test
public void regressionTestMLP2() throws Exception {
File f = new ClassPathResource("regression_testing/050/050_ModelSerializer_Regression_MLP_2.zip").getTempFileFromArchive();
MultiLayerNetwork net = ModelSerializer.restoreMultiLayerNetwork(f, true);
MultiLayerConfiguration conf = net.getLayerWiseConfigurations();
assertEquals(2, conf.getConfs().size());
assertTrue(conf.isBackprop());
assertFalse(conf.isPretrain());
DenseLayer l0 = (DenseLayer) conf.getConf(0).getLayer();
assertTrue(l0.getActivationFn() instanceof ActivationLReLU);
assertEquals(3, l0.getNIn());
assertEquals(4, l0.getNOut());
assertEquals(WeightInit.DISTRIBUTION, l0.getWeightInit());
assertEquals(new NormalDistribution(0.1, 1.2), l0.getDist());
assertEquals(Updater.RMSPROP, l0.getUpdater());
assertEquals(0.96, l0.getRmsDecay(), 1e-6);
assertEquals(0.15, l0.getLearningRate(), 1e-6);
assertEquals(0.6, l0.getDropOut(), 1e-6);
assertEquals(0.1, l0.getL1(), 1e-6);
assertEquals(0.2, l0.getL2(), 1e-6);
OutputLayer l1 = (OutputLayer) conf.getConf(1).getLayer();
assertEquals("identity", l1.getActivationFn().toString());
assertEquals(LossFunctions.LossFunction.MSE, l1.getLossFunction());
assertTrue(l1.getLossFn() instanceof LossMSE);
assertEquals(4, l1.getNIn());
assertEquals(5, l1.getNOut());
assertEquals(WeightInit.DISTRIBUTION, l0.getWeightInit());
assertEquals(new NormalDistribution(0.1, 1.2), l0.getDist());
assertEquals(Updater.RMSPROP, l0.getUpdater());
assertEquals(0.96, l1.getRmsDecay(), 1e-6);
assertEquals(0.15, l1.getLearningRate(), 1e-6);
assertEquals(0.6, l1.getDropOut(), 1e-6);
assertEquals(0.1, l1.getL1(), 1e-6);
assertEquals(0.2, l1.getL2(), 1e-6);
int numParams = net.numParams();
assertEquals(Nd4j.linspace(1, numParams, numParams), net.params());
int updaterSize = net.getUpdater().stateSizeForLayer(net);
assertEquals(Nd4j.linspace(1, updaterSize, updaterSize), net.getUpdater().getStateViewArray());
}
use of org.nd4j.linalg.lossfunctions.impl.LossMSE in project deeplearning4j by deeplearning4j.
the class TestMiscFunctions method testVaeReconstructionErrorWithKey.
@Test
public void testVaeReconstructionErrorWithKey() {
//Simple test. We CAN do a direct comparison here vs. local, as reconstruction error is deterministic
int nIn = 10;
MultiLayerConfiguration mlc = new NeuralNetConfiguration.Builder().list().layer(0, new org.deeplearning4j.nn.conf.layers.variational.VariationalAutoencoder.Builder().reconstructionDistribution(new LossFunctionWrapper(Activation.IDENTITY, new LossMSE())).nIn(nIn).nOut(5).encoderLayerSizes(12).decoderLayerSizes(13).build()).build();
MultiLayerNetwork net = new MultiLayerNetwork(mlc);
net.init();
VariationalAutoencoder vae = (VariationalAutoencoder) net.getLayer(0);
List<Tuple2<Integer, INDArray>> toScore = new ArrayList<>();
for (int i = 0; i < 100; i++) {
INDArray arr = Nd4j.rand(1, nIn);
toScore.add(new Tuple2<Integer, INDArray>(i, arr));
}
JavaPairRDD<Integer, INDArray> rdd = sc.parallelizePairs(toScore);
JavaPairRDD<Integer, Double> reconstrErrors = rdd.mapPartitionsToPair(new VaeReconstructionErrorWithKeyFunction<Integer>(sc.broadcast(net.params()), sc.broadcast(mlc.toJson()), 16));
Map<Integer, Double> l = reconstrErrors.collectAsMap();
assertEquals(100, l.size());
for (int i = 0; i < 100; i++) {
assertTrue(l.containsKey(i));
INDArray localToScore = toScore.get(i)._2();
double localScore = vae.reconstructionError(localToScore).data().asDouble()[0];
assertEquals(localScore, l.get(i), 1e-6);
}
}
Aggregations