我在安装tensorflow (go语言)时,按照官方教程来,发现有错误,经过几位外国友人的知道,解决问题,现贴出这个坑,供大家借鉴学习。
安装tensorflow这里直接参考官方链接,记住,tensorflow的c语言版本也要安装,才能使用,并且安装位置最好使用教程里的位置。
问题来了:我们执行到这一步时:
go test github.com/tensorflow/tensorflow/tensorflow/go
提示我们找不到包:
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)
仔细看竟然有两层github的引用,并且去github也找不到这个文件。
解决办法:外国友人帮助我的链接(stackoverflow网站),下面是原文截图:
下面我把需要我们执行的代码拿出来(记住,C语言的tensorflow一定要安装到官方教程中的指定位置):
cd $GOPATH/src/github.com/tensorflow/tensorflow/tensorflow/go
git checkout r1.11
go get github.com/tensorflow/tensorflow/tensorflow/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())
}