Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In tensorflow, how to iterate over a sequence of inputs stored in a tensor?

I am trying RNN on a variable length multivariate sequence classification problem.

I have defined following function to get the output of the sequence (i.e. the output of RNN cell after the final input from sequence is fed)

def get_sequence_output(x_sequence, initial_hidden_state):
    previous_hidden_state = initial_hidden_state
    for x_single in x_sequence:
        hidden_state = gru_unit(previous_hidden_state, x_single)
        previous_hidden_state = hidden_state
    final_hidden_state = hidden_state
    return final_hidden_state

Here x_sequence is tensor of shape (?, ?, 10) where first ? is for batch size and second ? is for sequence length and each input element is of length 10. gru function takes a previous hidden state and current input and spits out next hidden state (a standard gated recurrent unit).

I am getting an error: 'Tensor' object is not iterable. How do I iterate over a Tensor in sequence manner (reading single element at a time)?

My objective is to apply gru function for every input from the sequence and get the final hidden state.

like image 233
exAres Avatar asked Jul 22 '16 10:07

exAres


People also ask

How do iterate over a TensorFlow tensor?

To iterate through a tensor in Python, we can easily use the for loop method and it will iterate through the tensor directly. To iterate over tensor defines that we have to print a new line tensor and also it will return the number of elements in the tensor.

How do you access the element in a tensor?

We use Indexing and Slicing to access the values of a tensor. Indexing is used to access the value of a single element of the tensor, whereasSlicing is used to access the values of a sequence of elements. We use the assignment operator to modify the values of a tensor.

Which are the three main methods of getting data into a TensorFlow program?

TensorFlow's Basic Programming Elements TensorFlow allows us to assign data to three kinds of data elements: constants, variables, and placeholders. Let's take a closer look at what each of these data components represents.

What is retracing TensorFlow?

Retracing, which is when your Function creates more than one trace, helps ensures that TensorFlow generates correct graphs for each set of inputs. However, tracing is an expensive operation! If your Function retraces a new graph for every call, you'll find that your code executes more slowly than if you didn't use tf.


1 Answers

You can convert a tensor into a list using the unpack function which converts the first dimension into a list. There is also a split function which does something similar. I use unstack in an RNN model I am working on.

y = tf.unstack(tf.transpose(y, (1, 0, 2)))

In this case y starts out with shape (BATCH_SIZE, TIME_STEPS, 128) I transpose it to make the time steps the outer dimension and then unpack it into a list of tensors, one per time step. Now every element in the y list if of shape (BATCH_SIZE, 128) and I can feed it into my RNN.

like image 133
chasep255 Avatar answered Sep 20 '22 06:09

chasep255