MAC OS TensorFlow in Go--installing tensorflow for go - Go语言中文社区

MAC OS TensorFlow in Go--installing tensorflow for go


参考文献:

1.https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/README.md

2.https://tensorflow.google.cn/install/install_go


Go语言要使用tensorflow C库,所以需要想办法在go的工作环境中创建tensorflow C library并设置相关环境变量。


准备工作:

1.需要将go语言的版本升级到1.8以上(或最新版本)。可以在终端使用指令:

brew install go
系统会安装go语言。

tips:可以使用brew镜像提升下载速度。
可以根据http://blog.sina.com.cn/s/blog_669fb0c30102x77w.html文章中的内容替换brew默认源

2.安装完成后,可以输入

go version
查看系统go语言版本。如果非最新版本,可以根据brew安装记录更改go安装路径,假设安装记录给出的安装路径是the_path,那么可以输入:
export GOROOT=the_path
系统将会把go语言版本指向最新安装版本。

3.由于后文中将利用go get指令下载tensorflow C库,所以必须确保GOPATH 被正确设置。可以通过go env指令查看go语言工作环境,如果GOPATH 未被设置,需要设置相关路径。

mkdir some_path
vim ~/.bash_profile #按i开始编辑
export GOPATH=some_path
#esc退出vim,:wq保存并退出。
source ~./bash_profile
这样就可以永久设置gopath工作路径,相关的库也都会下载到该工作路径内。




安装:

方法1:在tensorflow官网下载

1.step1:下载C库

终端中输入

 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.5.0.tar.gz" |
 sudo tar -C $TARGET_DIRECTORY -xz
相关的c库会下载到系统工作路径 /usr/local/lib中。

2.step2:配置动态链接库环境变量(这一步tensorflow官网指南有问题。。花了很多时间)

vim ~/.bash_profile #按i开始编辑
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/lib
#esc退出vim,:wq保存并退出。
source ~./bash_profile
这样go调用tensorflow c库的环境变量就设置好了。

3.通过go get 下载合适的包与依赖。(原文是download the appropriate packages and their dependencies

终端输入:

go get github.com/tensorflow/tensorflow/tensorflow/go
如果报错RPC filed,result=56. 是因为远程便捷HTTP传输请求数据时最大的缓存字节数,默认时1M字节,默认能满足大多数请求。


sudo git config --global http.postBuffer 524288000

调大最后一个数字输入即可。

4.确认未报错,环境搭建完毕,那么就开始测试是否go已经可以调用tensorflow了

输入

go test github.com/tensorflow/tensorflow/tensorflow/go
如果成功会显示:



方法2:自行拉取github tensorflow c 源码并编译成c库。

准备工作:安装bazel,swig.

brew install swig 
brew install bazel
如果遇到错误按照终端提示操作。

1.清空上一步操作。

cd $GOPATH
rm -rf *

2.参考文献1解释 if do not work (perhaps the release archives are not available for your operating system or architecture, or you're using a different version of CUDA/cuDNN), then the TensorFlow C library must be built from source.


3.go get 下载tensorflow源码

go get -d github.com/tensorflow/tensorflow/tensorflow/go
4.利用bazel编译tensorflow c 库

cd ${GOPATH}/src/github.com/tensorflow/tensorflow
./configure
输入上两行命令后会出现一些编译选项。其中为了顺利调通,涉及选项[y/n]一律选n,
并且会让用户输入python地址(

location of python. [Default is /usr/bin/python]


)与python库地址(

Python library paths


),请事先查好。

配置好后,输入

bazel build --config opt //tensorflow:libtensorflow.so
等待十到二十分钟完成编译。

5.配置环境变量。

同方法1中的step2,进入编辑后,要更换环境变量地址如下:

export LIBRARY_PATH=${GOPATH}/src/github.com/tensorflow/tensorflow/bazel-bin/tensorflow
export DYLD_LIBRARY_PATH=${GOPATH}/src/github.com/tensorflow/tensorflow/bazel-bin/tensorflow
6.测试

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

完成!


最后附上官网一段测试代码,贴到脚本内可以用go run 运行。

package main

import (
	"fmt"

	tf "github.com/tensorflow/tensorflow/tensorflow/go"
	"github.com/tensorflow/tensorflow/tensorflow/go/op"
)

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








版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/simoe001/article/details/79283216
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-02-24 22:15:13
  • 阅读 ( 1692 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