Here is Simple TensorFlow Application Go Programming Language

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/05/simple-tensorflow-application-go-programming-language/
  1. Download and extract the TensorFlow C library into /usr/local/lib by invoking the following shell commands:
     TF_TYPE="cpu" # Change to "gpu" for GPU support
     TARGET_DIRECTORY='/usr/local'
     curl -L \
       "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-${TF_TYPE}-$(go env GOOS)-x86_64-1.1.0.tar.gz" |
     sudo tar -C $TARGET_DIRECTORY -xz
    

    The tar command extracts the TensorFlow C library into the lib subdirectory of TARGET_DIRECTORY. For example, specifying /usr/local as TARGET_DIRECTORY causes tar to extract the TensorFlow C library into /usr/local/lib.

    If you’d prefer to extract the library into a different directory, adjust TARGET_DIRECTORY accordingly.

  2. In Step 2, if you specified a system directory (for example, /usr/local) as the TARGET_DIRECTORY, then run ldconfig to configure the linker. For example:
    sudo ldconfig

    If you assigned a TARGET_DIRECTORY other than a system directory (for example, ~/mydir), then you must append the extraction directory (for example, ~/mydir/lib) to two environment variables as follows:

     export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib # For both Linux and Mac OS X
     export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/mydir/lib # For Linux only
     export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:~/mydir/lib # For Mac OS X only
  3. Now that the TensorFlow C library is installed, invoke go get as follows to download the appropriate packages and their dependencies:
    go get github.com/tensorflow/tensorflow/tensorflow/go
  4. Invoke go test as follows to validate the TensorFlow for Go installation:
    go test github.com/tensorflow/tensorflow/tensorflow/go

If go get or go test generate error messages, search (or post to) StackOverflow for possible solutions.

Hello World

After installing TensorFlow for Go, enter the following code into a file named hello_tf.go:

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())
}

For a more advanced example of TensorFlow in Go, look at the example in the API documentation, which uses a pre-trained TensorFlow model to label contents of an image.

Running

Run hello_tf.go by invoking the following command:

go run hello_tf.go
Hello from TensorFlow version number

Take your time to comment on this article.

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2017/05/simple-tensorflow-application-go-programming-language/

Add a Comment

Your email address will not be published. Required fields are marked *

Like the article? please consider sharing it. Thank you

Advertisment ad adsense adlogger