Created
September 10, 2016 16:18
-
-
Save claytantor/b3f529650c2898ba7255524845d04493 to your computer and use it in GitHub Desktop.
Starting point for utilities to make simple data structures from buffers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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