Skip to content

Instantly share code, notes, and snippets.

@claytantor
Created September 10, 2016 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claytantor/b3f529650c2898ba7255524845d04493 to your computer and use it in GitHub Desktop.
Save claytantor/b3f529650c2898ba7255524845d04493 to your computer and use it in GitHub Desktop.
Starting point for utilities to make simple data structures from buffers.
package org.deeplearning4j.examples.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.string.NDArrayStrings;
import java.io.IOException;
import java.util.List;
/**
* So the intention of this utility is to make it easy to get structured data out of
* the result buffers and data sets so that its easier to do things like build data
* Java like structures, which would be important for enterprise use cases.
*
* Created by claytantor on 9/10/16.
*/
public class NDArrayUtils {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
/**
* This implementation uses Jackson, which I dont like because its a cheat. What would be thinner and
* faster would be to re-implement the format method here:
* https://github.com/deeplearning4j/nd4j/blob/2b3e8c60e42e3ed7f3ef72d45a599b74da31354c/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/string/NDArrayStrings.java#L66
*
* @param source
* @param precision
* @return rows
* @throws IOException
*/
public static List<List<Double>> makeRowsFromData(INDArray source, int precision) throws IOException {
String serializedData = new NDArrayStrings(", ",precision).format(source);
List<List<Double>> rows = (List<List<Double>>)OBJECT_MAPPER.readValue(
serializedData.getBytes(),List.class);
return rows;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment