go - 如何使用 Golang 将数据转换为序列化 tf.Example(tensorflow tfrecords)

标签 go tensorflow tensorflow-serving

是否有 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/

相关文章:

python - tensorflow-gpu 1.8.0 ImportError : libcudnn. so.7:无法打开共享对象文件:没有这样的文件或目录

python - 有没有办法加速 tf.keras 中的嵌入层?

python - 同时使用图像和数字输入的神经网络

mongodb - Mongo-go-driver error New client error ClientOptions in argument to mongo.NewClient 错误

file - 如何使用Golang在网络打印机上打印文件或格式文本

google-app-engine - 执行 : "gcc": executable file not found in $PATH

TensorFlow:如何使用 TensorHub 模块导出估算器?

tensorflow - 如何使用 tf.estimator 导入保存的 Tensorflow 模型训练并预测输入数据

python - 去除 Bazel-ing TensorFlow Serving

go - 如何为滚动文件系统日志配置 uber-go/zap 记录器?