在一台ubuntu 16.04.2虚拟机上为golang安装TensorFlow。
官方参考:https://www.tensorflow.org/install/install_go
首先安装go 1.9 (参考 https://golang.org/doc/install)
下载1.9安装包
wget https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz
解压到/usr/local
sudo tar -C /usr/local -xzf go1.9.linux-amd64.tar.gz
添加环境变量,编辑/etc/profile,
sudo gedit /etc/profile
在文件最后加入如下内容:
export PATH=$PATH:/usr/local/go/bin
export GOPATH=/home/dell/mygo
更新环境变量:
source /etc/profile
或重启虚拟机,否则每次打开终端,都要执行下source /etc/profile
接下来安装tensorflow C库
TF_TYPE="cpu" # Change to "gpu" for GPU support
wget "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-${TF_TYPE}-$(go env GOOS)-x86_64-1.3.0.tar.gz"
sudo tar -C /usr/local -xzf "libtensorflow-${TF_TYPE}-$(go env GOOS)-x86_64-1.3.0.tar.gz"
解压后,/usr/local中出现两个文件夹:
/usr/local/lib
/usr/local/include
配置链接器
sudo ldconfig
最后下载go包
go get github.com/tensorflow/tensorflow/tensorflow/go
执行go test 验证
go test github.com/tensorflow/tensorflow/tensorflow/go
Hello World
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())
}
编译运行:
输出了一些指令集相关的告警,最后一行是输出结果。