Return zero-filled state tensor(s). Args: batch_size: int, float, or unit Tensor representing the batch size. dtype: the data type to use for the state. Returns: If `state_size` is an int or TensorShape, then the return value is a `N-D` tensor of shape `[batch_size,
(self, batch_size, dtype)
| 309 | return self.zero_state(batch_size, dtype) |
| 310 | |
| 311 | def zero_state(self, batch_size, dtype): |
| 312 | """Return zero-filled state tensor(s). |
| 313 | |
| 314 | Args: |
| 315 | batch_size: int, float, or unit Tensor representing the batch size. |
| 316 | dtype: the data type to use for the state. |
| 317 | |
| 318 | Returns: |
| 319 | If `state_size` is an int or TensorShape, then the return value is a |
| 320 | `N-D` tensor of shape `[batch_size, state_size]` filled with zeros. |
| 321 | |
| 322 | If `state_size` is a nested list or tuple, then the return value is |
| 323 | a nested list or tuple (of the same structure) of `2-D` tensors with |
| 324 | the shapes `[batch_size, s]` for each s in `state_size`. |
| 325 | """ |
| 326 | # Try to use the last cached zero_state. This is done to avoid recreating |
| 327 | # zeros, especially when eager execution is enabled. |
| 328 | state_size = self.state_size |
| 329 | is_eager = context.executing_eagerly() |
| 330 | if is_eager and _hasattr(self, "_last_zero_state"): |
| 331 | (last_state_size, last_batch_size, last_dtype, |
| 332 | last_output) = getattr(self, "_last_zero_state") |
| 333 | if (last_batch_size == batch_size and last_dtype == dtype and |
| 334 | last_state_size == state_size): |
| 335 | return last_output |
| 336 | with ops.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): |
| 337 | output = _zero_state_tensors(state_size, batch_size, dtype) |
| 338 | if is_eager: |
| 339 | self._last_zero_state = (state_size, batch_size, dtype, output) |
| 340 | return output |
| 341 | |
| 342 | # TODO(b/134773139): Remove when contrib RNN cells implement `get_config` |
| 343 | def get_config(self): # pylint: disable=useless-super-delegation |