是否有 Golang API 将数据转换为序列化的 tf.Example,也称为tensorflow tfrecords。
我可以找到一个 Python api tf.python_io.TFRecordWriter()
来实现此目的。
但是找不到golang的例子。
我想这样做,因为我使用 golang 客户端来调用 tensorflow 服务,并且我的 nn 的输入功能是 SparseTensor。
最佳答案
tf.Example
指 Protocol Buffer ,而 tfrecords
通常指以字符串形式存储“记录”的文件格式(这就是为什么 tf.python_io.TFRecordWriter.write()
采用一个字符串)。因此,两者是独立的事物,在技术上没有关联。尽管人们经常将 tf.Example
Protocol Buffer 序列化为字符串,TFRecordWriter
中的每个记录各一个。
也就是说,模型通常将序列化的 tf.Example
Protocol Buffer 作为 STRING
张量形式的输入。如果是这种情况,那么您需要在 Go 中构造 tf.Example
Protocol Buffer ,然后使用 proto.Marshal
构造要提供的 tf.Tensor
对象。
不幸的是,从 2018 年 1 月开始,您必须自己为 tf.Example
原型(prototype)生成 Go 文件。这可以通过以下方式完成:
# Fetch the tools
GOPATH=$(go env GOPATH)
go get github.com/golang/protobuf/proto
go get github.com/golang/protobuf/protoc-gen-go
# Create a directory to generate the files
mkdir -p /tmp/protos/out
cd /tmp/protos
# Clone the TensorFlow sources to get the source .proto files
git clone https://github.com/tensorflow/tensorflow ./src
PATH=$PATH:${GOPATH}/bin
# Generate Go source files from the .proto files.
# Assuming protoc is installed.
# See https://github.com/google/protobuf/releases
protoc \
-I ./src \
--go_out ./out \
./src/tensorflow/core/framework/*.proto ./src/tensorflow/core/example/*.proto
rm -rf ./src
这将在 /tmp/protos/out
中生成一堆 .pb.go
文件,可用于构建 tf.Example
code> 要编码的 Protocol Buffer 结构。
希望这能为您提供足够的信息来开始。
关于go - 如何使用 Golang 将数据转换为序列化 tf.Example(tensorflow tfrecords),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48393098/