golang 安装 tensorflow
Yuxuan Wu Lv13

本文记录tfgo的安装和使用教程 (mac)

  • 下载安装依赖(dependency)
  • go get 安装
  • 测试

TF GitHub 地址

https://github.com/galeone/tfgo#tensorflow-installation

Dependencies (依赖)

需要安装c library

https://www.tensorflow.org/install/lang_c

因为我是mac系统,所以下载如下的链接到本地

image-20211008222851847

image-20211008223042617

可以选择主动解压,然后将解压后的文件移到如下的位置 /usr/local

1
sudo mv libtensorflow-cpu-darwin-x86_64-2.6.0 /usr/local

Installation (安装)

在命令行输入go get 下载tfgo

1
go get github.com/galeone/tfgo

test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main

import (
"fmt"
tg "github.com/galeone/tfgo"
tf "github.com/galeone/tensorflow/tensorflow/go"
)

func main() {
root := tg.NewRoot()
A := tg.NewTensor(root, tg.Const(root, [2][2]int32{{1, 2}, {-1, -2}}))
x := tg.NewTensor(root, tg.Const(root, [2][1]int64{{10}, {100}}))
b := tg.NewTensor(root, tg.Const(root, [2][1]int32{{-10}, {10}}))
Y := A.MatMul(x.Output).Add(b.Output)
// Please note that Y is just a pointer to A!

// If we want to create a different node in the graph, we have to clone Y
// or equivalently A
Z := A.Clone()
results := tg.Exec(root, []tf.Output{Y.Output, Z.Output}, nil, &tf.SessionOptions{})
fmt.Println("Y: ", results[0].Value(), "Z: ", results[1].Value())
fmt.Println("Y == A", Y == A) // ==> true
fmt.Println("Z == A", Z == A) // ==> false
}

看看是否输出了如下

1
2
3
Y:  [[200] [-200]] Z:  [[200] [-200]]
Y == A true
Z == A false

image-20211008223622383

上述结果说明已经成功安装成功tfgo

  • Post title:golang 安装 tensorflow
  • Post author:Yuxuan Wu
  • Create time:2021-10-08 22:20:23
  • Post link:yuxuanwu17.github.io2021/10/08/2021-10-08-golang-安装-tensorflow/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.