Search in sources :

Example 1 with LossNegativeLogLikelihood

use of org.nd4j.linalg.lossfunctions.impl.LossNegativeLogLikelihood in project deeplearning4j by deeplearning4j.

the class MultiLayerConfiguration method fromJson.

/**
     * Create a neural net configuration from json
     * @param json the neural net configuration from json
     * @return {@link MultiLayerConfiguration}
     */
public static MultiLayerConfiguration fromJson(String json) {
    MultiLayerConfiguration conf;
    ObjectMapper mapper = NeuralNetConfiguration.mapper();
    try {
        conf = mapper.readValue(json, MultiLayerConfiguration.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    //To maintain backward compatibility after loss function refactoring (configs generated with v0.5.0 or earlier)
    // Previously: enumeration used for loss functions. Now: use classes
    // IN the past, could have only been an OutputLayer or RnnOutputLayer using these enums
    int layerCount = 0;
    JsonNode confs = null;
    for (NeuralNetConfiguration nnc : conf.getConfs()) {
        Layer l = nnc.getLayer();
        if (l instanceof BaseOutputLayer && ((BaseOutputLayer) l).getLossFn() == null) {
            //lossFn field null -> may be an old config format, with lossFunction field being for the enum
            //if so, try walking the JSON graph to extract out the appropriate enum value
            BaseOutputLayer ol = (BaseOutputLayer) l;
            try {
                JsonNode jsonNode = mapper.readTree(json);
                if (confs == null) {
                    confs = jsonNode.get("confs");
                }
                if (confs instanceof ArrayNode) {
                    ArrayNode layerConfs = (ArrayNode) confs;
                    JsonNode outputLayerNNCNode = layerConfs.get(layerCount);
                    if (outputLayerNNCNode == null)
                        //Should never happen...
                        return conf;
                    JsonNode outputLayerNode = outputLayerNNCNode.get("layer");
                    JsonNode lossFunctionNode = null;
                    if (outputLayerNode.has("output")) {
                        lossFunctionNode = outputLayerNode.get("output").get("lossFunction");
                    } else if (outputLayerNode.has("rnnoutput")) {
                        lossFunctionNode = outputLayerNode.get("rnnoutput").get("lossFunction");
                    }
                    if (lossFunctionNode != null) {
                        String lossFunctionEnumStr = lossFunctionNode.asText();
                        LossFunctions.LossFunction lossFunction = null;
                        try {
                            lossFunction = LossFunctions.LossFunction.valueOf(lossFunctionEnumStr);
                        } catch (Exception e) {
                            log.warn("OutputLayer with null LossFunction or pre-0.6.0 loss function configuration detected: could not parse JSON", e);
                        }
                        if (lossFunction != null) {
                            switch(lossFunction) {
                                case MSE:
                                    ol.setLossFn(new LossMSE());
                                    break;
                                case XENT:
                                    ol.setLossFn(new LossBinaryXENT());
                                    break;
                                case NEGATIVELOGLIKELIHOOD:
                                    ol.setLossFn(new LossNegativeLogLikelihood());
                                    break;
                                case MCXENT:
                                    ol.setLossFn(new LossMCXENT());
                                    break;
                                //Remaining: TODO
                                case EXPLL:
                                case RMSE_XENT:
                                case SQUARED_LOSS:
                                case RECONSTRUCTION_CROSSENTROPY:
                                case CUSTOM:
                                default:
                                    log.warn("OutputLayer with null LossFunction or pre-0.6.0 loss function configuration detected: could not set loss function for {}", lossFunction);
                                    break;
                            }
                        }
                    }
                } else {
                    log.warn("OutputLayer with null LossFunction or pre-0.6.0 loss function configuration detected: could not parse JSON: layer 'confs' field is not an ArrayNode (is: {})", (confs != null ? confs.getClass() : null));
                }
            } catch (IOException e) {
                log.warn("OutputLayer with null LossFunction or pre-0.6.0 loss function configuration detected: could not parse JSON", e);
                break;
            }
        }
        //Try to load the old format if necessary, and create the appropriate IActivation instance
        if (l.getActivationFn() == null) {
            try {
                JsonNode jsonNode = mapper.readTree(json);
                if (confs == null) {
                    confs = jsonNode.get("confs");
                }
                if (confs instanceof ArrayNode) {
                    ArrayNode layerConfs = (ArrayNode) confs;
                    JsonNode outputLayerNNCNode = layerConfs.get(layerCount);
                    if (outputLayerNNCNode == null)
                        //Should never happen...
                        return conf;
                    JsonNode layerWrapperNode = outputLayerNNCNode.get("layer");
                    if (layerWrapperNode == null || layerWrapperNode.size() != 1) {
                        continue;
                    }
                    JsonNode layerNode = layerWrapperNode.elements().next();
                    //Should only have 1 element: "dense", "output", etc
                    JsonNode activationFunction = layerNode.get("activationFunction");
                    if (activationFunction != null) {
                        IActivation ia = Activation.fromString(activationFunction.asText()).getActivationFunction();
                        l.setActivationFn(ia);
                    }
                }
            } catch (IOException e) {
                log.warn("Layer with null ActivationFn field or pre-0.7.2 activation function detected: could not parse JSON", e);
            }
        }
        layerCount++;
    }
    return conf;
}
Also used : LossNegativeLogLikelihood(org.nd4j.linalg.lossfunctions.impl.LossNegativeLogLikelihood) LossMCXENT(org.nd4j.linalg.lossfunctions.impl.LossMCXENT) JsonNode(org.nd4j.shade.jackson.databind.JsonNode) IOException(java.io.IOException) IActivation(org.nd4j.linalg.activations.IActivation) IOException(java.io.IOException) LossFunctions(org.nd4j.linalg.lossfunctions.LossFunctions) LossMSE(org.nd4j.linalg.lossfunctions.impl.LossMSE) ArrayNode(org.nd4j.shade.jackson.databind.node.ArrayNode) LossBinaryXENT(org.nd4j.linalg.lossfunctions.impl.LossBinaryXENT) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper)

Example 2 with LossNegativeLogLikelihood

use of org.nd4j.linalg.lossfunctions.impl.LossNegativeLogLikelihood in project deeplearning4j by deeplearning4j.

the class RegressionTest050 method regressionTestCNN1.

@Test
public void regressionTestCNN1() throws Exception {
    File f = new ClassPathResource("regression_testing/050/050_ModelSerializer_Regression_CNN_1.zip").getTempFileFromArchive();
    MultiLayerNetwork net = ModelSerializer.restoreMultiLayerNetwork(f, true);
    MultiLayerConfiguration conf = net.getLayerWiseConfigurations();
    assertEquals(3, conf.getConfs().size());
    assertTrue(conf.isBackprop());
    assertFalse(conf.isPretrain());
    ConvolutionLayer l0 = (ConvolutionLayer) conf.getConf(0).getLayer();
    assertEquals("tanh", l0.getActivationFn().toString());
    assertEquals(3, l0.getNIn());
    assertEquals(3, l0.getNOut());
    assertEquals(WeightInit.RELU, l0.getWeightInit());
    assertEquals(Updater.RMSPROP, l0.getUpdater());
    assertEquals(0.96, l0.getRmsDecay(), 1e-6);
    assertEquals(0.15, l0.getLearningRate(), 1e-6);
    assertArrayEquals(new int[] { 2, 2 }, l0.getKernelSize());
    assertArrayEquals(new int[] { 1, 1 }, l0.getStride());
    assertArrayEquals(new int[] { 0, 0 }, l0.getPadding());
    //Pre-0.7.0: no ConvolutionMode. Want to default to truncate here if not set
    assertEquals(l0.getConvolutionMode(), ConvolutionMode.Truncate);
    SubsamplingLayer l1 = (SubsamplingLayer) conf.getConf(1).getLayer();
    assertArrayEquals(new int[] { 2, 2 }, l1.getKernelSize());
    assertArrayEquals(new int[] { 1, 1 }, l1.getStride());
    assertArrayEquals(new int[] { 0, 0 }, l1.getPadding());
    assertEquals(PoolingType.MAX, l1.getPoolingType());
    //Pre-0.7.0: no ConvolutionMode. Want to default to truncate here if not set
    assertEquals(l1.getConvolutionMode(), ConvolutionMode.Truncate);
    OutputLayer l2 = (OutputLayer) conf.getConf(2).getLayer();
    assertEquals("sigmoid", l1.getActivationFn().toString());
    assertEquals(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD, l2.getLossFunction());
    //TODO
    assertTrue(l2.getLossFn() instanceof LossNegativeLogLikelihood);
    assertEquals(26 * 26 * 3, l2.getNIn());
    assertEquals(5, l2.getNOut());
    assertEquals(WeightInit.RELU, l0.getWeightInit());
    assertEquals(Updater.RMSPROP, l0.getUpdater());
    assertEquals(0.96, l0.getRmsDecay(), 1e-6);
    assertEquals(0.15, l0.getLearningRate(), 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());
}
Also used : LossNegativeLogLikelihood(org.nd4j.linalg.lossfunctions.impl.LossNegativeLogLikelihood) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) File(java.io.File) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) Test(org.junit.Test)

