本文整理汇总了Python中tensorflow.python.ops.array_ops.stop_gradient方法的典型用法代码示例。如果您正苦于以下问题:Python array_ops.stop_gradient方法的具体用法?Python array_ops.stop_gradient怎么用?Python array_ops.stop_gradient使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.ops.array_ops
的用法示例。
在下文中一共展示了array_ops.stop_gradient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_score_function_with_advantage
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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
示例2: loss
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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)
示例3: loss
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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")
示例4: _create_value
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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)
示例5: _create_value
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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)
示例6: _argmax_or_mcsearch
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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
示例7: _extract_argmax_and_embed
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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
示例8: evaluate_on_sample
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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?
示例9: _extract_argmax_and_embed
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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
示例10: stop_gradient
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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
开发者ID:PacktPublishing,项目名称:Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda,代码行数:20,代码来源:backend.py
示例11: _force_data_dependency
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
def _force_data_dependency(first_compute, then_compute):
"""Force all of `then_compute` to depend on all of `first_compute`.
Uses a dummy data dependency, which is useful when running on TPUs because
XLA ignores control dependencies. Only supports float arguments.
Args:
first_compute: `list<Tensor>`. These will be made to run before the
`Tensor`s `then_compute`.
then_compute: `list<Tensor>`. These will run after all the `Tensor`s in
`first_compute`.
Returns:
`list<Tensor>`, same length as `then_compute`.
Raises:
ValueError: if ranks are unknown or types are not floating.
"""
def _first_element(x):
if x.get_shape().ndims is None:
raise ValueError("Rank of Tensor %s must be known" % x)
ndims = x.get_shape().ndims
begin = framework_ops.convert_to_tensor([0] * ndims, dtype=dtypes.int32)
size = framework_ops.convert_to_tensor([1] * ndims, dtype=dtypes.int32)
return array_ops.reshape(array_ops.slice(x, begin, size), [])
first_compute_sum = math_ops.add_n(
[_first_element(x) for x in first_compute if x is not None])
dtype = first_compute_sum.dtype
if not dtype.is_floating:
raise ValueError("_force_data_dependency only supports floating dtypes.")
epsilon = np.finfo(dtype.as_numpy_dtype).tiny
zero = array_ops.stop_gradient(epsilon * first_compute_sum)
return [
array_ops.identity(x) + zero if x is not None else None
for x in then_compute
]
示例12: _AvgPool3DGradGrad
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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")))
示例13: _AvgPoolGradGrad
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
def _AvgPoolGradGrad(op, grad):
return (array_ops.stop_gradient(op.inputs[0]), gen_nn_ops._avg_pool(
grad,
op.get_attr("ksize"),
op.get_attr("strides"),
op.get_attr("padding"),
data_format=op.get_attr("data_format")))
示例14: stop_gradient
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
def stop_gradient(variables):
"""Returns `variables` but with zero gradient w.r.t. every other variable.
Arguments:
variables: List of variables.
Returns:
The same list of variables.
"""
return array_ops.stop_gradient(variables)
# CONTROL FLOW
示例15: _extract_argmax_and_embed
# 需要导入模块: from tensorflow.python.ops import array_ops [as 别名]
# 或者: from tensorflow.python.ops.array_ops import stop_gradient [as 别名]
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