ZEWEI CHU
ZEWEI CHU

Reputation: 447

Placeholders as fetches in Tensorflow

I have seen Tensorflow code as following from a project:

sess.run(train_enqeue, feed_dict)

where train_enqeue is a built from:

train_queue = tf.FIFOQueue(train_params.async_encoding, [x.dtype for x in placeholders], name="train_queue")
train_enqeue = train_queue.enqueue(placeholders)

It's basically a FIFO queue of placeholders. I wonder what does it mean to pass placeholders as fetches in this case? Does it return the values from the placeholder?

The code is from line 561 of https://github.com/allenai/document-qa/blob/master/docqa/trainer.py

Upvotes: 1

Views: 110

Answers (1)

Patwie
Patwie

Reputation: 4450

It is not a FIFO-queue of placeholders. It is a FIFO-queue of tensors. The placeholders are required to specify which values should be added to the queue.

The dequeue returns/fetches the elements which are enqueued:

import tensorflow as tf

input_a = tf.placeholder(tf.int32)
input_b = tf.placeholder(tf.float32)

queue = tf.FIFOQueue(20, [tf.int32, tf.float32], name="train_queue")
queue_add = queue.enqueue([input_a, input_b])
queue_fetch = queue.dequeue()

with tf.Session() as sess:
    sess.run(queue_add, {input_a: 42, input_b: 3.14159265358979})
    sess.run(queue_add, {input_a: 43, input_b: 4.14159265358979})
    sess.run(queue_add, {input_a: 44, input_b: 5.14159265358979})
    print(sess.run(queue_fetch))  # gives [42, 3.1415927]
    print(sess.run(queue_fetch))  # gives [43, 4.1415925]
    print(sess.run(queue_fetch))  # gives [44, 5.1415925]

For the sake of DRY, you can rewrite:

inputs = []
inputs.append(tf.placeholder(tf.int32))
inputs.append(tf.placeholder(tf.float32))

queue = tf.FIFOQueue(20, [x.dtype for x in inputs], name="train_queue")

instead of

input_a = tf.placeholder(tf.int32)
input_b = tf.placeholder(tf.float32)

queue = tf.FIFOQueue(20, [tf.int32, tf.float32], name="train_queue")

Think of having 50 inputs and you are lazy to write all these types or you just favor a generic implementation.

Permanent-link to line mentioned in question

Upvotes: 2

Related Questions