Last active
July 15, 2019 04:19
-
-
Save suyashcjoshi/4bedebbce064d8f518d3bb4bdbe7793a to your computer and use it in GitHub Desktop.
HelloWorld App using TensorFlow (Java) library
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
import org.tensorflow.Graph; | |
import org.tensorflow.Session; | |
import org.tensorflow.Tensor; | |
import org.tensorflow.TensorFlow; | |
public class HelloWorld { | |
public static void main(String[] args) throws Exception { | |
// Creates a Computation Graph | |
try (Graph g = new Graph()) { | |
// This is the string that we use in our Input Tensor | |
final String value = "Hello World 👋 this is TensorFlow version " + | |
TensorFlow.version() + " inside a Java program!"; | |
// Creates an Input Tensor with a single operation, | |
// a constant named "MyConst" with a value "value" | |
// and sets its attributes | |
try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) { | |
g.opBuilder("Const", "MyConst") | |
.setAttr("dtype", t.dataType()) | |
.setAttr("value", t) | |
.build(); | |
} | |
// Creates a Session inside of which the Output Tensor performs a | |
// single operation of fetching the value of a constant and it is printed in the end | |
try (Session s = new Session(g); | |
Tensor output = s.runner().fetch("MyConst").run().get(0)) { | |
System.out.println(new String(output.bytesValue(), "UTF-8")); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment