Tensorflow For Go

Tensorflow For Go

Tensorflow For Go支持Linux和OSX。

安装

下载动态链接库

$ 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.2.0.tar.gz" | sudo tar -C $TARGET_DIRECTORY -xz # Linux上还需要执行ldconfig $ sudo ldconfig 

下载Tensorflow Go库

$ go get github.com/tensorflow/tensorflow/tensorflow/go

下载完成后,可以用go test验证安装

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

Go示例

$ cat <<EOF | tee tf-hello.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()) } EOF $ export LIBRARY_PATH=/usr/local/lib $ go run tf-hello.go 2017-06-19 15:38:38.569010: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. 2017-06-19 15:38:38.569633: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. 2017-06-19 15:38:38.569640: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. 2017-06-19 15:38:38.569646: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations. Hello from TensorFlow version 1.2.0 

C示例

C和Go实际上共用了相同的动态链接库和头文件,安装好Go版本后,C的API可以直接使用,如

#include <stdio.h>
#include <tensorflow/c/c_api.h> int main() { printf("Hello from TensorFlow C library version %s\n", TF_Version()); return 0; } 

编译,运行

$ gcc -I/usr/local/include -L/usr/local/lib tf.c -ltensorflow
$ ./a.out
Hello from TensorFlow C library version 1.2.0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值