Java Code Examples for org.nd4j.shade.jackson.databind.ObjectMapper#readValue()

The following examples show how to use org.nd4j.shade.jackson.databind.ObjectMapper#readValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestSchedules.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJson() throws Exception {

    ObjectMapper om = new ObjectMapper();
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    om.enable(SerializationFeature.INDENT_OUTPUT);

    ISchedule[] schedules = new ISchedule[]{
            new ExponentialSchedule(ScheduleType.ITERATION, 1.0, 0.5),
            new InverseSchedule(ScheduleType.ITERATION, 1.0, 0.5, 2),
            new MapSchedule.Builder(ScheduleType.ITERATION).add(0, 1.0).add(10,0.5).build(),
            new PolySchedule(ScheduleType.ITERATION, 1.0, 2, 100),
            new SigmoidSchedule(ScheduleType.ITERATION, 1.0, 0.5, 10),
            new StepSchedule(ScheduleType.ITERATION, 1.0, 0.9, 100)};


    for(ISchedule s : schedules){
        String json = om.writeValueAsString(s);
        ISchedule fromJson = om.readValue(json, ISchedule.class);
        assertEquals(s, fromJson);
    }
}
 
Example 2
Source File: TestDistributionDeserializer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDistributionDeserializer() throws Exception {
    //Test current format:
    Distribution[] distributions =
                    new Distribution[] {new NormalDistribution(3, 0.5), new UniformDistribution(-2, 1),
                                    new GaussianDistribution(2, 1.0), new BinomialDistribution(10, 0.3)};

    ObjectMapper om = NeuralNetConfiguration.mapper();

    for (Distribution d : distributions) {
        String json = om.writeValueAsString(d);
        Distribution fromJson = om.readValue(json, Distribution.class);

        assertEquals(d, fromJson);
    }
}
 
Example 3
Source File: Nd4jCommonValidator.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
protected static ValidationResult isValidJson(String content, File f) {
    try {
        ObjectMapper om = new ObjectMapper();
        JavaType javaType = om.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
        om.readValue(content, javaType);    //Don't care about result, just that it can be parsed successfully
    } catch (Throwable t) {
        //Jackson should tell us specifically where error occurred also
        return ValidationResult.builder()
                .valid(false)
                .formatType("JSON")
                .path(getPath(f))
                .issues(Collections.singletonList("File does not appear to be valid JSON"))
                .exception(t)
                .build();
    }


    return ValidationResult.builder()
            .valid(true)
            .formatType("JSON")
            .path(getPath(f))
            .build();
}
 
Example 4
Source File: TestSchedules.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJson() throws Exception {

    ObjectMapper om = new ObjectMapper();
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    om.enable(SerializationFeature.INDENT_OUTPUT);

    ISchedule[] schedules = new ISchedule[]{
            new ExponentialSchedule(ScheduleType.ITERATION, 1.0, 0.5),
            new InverseSchedule(ScheduleType.ITERATION, 1.0, 0.5, 2),
            new MapSchedule.Builder(ScheduleType.ITERATION).add(0, 1.0).add(10,0.5).build(),
            new PolySchedule(ScheduleType.ITERATION, 1.0, 2, 100),
            new SigmoidSchedule(ScheduleType.ITERATION, 1.0, 0.5, 10),
            new StepSchedule(ScheduleType.ITERATION, 1.0, 0.9, 100),
            new CycleSchedule(ScheduleType.ITERATION, 1.5, 100)
    };


    for(ISchedule s : schedules){
        String json = om.writeValueAsString(s);
        ISchedule fromJson = om.readValue(json, ISchedule.class);
        assertEquals(s, fromJson);
    }
}
 
Example 5
Source File: LossFunctionJson.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonSerialization() throws Exception {

    INDArray w = Nd4j.create(new double[] {1.0, 2.0, 3.0});

    ILossFunction[] lossFns = new ILossFunction[] {new LossBinaryXENT(), new LossBinaryXENT(w),
                    new LossCosineProximity(), new LossHinge(), new LossKLD(), new LossL1(), new LossL1(w),
                    new LossL2(), new LossL2(w), new LossMAE(), new LossMAE(w), new LossMAPE(), new LossMAPE(w),
                    new LossMCXENT(), new LossMCXENT(w), new LossMSE(), new LossMSE(w), new LossMSLE(),
                    new LossMSLE(w), new LossNegativeLogLikelihood(), new LossNegativeLogLikelihood(w),
                    new LossPoisson(), new LossSquaredHinge(), new LossMultiLabel()};

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    for (ILossFunction lf : lossFns) {
        String asJson = mapper.writeValueAsString(lf);
        //            System.out.println(asJson);

        ILossFunction fromJson = mapper.readValue(asJson, ILossFunction.class);
        assertEquals(lf, fromJson);
    }
}
 
