import tempfile
import tensorflow as tf 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))

# 创建文本文件作为本例的输入。
with open("E:\\temp\\test1.txt", "w") as file:
file.write("File1, line1.\n")
file.write("File1, line2.\n")
with open("E:\\temp\\test2.txt", "w") as file:
file.write("File2, line1.\n")
file.write("File2, line2.\n") # 从文本文件创建数据集。这里可以提供多个文件。
input_files = ["E:\\temp\\test1.txt", "E:\\temp\\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的方法。
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 = ["E:\\MNIST_data\\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)

# 从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: ["E:\\MNIST_data\\output.tfrecords"]})
# 遍历所有数据一个epoch。当遍历结束时,程序会抛出OutOfRangeError。
while True:
try:
x, y = sess.run([image, label])
except tf.errors.OutOfRangeError:
break

吴裕雄 python 神经网络——TensorFlow 数据集基本使用方法的更多相关文章

  1. 吴裕雄 python 神经网络——TensorFlow 数据集高层操作

    import tempfile import tensorflow as tf train_files = tf.train.match_filenames_once("E:\\output ...

  2. 吴裕雄 python 神经网络——TensorFlow pb文件保存方法

    import tensorflow as tf from tensorflow.python.framework import graph_util v1 = tf.Variable(tf.const ...

  3. 吴裕雄 python 神经网络——TensorFlow ckpt文件保存方法

    import tensorflow as tf v1 = tf.Variable(tf.random_normal([1], stddev=1, seed=1)) v2 = tf.Variable(t ...

  4. 吴裕雄 python 神经网络——TensorFlow 循环神经网络处理MNIST手写数字数据集

    #加载TF并导入数据集 import tensorflow as tf from tensorflow.contrib import rnn from tensorflow.examples.tuto ...

  5. 吴裕雄 python 神经网络TensorFlow实现LeNet模型处理手写数字识别MNIST数据集

    import tensorflow as tf tf.reset_default_graph() # 配置神经网络的参数 INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE ...

  6. 吴裕雄 python 神经网络——TensorFlow 使用卷积神经网络训练和预测MNIST手写数据集

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

  7. 吴裕雄 PYTHON 神经网络——TENSORFLOW 无监督学习处理MNIST手写数字数据集

    # 导入模块 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 加载数据 from tensor ...

  8. 吴裕雄 python 神经网络——TensorFlow实现回归模型训练预测MNIST手写数据集

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...

  9. 吴裕雄 python 神经网络——TensorFlow实现AlexNet模型处理手写数字识别MNIST数据集

    import tensorflow as tf # 输入数据 from tensorflow.examples.tutorials.mnist import input_data mnist = in ...

随机推荐

  1. mysql分组,然后组内排序取最新的一条

    参照: https://blog.csdn.net/qq_16504067/article/details/78589232 https://www.cnblogs.com/w1441639547/p ...

  2. JavaWeb——第1章Web技术概述

    Web本意是蜘蛛网的意思,现常指Internet的Web技术.Web技术提供了方便的信息发布和交流方式,是一种典型的分布式应用结构,Web应用中的每一次信息交换都要涉及客户端和服务器. 一.Inter ...

  3. 微信小程序 购物车流程

    购物车流程 一.需求分析 a:全选,单选,根据选中的计算数目和总价 b:单个商品加减 c:删除一个商品 wxml 布局 <view> <view v-if="flag&qu ...

  4. 【转载】Windows环境下JNI的实现实例

    转自:http://blog.csdn.net/jjunjoe/article/details/6987183 一.关于JNI: JNI(Java Native Interface):Java本地调用 ...

  5. JS高级---新内容课程介绍

    重点: 原型链 重点:不同的继承 原型的另一个作用 重点:this指向要知道到底是谁   复习原型 原型链 原型的指向是否可以改变 继承 如何实现继承 原型的方式继承 借用构造函数继承 组合继承 拷贝 ...

  6. Html学习笔记(二)

    Html头部 HTML <link>元素 <link> 标签定义了文档与外部资源之间的关系. <link> 标签通常用于链接到样式表: <head> & ...

  7. sqli-libs(23-28a关)

    Less_23 首先在php文件中加入echo $sql;echo "<br>"; 方法一: :%00 输入?id=1’,报错,说明存在注入漏洞: 输入?id=1' - ...

  8. 数据库程序接口——JDBC——功能第二篇——数据源之C3P0数据源

    综述 C3P0由三部分内容组成.实例化对象,各配置项的含义,以及加载配置项的方式. 实例化对象的方式有三种,第一种方式直接new ComboPooledDataSource,第二种方式使用工厂类Dat ...

  9. CSS学习(3)样式表

    如何插入样式表 插入样式表的方法有三种: 外部样式表(External style sheet) 内部样式表(Internal style sheet) 内联样式(Inline style) 外部样式 ...

  10. django template 模板

    九.Template模板 Template 模板是根据view传过来数据在html展示的功能,典型python 模板jinjia2库提供丰富的上下文展示func 创建template位置在项目下与ap ...