0

In this question How do I set TensorFlow RNN state when state_is_tuple=True?: the accepted answer initialize the initial state like this:

state_placeholder = tf.placeholder(tf.float32, [num_layers, 2, batch_size, state_size])

I assume this requires a specific batch size, while what I have now is:

inputSeq = tf.placeholder(tf.float32, [None, seqLength, observationDim], name='input_seq')
outputs, final_state = tf.nn.dynamic_rnn(cell, inputSeq, initial_state=initialState)

And I want this initialState to be a zero state, and can be configurable as the batch size of inputSeq could vary. However, cell.zero_state does not accept None as batch size. Is there any workaround?

Kevin Fang
  • 1,966
  • 2
  • 16
  • 31

1 Answers1

1

cell.zero_state accepts a scalar tensor.

Get the batch size of the place holder via tf.shape, then it is done: B = tf.shape(state_placeholder)[0] # the batch size scalar tensor initial_state = cell.zero_state(B)

yantao du
  • 61
  • 3