Assume you have an input batch which contains variable length sequences. The batch dimension is:
input: [batch_size, max_time, dim_feature]
and you also stored the length of each sequence in a vector, say sequence_length. Now you can easily get the states output by:
_, state = tf.nn.dynamic_rnn(some_RNN_cell, input, sequence_length=sequence_length)
then you can get both the hidden and cell states output:
state.h: hidden states output, [batch_size, hidden_states_size]
state.c: cell states output
I give credit to these two sources:
https://danijar.com/variable-sequence-lengths-in-tensorflow/
https://github.com/shane-settle/neural-acoustic-word-embeddings/blob/4cc3878e6715860bcce202aea7c5a6b7284292a1/code/lstm.py#L25