use of org.nd4j.shade.jackson.databind.ObjectMapper in project deeplearning4j by deeplearning4j.
the class TestComponentSerialization method assertSerializable.
private static void assertSerializable(Component component) throws Exception {
ObjectMapper om = new ObjectMapper();
String json = om.writeValueAsString(component);
Component fromJson = om.readValue(json, Component.class);
//Yes, this is a bit hacky, but lombok equal method doesn't seem to work properly for List<double[]> etc
assertEquals(component.toString(), fromJson.toString());
}
use of org.nd4j.shade.jackson.databind.ObjectMapper in project deeplearning4j by deeplearning4j.
the class TestComponentSerialization method assertSerializable.
private static void assertSerializable(Style style) throws Exception {
ObjectMapper om = new ObjectMapper();
String json = om.writeValueAsString(style);
Style fromJson = om.readValue(json, Style.class);
//Yes, this is a bit hacky, but lombok equal method doesn't seem to work properly for List<double[]> etc
assertEquals(style.toString(), fromJson.toString());
}
use of org.nd4j.shade.jackson.databind.ObjectMapper 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);
}
use of org.nd4j.shade.jackson.databind.ObjectMapper in project deeplearning4j by deeplearning4j.
the class Hdf5Archive method readAttributeAsJson.
/**
* Read JSON-formatted string attribute.
*
* @param attribute HDF5 attribute to read as JSON formatted string.
* @return
* @throws UnsupportedKerasConfigurationException
*/
private String readAttributeAsJson(hdf5.Attribute attribute) throws UnsupportedKerasConfigurationException {
hdf5.VarLenType vl = attribute.getVarLenType();
int bufferSizeMult = 1;
String s = null;
/* TODO: find a less hacky way to do this.
* Reading variable length strings (from attributes) is a giant
* pain. There does not appear to be any way to determine the
* length of the string in advance, so we use a hack: choose a
* buffer size and read the config. If Jackson fails to parse
* it, then we must not have read the entire config. Increase
* buffer and repeat.
*/
while (true) {
byte[] attrBuffer = new byte[bufferSizeMult * 2000];
BytePointer attrPointer = new BytePointer(attrBuffer);
attribute.read(vl, attrPointer);
attrPointer.get(attrBuffer);
s = new String(attrBuffer);
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
try {
mapper.readTree(s);
break;
} catch (IOException e) {
}
bufferSizeMult++;
if (bufferSizeMult > 100) {
throw new UnsupportedKerasConfigurationException("Could not read abnormally long HDF5 attribute");
}
}
return s;
}
use of org.nd4j.shade.jackson.databind.ObjectMapper in project deeplearning4j by deeplearning4j.
the class KerasModel method parseJsonString.
/**
* Convenience function for parsing JSON strings.
*
* @param json String containing valid JSON
* @return Nested (key,value) map of arbitrary depth
* @throws IOException
*/
public static Map<String, Object> parseJsonString(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
};
return mapper.readValue(json, typeRef);
}
Aggregations