前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow编程: Inputs and Readers

tensorflow编程: Inputs and Readers

作者头像
JNingWei
发布2018-09-28 15:22:44
6900
发布2018-09-28 15:22:44
举报
文章被收录于专栏:JNing的专栏

Placeholders

tf.placeholder

插入一个坐等 被feed_dicttensor占位符

tf.placeholder (dtype, shape=None, name=None)

代码语言:javascript
复制
import tensorflow as tf

x = tf.placeholder(dtype=tf.int32)
print x
with tf.Session() as sess:
    print sess.run(x, feed_dict={x:[10, 20]})
代码语言:javascript
复制
Tensor("Placeholder:0", dtype=int32)

[10 20]

tf.placeholder_with_default

tf.placeholder 不同的是,这里如果 未 被feed_dict,并不会 打印报错,而是打印出 默认数据。

tf.placeholder_with_default (input, shape, name=None)

代码语言:javascript
复制
import tensorflow as tf

x = tf.placeholder_with_default(input=[3, 3], shape=(2,))
print x
with tf.Session() as sess:
    print sess.run(x)
    print sess.run(x, feed_dict={x:[10, 20]})
代码语言:javascript
复制
# 占位符 未 被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’)

队列属性读取:

代码语言:javascript
复制
import tensorflow as tf

q = tf.FIFOQueue(capacity=3, dtypes=tf.float32)

print q.dtypes
print q.name
print q.names
print q.queue_ref
print q.shapes

队列基本操作:

代码语言:javascript
复制
[tf.float32]
fifo_queue
None
Tensor("fifo_queue:0", shape=(), dtype=resource)
[TensorShape(None)]
代码语言:javascript
复制
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 + 1
en_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)
代码语言:javascript
复制
[ 1.  1.  2.]

tf.FIFOQueue 出入队列的具体探究:

shapes=[(), ()] ,入队时,由 enqueue_many 中的两个 list 各出表首元素 同时入队。由于 len(每个list) = 3 ,所以每执行一次 sess.run(enqueue_op),就会表演 三次 双表首 同时入队

代码语言:javascript
复制
# coding=utf-8
import 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.FIFOQueueshape参数项 的设置 直接决定 了 入队(enqueue)和出队(dequeue)的具体内容。

原因:shape参数项 决定了 队列管道横切面 的状况,影响了 入队 的 shape,自然影响到了 入队 的 内容;跟着也决定了 出队 的 内容。

代码语言:javascript
复制
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)

代码语言:javascript
复制
import tensorflow as tf

txt_file = './1.txt'

content = tf.read_file(txt_file)
print content
with tf.Session() as sess:
    print sess.run(content)
代码语言:javascript
复制
Tensor("ReadFile:0", shape=(), dtype=string)

Hello Tensorflow !
Hello October !

tf.write_file

string型 tensor 写覆盖 入文件

tf.write_file (filename, contents, name=None)

代码语言:javascript
复制
import tensorflow as tf

txt_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))
代码语言:javascript
复制
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)

代码语言:javascript
复制
"""
So sorry, 
代码没试通,以后再回过头来琢磨到底哪出错了
"""
import tensorflow as tf


list = [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])
代码语言:javascript
复制

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



本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年10月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Placeholders
    • tf.placeholder
      • tf.placeholder_with_default
      • Readers
      • Converting
      • Example protocol buffer
      • Queues
        • tf.QueueBase
          • tf.FIFOQueue
            • tf.PaddingFIFOQueue
              • tf.RandomShuffleQueue
                • tf.PriorityQueue
                • Conditional Accumulators
                • Dealing with the filesystem
                  • tf.matching_files
                    • tf.read_file
                      • tf.write_file
                      • Input pipeline
                        • tf.train.batch
                          • 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
                                      领券
                                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档