Last active
April 6, 2018 20:18
-
-
Save PatrickWalker/78e7cbaf3bcb7911505990409a33dfa6 to your computer and use it in GitHub Desktop.
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 main | |
import ( | |
"fmt" | |
tf "github.com/tensorflow/tensorflow/tensorflow/go" | |
"github.com/tensorflow/tensorflow/tensorflow/go/op" | |
) | |
func main() { | |
// Construct an empty graph | |
s := op.NewScope() | |
//Associates a new string constant with the graph s and the output is allocated to c | |
c := op.Const(s, "Hello from TensorFlow version "+tf.Version()) | |
//builds and finalizes the graph rendering s unusable | |
graph, err := s.Finalize() | |
if err != nil { | |
panic(err) | |
} | |
// Sets up a session using the graph | |
/* | |
https://danijar.com/what-is-a-tensorflow-session/ | |
A graph defines the computation. | |
It doesn’t compute anything, it doesn’t hold any values, it just defines the operations that you specified in your code. | |
A session allows to execute graphs or part of graphs. | |
It allocates resources (on one or more machines) for that and holds the actual values of intermediate results | |
and variables. | |
*/ | |
sess, err := tf.NewSession(graph, nil) | |
if err != nil { | |
panic(err) | |
} | |
//https://godoc.org/github.com/tensorflow/tensorflow/tensorflow/go#Session.Run | |
//input, fetches and lastly targets which are computed and not returned | |
//output is the fetches specified. if nil then it's empty | |
output, err := sess.Run(nil, []tf.Output{c}, nil) | |
if err != nil { | |
panic(err) | |
} | |
//As we said they are returned in order so this is the value of c | |
fmt.Println(output[0].Value()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment