What is equivalent to the following code in a tensor flow?

I have the following function:

import random

lst = []
for i in range(100):
    lst.append(random.randint(1, 10))

print(lst)

buffer = []

# This is the peace of code which I am interested to convert into tensorflow.
for a in lst:
    buffer.append(a)

    if len(buffer) > 5:
        buffer.pop(0)

    if len(buffer) == 5:
        print(buffer)

So, from the code I need to create a buffer (which can be a variable in a tensor stream). This buffer should contain the extracted functions from the latter conv layer. variablewill be the input for RNNin my case.

, , RNN (batch of images) * (sequence length) * (size of 1 image), . , , 1 , Datasets input queue . : batch_size * sequence_length * feature space. , :

if len(buffer) == n:
    # empty out the buffer after using its elements
    buffer = [] # Or any other alternative way

, batches , .

!

+6
1

, tf.FIFOQueue (https://www.tensorflow.org/api_docs/python/tf/FIFOQueue). .

BATCH_SIZE = 20

lst = []
for i in range(BATCH_SIZE):
    lst.append(random.randint(1, 10))
print(lst)

curr_data = np.reshape(lst, (BATCH_SIZE, 1)) # reshape the tensor so that [BATCH_SIZE 1]

# queue starts here
queue_input_data = tf.placeholder(tf.int32, shape=[1]) # Placeholder for feed the data

queue = tf.FIFOQueue(capacity=50, dtypes=[tf.int32], shapes=[1]) # Queue define here

enqueue_op = queue.enqueue([queue_input_data])  # enqueue operation
len_op = queue.size()  # chek the queue size

#check the length of the queue and dequeue one if greater than 5
dequeue_one = tf.cond(tf.greater(len_op, 5), lambda: queue.dequeue(), lambda: 0)
#check the length of the queue and dequeue five elemts if equals to 5
dequeue_many = tf.cond(tf.equal(len_op, 5), lambda:queue.dequeue_many(5), lambda: 0)

with tf.Session() as session:
    for i in range(BATCH_SIZE):
        _ = session.run(enqueue_op, feed_dict={queue_input_data: curr_data[i]}) # enqueue one element each ietaration
        len = session.run(len_op)  # check the legth of the queue
        print(len)

        element = session.run(dequeue_one)  # dequeue the first element
        print(element)

,

  • dequeue one dequeue many, ( , - ).

  • , tf.cond - ( , ). , if-then-else, , ( if ). Tensorflow , ( ).

, Tensorflow (http://ischlag.imtqy.com/2016/11/07/tensorflow-input-pipeline-for-large-datasets/).

, .

+2

All Articles