Go version tensorflow installation guide

Go version tensorflow installation

This article is based on ubuntu16.04

First post the official tutorial here
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/go

Note that when installing the go version of tensorflow, you must first install the C version of tensorflow and deploy the go language

Official tensorflow tutorial for c version
(Go version TensorFlow depends on TensorFlow C language library)
Just follow the official instructions, there is no pit, it is best to specify the location, otherwise there may be pits behind!

Execute this command after installation

go get -d github.com/tensorflow/tensorflow/tensorflow/go

May appear go get can not find google.golang.org/protobuf/×××××××××× such problems

At this point we can use the git clone command, execute the following code "https://github.com/protocolbuffers/protobuf-go.git", put the downloaded content into the protobuf folder, that is, replace all the downloaded content with The problematic protobuf directory, this may be caused by the previous code change address

执行如下命令(下载下来后记得把文件替换,或者直接改名字把protobuf-go改成protobuf换目录)
git clone https://github.com/protocolbuffers/protobuf-go.git

If you can't find the corresponding directory, you can get it with the following command

    git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc

    git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net

    git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text

    go get -u github.com/golang/protobuf/{
    
    proto,protoc-gen-go}

    git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto

If the package is not found

 cannot find package 
"github.com/tensorflow/tensorflow/tensorflow/go/genop/internal/proto/github.com/tensorflow/tensorflow/tensorflow/go/core" in any of: /home/go/src/github.com/tensorflow/tensorflow/tensorflow/go/genop/internal/proto/github.com/tensorflow/tensorflow/tensorflow/go/core (from $GOROOT)
 
/home/go_work/src/github.com/tensorflow/tensorflow/tensorflow/go/genop/internal/proto/github.com/tensorflow/tensorflow/tensorflow/go/core (from $GOPATH)

At this point we can execute the following command to try to solve the problem

    cd $GOPATH/src/github.com/tensorflow/tensorflow/tensorflow/go
     
    git checkout r1.11
     
    go get github.com/tensorflow/tensorflow/tensorflow/go

You can also search and ask questions on StackOverflow for possible solutions.

If go get is successful, no error is reported, you can use the following command to test

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

Hello World


After installing the Go version of TensorFlow, enter the following code in the hello_tf.go file:

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

Run hello_tf.go by calling the following command:

go run hello_tf.go
Hello from TensorFlow version number

This program may output warning messages similar to the following, you can ignore them:


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.


Reference
[1]: https://blog.csdn.net/jj546630576/article/details/88179569
[2]: https://www.cnblogs.com/personblog/p/13084723.html
[3]: https:/ /blog.csdn.net/qq_38431572/article/details/103671986

Guess you like

Origin blog.csdn.net/introsend/article/details/110784821