Tensorflow-Go的扩展
来源:互联网 发布:smt自动点胶机编程 编辑:程序博客网 时间:2024/06/06 04:44
谷歌的tensorflow虽然提供了go版本,但是官方的说法是:
TensorFlow provides APIs for use in Go programs. These APIs are particularly well-suited to loading models created in Python and executing them within a Go application.
意思是go的库只是用来装载python创建的模型,然后执行的,而且在go版本api的godoc中也写到:
The tensorflow package currently does not have the ability to export a model to a directory from Go. This function thus currently targets loading models exported in other languages, such as using tf.saved_model.builder in Python. See: https://www.tensorflow.org/code/tensorflow/python/saved_model/
说go不能将模型导出,而且现阶段go版本的api没有直接创建variable的op,但是通过实验可以发现其实是可以使用的,先看/tensorflow/core/ops/state_ops.cc
中variable这个op的声明:
REGISTER_OP("VariableV2") .Output("ref: Ref(dtype)") .Attr("shape: shape") .Attr("dtype: type") .Attr("container: string = ''") .Attr("shared_name: string = ''") .SetIsStateful() .SetShapeFn(shape_inference::ExplicitShape) .Doc(R"doc(Holds state in the form of a tensor that persists across steps.Outputs a ref to the tensor state so it may be read or modified.TODO(zhifengc/mrry): Adds a pointer to a more detail documentabout sharing states in tensorflow.ref: A reference to the variable tensor.shape: The shape of the variable tensor.dtype: The type of elements in the variable tensor.container: If non-empty, this variable is placed in the given container. Otherwise, a default container is used.shared_name: If non-empty, this variable is named in the given bucket with this shared_name. Otherwise, the node name is used instead.)doc");
然后观察/tensorflow/go/op/wrappers.go
中调用类似的一个op叫placeholder的方法:
// A placeholder op that passes through `input` when its output is not fed.//// Arguments:// input: The default value to produce when `output` is not fed.// shape: The (possibly partial) shape of the tensor.//// Returns A placeholder tensor that defaults to `input` if it is not fed.func PlaceholderWithDefault(scope *Scope, input tf.Output, shape tf.Shape) (output tf.Output) { if scope.Err() != nil { return } attrs := map[string]interface{}{"shape": shape} opspec := tf.OpSpec{ Type: "PlaceholderWithDefault", Input: []tf.Input{ input, }, Attrs: attrs, } op := scope.AddOperation(opspec) return op.Output(0)}
可以看到使用tf.OpSpec
结构体,并且对特定格式把参数装进去就可以,经过实验,添加一个Variable的变量op到一个Scope是成功的。以此,在go版本上面做出optimizer等训练需要的东西,只需要自己封装好梯度计算的op,然后对变量进行增改op,完全可以做出一个拥有tensorflow-python版完整功能的api库。
另外,有一点是官方编译的libtensorflow.so文件里面是缺少contrib的内容的,具体解决办法是在tensorflow/BUILD
文件(r1.3)的以下小节加入依赖:
cc_binary( name = "libtensorflow.so", linkopts = select({ "//tensorflow:darwin": [ "-Wl,-exported_symbols_list", # This line must be directly followed by the exported_symbols.lds file "//tensorflow/c:exported_symbols.lds", ], "//tensorflow:windows": [], "//tensorflow:windows_msvc": [], "//conditions:default": [ "-z defs", "-s", "-Wl,--version-script", # This line must be directly followed by the version_script.lds file "//tensorflow/c:version_script.lds", ], }), linkshared = 1, deps = [ "//tensorflow/contrib:contrib_kernels", #Add "//tensorflow/contrib:contrib_ops_op_lib", #Add "//tensorflow/c:c_api", "//tensorflow/c:exported_symbols.lds", "//tensorflow/c:version_script.lds", "//tensorflow/core:tensorflow", ],)
- Tensorflow-Go的扩展
- go语言 TensorFlow for Go
- TensorFlow in Go
- go接口扩展注意事项
- Go 语言编写 CPython 扩展 goPy
- go 语言 net 扩展 模拟 tcp 收发
- Go 面向对象扩展——接口
- linux go安装redis/mongo扩展
- 【Go学习】Go的函数
- 【TensorFlow】TensorFlow的线性回归
- 【TensorFlow】TensorFlow 的 Logistic Regression
- TensorFlow 完整的TensorFlow入门教程
- [TensorFlow] TensorFlow学习的方向
- Go?Go!(二) 初窥Go的特性
- Go?Go!(四) Go command的使用
- Go! Go! Go! 来我的另一个博客
- GO!---Windows下GO的环境搭建
- 一个新的开始,go,go加油!
- Django视图与网址
- bootstrap table 表格内容溢出隐藏
- AB1601中volatile的使用
- Java
- MongoDB 主从复制
- Tensorflow-Go的扩展
- IOC和AOP的个人理解
- 彻底解决Spring MVC XSS注入问题
- JAVA-ACE-架构师系列视频课程- RocketMQ(下)订单实战视频课程
- iOS 11.2 问题未停?用户投诉仍遇到各种奇难杂症!
- MongoDB 切换方案
- CentOS 7.0 防火墙开启/关闭
- 如何防止别人看到网页源代码
- linux 安装sysstat使用iostat、mpstat、sar、sa