import tempfile
import tensorflow as tf # 1. 从数组创建数据集。
input_data = [1, 2, 3, 5, 8]
dataset = tf.data.Dataset.from_tensor_slices(input_data) # 定义迭代器。
iterator = dataset.make_one_shot_iterator() # get_next() 返回代表一个输入数据的张量。
x = iterator.get_next()
y = x * x with tf.Session() as sess:
for i in range(len(input_data)):
print(sess.run(y))

# 2. 读取文本文件里的数据。
# 创建文本文件作为本例的输入。
with open("E:\\test1.txt", "w") as file:
file.write("File1, line1.\n")
file.write("File1, line2.\n") with open("E:\\test2.txt", "w") as file:
file.write("File2, line1.\n")
file.write("File2, line2.\n") # 从文本文件创建数据集。这里可以提供多个文件。
input_files = ["E:\\test1.txt", "E:\\test2.txt"]
dataset = tf.data.TextLineDataset(input_files) # 定义迭代器。
iterator = dataset.make_one_shot_iterator() # 这里get_next()返回一个字符串类型的张量,代表文件中的一行。
x = iterator.get_next()
with tf.Session() as sess:
for i in range(4):
print(sess.run(x))

# 解析TFRecord文件里的数据。
# 解析一个TFRecord的方法。
def parser(record):
features = tf.parse_single_example(record,features={'image_raw':tf.FixedLenFeature([],tf.string),'pixels':tf.FixedLenFeature([],tf.int64),'label':tf.FixedLenFeature([],tf.int64)})
decoded_images = tf.decode_raw(features['image_raw'],tf.uint8)
retyped_images = tf.cast(decoded_images, tf.float32)
images = tf.reshape(retyped_images, [784])
labels = tf.cast(features['label'],tf.int32)
#pixels = tf.cast(features['pixels'],tf.int32)
return images, labels # 从TFRecord文件创建数据集。这里可以提供多个文件。
input_files = ["F:\\output.tfrecords"]
dataset = tf.data.TFRecordDataset(input_files) # map()函数表示对数据集中的每一条数据进行调用解析方法。
dataset = dataset.map(parser) # 定义遍历数据集的迭代器。
iterator = dataset.make_one_shot_iterator() # 读取数据,可用于进一步计算
image, label = iterator.get_next() with tf.Session() as sess:
for i in range(10):
x, y = sess.run([image, label])
print(y)

# 使用initializable_iterator来动态初始化数据集。
# 从TFRecord文件创建数据集,具体文件路径是一个placeholder,稍后再提供具体路径。
input_files = tf.placeholder(tf.string)
dataset = tf.data.TFRecordDataset(input_files)
dataset = dataset.map(parser) # 定义遍历dataset的initializable_iterator。
iterator = dataset.make_initializable_iterator()
image, label = iterator.get_next() with tf.Session() as sess:
# 首先初始化iterator,并给出input_files的值。
sess.run(iterator.initializer,feed_dict={input_files: ["F:\\output.tfrecords"]})
# 遍历所有数据一个epoch。当遍历结束时,程序会抛出OutOfRangeError。
while True:
try:
x, y = sess.run([image, label])
except tf.errors.OutOfRangeError:
break

吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集基本使用方法的更多相关文章

  1. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集高层操作

    import tempfile import tensorflow as tf # 1. 列举输入文件. # 输入数据生成的训练和测试数据. train_files = tf.train.match_ ...

  2. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:循环神经网络预测正弦函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 定义RNN的参数. HIDDEN_SIZE = ...

  3. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:输入数据处理框架

    import tensorflow as tf # 1. 创建文件列表,通过文件列表创建输入文件队列 files = tf.train.match_filenames_once("F:\\o ...

  4. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:输入文件队列

    import tensorflow as tf # 1. 生成文件存储样例数据. def _int64_feature(value): return tf.train.Feature(int64_li ...

  5. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:多线程队列操作

    import tensorflow as tf #1. 定义队列及其操作. queue = tf.FIFOQueue(100,"float") enqueue_op = queue ...

  6. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:队列操作

    import tensorflow as tf #1. 创建队列,并操作里面的元素. q = tf.FIFOQueue(2, "int32") init = q.enqueue_m ...

  7. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:图像预处理完整样例

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #随机调整图片的色彩,定义两种顺序. def di ...

  8. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:TensorFlow图像处理函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #读取图片 image_raw_data = tf ...

  9. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:读取MNIST手写图片数据写入的TFRecord文件

    import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...

随机推荐

  1. NRF52811-QCAA 蓝牙5.1芯片资料解析

    为了满足市场需求Nordic 宣布推出nRF52811系统级芯片(SoC),这个全功能无线连接解决方案支持蓝牙5.1 测向(Direction Finding)功能和一系列流行低功耗无线协议,用于智能 ...

  2. 11 ~ express ~ 解决 cookie 中文报错的问题

    使用cookies包需要注意:1,cookie中是不能有中文的,一旦有中文,就会报错2,cookie是通过 中间件的形式直接挂载到 req对象上的,那么cookies有的方法,req.cookies就 ...

  3. MyBatis的初始化过程。

    对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外.本章将通过以下几点详细介绍MyBatis的初始化过程. 1.MyBatis的初始化做了什么 2. MyBatis基于XML配置 ...

  4. 并发 ping

    参考 [root@RS2 ~]# cat .sh #!/bin/bash # --, by wwy #------------------------------------------------- ...

  5. 常用ES6-ES10知识点总结

    在工作中我们会常用到的一些es6-es10的一些特性还记得多少,今天就让我们重新复习一遍 ES6语法 1.Let 1.let声明的变量具有块级作用域, { let a = 1 } console.lo ...

  6. <mvc:default-servlet-handler />说明

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  7. 直播课(1)如何通过数据劫持实现Vue(mvvm)框架

    19.6.28更新: 这篇博客比较完善:将每一部分都分装在单独的js文件中: 剖析Vue原理&实现双向绑定MVVM 半个月前看的直播课,现在才自己敲了一遍,罪过罪过 预览: 思路: 简单实现V ...

  8. Centos7.4系统 httpd模式搭建文件服务器

    环境:服务环境:centos7.4 说明:搭建Apache文件服务器,下载路径为/opt/ymyg(下载路径根据实际需要自己定义) 步骤: 1.安装httpd服务   [root@localhost ...

  9. [GWCTF 2019]枯燥的抽奖

    0x00 知识点 种子爆破 工具 http://www.openwall.com/php_mt_seed 0x01 解题 查看源码进入check.php zOHF0Cxp49 <?php #这不 ...

  10. 运行xv6

    我们使用Qemu在Ubuntu下运行 1. 安装Qemu sudo apt-get install qemu 执行 qemu-system-i386 ,如果弹出Qemu界面说明安装成功了 2. 编译x ...