Using TFRecords and tf.Example
-----这篇其实是TensorFlow的官方tutorials,由于没有翻译,笔者姑且翻译一下,用来日后思考。-------
原址:https://www.tensorflow.org/tutorials/load_data/tf_records
读取数据的效率对于连续载入数据和将数据储存在文件集(每个文件大概100-200MB)是很有帮助的。如果数据是输入到一个网络上来说更是如此,当然这对将预处理的数据输入到cache同样有用。
储存二进制的数据的一种简单的格式就是TFRecord格式。
Protocol buffers 是一个跨平台、跨语言的库,它被使用在高效率的连续载入结构化的数据。Protocol messages 以 .proto结尾,这通常是理解消息类型的最简单的方式。
tf.Example 消息(或者protobuf)是一种象征着 {"string":value} 映射的灵活的消息类型。它被设计与TensorFlow使用,在高阶的APIs(例如TFX)中使用。
这份说明将会阐释关于 tf.Example 的创建,语法和使用,以及之后的载入,write 和读取 tf.Example 消息,以及读取 .tfrecord 文件。
注意:当必要时,这些结构是可选的,对于将已经存在的代码再去使用TFRecord, 除非你正在使用tf.data以及读取数据仍然对训练来说是一个瓶颈。
Setup
from __future__ import absolute_import, division, print_function, unicode_literals import tensorflow as tf
tf.enable_eager_execution() import numpy as np
import IPython.display as display
tf.Example
1. tf.Eample的数据类型
实际上,一个 tf.Example 是一个{"string":tf.train.Feature}的映射。
tf.train.Feature消息类型能接受以下三种类型(查看.proto file寻求帮助)
大部分其他的常见普通类型都可强制转换成下面其中的一个。
- tf.train.BytesList(下列类型可强制转换)
- string
- byte
- tf.train.FloatList
- float(float32)
- double(float64)
- tf.train.Int64List
- bool
- enum
- int32
- int64
- uint32
- uint64
你可以使用下列函数来将标准的TensorFlow类型转换为与tf.Example相兼容的tf.train.Feature.
每个函数有一个标量输入,返回一个包含上述三种list类型中的一种的tf.train.Feature
# The following functions can be used to convert a value to a type compatible
# with tf.Example. def _bytes_feature(value):
"""Returns a bytes_list from a string / byte."""
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) def _float_feature(value):
"""Returns a float_list from a float / double."""
return tf.train.Feature(float_list=tf.train.FloatList(value=[value])) def _int64_feature(value):
"""Returns an int64_list from a bool / enum / int / uint."""
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
Note: 为了简单起见,本示例只使用了标量输入。处理非标量输入最简单的方法就是使用tf.serialize_tensor来将tensor转换为binary-strings。字符串在tensorflow中是标量。使用tf.parse_tensor可以将binary-string转换回tensor.
下面是这些函数如何工作的一些例子,我们注意下不同的输入类型和标准化的输出类型。如果输入类型与前面列举出来的类型不匹配的话,函数就会报错。
print(_bytes_feature(b'test_string'))
print(_bytes_feature(u'test_bytes'.encode('utf-8'))) print(_float_feature(np.exp(1))) print(_int64_feature(True))
print(_int64_feature(1))
bytes_list {
value: "test_string"
}
bytes_list {
value: "test_bytes"
}
float_list {
value: 2.7182817459106445
}
int64_list {
value: 1
}
int64_list {
value: 1
}
所有的 proto messages 可以使用.SerializeToString 方法被载入成为 binary-string 。
feature = _float_feature(np.exp(1)) feature.SerializeToString()
b'\x12\x06\n\x04T\xf8-@'
创建一个tf.Example消息
假设你要从已经存在的数据中生成tf.Example消息,在实际中,这个数据集可能来自任何地方。但是从单样本生成tf.Example消息的步骤是相同的。
- 在每个样本中,每个值需要被转换成包含三种兼容类型中的一个的tf.train.Feature,使用上述的函数。
- 我们从在步骤1中产生的 feature name string 和编码过的value组成一个字典
- #2中产生的字典被转换为一个Features message
下面的代码,我们用numpy产生一个dataset
这个dataset有4个特征:一个bool特征,False 和True出现的概率相等;一个整型特征[0,5)均匀分布;一个字符串 特征,他是从一个用整型特征作为index,一个字符串表格生成的;一个float 特征,标准正态分布生成。
10,000个独立同分布的样本。
# the number of observations in the dataset
n_observations = int(1e4) # boolean feature, encoded as False or True
feature0 = np.random.choice([False, True], n_observations) # integer feature, random from 0 .. 4
feature1 = np.random.randint(0, 5, n_observations) # string feature
strings = np.array([b'cat', b'dog', b'chicken', b'horse', b'goat'])
feature2 = strings[feature1] # float feature, from a standard normal distribution
feature3 = np.random.randn(n_observations)
--to be continued--
Using TFRecords and tf.Example的更多相关文章
- TF Boys (TensorFlow Boys ) 养成记(二)
TensorFlow 的 How-Tos,讲解了这么几点: 1. 变量:创建,初始化,保存,加载,共享: 2. TensorFlow 的可视化学习,(r0.12版本后,加入了Embedding Vis ...
- 图像转化成TFrecords格式并回转
import os import tensorflow as tf from PIL import Image import numpy as np cat_image_path='D:/软件/pyc ...
- TensorFlow中数据读取之tfrecords
关于Tensorflow读取数据,官网给出了三种方法: 供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据. 从文件读取数据: 在TensorFlow ...
- Tensorflow读写TFRecords文件
在使用slim之类的tensorflow自带框架的时候一般默认的数据格式就是TFRecords,在训练的时候使用TFRecords中数据的流程如下:使用input pipeline读取tfrecord ...
- 由浅入深之Tensorflow(3)----数据读取之TFRecords
转载自http://blog.csdn.net/u012759136/article/details/52232266 原文作者github地址 概述 关于Tensorflow读取数据,官网给出了三种 ...
- TF Boys (TensorFlow Boys ) 养成记(二): TensorFlow 数据读取
TensorFlow 的 How-Tos,讲解了这么几点: 1. 变量:创建,初始化,保存,加载,共享: 2. TensorFlow 的可视化学习,(r0.12版本后,加入了Embedding Vis ...
- Tensorflow中使用TFRecords高效读取数据--结合Attention-over-Attention Neural Network for Reading Comprehension
原文链接:https://arxiv.org/pdf/1607.04423.pdf 本片论文主要讲了Attention Model在完形填空类的阅读理解上的应用. 转载:https://blog.cs ...
- tensorflowxun训练自己的数据集之从tfrecords读取数据
当训练数据量较小时,采用直接读取文件的方式,当训练数据量非常大时,直接读取文件的方式太耗内存,这时应采用高效的读取方法,读取tfrecords文件,这其实是一种二进制文件.tensorflow为其内置 ...
- 机器学习: TensorFlow 的数据读取与TFRecords 格式
最近学习tensorflow,发现其读取数据的方式看起来有些不同,所以又重新系统地看了一下文档,总得来说,tensorflow 有三种主流的数据读取方式: 1) 传送 (feeding): Pytho ...
随机推荐
- BLOB类型对应Long binary,CLOB对应Long characters
BLOB类型对应Long binary,CLOB对应Long characters
- HDU 4417 Super Mario 主席树查询区间小于某个值的个数
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...
- 将 vue.js 获取的 html 文本转化为纯文本
我存入数据表中的数据是使用 html 格式,获取数据是使用 vue 获取. 遇到了一个问题,就是界面上显示的数据是 html 格式的,但是我需要它显示纯文本. 怎么做呢?首先在 js 中写一个将 ...
- oracle sum()聚合函数
原文链接:https://blog.csdn.net/cizatu5130/article/details/100291347 oracle sum()聚合函数 2016-05-13 20:08:00 ...
- HTML静态网页---标签
一. 创建HTML: (一) body的属性: bgcolor 页面背景色 background 背景壁纸.图片 text 文字颜色 topmargin 上边距 leftmargin ...
- 搭建服务器上的GIT并实现自动同步到站点目录(www)
https://blog.csdn.net/baidu_30000217/article/details/51327289 前言:当我们想要实现几个小伙伴合作开发同一个项目,或者建立一个资源分享平台的 ...
- Native memory allocation (mmap) failed to map xxx bytes for committing reserved memory
遇到问题 在服务器上运行 nexus 出现Native memory allocation (mmap) failed to map 838860800 bytes for committing re ...
- PHP开源框架Laravel的安装与配置
编将带领大家一步步在Windows 7平台下搭建该框架: 工具/原料 windows 7 Composer Laravel最新框架 方法/步骤 1 安装composer.安装之前要确保目录:w ...
- python的if判断
if 判断条件的时候,如果是多个条件一起进行判断,那么就需要逻辑运算符 并且-----------and 或者-----------or 非(取反)----not if 条件1 and 条件2 ...
- Redux 认识之后进阶
两个东西 action 状态 路由 以及嵌套路由 完整结构 进阶+源代码 源代码在我的 gitHub 存储库里面 https://github.com/Haisenan/Redux2.0