一、安装依赖
点击如下链接下载依赖:
c版tensorflow
点击如图所示进行下载:
下载后直接双击解压
解压后命令行执行如下命令
sudo mv libtensorflow-cpu-darwin-x86_64-2.6.0 /usr/local
二、导入tensorflow
命令行执行如下命令
go get github.com/galeone/tfgo
(如果报错可以重启电脑再试)
三、测试
package main
import (
"fmt"
tf "github.com/galeone/tensorflow/tensorflow/go"
tg "github.com/galeone/tfgo"
)
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
}
运行,观察输出是否符合期望: