tensorflow编程: Inputs and Readers

Placeholders

tf.placeholder

插入一个坐等 被feed_dicttensor占位符

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

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]})
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 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]})
# 占位符 未 被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 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


队列基本操作:

[tf.float32]
fifo_queue
None
Tensor("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 + 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)
[ 1.  1.  2.]


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

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

# 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,自然影响到了 入队 的 内容;跟着也决定了 出队 的 内容。

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 tf

txt_file = './1.txt'

content = tf.read_file(txt_file)
print content
with 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 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))
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 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])

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



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值