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. CoreData的学习

    第一步:创建项目是勾选coredata,当然创建的时候没有勾选,之后还可以手动生产, 然后:创建数据库模型,及为其添加模型的属性. 然后生成模型文件: 注意⚠️:首先设置为Manual/None  不 ...

  2. 头部导航悬浮,css

    .header{ position:fixed; z-index:100; left:; right:; } 如图.

  3. 其它内置函数(zip等)

      python内置函数 截止到python版本3.6.2,python一共为我们提供了68个内置函数.它们就是python提供给你直接可以拿来使用的所有函数     Built-in Functio ...

  4. Linux更改ssh端口号,很easy!

    因为公司业务需求,可能涉及到更改ssh远程的端口号,用下面方法轻松解决,废话不多说! 1.打开ssh端口配置文件:vim /etc/ssh/sshd_config,找到如下图所示的端口,改为自己想改的 ...

  5. mongodb多个查询语句

    db.getCollection('costitems').find({"created":{"$gte":ISODate("2019-01-02T0 ...

  6. scrapy--dbmeinv

    第一次将自己的爬虫文件与大家分享.豆瓣美女网页图片爬取.比较简单,但很实用.给大家提供思路为主,增强个人的爬虫能力.希望能帮助到大家!!! 好了,让我们进入正题. 先给大家看下成果!!!激励大家赶快行 ...

  7. 【jQuery】阶段(插入、复制、替换、删除)

    <p>你好!</p> 你最喜欢的水果是? <ul> <li title="苹果">苹果</li> <li titl ...

  8. php-5.6.26源代码 - opcode处理器,“乘法opcode”处理器

    // opcode处理器 - 运算符怎么执行: “*” 乘法opcode处理器 static int ZEND_FASTCALL ZEND_MUL_SPEC_CONST_CONST_HANDLER(Z ...

  9. yii rbac

    一.简介 什么是rbac ? rbac是就是基于角色的访问控制. yii提供一套基础的底层接口,我们知道,rbac经历好几个阶段,从rbac0到rbac3,从基础的用户.角色.权限,到动态的rbac处 ...

  10. Python学习之函数参数

    上一节,我们学习了Python中是如何定义和调用函数且如何得到返回值的.在调用函数时,有的函数需要参数来启动函数,有的则无需参数.这一节我们来介绍Python中有哪些参数类型. 位置参数 在调用函数时 ...