Python tensorflow.python.ops.array_ops.stop_gradient() Examples
The following are 30
code examples of tensorflow.python.ops.array_ops.stop_gradient().
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.array_ops
, or try the search function
.
Example #1
Source File: tf_seq2seq_model.py From Conditional-SeqGAN-Tensorflow with MIT License | 7 votes |
def _argmax_or_mcsearch(embedding, output_projection=None, update_embedding=True, mc_search=False): def loop_function(prev, _): if output_projection is not None: prev = nn_ops.xw_plus_b(prev, output_projection[0], output_projection[1]) if isinstance(mc_search, bool): prev_symbol = tf.reshape(tf.multinomial(prev, 1), [-1]) if mc_search else math_ops.argmax(prev, 1) else: prev_symbol = tf.cond(mc_search, lambda: tf.reshape(tf.multinomial(prev, 1), [-1]), lambda: tf.argmax(prev, 1)) emb_prev = embedding_ops.embedding_lookup(embedding, prev_symbol) if not update_embedding: emb_prev = array_ops.stop_gradient(emb_prev) return emb_prev return loop_function
Example #2
Source File: stochastic_tensor.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def loss(self, sample_loss): """Returns the term to add to the surrogate loss. This method is called by `surrogate_loss`. The input `sample_loss` should have already had `stop_gradient` applied to it. This is because the surrogate_loss usually provides a Monte Carlo sample term of the form `differentiable_surrogate * sample_loss` where `sample_loss` is considered constant with respect to the input for purposes of the gradient. Args: sample_loss: `Tensor`, sample loss downstream of this `StochasticTensor`. Returns: Either `None` or a `Tensor`. """ raise NotImplementedError("surrogate_loss not implemented")
Example #3
Source File: stochastic_tensor.py From deep_image_model with Apache License 2.0 | 6 votes |
def loss(self, final_loss, name="Loss"): # Return a loss based on final_loss and the distribution. Returns # None if pathwise derivatives are supported, if the loss_fn # was explicitly set to None, or if the value type is MeanValue. if self._loss_fn is None: return None if (self._dist.is_continuous and self._dist.is_reparameterized and not self._value_type.stop_gradient): # Can perform pathwise-derivative on this one; no additional loss needed. return None with ops.name_scope(self.name, values=[final_loss]): with ops.name_scope(name): if (self._value_type.stop_gradient or isinstance(self._value_type, SampleValue)): return self._loss_fn(self, self._value, final_loss) elif isinstance(self._value_type, MeanValue): return None # MeanValue generally provides its own gradient else: raise TypeError("Unrecognized Distribution Value Type: %s", self._value_type)
Example #4
Source File: stochastic_tensor.py From deep_image_model with Apache License 2.0 | 6 votes |
def loss(self, sample_loss): """Returns the term to add to the surrogate loss. This method is called by `surrogate_loss`. The input `sample_loss` should have already had `stop_gradient` applied to it. This is because the surrogate_loss usually provides a Monte Carlo sample term of the form `differentiable_surrogate * sample_loss` where `sample_loss` is considered constant with respect to the input for purposes of the gradient. Args: sample_loss: `Tensor`, sample loss downstream of this `StochasticTensor`. Returns: Either `None` or a `Tensor`. """ raise NotImplementedError("surrogate_loss not implemented")
Example #5
Source File: backend.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 6 votes |
def stop_gradient(variables): """Returns `variables` but with zero gradient w.r.t. every other variable. Arguments: variables: Tensor or list of tensors to consider constant with respect to any other variable. Returns: A single tensor or a list of tensors (depending on the passed argument) that has no gradient with respect to any other variable. """ if isinstance(variables, (list, tuple)): return map(array_ops.stop_gradient, variables) return array_ops.stop_gradient(variables) # CONTROL FLOW
Example #6
Source File: my_seq2seq.py From Neural_Conversation_Models with Apache License 2.0 | 6 votes |
def _extract_argmax_and_embed(embedding, output_projection=None, update_embedding=True): """Get a loop_function that extracts the previous symbol and embeds it. Args: embedding: embedding tensor for symbols. output_projection: None or a pair (W, B). If provided, each fed previous output will first be multiplied by W and added B. update_embedding: Boolean; if False, the gradients will not propagate through the embeddings. Returns: A loop function. """ def loop_function(prev, _): if output_projection is not None: prev = nn_ops.xw_plus_b( prev, output_projection[0], output_projection[1]) prev_symbol = math_ops.argmax(prev, 1) # Note that gradients will not propagate through the second parameter of # embedding_lookup. emb_prev = embedding_ops.embedding_lookup(embedding, prev_symbol) if not update_embedding: emb_prev = array_ops.stop_gradient(emb_prev) return emb_prev return loop_function
Example #7
Source File: loss_functions.py From noisy-K-FAC with Apache License 2.0 | 6 votes |
def evaluate_on_sample(self, seed=None): """Evaluates the log probability on a random sample. Args: seed: int or None. Random seed for this draw from the distribution. Returns: Log probability of sampled targets, summed across examples. """ if seed is None: seed = self._default_seed # We treat the targets as "constant". It's only the inputs that get # "back-propped" through. return self._evaluate(array_ops.stop_gradient(self.sample(seed))) # TODO(jamesmartens): should this just inherit from object to avoid "diamond" # inheritance, or is there a better way?
Example #8
Source File: stochastic_tensor.py From deep_image_model with Apache License 2.0 | 6 votes |
def _create_value(self): """Create the value Tensor based on the value type, store as self._value.""" if isinstance(self._value_type, MeanValue): value_tensor = self._dist.mean() elif isinstance(self._value_type, SampleValue): value_tensor = self._dist.sample(self._value_type.shape) else: raise TypeError( "Unrecognized Distribution Value Type: %s", self._value_type) if self._value_type.stop_gradient: # stop_gradient is being enforced by the value type return array_ops.stop_gradient(value_tensor) if isinstance(self._value_type, MeanValue): return value_tensor # Using pathwise-derivative for this one. if self._dist.is_continuous and self._dist.is_reparameterized: return value_tensor # Using pathwise-derivative for this one. else: # Will have to perform some variant of score function # estimation. Call stop_gradient on the sampler just in case we # may accidentally leak some gradient from it. return array_ops.stop_gradient(value_tensor)
Example #9
Source File: stochastic_tensor_impl.py From lambda-packs with MIT License | 6 votes |
def loss(self, final_loss, name="Loss"): # Return a loss based on final_loss and the distribution. Returns # None if pathwise derivatives are supported, if the loss_fn # was explicitly set to None, or if the value type is MeanValue. if self._loss_fn is None: return None if (self._dist.reparameterization_type == distribution.FULLY_REPARAMETERIZED and not self._value_type.stop_gradient): # Can perform pathwise-derivative on this one; no additional loss needed. return None with ops.name_scope(self.name, values=[final_loss]): with ops.name_scope(name): if (self._value_type.stop_gradient or isinstance(self._value_type, SampleValue)): return self._loss_fn(self, self._value, final_loss) elif isinstance(self._value_type, MeanValue): return None # MeanValue generally provides its own gradient else: raise TypeError("Unrecognized Distribution Value Type: %s", self._value_type)
Example #10
Source File: tf_seq2seq_model.py From Conditional-SeqGAN-Tensorflow with MIT License | 6 votes |
def _extract_argmax_and_embed(embedding, output_projection=None, update_embedding=True): """Get a loop_function that extracts the previous symbol and embeds it. Args: embedding: embedding tensor for symbols. output_projection: None or a pair (W, B). If provided, each fed previous output will first be multiplied by W and added B. update_embedding: Boolean; if False, the gradients will not propagate through the embeddings. Returns: A loop function. """ def loop_function(prev, _): if output_projection is not None: prev = nn_ops.xw_plus_b( prev, output_projection[0], output_projection[1]) prev_symbol = math_ops.argmax(prev, 1) # Note that gradients will not propagate through the second parameter of # embedding_lookup. emb_prev = embedding_ops.embedding_lookup(embedding, prev_symbol) if not update_embedding: emb_prev = array_ops.stop_gradient(emb_prev) return emb_prev return loop_function
Example #11
Source File: stochastic_gradient_estimators.py From lambda-packs with MIT License | 6 votes |
def get_score_function_with_advantage(advantage_fn=None, name="ScoreFunctionWithAdvantage"): """Score function estimator with advantage function. Args: advantage_fn: callable that takes the `StochasticTensor` and the downstream `loss` and returns a `Tensor` advantage (e.g. `loss - baseline`). name: name to prepend ops with. Returns: Callable score function estimator that takes the `StochasticTensor`, the sampled `value`, and the downstream `loss`, and uses the provided advantage. """ def score_function_with_advantage(stochastic_tensor, value, loss): with ops.name_scope(name, values=[value, loss]): advantage = advantage_fn(stochastic_tensor, loss) advantage = array_ops.stop_gradient(advantage) return stochastic_tensor.distribution.log_prob(value) * advantage return score_function_with_advantage
Example #12
Source File: stochastic_tensor.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def loss(self, final_loss, name="Loss"): # Return a loss based on final_loss and the distribution. Returns # None if pathwise derivatives are supported, if the loss_fn # was explicitly set to None, or if the value type is MeanValue. if self._loss_fn is None: return None if (self._dist.is_continuous and self._dist.is_reparameterized and not self._value_type.stop_gradient): # Can perform pathwise-derivative on this one; no additional loss needed. return None with ops.name_scope(self.name, values=[final_loss]): with ops.name_scope(name): if (self._value_type.stop_gradient or isinstance(self._value_type, SampleValue)): return self._loss_fn(self, self._value, final_loss) elif isinstance(self._value_type, MeanValue): return None # MeanValue generally provides its own gradient else: raise TypeError("Unrecognized Distribution Value Type: %s", self._value_type)
Example #13
Source File: stochastic_tensor.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _create_value(self): """Create the value Tensor based on the value type, store as self._value.""" if isinstance(self._value_type, MeanValue): value_tensor = self._dist.mean() elif isinstance(self._value_type, SampleValue): value_tensor = self._dist.sample(self._value_type.shape) else: raise TypeError("Unrecognized Distribution Value Type: %s", self._value_type) if self._value_type.stop_gradient: # stop_gradient is being enforced by the value type return array_ops.stop_gradient(value_tensor) if isinstance(self._value_type, MeanValue): return value_tensor # Using pathwise-derivative for this one. if self._dist.is_continuous and self._dist.is_reparameterized: return value_tensor # Using pathwise-derivative for this one. else: # Will have to perform some variant of score function # estimation. Call stop_gradient on the sampler just in case we # may accidentally leak some gradient from it. return array_ops.stop_gradient(value_tensor)
Example #14
Source File: seq2seq.py From Attention-OCR with MIT License | 5 votes |
def _extract_argmax_and_embed(embedding, output_projection=None, update_embedding=True): """Get a loop_function that extracts the previous symbol and embeds it. Args: embedding: embedding tensor for symbols. output_projection: None or a pair (W, B). If provided, each fed previous output will first be multiplied by W and added B. update_embedding: Boolean; if False, the gradients will not propagate through the embeddings. Returns: A loop function. """ def loop_function(prev, _): if output_projection is not None: prev = nn_ops.xw_plus_b( prev, output_projection[0], output_projection[1]) prev_symbol = math_ops.argmax(prev, 1) # Note that gradients will not propagate through the second parameter of # embedding_lookup. emb_prev = embedding_ops.embedding_lookup(embedding, prev_symbol) if not update_embedding: emb_prev = array_ops.stop_gradient(emb_prev) return emb_prev return loop_function
Example #15
Source File: stochastic_tensor.py From deep_image_model with Apache License 2.0 | 5 votes |
def stop_gradient(self): return self._stop_gradient
Example #16
Source File: nn_grad.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def _AvgPool3DGradGrad(op, grad): return (array_ops.stop_gradient(op.inputs[0]), gen_nn_ops.avg_pool3d( grad, op.get_attr("ksize"), op.get_attr("strides"), op.get_attr("padding"), data_format=op.get_attr("data_format")))
Example #17
Source File: seq2seq.py From DeepAffinity with GNU General Public License v3.0 | 5 votes |
def _extract_argmax_and_embed(embedding, output_projection=None, update_embedding=True): """Get a loop_function that extracts the previous symbol and embeds it. Args: embedding: embedding tensor for symbols. output_projection: None or a pair (W, B). If provided, each fed previous output will first be multiplied by W and added B. update_embedding: Boolean; if False, the gradients will not propagate through the embeddings. Returns: A loop function. """ def loop_function(prev, _): if output_projection is not None: prev = nn_ops.xw_plus_b(prev, output_projection[0], output_projection[1]) prev_symbol = math_ops.argmax(prev, 1) # Note that gradients will not propagate through the second parameter of # embedding_lookup. emb_prev = embedding_ops.embedding_lookup(embedding, prev_symbol) if not update_embedding: emb_prev = array_ops.stop_gradient(emb_prev) return emb_prev return loop_function
Example #18
Source File: entropy_models.py From pcc_geo_cnn with MIT License | 5 votes |
def _logits_cumulative(self, inputs, stop_gradient): """Evaluate logits of the cumulative densities. Args: inputs: The values at which to evaluate the cumulative densities, expected to be a `Tensor` of shape `(channels, 1, batch)`. stop_gradient: Boolean. Whether to add `array_ops.stop_gradient` calls so that the gradient of the output with respect to the density model parameters is disconnected (the gradient with respect to `inputs` is left untouched). Returns: A `Tensor` of the same shape as `inputs`, containing the logits of the cumulative densities evaluated at the given inputs. """ logits = inputs for i in range(len(self.filters) + 1): matrix = self._matrices[i] if stop_gradient: matrix = array_ops.stop_gradient(matrix) logits = math_ops.matmul(matrix, logits) bias = self._biases[i] if stop_gradient: bias = array_ops.stop_gradient(bias) logits += bias if i < len(self._factors): factor = self._factors[i] if stop_gradient: factor = array_ops.stop_gradient(factor) logits += factor * math_ops.tanh(logits) return logits
Example #19
Source File: loss_functions.py From noisy-K-FAC with Apache License 2.0 | 5 votes |
def evaluate(self): """Evaluate the loss function on the targets.""" if self.targets is not None: # We treat the targets as "constant". It's only the inputs that get # "back-propped" through. return self._evaluate(array_ops.stop_gradient(self.targets)) else: raise Exception("Cannot evaluate losses with unspecified targets.")
Example #20
Source File: seq2seq.py From keras-lambda with MIT License | 5 votes |
def _extract_argmax_and_embed(embedding, output_projection=None, update_embedding=True): """Get a loop_function that extracts the previous symbol and embeds it. Args: embedding: embedding tensor for symbols. output_projection: None or a pair (W, B). If provided, each fed previous output will first be multiplied by W and added B. update_embedding: Boolean; if False, the gradients will not propagate through the embeddings. Returns: A loop function. """ def loop_function(prev, _): if output_projection is not None: prev = nn_ops.xw_plus_b(prev, output_projection[0], output_projection[1]) prev_symbol = math_ops.argmax(prev, 1) # Note that gradients will not propagate through the second parameter of # embedding_lookup. emb_prev = embedding_ops.embedding_lookup(embedding, prev_symbol) if not update_embedding: emb_prev = array_ops.stop_gradient(emb_prev) return emb_prev return loop_function
Example #21
Source File: stochastic_gradient_estimators.py From keras-lambda with MIT License | 5 votes |
def score_function(stochastic_tensor, value, loss, baseline=None, name="ScoreFunction"): """Score function estimator. Computes the integrand of the score function with a baseline: `p.log_prob(value) * (loss - baseline)`. It will add a `stop_gradient` to the advantage `(loss - baseline)`. Args: stochastic_tensor: `StochasticTensor` p(x). value: `Tensor` x. Samples from p(x). loss: `Tensor`. baseline: `Tensor` broadcastable to `loss`. name: name to prepend ops with. Returns: `Tensor` `p.log_prob(x) * (loss - b)`. Taking the gradient yields the score function estimator. """ with ops.name_scope(name, values=[value, loss, baseline]): value = ops.convert_to_tensor(value) loss = ops.convert_to_tensor(loss) if baseline is not None: baseline = ops.convert_to_tensor(baseline) advantage = loss - baseline else: advantage = loss advantage = array_ops.stop_gradient(advantage) return stochastic_tensor.distribution.log_prob(value) * advantage
Example #22
Source File: seq2seq.py From DeepAffinity with GNU General Public License v3.0 | 5 votes |
def _extract_argmax_and_embed(embedding, output_projection=None, update_embedding=True): """Get a loop_function that extracts the previous symbol and embeds it. Args: embedding: embedding tensor for symbols. output_projection: None or a pair (W, B). If provided, each fed previous output will first be multiplied by W and added B. update_embedding: Boolean; if False, the gradients will not propagate through the embeddings. Returns: A loop function. """ def loop_function(prev, _): if output_projection is not None: prev = nn_ops.xw_plus_b(prev, output_projection[0], output_projection[1]) prev_symbol = math_ops.argmax(prev, 1) # Note that gradients will not propagate through the second parameter of # embedding_lookup. emb_prev = embedding_ops.embedding_lookup(embedding, prev_symbol) if not update_embedding: emb_prev = array_ops.stop_gradient(emb_prev) return emb_prev return loop_function
Example #23
Source File: seq2seq.py From ecm with Apache License 2.0 | 5 votes |
def _extract_argmax_and_embed(embedding, num_symbols, output_projection=None, update_embedding=True): """Get a loop_function that extracts the previous symbol and embeds it. Args: embedding: embedding tensor for symbols. output_projection: None or a pair (W, B). If provided, each fed previous output will first be multiplied by W and added B. update_embedding: Boolean; if False, the gradients will not propagate through the embeddings. Returns: A loop function. """ def loop_function(prev, _): #if output_projection is not None: # prev = nn_ops.xw_plus_b( # prev, output_projection[0], output_projection[1]) #prev_symbol = math_ops.argmax(prev, 1) prev_symbol = math_ops.argmax(array_ops.split_v(prev, [2, num_symbols-2], 1)[1], 1) + 2 # Note that gradients will not propagate through the second parameter of # embedding_lookup. emb_prev = embedding_ops.embedding_lookup(embedding, prev_symbol) if not update_embedding: emb_prev = array_ops.stop_gradient(emb_prev) return emb_prev return loop_function
Example #24
Source File: stochastic_tensor.py From deep_image_model with Apache License 2.0 | 5 votes |
def stop_gradient(self): return self._stop_gradient # Keeps track of how a StochasticTensor's value should be accessed. # Used by value_type and get_current_value_type below.
Example #25
Source File: seq2seq.py From DeepAffinity with GNU General Public License v3.0 | 5 votes |
def _extract_argmax_and_embed(embedding, output_projection=None, update_embedding=True): """Get a loop_function that extracts the previous symbol and embeds it. Args: embedding: embedding tensor for symbols. output_projection: None or a pair (W, B). If provided, each fed previous output will first be multiplied by W and added B. update_embedding: Boolean; if False, the gradients will not propagate through the embeddings. Returns: A loop function. """ def loop_function(prev, _): if output_projection is not None: prev = nn_ops.xw_plus_b(prev, output_projection[0], output_projection[1]) prev_symbol = math_ops.argmax(prev, 1) # Note that gradients will not propagate through the second parameter of # embedding_lookup. emb_prev = embedding_ops.embedding_lookup(embedding, prev_symbol) if not update_embedding: emb_prev = array_ops.stop_gradient(emb_prev) return emb_prev return loop_function
Example #26
Source File: stochastic_tensor.py From deep_image_model with Apache License 2.0 | 5 votes |
def __init__(self, stop_gradient=False): self._stop_gradient = stop_gradient
Example #27
Source File: stochastic_tensor.py From deep_image_model with Apache License 2.0 | 5 votes |
def stop_gradient(self): """Whether the value should be wrapped in stop_gradient. StochasticTensors must respect this property. """ pass
Example #28
Source File: monte_carlo.py From deep_image_model with Apache License 2.0 | 5 votes |
def _logspace_mean(log_values): """Evaluate `Log[E[values]]` in a stable manner. Args: log_values: `Tensor` holding `Log[values]`. Returns: `Tensor` of same `dtype` as `log_values`, reduced across dim 0. `Log[Mean[values]]`. """ # center = Max[Log[values]], with stop-gradient # The center hopefully keep the exponentiated term small. It is cancelled # from the final result, so putting stop gradient on it will not change the # final result. We put stop gradient on to eliminate unnecessary computation. center = array_ops.stop_gradient(_sample_max(log_values)) # centered_values = exp{Log[values] - E[Log[values]]} centered_values = math_ops.exp(log_values - center) # log_mean_of_values = Log[ E[centered_values] ] + center # = Log[ E[exp{log_values - E[log_values]}] ] + center # = Log[E[values]] - E[log_values] + center # = Log[E[values]] log_mean_of_values = math_ops.log(_sample_mean(centered_values)) + center return log_mean_of_values
Example #29
Source File: stochastic_gradient_estimators.py From deep_image_model with Apache License 2.0 | 5 votes |
def score_function(stochastic_tensor, value, loss, baseline=None, name="ScoreFunction"): """Score function estimator. Computes the integrand of the score function with a baseline: `p.log_prob(value) * (loss - baseline)`. It will add a `stop_gradient` to the advantage `(loss - baseline)`. Args: stochastic_tensor: `StochasticTensor` p(x). value: `Tensor` x. Samples from p(x). loss: `Tensor`. baseline: `Tensor` broadcastable to `loss`. name: name to prepend ops with. Returns: `Tensor` `p.log_prob(x) * (loss - b)`. Taking the gradient yields the score function estimator. """ with ops.name_scope(name, values=[value, loss, baseline]): value = ops.convert_to_tensor(value) loss = ops.convert_to_tensor(loss) if baseline is not None: baseline = ops.convert_to_tensor(baseline) advantage = loss - baseline else: advantage = loss advantage = array_ops.stop_gradient(advantage) return stochastic_tensor.distribution.log_prob(value) * advantage
Example #30
Source File: gradients_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testStopGradient(self): with ops.Graph().as_default(): inp = constant(1.0, shape=[100, 32], name="in") out = array_ops.stop_gradient(inp) igrad = gradients.gradients(out, inp)[0] assert igrad is None