Example 3 with LossNegativeLogLikelihood

use of org.nd4j.linalg.lossfunctions.impl.LossNegativeLogLikelihood in project deeplearning4j by deeplearning4j.

the class RegressionTest071 method regressionTestCNN1.

@Test
public void regressionTestCNN1() throws Exception {
    File f = new ClassPathResource("regression_testing/071/071_ModelSerializer_Regression_CNN_1.zip").getTempFileFromArchive();
    MultiLayerNetwork net = ModelSerializer.restoreMultiLayerNetwork(f, true);
    MultiLayerConfiguration conf = net.getLayerWiseConfigurations();
    assertEquals(3, conf.getConfs().size());
    assertTrue(conf.isBackprop());
    assertFalse(conf.isPretrain());
    ConvolutionLayer l0 = (ConvolutionLayer) conf.getConf(0).getLayer();
    assertEquals("tanh", l0.getActivationFn().toString());
    assertEquals(3, l0.getNIn());
    assertEquals(3, l0.getNOut());
    assertEquals(WeightInit.RELU, l0.getWeightInit());
    assertEquals(Updater.RMSPROP, l0.getUpdater());
    assertEquals(0.96, l0.getRmsDecay(), 1e-6);
    assertEquals(0.15, l0.getLearningRate(), 1e-6);
    assertArrayEquals(new int[] { 2, 2 }, l0.getKernelSize());
    assertArrayEquals(new int[] { 1, 1 }, l0.getStride());
    assertArrayEquals(new int[] { 0, 0 }, l0.getPadding());
    assertEquals(l0.getConvolutionMode(), ConvolutionMode.Same);
    SubsamplingLayer l1 = (SubsamplingLayer) conf.getConf(1).getLayer();
    assertArrayEquals(new int[] { 2, 2 }, l1.getKernelSize());
    assertArrayEquals(new int[] { 1, 1 }, l1.getStride());
    assertArrayEquals(new int[] { 0, 0 }, l1.getPadding());
    assertEquals(PoolingType.MAX, l1.getPoolingType());
    assertEquals(l1.getConvolutionMode(), ConvolutionMode.Same);
    OutputLayer l2 = (OutputLayer) conf.getConf(2).getLayer();
    assertEquals("sigmoid", l1.getActivationFn().toString());
    assertEquals(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD, l2.getLossFunction());
    //TODO
    assertTrue(l2.getLossFn() instanceof LossNegativeLogLikelihood);
    assertEquals(26 * 26 * 3, l2.getNIn());
    assertEquals(5, l2.getNOut());
    assertEquals(WeightInit.RELU, l0.getWeightInit());
    assertEquals(Updater.RMSPROP, l0.getUpdater());
    assertEquals(0.96, l0.getRmsDecay(), 1e-6);
    assertEquals(0.15, l0.getLearningRate(), 1e-6);
    assertTrue(conf.getInputPreProcess(2) instanceof CnnToFeedForwardPreProcessor);
    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());
}
Also used : LossNegativeLogLikelihood(org.nd4j.linalg.lossfunctions.impl.LossNegativeLogLikelihood) CnnToFeedForwardPreProcessor(org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor) File(java.io.File) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) Test(org.junit.Test)

