Search in sources :

Example 1 with CustomActivation

use of org.deeplearning4j.nn.layers.custom.testclasses.CustomActivation in project deeplearning4j by deeplearning4j.

the class TestCustomActivation method testCustomActivationFn.

@Test
public void testCustomActivationFn() {
    //First: Ensure that the CustomActivation class is registered
    ObjectMapper mapper = NeuralNetConfiguration.mapper();
    AnnotatedClass ac = AnnotatedClass.construct(IActivation.class, mapper.getSerializationConfig().getAnnotationIntrospector(), null);
    Collection<NamedType> types = mapper.getSubtypeResolver().collectAndResolveSubtypes(ac, mapper.getSerializationConfig(), mapper.getSerializationConfig().getAnnotationIntrospector());
    boolean found = false;
    for (NamedType nt : types) {
        System.out.println(nt);
        if (nt.getType() == CustomActivation.class)
            found = true;
    }
    assertTrue("CustomActivation: not registered with NeuralNetConfiguration mapper", found);
    //Second: let's create a MultiLayerCofiguration with one, and check JSON and YAML config actually works...
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().learningRate(0.1).list().layer(0, new DenseLayer.Builder().nIn(10).nOut(10).activation(new CustomActivation()).build()).layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(10).nOut(10).build()).pretrain(false).backprop(true).build();
    String json = conf.toJson();
    String yaml = conf.toYaml();
    System.out.println(json);
    MultiLayerConfiguration confFromJson = MultiLayerConfiguration.fromJson(json);
    assertEquals(conf, confFromJson);
    MultiLayerConfiguration confFromYaml = MultiLayerConfiguration.fromYaml(yaml);
    assertEquals(conf, confFromYaml);
}
Also used : OutputLayer(org.deeplearning4j.nn.conf.layers.OutputLayer) NamedType(org.nd4j.shade.jackson.databind.jsontype.NamedType) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) DenseLayer(org.deeplearning4j.nn.conf.layers.DenseLayer) AnnotatedClass(org.nd4j.shade.jackson.databind.introspect.AnnotatedClass) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper) CustomActivation(org.deeplearning4j.nn.layers.custom.testclasses.CustomActivation) Test(org.junit.Test)

Aggregations

MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)1 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)1 DenseLayer (org.deeplearning4j.nn.conf.layers.DenseLayer)1 OutputLayer (org.deeplearning4j.nn.conf.layers.OutputLayer)1 CustomActivation (org.deeplearning4j.nn.layers.custom.testclasses.CustomActivation)1 Test (org.junit.Test)1 ObjectMapper (org.nd4j.shade.jackson.databind.ObjectMapper)1 AnnotatedClass (org.nd4j.shade.jackson.databind.introspect.AnnotatedClass)1 NamedType (org.nd4j.shade.jackson.databind.jsontype.NamedType)1