import org.tensorflow.Graph;import org.tensorflow.Session;import org.tensorflow.Tensor;import org.tensorflow.TensorFlow;publicclasstest{publicstaticvoidmain(String[] args)throws Exception {try(Graph g =newGraph()){final String value ="Hello from "+ TensorFlow.version();// Construct the computation graph with a single operation, a constant// named "MyConst" with a value "value".try(Tensor t = Tensor.create(value.getBytes("UTF-8"))){// The Java API doesn't yet include convenience functions for adding operations.
g.opBuilder("Const","MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();}// Execute the "MyConst" operation in a Session.try(Session s =newSession(g);
Tensor output = s.runner().fetch("MyConst").run().get(0)){
System.out.println(newString(output.bytesValue(),"UTF-8"));}}}}/* 输出
Hello from 1.6.0
*/
3. 可能遇到的问题及解决方案
3.1 问题:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: windows, architecture: x86. See https://github.com/tensorflow/tensorflow/tree/master/java/README.md for possible solutions (such as building the library from source).
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.TensorFlow.init(TensorFlow.java:27)
at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:31)
at ai.advance.project.App.main(App.java:12)