tensorflow编程: Inputs and Readers
来源:互联网 发布:淘宝买家好评率 编辑:程序博客网 时间:2024/05/08 00:01
Placeholders
tf.placeholder
插入一个坐等 被feed_dict 的 tensor占位符
。
tf.placeholder (dtype, shape=None, name=None)
import tensorflow as tfx = tf.placeholder(dtype=tf.int32)print xwith tf.Session() as sess: print sess.run(x, feed_dict={x:[10, 20]})
Tensor("Placeholder:0", dtype=int32)[10 20]
tf.placeholder_with_default
与 tf.placeholder
不同的是,这里如果 未 被feed_dict,并不会 打印报错,而是打印出 默认数据。
tf.placeholder_with_default (input, shape, name=None)
import tensorflow as tfx = tf.placeholder_with_default(input=[3, 3], shape=(2,))print xwith tf.Session() as sess: print sess.run(x) print sess.run(x, feed_dict={x:[10, 20]})
# 占位符 未 被feed_dict,打印默认输出[3 3]# 占位符 被feed_dict 了,打印 所被feed_dict 的内容[10 20]
Readers
Converting
Example protocol buffer
Queues
tf.QueueBase
暂时没搞懂。。。
tf.FIFOQueue
FIFO队列 的 相关操作
tf.FIFOQueue.__init__ (capacity, dtypes, shapes=None, names=None, shared_name=None, name=’fifo_queue’)
队列属性读取:
import tensorflow as tfq = tf.FIFOQueue(capacity=3, dtypes=tf.float32)print q.dtypesprint q.nameprint q.namesprint q.queue_refprint q.shapes
队列基本操作:
[tf.float32]fifo_queueNoneTensor("fifo_queue:0", shape=(), dtype=resource)[TensorShape(None)]
import tensorflow as tf# 当后面需要调用dequeue_many接口时,初始定义的tf.FIFOQueue必须要指定shapes参数,否则会报错。q = tf.FIFOQueue(capacity=3, dtypes=tf.float32, shapes=[(),])en_m_op = q.enqueue_many(vals=([0, 0, 0],))de_op = q.dequeue()y = de_op + 1en_op = q.enqueue([y])de_m_op = q.dequeue_many(n=[3])close_op = q.close()with tf.Session() as sess: # 多个变量入队 sess.run(en_m_op) # 每次一个计算后的单变量入队(sess.run()会自动把en_op拓扑调用的de_op一块执行了) sess.run(en_op) sess.run(en_op) sess.run(en_op) sess.run(en_op) # 多个变量出队并打印 print sess.run(de_m_op) # 关闭队列 sess.run(close_op)
[ 1. 1. 2.]
tf.FIFOQueue 出入队列的具体探究:
当 shapes=[(), ()]
,入队时,由 enqueue_many
中的两个 list 各出表首元素 同时入队。由于 len(每个list) = 3
,所以每执行一次 sess.run(enqueue_op)
,就会表演 三次 双表首 同时入队 。
# coding=utf-8import tensorflow as tf# 这里的 “capacity参数项”,即指的是 有多少个 “shapes=[(), ()]”。本程序中 sess.run(enqueue_op) 出现两次,所以 capacity参数项 最低为 6,否则电脑会跑不出结果,终端打印的内容就会一直悬停在那边。queue = tf.FIFOQueue(capacity=6, dtypes=[tf.float32, tf.float32], shapes=[(), ()])enqueue_op = queue.enqueue_many([[11, 12, 13], [21, 22, 23]])for i in xrange(1, 7): with tf.Session() as sess: # 当 shapes=[(), ()] 时,每一次的 sess.run(enqueue_op) ,入队的内容以及顺序为:[11, 21] -> [12, 22] -> [13, 23] 。即:每次入队的内容由 shapes参数项 决定。 sess.run(enqueue_op) sess.run(enqueue_op) print ('\ndequeue_many({}) :'.format(i)) print sess.run(queue.dequeue_many(i))
可以看到,tf.FIFOQueue
中 shape参数项
的设置 直接决定 了 入队(enqueue)和出队(dequeue)的具体内容。
原因:shape参数项
决定了 队列管道横切面 的状况,影响了 入队 的 shape,自然影响到了 入队 的 内容;跟着也决定了 出队 的 内容。
dequeue_many(1) :[array([ 11.], dtype=float32), array([ 21.], dtype=float32)]dequeue_many(2) :[array([ 11., 12.], dtype=float32), array([ 21., 22.], dtype=float32)]dequeue_many(3) :[array([ 11., 12., 13.], dtype=float32), array([ 21., 22., 23.], dtype=float32)]dequeue_many(4) :[array([ 11., 12., 13., 11.], dtype=float32), array([ 21., 22., 23., 21.], dtype=float32)]dequeue_many(5) :[array([ 11., 12., 13., 11., 12.], dtype=float32), array([ 21., 22., 23., 21., 22.], dtype=float32)]dequeue_many(6) :[array([ 11., 12., 13., 11., 12., 13.], dtype=float32), array([ 21., 22., 23., 21., 22., 23.], dtype=float32)]
tf.PaddingFIFOQueue
tf.RandomShuffleQueue
tf.PriorityQueue
Conditional Accumulators
Dealing with the filesystem
tf.matching_files
tf.read_file
读取文件,返回 string型
tensor
tf.read_file(filename, name=None)
import tensorflow as tftxt_file = './1.txt'content = tf.read_file(txt_file)print contentwith tf.Session() as sess: print sess.run(content)
Tensor("ReadFile:0", shape=(), dtype=string)Hello Tensorflow !Hello October !
tf.write_file
将 string型
tensor 写覆盖 入文件
tf.write_file (filename, contents, name=None)
import tensorflow as tftxt_file = './1.txt'with tf.Session() as sess: sess.run(tf.write_file(txt_file, contents=tf.convert_to_tensor('Hello Petrichor !\n'))) print sess.run(tf.read_file(txt_file))
Hello Petrichor !
Input pipeline
tf.train.batch
将 tensor 捆成 batch_tensor
。
tf.train.batch (tensors, batch_size, num_threads=1, capacity=32, enqueue_many=False, shapes=None, dynamic_pad=False, allow_smaller_final_batch=False, shared_name=None, name=None)
"""So sorry, 代码没试通,以后再回过头来琢磨到底哪出错了"""import tensorflow as tflist = [range(11, 14), range(21, 24)]q = tf.FIFOQueue(capacity=10, dtypes=[tf.float32, tf.float32], shapes=[(), ()])en_m_op = q.enqueue_many(vals=list)t_1, t_2 = q.dequeue()ele_1, ele_2 = tf.train.batch(tensors=[t_1, t_2], batch_size=2)with tf.Session() as sess: sess.run(en_m_op) # print sess.run([t_1, t_2]) print sess.run([ele_1, ele_2])
tf.train.maybe_batch
tf.train.batch_join
tf.train.maybe_batch_join
tf.train.shuffle_batch
tf.train.maybe_shuffle_batch
tf.train.shuffle_batch_join
tf.train.maybe_shuffle_batch_join
- tensorflow编程: Inputs and Readers
- tf API 研读4:Inputs and Readers
- mex2 Inputs and Outputs
- Filter Readers and Writers
- c2 Streams Readers and Writers
- Video4Linux2 part 4: inputs and outputs
- 171116 Keras-Multiple inputs and outputs
- tensorflow编程: Constants, Sequences, and Random Values
- Understanding Single-Ended, Pseudo-Differential and Fully-Differential ADC Inputs
- Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
- Different Readers for different file types(Tensorflow 的几种读取数据的方式)
- Demonstration of how NOT to implement edge detection and asynchronous inputs to a state machine
- readers and writers problem 读者写者问题 写者优先 C++实现
- Tensorflow download and setup
- Install tensorflow and test
- tensorflow Shapes and Shaping
- Threading and Queues -- Tensorflow
- Tensorflow Graphs and Sessions
- 三 iOS之 CALayer的隐式动画
- NYOJ 737 石子合并(一)(区间dp)
- 外部排序与多路归并
- 第十七课 创建和操纵表
- SpringBoot添加JSP
- tensorflow编程: Inputs and Readers
- POJ 2318 TOYS
- gcc,make,gdb命令用法
- Lucene5(1)简单示例
- Unable to convert classes into dex format 解决方案之一
- Android 7 源码分析系列导读
- bzoj1537 [POI2005]Aut- The Bus 树状数组+DP。
- 洛谷1531 线段树模板:区间最值
- CF 438D The Child and Sequence(线段树取模)