Example 4 with LossNegativeLogLikelihood

use of org.nd4j.linalg.lossfunctions.impl.LossNegativeLogLikelihood in project deeplearning4j by deeplearning4j.

the class RegressionTest060 method regressionTestCNN1.

@Test
public void regressionTestCNN1() throws Exception {
    File f = new ClassPathResource("regression_testing/060/060_ModelSerializer_Regression_CNN_1.zip").getTempFileFromArchive();
    MultiLayerNetwork net = ModelSerializer.restoreMultiLayerNetwork(f, true);
    MultiLayerConfiguration conf = net.getLayerWiseConfigurations();
    assertEquals(3, conf.getConfs().size());
    assertTrue(conf.isBackprop());
    assertFalse(conf.isPretrain());
    ConvolutionLayer l0 = (ConvolutionLayer) conf.getConf(0).getLayer();
    assertEquals("tanh", l0.getActivationFn().toString());
    assertEquals(3, l0.getNIn());
    assertEquals(3, l0.getNOut());
    assertEquals(WeightInit.RELU, l0.getWeightInit());
    assertEquals(Updater.RMSPROP, l0.getUpdater());
    assertEquals(0.96, l0.getRmsDecay(), 1e-6);
    assertEquals(0.15, l0.getLearningRate(), 1e-6);
    assertArrayEquals(new int[] { 2, 2 }, l0.getKernelSize());
    assertArrayEquals(new int[] { 1, 1 }, l0.getStride());
    assertArrayEquals(new int[] { 0, 0 }, l0.getPadding());
    //Pre-0.7.0: no ConvolutionMode. Want to default to truncate here if not set
    assertEquals(l0.getConvolutionMode(), ConvolutionMode.Truncate);
    SubsamplingLayer l1 = (SubsamplingLayer) conf.getConf(1).getLayer();
    assertArrayEquals(new int[] { 2, 2 }, l1.getKernelSize());
    assertArrayEquals(new int[] { 1, 1 }, l1.getStride());
    assertArrayEquals(new int[] { 0, 0 }, l1.getPadding());
    assertEquals(PoolingType.MAX, l1.getPoolingType());
    //Pre-0.7.0: no ConvolutionMode. Want to default to truncate here if not set
    assertEquals(l1.getConvolutionMode(), ConvolutionMode.Truncate);
    OutputLayer l2 = (OutputLayer) conf.getConf(2).getLayer();
    assertEquals("sigmoid", l1.getActivationFn().toString());
    assertEquals(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD, l2.getLossFunction());
    //TODO
    assertTrue(l2.getLossFn() instanceof LossNegativeLogLikelihood);
    assertEquals(26 * 26 * 3, l2.getNIn());
    assertEquals(5, l2.getNOut());
    assertEquals(WeightInit.RELU, l0.getWeightInit());
    assertEquals(Updater.RMSPROP, l0.getUpdater());
    assertEquals(0.96, l0.getRmsDecay(), 1e-6);
    assertEquals(0.15, l0.getLearningRate(), 1e-6);
    assertTrue(conf.getInputPreProcess(2) instanceof CnnToFeedForwardPreProcessor);
    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());
}
Also used : LossNegativeLogLikelihood(org.nd4j.linalg.lossfunctions.impl.LossNegativeLogLikelihood) CnnToFeedForwardPreProcessor(org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor) File(java.io.File) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) Test(org.junit.Test)

Aggregations

LossNegativeLogLikelihood (org.nd4j.linalg.lossfunctions.impl.LossNegativeLogLikelihood)4 File (java.io.File)3 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)3 Test (org.junit.Test)3 ClassPathResource (org.nd4j.linalg.io.ClassPathResource)3 CnnToFeedForwardPreProcessor (org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor)2 IOException (java.io.IOException)1 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)1 IActivation (org.nd4j.linalg.activations.IActivation)1 LossFunctions (org.nd4j.linalg.lossfunctions.LossFunctions)1 LossBinaryXENT (org.nd4j.linalg.lossfunctions.impl.LossBinaryXENT)1 LossMCXENT (org.nd4j.linalg.lossfunctions.impl.LossMCXENT)1 LossMSE (org.nd4j.linalg.lossfunctions.impl.LossMSE)1 JsonNode (org.nd4j.shade.jackson.databind.JsonNode)1 ObjectMapper (org.nd4j.shade.jackson.databind.ObjectMapper)1 ArrayNode (org.nd4j.shade.jackson.databind.node.ArrayNode)1