Internal implementation of Dynamic RNN. Args: cell: An instance of RNNCell. inputs: A `Tensor` of shape [time, batch_size, input_size], or a nested tuple of such elements. initial_state: A `Tensor` of shape `[batch_size, state_size]`, or if `cell.state_size` is a tuple, th
(cell,
inputs,
initial_state,
parallel_iterations,
swap_memory,
att_scores = None,
sequence_length=None,
dtype=None)
| 622 | |
| 623 | |
| 624 | def _dynamic_rnn_loop(cell, |
| 625 | inputs, |
| 626 | initial_state, |
| 627 | parallel_iterations, |
| 628 | swap_memory, |
| 629 | att_scores = None, |
| 630 | sequence_length=None, |
| 631 | dtype=None): |
| 632 | """Internal implementation of Dynamic RNN. |
| 633 | |
| 634 | Args: |
| 635 | cell: An instance of RNNCell. |
| 636 | inputs: A `Tensor` of shape [time, batch_size, input_size], or a nested |
| 637 | tuple of such elements. |
| 638 | initial_state: A `Tensor` of shape `[batch_size, state_size]`, or if |
| 639 | `cell.state_size` is a tuple, then this should be a tuple of |
| 640 | tensors having shapes `[batch_size, s] for s in cell.state_size`. |
| 641 | parallel_iterations: Positive Python int. |
| 642 | swap_memory: A Python boolean |
| 643 | sequence_length: (optional) An `int32` `Tensor` of shape [batch_size]. |
| 644 | dtype: (optional) Expected dtype of output. If not specified, inferred from |
| 645 | initial_state. |
| 646 | |
| 647 | Returns: |
| 648 | Tuple `(final_outputs, final_state)`. |
| 649 | final_outputs: |
| 650 | A `Tensor` of shape `[time, batch_size, cell.output_size]`. If |
| 651 | `cell.output_size` is a (possibly nested) tuple of ints or `TensorShape` |
| 652 | objects, then this returns a (possibly nsted) tuple of Tensors matching |
| 653 | the corresponding shapes. |
| 654 | final_state: |
| 655 | A `Tensor`, or possibly nested tuple of Tensors, matching in length |
| 656 | and shapes to `initial_state`. |
| 657 | |
| 658 | Raises: |
| 659 | ValueError: If the input depth cannot be inferred via shape inference |
| 660 | from the inputs. |
| 661 | """ |
| 662 | state = initial_state |
| 663 | assert isinstance(parallel_iterations, int), "parallel_iterations must be int" |
| 664 | |
| 665 | state_size = cell.state_size |
| 666 | |
| 667 | flat_input = nest.flatten(inputs) |
| 668 | flat_output_size = nest.flatten(cell.output_size) |
| 669 | |
| 670 | # Construct an initial output |
| 671 | input_shape = array_ops.shape(flat_input[0]) |
| 672 | time_steps = input_shape[0] |
| 673 | batch_size = _best_effort_input_batch_size(flat_input) |
| 674 | |
| 675 | inputs_got_shape = tuple(input_.get_shape().with_rank_at_least(3) |
| 676 | for input_ in flat_input) |
| 677 | |
| 678 | const_time_steps, const_batch_size = inputs_got_shape[0].as_list()[:2] |
| 679 | |
| 680 | for shape in inputs_got_shape: |
| 681 | if not shape[2:].is_fully_defined(): |
no test coverage detected