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

The following examples show how to use org.nd4j.shade.jackson.databind.ObjectMapper#writeValueAsString() . 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: 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 2
Source File: LossFunctionJson.java    From nd4j 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 3
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 4
Source File: BaseSerializer.java    From DataVec with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize a list of DataActions
 */
public String serializeDataActionList(List<DataAction> list) {
    ObjectMapper om = getObjectMapper();
    try {
        return om.writeValueAsString(new ListWrappers.DataActionList(list));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: ComputationGraphConfiguration.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * @return YAML representation of configuration
 */
public String toYaml() {
    ObjectMapper mapper = NeuralNetConfiguration.mapperYaml();
    synchronized (mapper) {
        try {
            return mapper.writeValueAsString(this);
        } catch (org.nd4j.shade.jackson.core.JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example 6
Source File: SharedTrainingMaster.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public String toJson() {
    ObjectMapper om = getJsonMapper();

    try {
        return om.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Error producing JSON representation for ParameterAveragingTrainingMaster", e);
    }
}
 
Example 7
Source File: ComputationGraphConfiguration.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * @return JSON representation of computation graph configuration
 */
public String toJson() {
    //As per MultiLayerConfiguration.toJson()
    ObjectMapper mapper = NeuralNetConfiguration.mapper();
    synchronized (mapper) {
        //JSON mappers are supposed to be thread safe: however, in practice they seem to miss fields occasionally
        //when writeValueAsString is used by multiple threads. This results in invalid JSON. See issue #3243
        try {
            return mapper.writeValueAsString(this);
        } catch (org.nd4j.shade.jackson.core.JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example 8
Source File: SharedTrainingMaster.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public String toYaml() {
    ObjectMapper om = getYamlMapper();

    try {
        return om.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Error producing YAML representation for ParameterAveragingTrainingMaster", e);
    }
}
 
Example 9
Source File: LegacyWeightInitTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Test that weight inits can be serialized and de-serialized in JSON format
 */
@Test
public void serializeDeserializeJson() throws IOException {
    final long[] shape = {5, 5}; // 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();

    // Just use to enum to loop over all strategies
    for (WeightInit legacyWi : WeightInit.values()) {
        if (legacyWi != WeightInit.DISTRIBUTION) {
            Nd4j.getRandom().setSeed(SEED);
            final IWeightInit before = legacyWi.getWeightInitFunction();
            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 " + legacyWi + "!", shape, actual.shape());
            assertEquals("Incorrect weight initialization for " + legacyWi + "!", expected, actual);
        }
    }
}
 
Example 10
Source File: ParameterAveragingTrainingMaster.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public String toJson() {
    ObjectMapper om = getJsonMapper();

    try {
        return om.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Error producing JSON representation for ParameterAveragingTrainingMaster", e);
    }
}
 
Example 11
Source File: VocabularyWord.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public String toJson() {
    ObjectMapper mapper = mapper();
    try {
        /*
            we need JSON as single line to save it at first line of the CSV model file
        */
        return mapper.writeValueAsString(this);
    } catch (org.nd4j.shade.jackson.core.JsonProcessingException 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: BaseSerializer.java    From DataVec with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize a list of SequenceComparators
 */
public String serializeSequenceComparatorList(List<SequenceComparator> list) {
    ObjectMapper om = getObjectMapper();
    try {
        return om.writeValueAsString(new ListWrappers.SequenceComparatorList(list));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 14
Source File: ParameterAveragingTrainingMaster.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public String toYaml() {
    ObjectMapper om = getYamlMapper();

    try {
        return om.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Error producing YAML representation for ParameterAveragingTrainingMaster", e);
    }
}
 
Example 15
Source File: MultiLayerConfiguration.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * @return JSON representation of NN configuration
 */
public String toJson() {
    ObjectMapper mapper = NeuralNetConfiguration.mapper();
    synchronized (mapper) {
        //JSON mappers are supposed to be thread safe: however, in practice they seem to miss fields occasionally
        //when writeValueAsString is used by multiple threads. This results in invalid JSON. See issue #3243
        try {
            return mapper.writeValueAsString(this);
        } catch (org.nd4j.shade.jackson.core.JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example 16
Source File: BaseSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize a list of SequenceComparators
 */
public String serializeSequenceComparatorList(List<SequenceComparator> list) {
    ObjectMapper om = getObjectMapper();
    try {
        return om.writeValueAsString(new ListWrappers.SequenceComparatorList(list));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 17
Source File: MultiLayerConfiguration.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * @return JSON representation of NN configuration
 */
public String toYaml() {
    ObjectMapper mapper = NeuralNetConfiguration.mapperYaml();
    synchronized (mapper) {
        try {
            return mapper.writeValueAsString(this);
        } catch (org.nd4j.shade.jackson.core.JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example 18
Source File: NeuralNetConfiguration.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Return this configuration as json
 *
 * @return this configuration represented as json
 */
public String toYaml() {
    ObjectMapper mapper = mapperYaml();

    try {
        String ret = mapper.writeValueAsString(this);
        return ret;

    } catch (org.nd4j.shade.jackson.core.JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}
 
Example 19
Source File: BaseSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize a list of DataActions
 */
public String serializeDataActionList(List<DataAction> list) {
    ObjectMapper om = getObjectMapper();
    try {
        return om.writeValueAsString(new ListWrappers.DataActionList(list));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 20
Source File: BaseSerializer.java    From DataVec with Apache License 2.0 3 votes vote down vote up
/**
 * Serialize the specified object, such as a {@link Transform}, {@link Condition}, {@link Filter}, etc<br>
 * <b>NOTE:</b> For lists use the list methods, such as {@link #serializeTransformList(List)}<br>
 * <p>
 * To deserialize, use the appropriate method: {@link #deserializeTransform(String)} for example.
 *
 * @param o Object to serialize
 * @return String (json/yaml) representation of the object
 */
public String serialize(Object o) {
    ObjectMapper om = getObjectMapper();
    try {
        return om.writeValueAsString(o);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}