Python tensorflow.python.ops.rnn_cell_impl.LSTMStateTuple() Examples
The following are 30
code examples of tensorflow.python.ops.rnn_cell_impl.LSTMStateTuple().
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: convrnn.py From audio-super-res with MIT License | 6 votes |
def call(self, inputs, state, scope=None): cell, hidden = state new_hidden = _conv([inputs, hidden], self._kernel_shape, 4*self._output_channels, self._use_bias) gates = array_ops.split(value=new_hidden, num_or_size_splits=4, axis=self._conv_ndims+1) input_gate, new_input, forget_gate, output_gate = gates new_cell = math_ops.sigmoid(forget_gate + self._forget_bias) * cell new_cell += math_ops.sigmoid(input_gate) * math_ops.tanh(new_input) output = math_ops.tanh(new_cell) * math_ops.sigmoid(output_gate) if self._skip_connection: output = array_ops.concat([output, inputs], axis=-1) new_state = rnn_cell_impl.LSTMStateTuple(new_cell, output) return output, new_state
Example #2
Source File: convrnn.py From audio-super-res with MIT License | 6 votes |
def __call__(self, inputs, state, scope=None): """Long short-term memory cell (LSTM).""" with tf.variable_scope(scope or type(self).__name__): # "BasicLSTMCell" # Parameters of gates are concatenated into one multiply for efficiency. if self._state_is_tuple: c, h = state else: c, h = tf.split(axis=3, num_or_size_splits=2, value=state) concat = _conv_linear([inputs, h], self.filter_size, self.num_features * 4, True) # i = input_gate, j = new_input, f = forget_gate, o = output_gate i, j, f, o = tf.split(axis=3, num_or_size_splits=4, value=concat) new_c = (c * tf.nn.sigmoid(f + self._forget_bias) + tf.nn.sigmoid(i) * self._activation(j)) new_h = self._activation(new_c) * tf.nn.sigmoid(o) if self._state_is_tuple: new_state = LSTMStateTuple(new_c, new_h) else: new_state = tf.concat(axis=3, values=[new_c, new_h]) return new_h, new_state
Example #3
Source File: my_attention_decoder_fn.py From SentenceFunction with Apache License 2.0 | 6 votes |
def _init_attention(encoder_state): """Initialize attention. Handling both LSTM and GRU. Args: encoder_state: The encoded state to initialize the `dynamic_rnn_decoder`. Returns: attn: initial zero attention vector. """ # Multi- vs single-layer # TODO(thangluong): is this the best way to check? if isinstance(encoder_state, tuple): top_state = encoder_state[-1] else: top_state = encoder_state # LSTM vs GRU if isinstance(top_state, rnn_cell_impl.LSTMStateTuple): attn = array_ops.zeros_like(top_state.h) else: attn = array_ops.zeros_like(top_state) return attn
Example #4
Source File: rnn_cell.py From lambda-packs with MIT License | 6 votes |
def call(self, inputs, state): """LSTM cell with layer normalization and recurrent dropout.""" c, h = state args = array_ops.concat([inputs, h], 1) concat = self._linear(args) i, j, f, o = array_ops.split(value=concat, num_or_size_splits=4, axis=1) if self._layer_norm: i = self._norm(i, "input") j = self._norm(j, "transform") f = self._norm(f, "forget") o = self._norm(o, "output") g = self._activation(j) if (not isinstance(self._keep_prob, float)) or self._keep_prob < 1: g = nn_ops.dropout(g, self._keep_prob, seed=self._seed) new_c = (c * math_ops.sigmoid(f + self._forget_bias) + math_ops.sigmoid(i) * g) if self._layer_norm: new_c = self._norm(new_c, "state") new_h = self._activation(new_c) * math_ops.sigmoid(o) new_state = rnn_cell_impl.LSTMStateTuple(new_c, new_h) return new_h, new_state
Example #5
Source File: attention_decoder.py From TransDG with MIT License | 6 votes |
def _init_attention(encoder_state): """Initialize attention. Handling both LSTM and GRU. Args: encoder_state: The encoded state to initialize the `dynamic_rnn_decoder`. Returns: attn: initial zero attention vector. """ # Multi- vs single-layer if isinstance(encoder_state, tuple): top_state = encoder_state[-1] else: top_state = encoder_state # LSTM vs GRU if isinstance(top_state, rnn_cell_impl.LSTMStateTuple): attn = array_ops.zeros_like(top_state.h) else: attn = array_ops.zeros_like(top_state) return attn
Example #6
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 #7
Source File: attention_decoder.py From ccm with Apache License 2.0 | 6 votes |
def _init_attention(encoder_state): """Initialize attention. Handling both LSTM and GRU. Args: encoder_state: The encoded state to initialize the `dynamic_rnn_decoder`. Returns: attn: initial zero attention vector. """ # Multi- vs single-layer # TODO(thangluong): is this the best way to check? if isinstance(encoder_state, tuple): top_state = encoder_state[-1] else: top_state = encoder_state # LSTM vs GRU if isinstance(top_state, rnn_cell_impl.LSTMStateTuple): attn = array_ops.zeros_like(top_state.h) else: attn = array_ops.zeros_like(top_state) return attn
Example #8
Source File: rnn_cell.py From Multiview2Novelview with MIT License | 6 votes |
def __init__(self, num_units, num_proj=None, use_biases=False, reuse=None): """Initialize the parameters for a NAS cell. Args: num_units: int, The number of units in the NAS cell num_proj: (optional) int, The output dimensionality for the projection matrices. If None, no projection is performed. use_biases: (optional) bool, If True then use biases within the cell. This is False by default. reuse: (optional) Python boolean describing whether to reuse variables in an existing scope. If not `True`, and the existing scope already has the given variables, an error is raised. """ super(NASCell, self).__init__(_reuse=reuse) self._num_units = num_units self._num_proj = num_proj self._use_biases = use_biases self._reuse = reuse if num_proj is not None: self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_proj) self._output_size = num_proj else: self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_units) self._output_size = num_units
Example #9
Source File: attention_decoder.py From ccm with Apache License 2.0 | 6 votes |
def _init_attention(encoder_state): """Initialize attention. Handling both LSTM and GRU. Args: encoder_state: The encoded state to initialize the `dynamic_rnn_decoder`. Returns: attn: initial zero attention vector. """ # Multi- vs single-layer # TODO(thangluong): is this the best way to check? if isinstance(encoder_state, tuple): top_state = encoder_state[-1] else: top_state = encoder_state # LSTM vs GRU if isinstance(top_state, rnn_cell_impl.LSTMStateTuple): attn = array_ops.zeros_like(top_state.h) else: attn = array_ops.zeros_like(top_state) return attn
Example #10
Source File: attention_decoder.py From ccm with Apache License 2.0 | 6 votes |
def _init_attention(encoder_state): """Initialize attention. Handling both LSTM and GRU. Args: encoder_state: The encoded state to initialize the `dynamic_rnn_decoder`. Returns: attn: initial zero attention vector. """ # Multi- vs single-layer # TODO(thangluong): is this the best way to check? if isinstance(encoder_state, tuple): top_state = encoder_state[-1] else: top_state = encoder_state # LSTM vs GRU if isinstance(top_state, rnn_cell_impl.LSTMStateTuple): attn = array_ops.zeros_like(top_state.h) else: attn = array_ops.zeros_like(top_state) return attn
Example #11
Source File: flstm.py From OpenSeq2Seq with Apache License 2.0 | 5 votes |
def __init__(self, num_units, fact_size, initializer=None, num_proj=None, forget_bias=1.0, activation=math_ops.tanh, reuse=None): """Initialize the parameters of G-LSTM cell. Args: num_units: int, The number of units in the G-LSTM cell initializer: (optional) The initializer to use for the weight and projection matrices. num_proj: (optional) int, The output dimensionality for the projection matrices. If None, no projection is performed. forget_bias: Biases of the forget gate are initialized by default to 1 in order to reduce the scale of forgetting at the beginning of the training. activation: Activation function of the inner states. reuse: (optional) Python boolean describing whether to reuse variables in an existing scope. If not `True`, and the existing scope already has the given variables, an error is raised. Raises: ValueError: If `num_units` or `num_proj` is not divisible by `number_of_groups`. """ super(FLSTMCell, self).__init__(_reuse=reuse) self._num_units = num_units self._initializer = initializer self._fact_size = fact_size self._forget_bias = forget_bias self._activation = activation self._num_proj = num_proj if num_proj: self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_proj) self._output_size = num_proj else: self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_units) self._output_size = num_units self._linear1 = None self._linear2 = None self._linear3 = None
Example #12
Source File: bridges.py From NJUNMT-tf with Apache License 2.0 | 5 votes |
def _final_state(x, direction): """ Acquires final states. Args: x: A Tensor/LSTMStateTuple or a dictionary of Tensors/LSTMStateTuples. direction: The key for `x` if `x` is a dictionary. Returns: A Tensor or a LSTMStateTuple, according to x. Raises: ValueError: if the type of x is not mentioned above, or if `direction` is not a valid key of `x`, when `x` is a dictionary. """ if isinstance(x, tf.Tensor) or isinstance(x, rnn_cell_impl.LSTMStateTuple): return x elif isinstance(x, dict): try: ret = x[direction] except KeyError: raise ValueError( "Unrecognized type of direction: {}".format(direction)) return ret else: raise ValueError( "Unrecognized type of direction: {} " "or unknow type of final_states: {}".format(direction, type(x)))
Example #13
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 #14
Source File: rnn_dropout.py From GtS with MIT License | 5 votes |
def _default_dropout_state_filter_visitor(substate): if isinstance(substate, LSTMStateTuple): # Do not perform dropout on the memory state. return LSTMStateTuple(c=False, h=True) elif isinstance(substate, tensor_array_ops.TensorArray): return False return True
Example #15
Source File: zoneout.py From OpenSeq2Seq with Apache License 2.0 | 5 votes |
def __call__(self, inputs, state, scope=None): if isinstance(self.state_size, tuple) != isinstance(self._zoneout_prob, tuple): raise TypeError("Subdivided states need subdivided zoneouts.") if isinstance(self.state_size, tuple) and len(tuple(self.state_size) ) != len(tuple(self._zoneout_prob)): raise ValueError("State and zoneout need equally many parts.") output, new_state = self._cell(inputs, state, scope) if isinstance(self.state_size, tuple): if self._is_training: new_state = tuple( (1 - state_part_zoneout_prob) * dropout( new_state_part - state_part, (1 - state_part_zoneout_prob), seed=self._seed ) + state_part for new_state_part, state_part, state_part_zoneout_prob in zip(new_state, state, self._zoneout_prob) ) else: new_state = tuple( state_part_zoneout_prob * state_part + (1 - state_part_zoneout_prob) * new_state_part for new_state_part, state_part, state_part_zoneout_prob in zip(new_state, state, self._zoneout_prob) ) new_state = rnn_cell_impl.LSTMStateTuple(new_state[0], new_state[1]) else: raise ValueError("Only states that are tuples are supported") return output, new_state
Example #16
Source File: rnn_ops.py From video_prediction with MIT License | 5 votes |
def call(self, inputs, state): """2D Convolutional LSTM cell with (optional) normalization and recurrent dropout.""" c, h = state tile_concat = isinstance(inputs, (list, tuple)) if tile_concat: inputs, inputs_non_spatial = inputs args = array_ops.concat([inputs, h], -1) concat = self._conv2d(args) if tile_concat: concat = concat + self._dense(inputs_non_spatial)[:, None, None, :] if self._normalizer_fn and not self._separate_norms: concat = self._norm(concat, "input_transform_forget_output") i, j, f, o = array_ops.split(value=concat, num_or_size_splits=4, axis=-1) if self._normalizer_fn and self._separate_norms: i = self._norm(i, "input") j = self._norm(j, "transform") f = self._norm(f, "forget") o = self._norm(o, "output") g = self._activation_fn(j) if (not isinstance(self._keep_prob, float)) or self._keep_prob < 1: g = nn_ops.dropout(g, self._keep_prob, seed=self._seed) new_c = (c * math_ops.sigmoid(f + self._forget_bias) + math_ops.sigmoid(i) * g) if self._normalizer_fn: new_c = self._norm(new_c, "state") new_h = self._activation_fn(new_c) * math_ops.sigmoid(o) if self._skip_connection: new_h = array_ops.concat([new_h, inputs], axis=-1) new_state = rnn_cell_impl.LSTMStateTuple(new_c, new_h) return new_h, new_state
Example #17
Source File: flstm.py From OpenSeq2Seq with Apache License 2.0 | 5 votes |
def call(self, inputs, state): """ """ (c_prev, m_prev) = state self._batch_size = inputs.shape[0].value or array_ops.shape(inputs)[0] scope = vs.get_variable_scope() with vs.variable_scope(scope, initializer=self._initializer): x = array_ops.concat([inputs, m_prev], axis=1) with vs.variable_scope("first_gemm"): if self._linear1 is None: # no bias for bottleneck self._linear1 = _Linear(x, self._fact_size, False) R_fact = self._linear1(x) with vs.variable_scope("second_gemm"): if self._linear2 is None: self._linear2 = _Linear(R_fact, 4*self._num_units, True) R = self._linear2(R_fact) i, j, f, o = array_ops.split(R, 4, 1) c = (math_ops.sigmoid(f + self._forget_bias) * c_prev + math_ops.sigmoid(i) * math_ops.tanh(j)) m = math_ops.sigmoid(o) * self._activation(c) if self._num_proj is not None: with vs.variable_scope("projection"): if self._linear3 is None: self._linear3 = _Linear(m, self._num_proj, False) m = self._linear3(m) new_state = rnn_cell_impl.LSTMStateTuple(c, m) return m, new_state
Example #18
Source File: basic_rnn_cell.py From LSTMCell with MIT License | 5 votes |
def state_size(self): if self._highway_state_gate: return rnn_cell_impl.LSTMStateTuple(self._num_units, self._num_units) else: return self._num_units
Example #19
Source File: rnn.py From PointerSQL with MIT License | 5 votes |
def f_apply_multirnn_lstm_state(state1, state2, f): """ Given two multirnn lstm states, merge them into a new state of the same shape, merged by concatation and then projection Args: state1: the first state to mergem, of shape (s1, s2, s3, ...), each s is of shape LSTMStateTuple(c,h), h,c are of shape (batch_size, hidden_size) state2: the second state to merge, shape same as the first w: the projection weight, of shape (hidden_size * 2, hidden_size) b: the projection bias, of shape (hidden_size,) Returns: the merged states """ new_state = [] for i in range(len(state1)): new_state.append(f_apply_lstm_state(state1[i], state2[i], f)) return tuple(new_state)
Example #20
Source File: rnn.py From PointerSQL with MIT License | 5 votes |
def f_apply_lstm_state(state1, state2, f): """ merge two lstm states into one of the same shape as either or them, merged by concatation and then projection Args: state1, state2: two states to be merged, of shape LSTMStateTuple(c,h), h,c are of shape (batch_size, hidden_size) w: the projection weight, of shape (hidden_size * 2, hidden_size) b: the projection bias, of shape (hidden_size,) Returns: the merged state, of shape (batch_size, hidden_size) """ return rnn_cell_impl.LSTMStateTuple(f(state1.c, state2.c), f(state1.h, state2.h))
Example #21
Source File: rnn.py From PointerSQL with MIT License | 5 votes |
def merge_multirnn_lstm_state(states, w, b): """ Given two multirnn lstm states, merge them into a new state of the same shape, merged by concatation and then projection Args: state1: the first state to mergem, of shape (s1, s2, s3, ...), each s is of shape LSTMStateTuple(c,h), h,c are of shape (batch_size, hidden_size) state2: the second state to merge, shape same as the first w: the projection weight, of shape (hidden_size * 2, hidden_size) b: the projection bias, of shape (hidden_size,) Returns: the merged states """ new_state = [] for i in range(len(states[0])): new_state.append(merge_lstm_states([s[i] for s in states], w, b)) return tuple(new_state)
Example #22
Source File: rnn.py From PointerSQL with MIT License | 5 votes |
def merge_lstm_states(states, w, b): """ merge two lstm states into one of the same shape as either or them, merged by concatation and then projection Args: state1, state2: two states to be merged, of shape LSTMStateTuple(c,h), h,c are of shape (batch_size, hidden_size) w: the projection weight, of shape (hidden_size * k, hidden_size) b: the projection bias, of shape (hidden_size,) Returns: the merged state, of shape (batch_size, hidden_size) """ new_c = tf.add(tf.matmul(tf.concat([x.c for x in states], -1), w), b) new_h = tf.add(tf.matmul(tf.concat([x.h for x in states], -1), w), b) return rnn_cell_impl.LSTMStateTuple(new_c, new_h)
Example #23
Source File: hypernets_cell.py From LSTMCell with MIT License | 5 votes |
def state_size(self): full_num_units = self._num_units + self._num_units_hyper return rnn_cell_impl.LSTMStateTuple(full_num_units, full_num_units)
Example #24
Source File: basic_lstm_cell.py From LSTMCell with MIT License | 5 votes |
def state_size(self): return rnn_cell_impl.LSTMStateTuple(self._num_units, self._num_units)
Example #25
Source File: convrnn.py From audio-super-res with MIT License | 5 votes |
def state_size(self): return (LSTMStateTuple(self._num_units, self._num_units) if self._state_is_tuple else 2 * self._num_units)
Example #26
Source File: flstm.py From f-lm with MIT License | 5 votes |
def __init__(self, num_units, fact_size, initializer=None, num_proj=None, forget_bias=1.0, activation=math_ops.tanh, reuse=None): """Initialize the parameters of G-LSTM cell. Args: num_units: int, The number of units in the G-LSTM cell initializer: (optional) The initializer to use for the weight and projection matrices. num_proj: (optional) int, The output dimensionality for the projection matrices. If None, no projection is performed. number_of_groups: (optional) int, number of groups to use. If `number_of_groups` is 1, then it should be equivalent to LSTM cell forget_bias: Biases of the forget gate are initialized by default to 1 in order to reduce the scale of forgetting at the beginning of the training. activation: Activation function of the inner states. reuse: (optional) Python boolean describing whether to reuse variables in an existing scope. If not `True`, and the existing scope already has the given variables, an error is raised. Raises: ValueError: If `num_units` or `num_proj` is not divisible by `number_of_groups`. """ super(FLSTMCell, self).__init__(_reuse=reuse) self._num_units = num_units self._initializer = initializer self._fact_size = fact_size self._forget_bias = forget_bias self._activation = activation self._num_proj = num_proj if num_proj: self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_proj) self._output_size = num_proj else: self._state_size = rnn_cell_impl.LSTMStateTuple(num_units, num_units) self._output_size = num_units self._linear1 = None self._linear2 = None self._linear3 = None
Example #27
Source File: flstm.py From f-lm with MIT License | 5 votes |
def call(self, inputs, state): """ """ (c_prev, m_prev) = state self._batch_size = inputs.shape[0].value or array_ops.shape(inputs)[0] scope = vs.get_variable_scope() with vs.variable_scope(scope, initializer=self._initializer): x = array_ops.concat([inputs, m_prev], axis=1) with vs.variable_scope("first_gemm"): if self._linear1 is None: self._linear1 = _Linear(x, self._fact_size, False) # no bias for bottleneck R_fact = self._linear1(x) with vs.variable_scope("second_gemm"): if self._linear2 is None: self._linear2 = _Linear(R_fact, 4*self._num_units, True) R = self._linear2(R_fact) i, j, f, o = array_ops.split(R, 4, 1) c = (math_ops.sigmoid(f + self._forget_bias) * c_prev + math_ops.sigmoid(i) * math_ops.tanh(j)) m = math_ops.sigmoid(o) * self._activation(c) if self._num_proj is not None: with vs.variable_scope("projection"): if self._linear3 is None: self._linear3 = _Linear(m, self._num_proj, False) m = self._linear3(m) new_state = rnn_cell_impl.LSTMStateTuple(c, m) return m, new_state
Example #28
Source File: rnn_cell.py From Multiview2Novelview with MIT License | 5 votes |
def state_size(self): return rnn_cell_impl.LSTMStateTuple(self._num_units, self._num_units)
Example #29
Source File: rnn_cell.py From Multiview2Novelview with MIT License | 5 votes |
def call(self, inputs, state): """LSTM cell with layer normalization and recurrent dropout.""" c, h = state args = array_ops.concat([inputs, h], 1) concat = self._linear(args) dtype = args.dtype i, j, f, o = array_ops.split(value=concat, num_or_size_splits=4, axis=1) if self._layer_norm: i = self._norm(i, "input", dtype=dtype) j = self._norm(j, "transform", dtype=dtype) f = self._norm(f, "forget", dtype=dtype) o = self._norm(o, "output", dtype=dtype) g = self._activation(j) if (not isinstance(self._keep_prob, float)) or self._keep_prob < 1: g = nn_ops.dropout(g, self._keep_prob, seed=self._seed) new_c = (c * math_ops.sigmoid(f + self._forget_bias) + math_ops.sigmoid(i) * g) if self._layer_norm: new_c = self._norm(new_c, "state", dtype=dtype) new_h = self._activation(new_c) * math_ops.sigmoid(o) new_state = rnn_cell_impl.LSTMStateTuple(new_c, new_h) return new_h, new_state
Example #30
Source File: rnn_cell.py From Multiview2Novelview with MIT License | 5 votes |
def state_size(self): return rnn_cell_impl.LSTMStateTuple(self._num_units, self._num_units)