Example 6
Source File: ParameterAveragingTrainingMaster.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Create a ParameterAveragingTrainingMaster instance by deserializing a JSON string that has been serialized with
 * {@link #toJson()}
 *
 * @param jsonStr ParameterAveragingTrainingMaster configuration serialized as JSON
 */
public static ParameterAveragingTrainingMaster fromJson(String jsonStr) {
    ObjectMapper om = getJsonMapper();
    try {
        return om.readValue(jsonStr, ParameterAveragingTrainingMaster.class);
    } catch (IOException e) {
        throw new RuntimeException("Could not parse JSON", e);
    }
}
 
Example 7
Source File: Schema.java    From DataVec with Apache License 2.0 5 votes vote down vote up
private static Schema fromJacksonString(String str, JsonFactory factory) {
    ObjectMapper om = new ObjectMapper(factory);
    om.registerModule(new JodaModule());
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.enable(SerializationFeature.INDENT_OUTPUT);
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    try {
        return om.readValue(str, Schema.class);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: WordVectorSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This utility method returns ElementPair from Base64-encoded string
 *
 * @param encoded
 * @return
 */
protected static ElementPair fromEncodedJson(String encoded) {
    ObjectMapper mapper = SequenceElement.mapper();
    try {
        String decoded = new String(Base64.decodeBase64(encoded), "UTF-8");
        return mapper.readValue(decoded, ElementPair.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: AbstractElementFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method builds object from provided JSON
 *
 * @param json JSON for restored object
 * @return restored object
 */
@Override
public T deserialize(String json) {
    ObjectMapper mapper = SequenceElement.mapper();
    try {
        T ret = (T) mapper.readValue(json, targetClass);
        return ret;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: NeuralNetConfiguration.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Create a neural net configuration from json
 *
 * @param json the neural net configuration from json
 * @return
 */
public static NeuralNetConfiguration fromYaml(String json) {
    ObjectMapper mapper = mapperYaml();
    try {
        NeuralNetConfiguration ret = mapper.readValue(json, NeuralNetConfiguration.class);
        return ret;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 11
Source File: MultiLayerConfiguration.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Create a neural net configuration from json
 *
 * @param json the neural net configuration from json
 * @return {@link MultiLayerConfiguration}
 */
public static MultiLayerConfiguration fromYaml(String json) {
    ObjectMapper mapper = NeuralNetConfiguration.mapperYaml();
    try {
        return mapper.readValue(json, MultiLayerConfiguration.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: LegacyWeightInitTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Test that distribution can be serialized and de-serialized in JSON format
 */
@Test
public void serializeDeserializeDistributionJson() throws IOException {
    final long[] shape = {3, 7}; // To make identity happy
    final long fanIn = shape[0];
    final long fanOut = shape[1];

    final ObjectMapper mapper = JsonMappers.getMapper();
    final INDArray inBefore = Nd4j.create(fanIn * fanOut);
    final INDArray inAfter = inBefore.dup();

    for (Distribution dist : distributions) {

        Nd4j.getRandom().setSeed(SEED);
        final IWeightInit before = new WeightInitDistribution(dist);
        final INDArray expected = before.init(
                fanIn,
                fanOut,
                shape,
                inBefore.ordering(),
                inBefore);

        final String json = mapper.writeValueAsString(before);
        final IWeightInit after = mapper.readValue(json, IWeightInit.class);

        Nd4j.getRandom().setSeed(SEED);
        final INDArray actual = after.init(fanIn, fanOut, shape, inAfter.ordering(), inAfter);

        assertArrayEquals("Incorrect shape for " + dist.getClass().getSimpleName() + "!", shape, actual.shape());

        assertEquals("Incorrect weight initialization for " + dist.getClass().getSimpleName() + "!", expected, actual);
    }
}
 
Example 13
Source File: SharedTrainingMaster.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Create a SharedTrainingMaster instance by deserializing a JSON string that has been serialized with
 * {@link #toJson()}
 *
 * @param jsonStr SharedTrainingMaster configuration serialized as JSON
 */
public static SharedTrainingMaster fromJson(String jsonStr) {
    ObjectMapper om = getJsonMapper();
    try {
        return om.readValue(jsonStr, SharedTrainingMaster.class);
    } catch (IOException e) {
        throw new RuntimeException("Could not parse JSON", e);
    }
}
 
Example 14
Source File: OnnxDescriptorParser.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get the onnx op descriptors by name
 * @return the onnx op descriptors by name
 * @throws Exception
 */
public static Map<String,OpDescriptor> onnxOpDescriptors() throws Exception {
    try(InputStream is = new ClassPathResource("onnxops.json").getInputStream()) {
        ObjectMapper objectMapper = new ObjectMapper();
        OnnxDescriptor opDescriptor = objectMapper.readValue(is,OnnxDescriptor.class);
        Map<String,OpDescriptor> descriptorMap = new HashMap<>();
        for(OpDescriptor descriptor : opDescriptor.getDescriptors()) {
            descriptorMap.put(descriptor.getName(),descriptor);
        }



        return descriptorMap;
    }
}
 
Example 15
Source File: VocabWordFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method builds object from provided JSON
 *
 * @param json JSON for restored object
 * @return restored object
 */
@Override
public VocabWord deserialize(String json) {
    ObjectMapper mapper = SequenceElement.mapper();
    try {
        VocabWord ret = mapper.readValue(json, VocabWord.class);
        return ret;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 16
Source File: OnnxDescriptorParser.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get the onnx op descriptors by name
 * @return the onnx op descriptors by name
 * @throws Exception
 */
public static Map<String,OpDescriptor> onnxOpDescriptors() throws Exception {
    try(InputStream is = new ClassPathResource("onnxops.json").getInputStream()) {
        ObjectMapper objectMapper = new ObjectMapper();
        OnnxDescriptor opDescriptor = objectMapper.readValue(is,OnnxDescriptor.class);
        Map<String,OpDescriptor> descriptorMap = new HashMap<>();
        for(OpDescriptor descriptor : opDescriptor.getDescriptors()) {
            descriptorMap.put(descriptor.getName(),descriptor);
        }



        return descriptorMap;
    }
}
 
Example 17
Source File: ParameterAveragingTrainingMaster.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Create a ParameterAveragingTrainingMaster instance by deserializing a YAML string that has been serialized with
 * {@link #toYaml()}
 *
 * @param yamlStr ParameterAveragingTrainingMaster configuration serialized as YAML
 */
public static ParameterAveragingTrainingMaster fromYaml(String yamlStr) {
    ObjectMapper om = getYamlMapper();
    try {
        return om.readValue(yamlStr, ParameterAveragingTrainingMaster.class);
    } catch (IOException e) {
        throw new RuntimeException("Could not parse YAML", e);
    }
}
 
Example 18
Source File: BaseSerializer.java    From DataVec with Apache License 2.0 5 votes vote down vote up
private <T> T load(String str, TypeReference<T> typeReference) {
    ObjectMapper om = getObjectMapper();
    try {
        return om.readValue(str, typeReference);
    } catch (Exception e) {
        //TODO better exception
        throw new RuntimeException(e);
    }
}
 
Example 19
Source File: BaseSerializer.java    From DataVec with Apache License 2.0 5 votes vote down vote up
private <T> T load(String str, Class<T> clazz) {
    ObjectMapper om = getObjectMapper();
    try {
        return om.readValue(str, clazz);
    } catch (Exception e) {
        //TODO better exception
        throw new RuntimeException(e);
    }
}
 
Example 20
Source File: TestDistributionDeserializer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDistributionDeserializerLegacyFormat() throws Exception {
    ObjectMapper om = NeuralNetConfiguration.mapper();

    String normalJson = "{\n" + "          \"normal\" : {\n" + "            \"mean\" : 0.1,\n"
                    + "            \"std\" : 1.2\n" + "          }\n" + "        }";

    Distribution nd = om.readValue(normalJson, Distribution.class);
    assertTrue(nd instanceof NormalDistribution);
    NormalDistribution normDist = (NormalDistribution) nd;
    assertEquals(0.1, normDist.getMean(), 1e-6);
    assertEquals(1.2, normDist.getStd(), 1e-6);


    String uniformJson = "{\n" + "                \"uniform\" : {\n" + "                  \"lower\" : -1.1,\n"
                    + "                  \"upper\" : 2.2\n" + "                }\n" + "              }";

    Distribution ud = om.readValue(uniformJson, Distribution.class);
    assertTrue(ud instanceof UniformDistribution);
    UniformDistribution uniDist = (UniformDistribution) ud;
    assertEquals(-1.1, uniDist.getLower(), 1e-6);
    assertEquals(2.2, uniDist.getUpper(), 1e-6);


    String gaussianJson = "{\n" + "                \"gaussian\" : {\n" + "                  \"mean\" : 0.1,\n"
                    + "                  \"std\" : 1.2\n" + "                }\n" + "              }";

    Distribution gd = om.readValue(gaussianJson, Distribution.class);
    assertTrue(gd instanceof GaussianDistribution);
    GaussianDistribution gDist = (GaussianDistribution) gd;
    assertEquals(0.1, gDist.getMean(), 1e-6);
    assertEquals(1.2, gDist.getStd(), 1e-6);

    String bernoulliJson =
                    "{\n" + "                \"binomial\" : {\n" + "                  \"numberOfTrials\" : 10,\n"
                                    + "                  \"probabilityOfSuccess\" : 0.3\n" + "                }\n"
                                    + "              }";

    Distribution bd = om.readValue(bernoulliJson, Distribution.class);
    assertTrue(bd instanceof BinomialDistribution);
    BinomialDistribution binDist = (BinomialDistribution) bd;
    assertEquals(10, binDist.getNumberOfTrials());
    assertEquals(0.3, binDist.getProbabilityOfSuccess(), 1e-6);
}