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. 20 - CommonJS - 规范的具体内容

  2. mybatis初步配置容易出现的问题

    The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You ...

  3. POJ 2039:To and Fro

    To and Fro Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8632   Accepted: 5797 Descri ...

  4. POJ 2488:A Knight's Journey 深搜入门之走马观花

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35342   Accepted: 12 ...

  5. error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMT

    项目--属性 ---连接器---命令行 输入: /FORCE:MULTIPLE 编译环境:VS2012SP3

  6. ..\OBJ\LED.axf: Error: L6218E: Undefined symbol EXTI_Init (referred from exti.o). 错误修改

    今天在移植野火的程序到元子的开发平台上时候,发现自己在中断初话中断函数的时候出现了:..\OBJ\LED.axf: Error: L6218E: Undefined symbol EXTI_Init ...

  7. 【动手学pytorch】pytorch的基础操作

    一.Tensor a)       张量是torch的基础数据类型 b)       张量的核心是坐标的改变不会改变自身性质. c)        0阶张量为标量(只有数值,没有方向的量),因为它不随 ...

  8. delphi内嵌汇编

    { 前面知道了一个汇编的赋值指令(MOV), 再了解一个加法指令(ADD), 就可以做个例子了. 譬如: ADD AX,BX; 这相当于 Delphi 中的 AX := AX + BX; 另外提前来个 ...

  9. MySQL数据库索引常见问题

    笔者看过很多数据库相关方面的面试题,但大多数答案都不太准确,因此决定在自己blog进行一个总结. Q1:数据库有哪些索引?优缺点是什么? 1.B树索引:大多数数据库采用的索引(innoDB采用的是b+ ...

  10. 操作实践:Java桌面程序实现日志级别热修改

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 定位问题的时候往往需要动态修改日志级别并且不能影响业务的正常运行,也就是不能重启应用,此时就要使用到动态日 ...