Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert static_rnn inputs to dynamic_rnn inputs in tensorflow?

Tags:

tensorflow

I'm having trouble understanding the input parameter for tensorflow's dynamic_rnn. It'd help a lot if I could understand how to convert static_rnn inputs to dynamic_rnn inputs.

For a static_rnn, the input is supposed to be a length T list of tensors whose shapes are [batch_size, input_size], where T is the sequence length. This makes sense to me.

For a dynamic_rnn, the input is supposed to be a Tensor of shape [batch_size, max_time, ...]. I don't understand how to incorporate input_size here. More generally, I don't know what else you could put in the ellipsis.

Say, for example, that my data consists of 50-character-long sentences, so the input_size is the number of letters in the alphabet. For a static_rnn, I'd make a length 50 list of tensors whose shapes are [batch_size, input_size]. How do I convert this list of tensors to a single tensor, so that I can feed it to a dynamic_rnn?

like image 843
hahdawg Avatar asked Apr 13 '17 15:04

hahdawg


1 Answers

Your dynamic_rnn input should be of shape [batch_size, sequence_length, input_size].

Basically the tensor represents batch_size examples of length sequence_length, and whatever is left in the ellipsis is the shape of a single sequence element.

The thing is, with dynamic_rnn you don't need to know sequence_length beforehand, so your input placeholder could look like

x = tf.placeholder(tf.int32, shape=(batch_size, None, input_size))

Which comes in pretty handy. Furthermore, examples in a single batch can have different lengths (but must be padded to the same length), but you must pass the sequence_length parameter to dynamic_rnn so it knows when to stop calculations for each example.

like image 157
Dzjkb Avatar answered Oct 23 '22 14:10

Dzjkb