Python tensorflow.python.ops.rnn_cell_impl._linear() Examples
The following are 10
code examples of tensorflow.python.ops.rnn_cell_impl._linear().
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: rnn_cell.py From lambda-packs with MIT License | 6 votes |
def _attention(self, query, attn_states): conv2d = nn_ops.conv2d reduce_sum = math_ops.reduce_sum softmax = nn_ops.softmax tanh = math_ops.tanh with vs.variable_scope("attention"): k = vs.get_variable( "attn_w", [1, 1, self._attn_size, self._attn_vec_size]) v = vs.get_variable("attn_v", [self._attn_vec_size]) hidden = array_ops.reshape(attn_states, [-1, self._attn_length, 1, self._attn_size]) hidden_features = conv2d(hidden, k, [1, 1, 1, 1], "SAME") y = _linear(query, self._attn_vec_size, True) y = array_ops.reshape(y, [-1, 1, 1, self._attn_vec_size]) s = reduce_sum(v * tanh(hidden_features + y), [2, 3]) a = softmax(s) d = reduce_sum( array_ops.reshape(a, [-1, self._attn_length, 1, 1]) * hidden, [1, 2]) new_attns = array_ops.reshape(d, [-1, self._attn_size]) new_attn_states = array_ops.slice(attn_states, [0, 1, 0], [-1, -1, -1]) return new_attns, new_attn_states
Example #2
Source File: nn.py From AmusingPythonCodes with MIT License | 6 votes |
def linear(args, output_size, bias, bias_start=0.0, scope=None, squeeze=False, wd=0.0, input_keep_prob=1.0, is_train=None): if args is None or (nest.is_sequence(args) and not args): raise ValueError("`args` must be specified") if not nest.is_sequence(args): args = [args] flat_args = [flatten(arg, 1) for arg in args] if input_keep_prob < 1.0: assert is_train is not None flat_args = [tf.cond(is_train, lambda: tf.nn.dropout(arg, input_keep_prob), lambda: arg) for arg in flat_args] with tf.variable_scope(scope or 'Linear'): flat_out = _linear(flat_args, output_size, bias, bias_initializer=tf.constant_initializer(bias_start)) out = reconstruct(flat_out, args[0], 1) if squeeze: out = tf.squeeze(out, [len(args[0].get_shape().as_list())-1]) if wd: add_wd(wd) return out
Example #3
Source File: core_rnn_cell.py From lambda-packs with MIT License | 5 votes |
def call(self, inputs, state): """Run the input projection and then the cell.""" # Default scope: "InputProjectionWrapper" projected = _linear(inputs, self._num_proj, True) if self._activation: projected = self._activation(projected) return self._cell(projected, state)
Example #4
Source File: core_rnn_cell.py From lambda-packs with MIT License | 5 votes |
def call(self, inputs, state): """Run the cell and output projection on inputs, starting from state.""" output, res_state = self._cell(inputs, state) projected = _linear(output, self._output_size, True) if self._activation: projected = self._activation(projected) return projected, res_state
Example #5
Source File: rnn_cell.py From lambda-packs with MIT License | 5 votes |
def call(self, inputs, state): """Long short-term memory cell with attention (LSTMA).""" if self._state_is_tuple: state, attns, attn_states = state else: states = state state = array_ops.slice(states, [0, 0], [-1, self._cell.state_size]) attns = array_ops.slice( states, [0, self._cell.state_size], [-1, self._attn_size]) attn_states = array_ops.slice( states, [0, self._cell.state_size + self._attn_size], [-1, self._attn_size * self._attn_length]) attn_states = array_ops.reshape(attn_states, [-1, self._attn_length, self._attn_size]) input_size = self._input_size if input_size is None: input_size = inputs.get_shape().as_list()[1] inputs = _linear([inputs, attns], input_size, True) lstm_output, new_state = self._cell(inputs, state) if self._state_is_tuple: new_state_cat = array_ops.concat(nest.flatten(new_state), 1) else: new_state_cat = new_state new_attns, new_attn_states = self._attention(new_state_cat, attn_states) with vs.variable_scope("attn_output_projection"): output = _linear([lstm_output, new_attns], self._attn_size, True) new_attn_states = array_ops.concat( [new_attn_states, array_ops.expand_dims(output, 1)], 1) new_attn_states = array_ops.reshape( new_attn_states, [-1, self._attn_length * self._attn_size]) new_state = (new_state, new_attns, new_attn_states) if not self._state_is_tuple: new_state = array_ops.concat(list(new_state), 1) return output, new_state
Example #6
Source File: rnn_cell.py From lambda-packs with MIT License | 5 votes |
def _linear(self, args): out_size = 4 * self._num_units proj_size = args.get_shape()[-1] weights = vs.get_variable("kernel", [proj_size, out_size]) out = math_ops.matmul(args, weights) if not self._layer_norm: bias = vs.get_variable("bias", [out_size]) out = nn_ops.bias_add(out, bias) return out
Example #7
Source File: rnn_cell.py From lambda-packs with MIT License | 5 votes |
def call(self, inputs, state): """Run one step of UGRNN. Args: inputs: input Tensor, 2D, batch x input size. state: state Tensor, 2D, batch x num units. Returns: new_output: batch x num units, Tensor representing the output of the UGRNN after reading `inputs` when previous state was `state`. Identical to `new_state`. new_state: batch x num units, Tensor representing the state of the UGRNN after reading `inputs` when previous state was `state`. Raises: ValueError: If input size cannot be inferred from inputs via static shape inference. """ sigmoid = math_ops.sigmoid input_size = inputs.get_shape().with_rank(2)[1] if input_size.value is None: raise ValueError("Could not infer input size from inputs.get_shape()[-1]") with vs.variable_scope(vs.get_variable_scope(), initializer=self._initializer): cell_inputs = array_ops.concat([inputs, state], 1) rnn_matrix = _linear(cell_inputs, 2 * self._num_units, True) [g_act, c_act] = array_ops.split( axis=1, num_or_size_splits=2, value=rnn_matrix) c = self._activation(c_act) g = sigmoid(g_act + self._forget_bias) new_state = g * state + (1.0 - g) * c new_output = new_state return new_output, new_state
Example #8
Source File: nns.py From Dense_BiLSTM with MIT License | 5 votes |
def linear(args, output_size, bias, bias_start=0.0, scope=None, squeeze=False, keep_prob=None, is_train=None): if args is None or (nest.is_sequence(args) and not args): raise ValueError("args must be specified") if not nest.is_sequence(args): args = [args] flat_args = [flatten(arg, 1) for arg in args] if keep_prob is not None and is_train is not None: flat_args = [tf.cond(is_train, lambda: tf.nn.dropout(arg, keep_prob), lambda: arg) for arg in flat_args] with tf.variable_scope(scope or 'linear'): flat_out = _linear(flat_args, output_size, bias, bias_initializer=tf.constant_initializer(bias_start)) out = reconstruct(flat_out, args[0], 1) if squeeze: out = tf.squeeze(out, [len(args[0].get_shape().as_list())-1]) return out
Example #9
Source File: embed_compress.py From neuralcompressor with MIT License | 5 votes |
def _encode(self, input_matrix, word_ids, embed_size): input_embeds = tf.nn.embedding_lookup(input_matrix, word_ids, name="input_embeds") M, K = self.M, self.K with tf.variable_scope("h"): h = tf.nn.tanh(_linear(input_embeds, M * K/2, True)) with tf.variable_scope("logits"): logits = _linear(h, M * K, True) logits = tf.log(tf.nn.softplus(logits) + 1e-8) logits = tf.reshape(logits, [-1, M, K], name="logits") return input_embeds, logits
Example #10
Source File: rnn_cell.py From lambda-packs with MIT License | 4 votes |
def call(self, inputs, state): """Run one step of the Intersection RNN. Args: inputs: input Tensor, 2D, batch x input size. state: state Tensor, 2D, batch x num units. Returns: new_y: batch x num units, Tensor representing the output of the +RNN after reading `inputs` when previous state was `state`. new_state: batch x num units, Tensor representing the state of the +RNN after reading `inputs` when previous state was `state`. Raises: ValueError: If input size cannot be inferred from `inputs` via static shape inference. ValueError: If input size != output size (these must be equal when using the Intersection RNN). """ sigmoid = math_ops.sigmoid tanh = math_ops.tanh input_size = inputs.get_shape().with_rank(2)[1] if input_size.value is None: raise ValueError("Could not infer input size from inputs.get_shape()[-1]") with vs.variable_scope(vs.get_variable_scope(), initializer=self._initializer): # read-in projections (should be used for first layer in deep +RNN # to transform size of inputs from I --> N) if input_size.value != self._num_units: if self._num_input_proj: with vs.variable_scope("in_projection"): inputs = _linear(inputs, self._num_units, True) else: raise ValueError("Must have input size == output size for " "Intersection RNN. To fix, num_in_proj should " "be set to num_units at cell init.") n_dim = i_dim = self._num_units cell_inputs = array_ops.concat([inputs, state], 1) rnn_matrix = _linear(cell_inputs, 2*n_dim + 2*i_dim, True) gh_act = rnn_matrix[:, :n_dim] # b x n h_act = rnn_matrix[:, n_dim:2*n_dim] # b x n gy_act = rnn_matrix[:, 2*n_dim:2*n_dim+i_dim] # b x i y_act = rnn_matrix[:, 2*n_dim+i_dim:2*n_dim+2*i_dim] # b x i h = tanh(h_act) y = self._y_activation(y_act) gh = sigmoid(gh_act + self._forget_bias) gy = sigmoid(gy_act + self._forget_bias) new_state = gh * state + (1.0 - gh) * h # passed thru time new_y = gy * inputs + (1.0 - gy) * y # passed thru depth return new_y, new_state