TensorFlow dataset API 使用


由于本人感兴趣的是自然语言处理,所以下面有关dataset API 的使用偏向于变长数据的处理。

1. 从迭代器中引入数据

import numpy as np
import tensorflow as tf def gen():
for _ in range(10):
sz = np.random.randint(3, 20, 1)[0]
yield np.random.randint(1, 100, sz), np.random.randint(0, 10, 1)[0] dataset = tf.data.Dataset.from_generator(
gen, (tf.int32, tf.int32)).repeat(2).shuffle(buffer_size=100).padded_batch(3, padded_shapes=([None], []))
iter = dataset.make_one_shot_iterator()
x, y = iter.get_next() with tf.Session() as sess:
try:
while True:
_x, _y = sess.run([x, y])
print("x is :\n", _x)
print("y is :\n", _y)
print("*" * 50) except tf.errors.OutOfRangeError:
print("done")
finally:
pass

输出的结果如下所示,我们可以将X看作是句子,存的是词的ID,Y看作是对句子的分类标签。由于不同句子长度不一样,所以这里使用了0进行填充,使得每个batch内的句子长度一样。

x is :
[[41 57 68 84 40 72 98 71 95 50 94 17 78 60 69 29 77]
[55 44 11 70 39 39 97 86 71 20 0 0 0 0 0 0 0]
[12 36 75 49 86 0 0 0 0 0 0 0 0 0 0 0 0]]
y is :
[4 1 9]
**************************************************
x is :
[[59 33 64 47 20 53 93 68 73 57 68 59 34]
[69 39 12 83 54 11 92 89 60 21 30 30 31]
[19 32 62 9 66 34 85 86 22 33 19 79 28]]
y is :
[8 1 5]
**************************************************
x is :
[[47 24 96 38 21 53 78 52 74 15 87 37 21 29 45 61 19 56 73]
[ 1 24 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[73 52 14 11 83 77 83 24 34 0 0 0 0 0 0 0 0 0 0]]
y is :
[9 4 4]
**************************************************
x is :
[[34 21 36 17 90 96 19 3 28 60 87 93 4 41 22 89 70 83 58]
[70 25 84 42 45 29 40 0 0 0 0 0 0 0 0 0 0 0 0]
[97 72 19 73 7 9 83 46 72 64 98 13 78 94 66 10 30 46 13]]
y is :
[9 9 4]
**************************************************
x is :
[[33 27 59 45 79 21 57 17 46 24 67 64 83 95 59 65 7 26 82]
[84 31 48 91 7 51 14 71 17 40 89 44 25 17 42 13 99 0 0]
[63 97 45 49 68 70 79 28 90 4 68 77 27 0 0 0 0 0 0]]
y is :
[8 1 8]
**************************************************
x is :
[[62 19 42 88 3 16 20 38 5 59]
[99 84 87 10 8 13 0 0 0 0]
[44 45 45 58 34 53 8 54 0 0]]
y is :
[1 1 4]
**************************************************
x is :
[[77 51 44 51 2 38 60 46 12 78 20 15 23 57]
[25 81 23 22 0 0 0 0 0 0 0 0 0 0]]
y is :
[4 5]
**************************************************
done

TensorFlow dataset API 使用的更多相关文章

  1. TensorFlow数据读取方式:Dataset API

    英文详细版参考:https://www.cnblogs.com/jins-note/p/10243716.html Dataset API是TensorFlow 1.3版本中引入的一个新的模块,主要服 ...

  2. tensorflow models api:ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: 'Tensor("arg0:0", shape=(), dtype=float32, device=/device:CPU:0)'

    tensorflow models api:ValueError: Tensor conversion requested dtype string for Tensor with dtype flo ...

  3. element.dataset API

    不久之前我向大家展示了非常有用的classList API,它是一种HTML5里提供的原生的对页面元素的CSS类进行增.删改的接口,完全可以替代jQuery里的那些CSS类操作方法.而另外一个非常有用 ...

  4. TensorFlow - 相关 API

    来自:https://cloud.tencent.com/developer/labs/lab/10324 TensorFlow - 相关 API TensorFlow 相关函数理解 任务时间:时间未 ...

  5. TensorFlow — 相关 API

    TensorFlow — 相关 API TensorFlow 相关函数理解 任务时间:时间未知 tf.truncated_normal truncated_normal( shape, mean=0. ...

  6. flink dataset api使用及原理

    随着大数据技术在各行各业的广泛应用,要求能对海量数据进行实时处理的需求越来越多,同时数据处理的业务逻辑也越来越复杂,传统的批处理方式和早期的流式处理框架也越来越难以在延迟性.吞吐量.容错能力以及使用便 ...

  7. 开源框架---tensorflow c++ API 一个卡了很久的问题

    <开源框架---tensorflow c++ API 运行第一个“手写字的例子”> 中可以说明tensorflow c++ API是好用的,.......

  8. Apache Flink - Batch(DataSet API)

    Flink DataSet API编程指南: Flink中的DataSet程序是实现数据集转换的常规程序(例如,过滤,映射,连接,分组).数据集最初是从某些来源创建的(例如,通过读取文件或从本地集合创 ...

  9. 开源框架---通过Bazel编译使用tensorflow c++ API 记录

    开源框架---通过Bazel编译使用tensorflow c++ API 记录 tensorflow python API,在python中借用pip安装tensorflow,真的很方便,几句指令就完 ...

随机推荐

  1. Zabbix3.0部署实践

    Zabbix3.0部署实践   Zabbix3整个web界面做了一个全新的设计. 1.1Zabbix环境准备 [root@linux-node1 ~]# cat /etc/redhat-release ...

  2. 使用 NetBackup 命令创建 Hyper-V 策略(命令创建其他策略也是如此)

    Veritas NetBackup™ for Hyper-V 管理指南 Product(s): NetBackup (8.1) 使用 NetBackup 命令创建 Hyper-V 策略 本主题介绍如何 ...

  3. centos下yum安装mysql5.6后,无法启动 MySQL Daemon failed to start

    如果是全新安装应该就不会出现这个问题,升级安装的话,要运行 mysql_upgrade ,但是启动MYSQL就报错MySQL Daemon failed to start 如此就没办法运行mysql_ ...

  4. php无法保存SESSION问题总汇

    昨天客户又过来说网站的问题,说的也都是些毛毛雨的东西,管理那么多网站,再有这么些客户的存在,本人也是累了,但当登录后台的时候突然发现后台登录不了,查看了一下验证码服务器端的session为空值,之前登 ...

  5. 创建 XXXXXXXX 的配置节处理程序时出错: 请求失败

    今天碰到这个错误,之前的程序在测试的时候都没有问题,同样的程序打包通过QQ传给其他人,在XP下测试也没有问题,我的Win7系统从QQ信箱下载压缩包,解压之后执行程序就会出问题,本来还是考虑自己程序是不 ...

  6. R 语言学习日志 1

      1. CSV文件的的读取与写出 2. 数据集筛选 3. 简单随机抽样 sample函数   正文: 1. CSV文件的的读取与写出 文件读取: df2 <- read.table(" ...

  7. 手动创建maven项目+cmd+webapp+tomcat

    1.创建文件夹 2.在刚刚创建的文件夹目录下:创建一个scr文件夹和pom.xml文件 3.在pom.xml配置 <?xml version="1.0" encoding=& ...

  8. ibator自动代码生成

    首先,强烈推荐一篇文章,介绍的特详细 http://www.iteye.com/topic/821983 1. 插件安装 http://blog.csdn.net/rchm8519/article/d ...

  9. ReactiveCocoa实战: 模仿 "花瓣",重写 LeanCloud Rest Api的iOS REST Client.

    这一次我们将要讨论的是移动开发中比较重要的一环--网络请求的封装.鉴于个人经验有限,本文将在一定程度上参考 基于AFNetworking2.0和ReactiveCocoa2.1的iOS REST Cl ...

  10. oracle数据库 网页管理360浏览器登录不上

    使用谷歌浏览器可以登陆,然后在使用360之类的浏览器 就可以登录了