ActiveState ActiveGo 1.8
...

Configuring TensorFlow for ActiveGo

You can use the APIs provided by TensorFlow to load models created in Python and execute them from within a Go application. In order to do this, you need to install and configure TensorFlow to work with ActiveGo. ActiveGo includes the TensorFlow Go client and dependencies.

TensorFlow for Go is available for the Linux and macOS releases of ActiveGo.

These instructions are based on the steps in the TensorFlow documentation.

WARNING: The TensorFlow Go API is not covered by the TensorFlow API stability guarantees.

  1. Enter the following shell commands to download and extract the TensorFlow C library to the default location: /usr/local/lib. You can adjust the TARGET_DIRECTORY value to extract the library to a different location.

    TF_TYPE="cpu" # Must be "cpu" for the ActiveGo 1.8.3 release; "gpu" is not supported.
    TARGET_DIRECTORY='/usr/local'
    curl -L \
    "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-${TF_TYPE}-$(go env GOOS)-x86_64-1.2.0-rc1.tar.gz" |
    sudo tar -C $TARGET_DIRECTORY -xz
    
  2. In Step 1, if you specified a system directory (for example, /usr/local) as the TARGET_DIRECTORY on Linux, you must run ldconfig to configure the linker.

    sudo ldconfig
    
  3. In Step 1, if you specified a non-system directory (for example, `~/mydir) as the TARGET_DIRECTORY, you must append the extraction directory to two environment variables.

    • Linux:

      export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib
      export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/mydir/lib
      
    • macOS:

      export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib
      export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:~/mydir/lib
      
  4. Run the TensorFlow for Go tests to verify your configuration.

    go test github.com/tensorflow/tensorflow/tensorflow/go
    


    If go test generates error messages, contact support@activestate.com or search (or post to) StackOverflow for solutions.

  5. Enter the following code into a file named hello_tf.go to verify that you can run an application that interacts with the TensorFlow C library.

    package main
    
    import (
    tf "github.com/tensorflow/tensorflow/tensorflow/go"
    "github.com/tensorflow/tensorflow/tensorflow/go/op"
    "fmt"
    )
    
    func main() {
    // Construct a graph with an operation that produces a string constant.
    s := op.NewScope()
    c := op.Const(s, "Hello from TensorFlow version " + tf.Version())
    graph, err := s.Finalize()
    if err != nil {
    panic(err)
    }
    
    // Execute the graph in a session.
    sess, err := tf.NewSession(graph, nil)
    if err != nil {
    panic(err)
    }
    output, err := sess.Run(nil, []tf.Output{c}, nil)
    if err != nil {
    panic(err)
    }
    fmt.Println(output[0].Value())
    }
    
  6. Run hello_tf.go.

    go run hello_tf.go
    


    If the program runs successfully, you will see this output:

    Hello from TensorFlow <version_number>
    


    The program might also generate multiple warning messages similar to the following, which you can ignore:

    W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library
    wasn't compiled to use *Type* instructions, but these are available on your
    machine and could speed up CPU computations.