Perform dynamic decoding with `decoder`. Calls initialize() once and step() repeatedly on the Decoder object. Args: decoder: A `Decoder` instance. output_time_major: Python boolean. Default: `False` (batch major). If `True`, outputs are returned as time major tensors (this mode
(decoder,
output_time_major=False,
impute_finished=False,
maximum_iterations=None,
parallel_iterations=32,
swap_memory=False,
scope=None,
**kwargs)
| 280 | |
| 281 | |
| 282 | def dynamic_decode(decoder, |
| 283 | output_time_major=False, |
| 284 | impute_finished=False, |
| 285 | maximum_iterations=None, |
| 286 | parallel_iterations=32, |
| 287 | swap_memory=False, |
| 288 | scope=None, |
| 289 | **kwargs): |
| 290 | """Perform dynamic decoding with `decoder`. |
| 291 | |
| 292 | Calls initialize() once and step() repeatedly on the Decoder object. |
| 293 | |
| 294 | Args: |
| 295 | decoder: A `Decoder` instance. |
| 296 | output_time_major: Python boolean. Default: `False` (batch major). If |
| 297 | `True`, outputs are returned as time major tensors (this mode is faster). |
| 298 | Otherwise, outputs are returned as batch major tensors (this adds extra |
| 299 | time to the computation). |
| 300 | impute_finished: Python boolean. If `True`, then states for batch |
| 301 | entries which are marked as finished get copied through and the |
| 302 | corresponding outputs get zeroed out. This causes some slowdown at |
| 303 | each time step, but ensures that the final state and outputs have |
| 304 | the correct values and that backprop ignores time steps that were |
| 305 | marked as finished. |
| 306 | maximum_iterations: `int32` scalar, maximum allowed number of decoding |
| 307 | steps. Default is `None` (decode until the decoder is fully done). |
| 308 | parallel_iterations: Argument passed to `tf.while_loop`. |
| 309 | swap_memory: Argument passed to `tf.while_loop`. |
| 310 | scope: Optional variable scope to use. |
| 311 | **kwargs: dict, other keyword arguments for dynamic_decode. It might contain |
| 312 | arguments for `BaseDecoder` to initialize, which takes all tensor inputs |
| 313 | during call(). |
| 314 | |
| 315 | Returns: |
| 316 | `(final_outputs, final_state, final_sequence_lengths)`. |
| 317 | |
| 318 | Raises: |
| 319 | TypeError: if `decoder` is not an instance of `Decoder`. |
| 320 | ValueError: if `maximum_iterations` is provided but is not a scalar. |
| 321 | """ |
| 322 | if not isinstance(decoder, (Decoder, BaseDecoder)): |
| 323 | raise TypeError("Expected decoder to be type Decoder, but saw: %s" % |
| 324 | type(decoder)) |
| 325 | |
| 326 | with variable_scope.variable_scope(scope, "decoder") as varscope: |
| 327 | # Determine context types. |
| 328 | ctxt = ops.get_default_graph()._get_control_flow_context() # pylint: disable=protected-access |
| 329 | is_xla = control_flow_util.GetContainingXLAContext(ctxt) is not None |
| 330 | in_while_loop = ( |
| 331 | control_flow_util.GetContainingWhileContext(ctxt) is not None) |
| 332 | # Properly cache variable values inside the while_loop. |
| 333 | # Don't set a caching device when running in a loop, since it is possible |
| 334 | # that train steps could be wrapped in a tf.while_loop. In that scenario |
| 335 | # caching prevents forward computations in loop iterations from re-reading |
| 336 | # the updated weights. |
| 337 | if not context.executing_eagerly() and not in_while_loop: |
| 338 | if varscope.caching_device is None: |
| 339 | varscope.set_caching_device(lambda op: op.device) |
no test coverage detected