Reverse a list of Tensors up to specified lengths. Args: input_seq: Sequence of seq_len tensors of dimension (batch_size, n_features) or nested tuples of tensors. lengths: A `Tensor` of dimension batch_size, containing lengths for each sequence in the batch
(input_seq, lengths)
| 267 | |
| 268 | |
| 269 | def _reverse_seq(input_seq, lengths): |
| 270 | """Reverse a list of Tensors up to specified lengths. |
| 271 | |
| 272 | Args: |
| 273 | input_seq: Sequence of seq_len tensors of dimension (batch_size, n_features) |
| 274 | or nested tuples of tensors. |
| 275 | lengths: A `Tensor` of dimension batch_size, containing lengths for each |
| 276 | sequence in the batch. If "None" is specified, simply reverses |
| 277 | the list. |
| 278 | |
| 279 | Returns: |
| 280 | time-reversed sequence |
| 281 | """ |
| 282 | if lengths is None: |
| 283 | return list(reversed(input_seq)) |
| 284 | |
| 285 | flat_input_seq = tuple(nest.flatten(input_) for input_ in input_seq) |
| 286 | |
| 287 | flat_results = [[] for _ in range(len(input_seq))] |
| 288 | for sequence in zip(*flat_input_seq): |
| 289 | input_shape = tensor_shape.unknown_shape( |
| 290 | ndims=sequence[0].get_shape().ndims) |
| 291 | for input_ in sequence: |
| 292 | input_shape.merge_with(input_.get_shape()) |
| 293 | input_.set_shape(input_shape) |
| 294 | |
| 295 | # Join into (time, batch_size, depth) |
| 296 | s_joined = array_ops.stack(sequence) |
| 297 | |
| 298 | # Reverse along dimension 0 |
| 299 | s_reversed = array_ops.reverse_sequence(s_joined, lengths, 0, 1) |
| 300 | # Split again into list |
| 301 | result = array_ops.unstack(s_reversed) |
| 302 | for r, flat_result in zip(result, flat_results): |
| 303 | r.set_shape(input_shape) |
| 304 | flat_result.append(r) |
| 305 | |
| 306 | results = [nest.pack_sequence_as(structure=input_, flat_sequence=flat_result) |
| 307 | for input_, flat_result in zip(input_seq, flat_results)] |
| 308 | return results |
| 309 | |
| 310 | |
| 311 | def bidirectional_dynamic_rnn(cell_fw, cell_bw, inputs, sequence_length=None, |
no test coverage detected