tensorflow---之feed_dict和使用队列的区别

sess.run() 中的feed_dict

我们都知道feed_dict的作用是给使用placeholder创建出来的tensor赋值。其实,他的作用更加广泛:feed 使用一个 值临时替换一个 op 的输出结果. 你可以提供 feed 数据作为 run() 调用的参数. feed 只在调用它的方法内有效, 方法结束, feed 就会消失.

(意思就是和所只有使用feed_dict才能替换,tf.placeholder的值,x和y_的值才会变):

x = tf.placeholder("float", [None, 784])
y_ = tf.placeholder("float", [None,10])
for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

如果使用队列的话:

with tf.variable_scope("queue"):
    q = tf.FIFOQueue(capacity=5, dtypes=tf.float32) # enqueue 5 batches
    # We use the "enqueue" operation so 1 element of the queue is the full batch
    enqueue_op = q.enqueue(x_input_data)
    numberOfThreads = 1
    qr = tf.train.QueueRunner(q, [enqueue_op] * numberOfThreads)
    tf.train.add_queue_runner(qr)
    input = q.dequeue() # It replaces our input placeholder
    # We can also compute y_true right into the graph now
    y_true = tf.cast(tf.reduce_sum(input, axis=1, keep_dims=True) > 0, tf.int32)

for i in range(1000):
    print input

这样每次输出的input都会变,不需要feed_dict且不会占用内存。

参考:https://blog.csdn.net/coderpai/article/details/68952271

参考:https://blog.csdn.net/weixin_42052460/article/details/80718435

猜你喜欢

转载自blog.csdn.net/zxyhhjs2017/article/details/82556261
今日推荐