吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集基本使用方法
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图形数据处理:数据集基本使用方法的更多相关文章
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集高层操作
import tempfile import tensorflow as tf # 1. 列举输入文件. # 输入数据生成的训练和测试数据. train_files = tf.train.match_ ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:循环神经网络预测正弦函数
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 定义RNN的参数. HIDDEN_SIZE = ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:输入数据处理框架
import tensorflow as tf # 1. 创建文件列表,通过文件列表创建输入文件队列 files = tf.train.match_filenames_once("F:\\o ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:输入文件队列
import tensorflow as tf # 1. 生成文件存储样例数据. def _int64_feature(value): return tf.train.Feature(int64_li ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:多线程队列操作
import tensorflow as tf #1. 定义队列及其操作. queue = tf.FIFOQueue(100,"float") enqueue_op = queue ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:队列操作
import tensorflow as tf #1. 创建队列,并操作里面的元素. q = tf.FIFOQueue(2, "int32") init = q.enqueue_m ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:图像预处理完整样例
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #随机调整图片的色彩,定义两种顺序. def di ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:TensorFlow图像处理函数
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #读取图片 image_raw_data = tf ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:读取MNIST手写图片数据写入的TFRecord文件
import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...
随机推荐
- Day1-T2
原题目 在小X的认知里,质数是除了本身和1以外,没有其他因数的数. 但由于小 X对质数的热爱超乎寻常,所以小X同样喜欢那些虽然不是质数, 但却是由两个质数相乘得来的数. 于是,我们定义一个数小 X喜欢 ...
- -webkit-appearance —— webkit外观样式属性
-webkit-appearance —— webkit外观样式属性 -webkit-appearance 是一个 不规范的属性(unsupported WebKit property),它没有出现在 ...
- cf 478D.Santa Claus and a Palindrome
原来set,priority_queue也可以映射..涨姿势2333 比较麻烦的应该就是判断自身回文的串是选2个还是选一个吧. #include<bits/stdc++.h> #defin ...
- GitHub 网站上不去/加载慢/加载不全 解决办法
1. 当你打开你的 GitHub 2. F12 进入检查页面,点击 network 3. 找到变红的字段右键复制连接 4. 打开 DNS 查询网站,输入你复制的网址,点击查询 5. 选择国内的 ip ...
- No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst
今日遇到一个报错如下: No enclosing instance of type test is accessible. Must qualify the allocation with an en ...
- Social GAN代码要点记录
近日在阅读Social GAN文献的实验代码,加深对模型的理解,发现源代码的工程化很强,也比较适合构建实验模型的学习,故细致阅读.下文是笔者阅读中一些要点总结,有关于pytorch,也有关于模型自身的 ...
- 多线程开发之GCD
简介GCD本身是苹果公司为多核的并行运算提出的解决方案.GCD在工作时会自动利用更多的处理器核心,以充分利用更强大的机器.GCD是Grand Central Dispatch的简称,它是基于C语言的. ...
- 2,The AudioContext was not allowed to start.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on t ...
- 删除所有的docker容器和镜像(转载)
列出所有的容器 ID docker ps -aq 停止所有的容器 docker stop $(docker ps -aq) 删除所有的容器 docker rm $(docker ps -aq) 删除所 ...
- [GXYCTF2019]BabySQli
0x00 知识点 emmm这道题目就是脑洞得大,能猜后端源码 0x01 解题 查看源码: base32,base64解码得到 select * from user where username = ' ...