How to filter a tensor from a queue based on some predicate in a tensor flow?

How can I filter the data stored in the queue using the predicate function? For example, let's say we have a queue in which tensors of functions and labels are stored, and we only need those that correspond to the predicate. I tried the following implementation without success:

feature, label = queue.dequeue()
if (predicate(feature, label)):
    enqueue_op = another_queue.enqueue(feature, label)
+4
source share
1 answer

- , , tf.where, , , tf.gather, . , , - - tf.train.batch:

:

import numpy as np
import tensorflow as tf

a = tf.constant(np.array([5, 1, 9, 4, 7, 0], dtype=np.int32))

q = tf.FIFOQueue(6, dtypes=[tf.int32], shapes=[])
enqueue = q.enqueue_many([a])
dequeue = q.dequeue_many(6)
predmatch = tf.less(dequeue, [5])
selected_items = tf.reshape(tf.where(predmatch), [-1])
found = tf.gather(dequeue, selected_items)

secondqueue = tf.FIFOQueue(6, dtypes=[tf.int32], shapes=[])
enqueue2 = secondqueue.enqueue_many([found])
dequeue2 = secondqueue.dequeue_many(3) # XXX, hardcoded

with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  sess.run(enqueue)  # Fill the first queue
  sess.run(enqueue2) # Filter, push into queue 2
  print sess.run(dequeue2) # Pop items off of queue2

; tf.where , tf.gather .

, , , - , , , , ( ). , QueueRunners , . tf.train.batch - . Threading and Queues.

+8

Source: https://habr.com/ru/post/1617324/


All Articles