Python tensorflow.python.ops.rnn_cell_impl._zero_state_tensors() Examples
The following are 30
code examples of tensorflow.python.ops.rnn_cell_impl._zero_state_tensors().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.python.ops.rnn_cell_impl
, or try the search function
.
Example #1
Source File: attention_wrapper.py From lambda-packs with MIT License | 6 votes |
def initial_alignments(self, batch_size, dtype): """Creates the initial alignment values for the `AttentionWrapper` class. This is important for AttentionMechanisms that use the previous alignment to calculate the alignment at the next time step (e.g. monotonic attention). The default behavior is to return a tensor of all zeros. Args: batch_size: `int32` scalar, the batch_size. dtype: The `dtype`. Returns: A `dtype` tensor shaped `[batch_size, alignments_size]` (`alignments_size` is the values' `max_time`). """ max_time = self._alignments_size return _zero_state_tensors(max_time, batch_size, dtype)
Example #2
Source File: bridges.py From NJUNMT-tf with Apache License 2.0 | 6 votes |
def _create(self, encoder_output, decoder_state_size, **kwargs): """ Creates decoder's initial RNN states according to `decoder_state_size`. If `decoder_state_size` is int/LSTMStateTuple(int, int), return Tensor with shape [batch_size, int] or LSTMStateTuple([batch_size, int], [batch_size, int]). If `decoder_state_size` is a tuple of int/LSTMStateTupe, return a tuple whose elements' structure match the `decoder_state_size` respectively. Args: encoder_output: An instance of `collections.namedtuple` from `Encoder.encode()`. decoder_state_size: RNN decoder state size. **kwargs: Returns: The decoder states with the structure determined by `decoder_state_size`. """ batch_size = tf.shape(encoder_output.attention_length)[0] return rnn_cell_impl._zero_state_tensors( decoder_state_size, batch_size, tf.float32)
Example #3
Source File: attention_wrapper_mod.py From NQG_ASs2s with MIT License | 6 votes |
def initial_alignments(self, batch_size, dtype): """Creates the initial alignment values for the `AttentionWrapper` class. This is important for AttentionMechanisms that use the previous alignment to calculate the alignment at the next time step (e.g. monotonic attention). The default behavior is to return a tensor of all zeros. Args: batch_size: `int32` scalar, the batch_size. dtype: The `dtype`. Returns: A `dtype` tensor shaped `[batch_size, alignments_size]` (`alignments_size` is the values' `max_time`). """ max_time = self._alignments_size return _zero_state_tensors(max_time, batch_size, dtype)
Example #4
Source File: attention_wrapper.py From QGforQA with MIT License | 6 votes |
def initial_alignments(self, batch_size, dtype): """Creates the initial alignment values for the `AttentionWrapper` class. This is important for AttentionMechanisms that use the previous alignment to calculate the alignment at the next time step (e.g. monotonic attention). The default behavior is to return a tensor of all zeros. Args: batch_size: `int32` scalar, the batch_size. dtype: The `dtype`. Returns: A `dtype` tensor shaped `[batch_size, alignments_size]` (`alignments_size` is the values' `max_time`). """ max_time = self._alignments_size return _zero_state_tensors(max_time, batch_size, dtype)
Example #5
Source File: attention_wrapper.py From tf-var-attention with MIT License | 6 votes |
def initial_alignments(self, batch_size, dtype): """Creates the initial alignment values for the `AttentionWrapper` class. This is important for AttentionMechanisms that use the previous alignment to calculate the alignment at the next time step (e.g. monotonic attention). The default behavior is to return a tensor of all zeros. Args: batch_size: `int32` scalar, the batch_size. dtype: The `dtype`. Returns: A `dtype` tensor shaped `[batch_size, alignments_size]` (`alignments_size` is the values' `max_time`). """ max_time = self._alignments_size return _zero_state_tensors(max_time, batch_size, dtype)
Example #6
Source File: attention_wrapper.py From OpenSeq2Seq with Apache License 2.0 | 6 votes |
def initial_alignments(self, batch_size, dtype): """Creates the initial alignment values for the `AttentionWrapper` class. This is important for AttentionMechanisms that use the previous alignment to calculate the alignment at the next time step (e.g. monotonic attention). The default behavior is to return a tensor of all zeros. Args: batch_size: `int32` scalar, the batch_size. dtype: The `dtype`. Returns: A `dtype` tensor shaped `[batch_size, alignments_size]` (`alignments_size` is the values' `max_time`). """ max_time = self._alignments_size return _zero_state_tensors(max_time, batch_size, dtype)
Example #7
Source File: attention_wrapper.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def initial_alignments(self, batch_size, dtype): """Creates the initial alignment values for the `AttentionWrapper` class. This is important for AttentionMechanisms that use the previous alignment to calculate the alignment at the next time step (e.g. monotonic attention). The default behavior is to return a tensor of all zeros. Args: batch_size: `int32` scalar, the batch_size. dtype: The `dtype`. Returns: A `dtype` tensor shaped `[batch_size, alignments_size]` (`alignments_size` is the values' `max_time`). """ max_time = self._alignments_size return _zero_state_tensors(max_time, batch_size, dtype)
Example #8
Source File: attention_wrapper.py From CommonSenseMultiHopQA with MIT License | 6 votes |
def initial_alignments(self, batch_size, dtype): """Creates the initial alignment values for the `AttentionWrapper` class. This is important for AttentionMechanisms that use the previous alignment to calculate the alignment at the next time step (e.g. monotonic attention). The default behavior is to return a tensor of all zeros. Args: batch_size: `int32` scalar, the batch_size. dtype: The `dtype`. Returns: A `dtype` tensor shaped `[batch_size, alignments_size]` (`alignments_size` is the values' `max_time`). """ max_time = self._alignments_size return _zero_state_tensors(max_time, batch_size, dtype)
Example #9
Source File: attention_wrapper.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def initial_alignments(self, batch_size, dtype): """Creates the initial alignment values for the `AttentionWrapper` class. This is important for AttentionMechanisms that use the previous alignment to calculate the alignment at the next time step (e.g. monotonic attention). The default behavior is to return a tensor of all zeros. Args: batch_size: `int32` scalar, the batch_size. dtype: The `dtype`. Returns: A `dtype` tensor shaped `[batch_size, alignments_size]` (`alignments_size` is the values' `max_time`). """ max_time = self._alignments_size return _zero_state_tensors(max_time, batch_size, dtype)
Example #10
Source File: attention_wrapper.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def initial_alignments(self, batch_size, dtype): """Creates the initial alignment values for the `AttentionWrapper` class. This is important for AttentionMechanisms that use the previous alignment to calculate the alignment at the next time step (e.g. monotonic attention). The default behavior is to return a tensor of all zeros. Args: batch_size: `int32` scalar, the batch_size. dtype: The `dtype`. Returns: A `dtype` tensor shaped `[batch_size, alignments_size]` (`alignments_size` is the values' `max_time`). """ max_time = self._alignments_size return _zero_state_tensors(max_time, batch_size, dtype)
Example #11
Source File: copy_attention_wrapper.py From question-generation with MIT License | 6 votes |
def initial_alignments(self, batch_size, dtype): """Creates the initial alignment values for the `AttentionWrapper` class. This is important for AttentionMechanisms that use the previous alignment to calculate the alignment at the next time step (e.g. monotonic attention). The default behavior is to return a tensor of all zeros. Args: batch_size: `int32` scalar, the batch_size. dtype: The `dtype`. Returns: A `dtype` tensor shaped `[batch_size, alignments_size]` (`alignments_size` is the values' `max_time`). """ max_time = self._alignments_size return _zero_state_tensors(max_time, batch_size, dtype)
Example #12
Source File: attention_wrapper.py From CommonSenseMultiHopQA with MIT License | 5 votes |
def zero_state(self, batch_size, dtype): with ops.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): if self._initial_cell_state is not None: cell_state = self._initial_cell_state else: cell_state = self._cell.zero_state(batch_size, dtype) error_message = ( "When calling zero_state of AttentionWrapper %s: " % self._base_name + "Non-matching batch sizes between the memory " "(encoder output) and the requested batch size. Are you using " "the BeamSearchDecoder? If so, make sure your encoder output has " "been tiled to beam_width via tf.contrib.seq2seq.tile_batch, and " "the batch_size= argument passed to zero_state is " "batch_size * beam_width.") with ops.control_dependencies( self._batch_size_checks(batch_size, error_message)): cell_state = nest.map_structure( lambda s: array_ops.identity(s, name="checked_cell_state"), cell_state) return AttentionWrapperState( cell_state=cell_state, time=array_ops.zeros([], dtype=dtypes.int32), attention=_zero_state_tensors(self._attention_layer_size, batch_size, dtype), alignments=self._item_or_tuple( attention_mechanism.initial_alignments(batch_size, dtype) for attention_mechanism in self._attention_mechanisms), alignment_history=self._item_or_tuple( tensor_array_ops.TensorArray(dtype=dtype, size=0, dynamic_size=True) if self._alignment_history else () for _ in self._attention_mechanisms))
Example #13
Source File: rnn_wrappers.py From Tacotron2-Wavenet-Korean-TTS with MIT License | 5 votes |
def initial_alignments(self, batch_size, dtype): max_time = self._alignments_size return _zero_state_tensors(max_time, batch_size, dtype)
Example #14
Source File: hierarchical_ptr_wrapper.py From CommonSenseMultiHopQA with MIT License | 5 votes |
def zero_state(self, batch_size, dtype): with ops.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): if self._initial_cell_state is not None: cell_state = self._initial_cell_state else: cell_state = self._cell.zero_state(batch_size, dtype) error_message = ( "When calling zero_state of AttentionWrapper %s: " % self._base_name + "Non-matching batch sizes between the memory " "(encoder output) and the requested batch size. Are you using " "the BeamSearchDecoder? If so, make sure your encoder output has " "been tiled to beam_width via tf.contrib.seq2seq.tile_batch, and " "the batch_size= argument passed to zero_state is " "batch_size * beam_width.") with ops.control_dependencies( self._batch_size_checks(batch_size, error_message)): cell_state = nest.map_structure( lambda s: array_ops.identity(s, name="checked_cell_state"), cell_state) state = PointerWrapperState( cell_state=cell_state, time=array_ops.zeros([], dtype=dtypes.int32), attention=_zero_state_tensors(self._attention_layer_size, batch_size, dtype), alignment_history=self._item_or_tuple( tensor_array_ops.TensorArray(dtype=dtype, size=0, dynamic_size=True) if self._alignment_history else () for _ in self._attention_mechanisms), p_gen_history=self._item_or_tuple( tensor_array_ops.TensorArray(dtype=dtype, size=0, dynamic_size=True) for _ in self._attention_mechanisms), vocab_dist_history=tensor_array_ops.TensorArray( dtype=tf.float32, size=0, dynamic_size=True), copy_dist_history=tensor_array_ops.TensorArray( dtype=tf.float32, size=0, dynamic_size=True), final_dist_history=tensor_array_ops.TensorArray( dtype=tf.float32, size=0, dynamic_size=True)) return state
Example #15
Source File: dynamic_decoder_custom.py From icecaps with MIT License | 5 votes |
def _create_zero_outputs(size, dtype, batch_size): """Create a zero outputs Tensor structure.""" def _create(s, d): return _zero_state_tensors(s, batch_size, d) return nest.map_structure(_create, size, dtype)
Example #16
Source File: rnn_wrappers.py From Tacotron2-Wavenet-Korean-TTS with MIT License | 5 votes |
def initial_state(self, batch_size, dtype): state_size_ = self.state_size return _zero_state_tensors(state_size_, batch_size, dtype)
Example #17
Source File: attention_wrapper.py From QGforQA with MIT License | 5 votes |
def zero_state(self, batch_size, dtype): with ops.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): if self._initial_cell_state is not None: cell_state = self._initial_cell_state else: cell_state = self._cell.zero_state(batch_size, dtype) error_message = ( "When calling zero_state of AttentionWrapper %s: " % self._base_name + "Non-matching batch sizes between the memory " "(encoder output) and the requested batch size. Are you using " "the BeamSearchDecoder? If so, make sure your encoder output has " "been tiled to beam_width via tf.contrib.seq2seq.tile_batch, and " "the batch_size= argument passed to zero_state is " "batch_size * beam_width.") with ops.control_dependencies( self._batch_size_checks(batch_size, error_message)): cell_state = nest.map_structure( lambda s: array_ops.identity(s, name="checked_cell_state"), cell_state) return AttentionWrapperState( cell_state=cell_state, time=array_ops.zeros([], dtype=dtypes.int32), attention=_zero_state_tensors(self._attention_layer_size, batch_size, dtype), alignments=self._item_or_tuple( attention_mechanism.initial_alignments(batch_size, dtype) for attention_mechanism in self._attention_mechanisms), alignment_history=self._item_or_tuple( tensor_array_ops.TensorArray(dtype=dtype, size=0, dynamic_size=True) if self._alignment_history else () for _ in self._attention_mechanisms))
Example #18
Source File: Architecture_wrappers.py From style-token_tacotron2 with MIT License | 5 votes |
def zero_state(self, batch_size, dtype): """Return an initial (zero) state tuple for this `AttentionWrapper`. Args: batch_size: `0D` integer tensor: the batch size. dtype: The internal state data type. Returns: An `TacotronDecoderCellState` tuple containing zeroed out tensors and, possibly, empty `TensorArray` objects. Raises: ValueError: (or, possibly at runtime, InvalidArgument), if `batch_size` does not match the output size of the encoder passed to the wrapper object at initialization time. """ with ops.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): cell_state = self._cell._cell.zero_state(batch_size, dtype) error_message = ( "When calling zero_state of TacotronDecoderCell %s: " % self._base_name + "Non-matching batch sizes between the memory " "(encoder output) and the requested batch size.") with ops.control_dependencies( self._batch_size_checks(batch_size, error_message)): cell_state = nest.map_structure( lambda s: array_ops.identity(s, name="checked_cell_state"), cell_state) return TacotronDecoderCellState( cell_state=cell_state, time=array_ops.zeros([], dtype=tf.int32), attention=_zero_state_tensors(self._attention_layer_size, batch_size, dtype), alignments=self._attention_mechanism.initial_alignments(batch_size, dtype), alignment_history=tensor_array_ops.TensorArray(dtype=dtype, size=0, dynamic_size=True))
Example #19
Source File: hierarchical_ptr_wrapper.py From QGforQA with MIT License | 5 votes |
def zero_state(self, batch_size, dtype): with ops.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): if self._initial_cell_state is not None: cell_state = self._initial_cell_state else: cell_state = self._cell.zero_state(batch_size, dtype) error_message = ( "When calling zero_state of AttentionWrapper %s: " % self._base_name + "Non-matching batch sizes between the memory " "(encoder output) and the requested batch size. Are you using " "the BeamSearchDecoder? If so, make sure your encoder output has " "been tiled to beam_width via tf.contrib.seq2seq.tile_batch, and " "the batch_size= argument passed to zero_state is " "batch_size * beam_width.") with ops.control_dependencies( self._batch_size_checks(batch_size, error_message)): cell_state = nest.map_structure( lambda s: array_ops.identity(s, name="checked_cell_state"), cell_state) state = PointerWrapperState( cell_state=cell_state, time=array_ops.zeros([], dtype=dtypes.int32), attention=_zero_state_tensors(self._attention_layer_size, batch_size, dtype), alignment_history=self._item_or_tuple( tensor_array_ops.TensorArray(dtype=dtype, size=0, dynamic_size=True) if self._alignment_history else () for _ in self._attention_mechanisms), p_gen_history=self._item_or_tuple( tensor_array_ops.TensorArray(dtype=dtype, size=0, dynamic_size=True) for _ in self._attention_mechanisms), vocab_dist_history=tensor_array_ops.TensorArray( dtype=tf.float32, size=0, dynamic_size=True), copy_dist_history=tensor_array_ops.TensorArray( dtype=tf.float32, size=0, dynamic_size=True), final_dist_history=tensor_array_ops.TensorArray( dtype=tf.float32, size=0, dynamic_size=True)) return state
Example #20
Source File: Architecture_wrappers.py From gmvae_tacotron with MIT License | 5 votes |
def zero_state(self, batch_size, dtype): """Return an initial (zero) state tuple for this `AttentionWrapper`. Args: batch_size: `0D` integer tensor: the batch size. dtype: The internal state data type. Returns: An `TacotronDecoderCellState` tuple containing zeroed out tensors and, possibly, empty `TensorArray` objects. Raises: ValueError: (or, possibly at runtime, InvalidArgument), if `batch_size` does not match the output size of the encoder passed to the wrapper object at initialization time. """ with ops.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): cell_state = self._cell._cell.zero_state(batch_size, dtype) error_message = ( "When calling zero_state of TacotronDecoderCell %s: " % self._base_name + "Non-matching batch sizes between the memory " "(encoder output) and the requested batch size.") with ops.control_dependencies( self._batch_size_checks(batch_size, error_message)): cell_state = nest.map_structure( lambda s: array_ops.identity(s, name="checked_cell_state"), cell_state) return TacotronDecoderCellState( cell_state=cell_state, time=array_ops.zeros([], dtype=tf.int32), attention=_zero_state_tensors(self._attention_layer_size, batch_size, dtype), alignments=self._attention_mechanism.initial_alignments(batch_size, dtype), alignment_history=tensor_array_ops.TensorArray(dtype=dtype, size=0, dynamic_size=True), finished=tf.reshape(tf.tile([0.0], [batch_size]), [-1, 1]))
Example #21
Source File: bridges.py From NJUNMT-tf with Apache License 2.0 | 5 votes |
def _create(self, encoder_output, decoder_state_size, **kwargs): """ Creates decoder's initial RNN states according to `decoder_state_size`. Passes the final state of encoder to each layer in decoder. Args: encoder_output: An instance of `collections.namedtuple` from `Encoder.encode()`. decoder_state_size: RNN decoder state size. **kwargs: Returns: The decoder states with the structure determined by `decoder_state_size`. Raises: ValueError: if the structure of encoder RNN state does not have the same structure of decoder RNN state. """ batch_size = tf.shape(encoder_output.attention_length)[0] # of type LSTMStateTuple enc_final_state = _final_state( encoder_output.final_states, direction=self.params["direction"]) assert_state_is_compatible(rnn_cell_impl._zero_state_tensors( decoder_state_size[0], batch_size, tf.float32), enc_final_state) if nest.is_sequence(decoder_state_size): return tuple([enc_final_state for _ in decoder_state_size]) return enc_final_state
Example #22
Source File: Architecture_wrappers.py From tacotron2-mandarin-griffin-lim with MIT License | 5 votes |
def zero_state(self, batch_size, dtype): """Return an initial (zero) state tuple for this `AttentionWrapper`. Args: batch_size: `0D` integer tensor: the batch size. dtype: The internal state data type. Returns: An `TacotronDecoderCellState` tuple containing zeroed out tensors and, possibly, empty `TensorArray` objects. Raises: ValueError: (or, possibly at runtime, InvalidArgument), if `batch_size` does not match the output size of the encoder passed to the wrapper object at initialization time. """ with ops.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): cell_state = self._cell._cell.zero_state(batch_size, dtype) error_message = ( "When calling zero_state of TacotronDecoderCell %s: " % self._base_name + "Non-matching batch sizes between the memory " "(encoder output) and the requested batch size.") with ops.control_dependencies( self._batch_size_checks(batch_size, error_message)): cell_state = nest.map_structure( lambda s: array_ops.identity(s, name="checked_cell_state"), cell_state) return TacotronDecoderCellState( cell_state=cell_state, time=array_ops.zeros([], dtype=tf.int32), attention=_zero_state_tensors(self._attention_layer_size, batch_size, dtype), alignments=self._attention_mechanism.initial_alignments(batch_size, dtype), alignment_history=tensor_array_ops.TensorArray(dtype=dtype, size=0, dynamic_size=True), max_attentions=tf.zeros((batch_size, ), dtype=tf.int32))
Example #23
Source File: rnn_wrappers.py From Tacotron-Wavenet-Vocoder-Korean with MIT License | 5 votes |
def initial_state(self, batch_size, dtype): state_size_ = self.state_size return _zero_state_tensors(state_size_, batch_size, dtype)
Example #24
Source File: attention_wrapper.py From lambda-packs with MIT License | 5 votes |
def zero_state(self, batch_size, dtype): with ops.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): if self._initial_cell_state is not None: cell_state = self._initial_cell_state else: cell_state = self._cell.zero_state(batch_size, dtype) error_message = ( "When calling zero_state of AttentionWrapper %s: " % self._base_name + "Non-matching batch sizes between the memory " "(encoder output) and the requested batch size. Are you using " "the BeamSearchDecoder? If so, make sure your encoder output has " "been tiled to beam_width via tf.contrib.seq2seq.tile_batch, and " "the batch_size= argument passed to zero_state is " "batch_size * beam_width.") with ops.control_dependencies( [check_ops.assert_equal(batch_size, self._attention_mechanism.batch_size, message=error_message)]): cell_state = nest.map_structure( lambda s: array_ops.identity(s, name="checked_cell_state"), cell_state) if self._alignment_history: alignment_history = tensor_array_ops.TensorArray( dtype=dtype, size=0, dynamic_size=True) else: alignment_history = () return AttentionWrapperState( cell_state=cell_state, time=array_ops.zeros([], dtype=dtypes.int32), attention=_zero_state_tensors(self._attention_size, batch_size, dtype), alignments=self._attention_mechanism.initial_alignments( batch_size, dtype), alignment_history=alignment_history)
Example #25
Source File: Architecture_wrappers.py From vae_tacotron2 with MIT License | 5 votes |
def zero_state(self, batch_size, dtype): """Return an initial (zero) state tuple for this `AttentionWrapper`. Args: batch_size: `0D` integer tensor: the batch size. dtype: The internal state data type. Returns: An `TacotronDecoderCellState` tuple containing zeroed out tensors and, possibly, empty `TensorArray` objects. Raises: ValueError: (or, possibly at runtime, InvalidArgument), if `batch_size` does not match the output size of the encoder passed to the wrapper object at initialization time. """ with ops.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): cell_state = self._cell._cell.zero_state(batch_size, dtype) error_message = ( "When calling zero_state of TacotronDecoderCell %s: " % self._base_name + "Non-matching batch sizes between the memory " "(encoder output) and the requested batch size.") with ops.control_dependencies( self._batch_size_checks(batch_size, error_message)): cell_state = nest.map_structure( lambda s: array_ops.identity(s, name="checked_cell_state"), cell_state) return TacotronDecoderCellState( cell_state=cell_state, time=array_ops.zeros([], dtype=tf.int32), attention=_zero_state_tensors(self._attention_layer_size, batch_size, dtype), alignments=self._attention_mechanism.initial_alignments(batch_size, dtype), alignment_history=tensor_array_ops.TensorArray(dtype=dtype, size=0, dynamic_size=True), finished=tf.reshape(tf.tile([0.0], [batch_size]), [-1, 1]))
Example #26
Source File: decoder.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def _create_zero_outputs(size, dtype, batch_size): """Create a zero outputs Tensor structure.""" def _create(s, d): return _zero_state_tensors(s, batch_size, d) return tf.contrib.framework.nest.map_structure(_create, size, dtype)
Example #27
Source File: decoder.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def _create_zero_outputs(size, dtype, batch_size): """Create a zero outputs Tensor structure.""" def _create(s, d): return _zero_state_tensors(s, batch_size, d) return tf.contrib.framework.nest.map_structure(_create, size, dtype)
Example #28
Source File: decoder.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def _create_zero_outputs(size, dtype, batch_size): """Create a zero outputs Tensor structure.""" def _create(s, d): return _zero_state_tensors(s, batch_size, d) return tf.contrib.framework.nest.map_structure(_create, size, dtype)
Example #29
Source File: _rnn.py From DeepChatModels with MIT License | 5 votes |
def zero_state(self, batch_size, dtype): with tf.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): if self._initial_cell_state is not None: cell_state = self._initial_cell_state else: cell_state = self._cell.zero_state(batch_size, dtype) error_message = ( "zero_state of AttentionWrapper %s: " % self._base_name + "Non-matching batch sizes between the memory " "(encoder output) and the requested batch size.") with tf.control_dependencies( [tf.assert_equal(batch_size, self._attention_mechanism.batch_size, message=error_message)]): cell_state = nest.map_structure( lambda s: tf.identity(s, name="checked_cell_state"), cell_state) alignment_history = () _zero_state_tensors = rnn_cell_impl._zero_state_tensors return AttentionWrapperState( cell_state=cell_state, time=tf.zeros([], dtype=tf.int32), attention=_zero_state_tensors(self._attention_size, batch_size, dtype), alignments=self._attention_mechanism.initial_alignments( batch_size, dtype), alignment_history=alignment_history)
Example #30
Source File: rnn_wrappers.py From arabic-tacotron-tts with MIT License | 5 votes |
def zero_state(self, batch_size, dtype): """Return an initial (zero) state tuple for this `AttentionWrapper`. Args: batch_size: `0D` integer tensor: the batch size. dtype: The internal state data type. Returns: An `TacotronDecoderCellState` tuple containing zeroed out tensors and, possibly, empty `TensorArray` objects. Raises: ValueError: (or, possibly at runtime, InvalidArgument), if `batch_size` does not match the output size of the encoder passed to the wrapper object at initialization time. """ with ops.name_scope(type(self).__name__ + "ZeroState", values=[batch_size]): cell_state = self._cell.zero_state(batch_size, dtype) error_message = ( "When calling zero_state of TacotronDecoderCell %s: " % self._base_name + "Non-matching batch sizes between the memory " "(encoder output) and the requested batch size.") with ops.control_dependencies( self._batch_size_checks(batch_size, error_message)): cell_state = nest.map_structure( lambda s: array_ops.identity(s, name="checked_cell_state"), cell_state) return TacotronDecoderCellState( cell_state=cell_state, time=array_ops.zeros([], dtype=tf.int32), attention=rnn_cell_impl._zero_state_tensors(self._attention_layer_size, batch_size, dtype), alignments=self._attention_mechanism.initial_alignments(batch_size, dtype), alignment_history=tensor_array_ops.TensorArray(dtype=dtype, size=0, dynamic_size=True))