本文整理汇总了Python中tensorflow.custom_gradient方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.custom_gradient方法的具体用法?Python tensorflow.custom_gradient怎么用?Python tensorflow.custom_gradient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.custom_gradient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_loss_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def get_loss_fn(self, minimization_problem):
"""Returns the loss function.
The resulting loss function should use `tf.custom_gradient` to override its
gradients. First, the gradients w.r.t. the internal state should be written
in terms of the constraints, instead of the proxy_constraints. Second, the
gradients may be negated, depending on the formulation (for example, for the
Lagrangian formulation, we wish to maximize over the Lagrange multipliers,
so the associated gradients will be negated).
Args:
minimization_problem: `ConstrainedMinimizationProblem`, the problem to
minimize.
Returns:
The loss function.
"""
开发者ID:google-research,项目名称:tensorflow_constrained_optimization,代码行数:19,代码来源:constrained_optimizer.py
示例2: ste_tern
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def ste_tern(
x: tf.Tensor,
threshold_value: float = 0.05,
ternary_weight_networks: bool = False,
clip_value: float = 1.0,
) -> tf.Tensor:
@tf.custom_gradient
def _call(x):
if ternary_weight_networks:
threshold = 0.7 * tf.reduce_sum(tf.abs(x)) / tf.cast(tf.size(x), x.dtype)
else:
threshold = threshold_value
def grad(dy):
return _clipped_gradient(x, dy, clip_value)
return tf.sign(tf.sign(x + threshold) + tf.sign(x - threshold)), grad
return _call(x)
示例3: quantize
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def quantize(self,tensor):
@tf.custom_gradient
def op(tensor):
def grad(dy):
return dy
randn = tf.random.uniform(tensor.shape, minval=0, maxval=1 )
out_up = tf.math.ceil( tensor*(1<<self.fixed_prec) ) / (1<<self.fixed_prec)
out_down = tf.math.floor( tensor*(1<<self.fixed_prec) ) / (1<<self.fixed_prec)
out_mask = tf.less_equal( (tensor-tf.math.floor(tensor))*(1<<self.fixed_prec) ,randn )
out = out_down * tf.dtypes.cast(out_mask, tensor.dtype) + out_up * tf.dtypes.cast(tf.math.logical_not(out_mask), tensor.dtype)
# handle overflow (saturate number towards maximum or minimum)
out = tf.math.maximum( tf.math.minimum( out, self.fixed_max_signed ), self.fixed_min_signed)
# tag output
out = tf.identity(out, name=str(self)+"_output")
return out, grad
return op(tensor)
###############################
### Logarithmic
###############################
示例4: P_quantize
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def P_quantize(self, tensor):
@tf.custom_gradient
def op(tensor):
def grad(dy):
return dy
#randn = tf.random.uniform(tensor.shape, minval=0, maxval=1 )
#mask = tf.dtypes.cast(tf.less(tensor,randn), tensor.dtype)
out= tf.math.floor(tf.math.log(tf.math.abs(tensor))/tf.math.log(tf.constant(2,dtype=tensor.dtype))) #+ mask
out= tf.math.pow(2*tf.ones_like(tensor),out)
out= out*tf.sign(tensor)
out = tf.identity(out, name=str(self)+"_output")
return out, grad
return op(tensor)
###############################
### Sparse
###############################
示例5: __call__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def __call__(self, *parameters, solver_args={}):
"""Solve problem (or a batch of problems) corresponding to `parameters`
Args:
parameters: a sequence of tf.Tensors; the n-th Tensor specifies
the value for the n-th CVXPY Parameter. These Tensors
can be batched: if a Tensor has 3 dimensions, then its
first dimension is interpreted as the batch size.
solver_args: a dict of optional arguments, to send to `diffcp`. Keys
should be the names of keyword arguments.
Returns:
a list of optimal variable values, one for each CVXPY Variable
supplied to the constructor.
"""
if len(parameters) != len(self.params):
raise ValueError('A tensor must be provided for each CVXPY '
'parameter; received %d tensors, expected %d' % (
len(parameters), len(self.params)))
compute = tf.custom_gradient(
lambda *parameters: self._compute(parameters, solver_args))
return compute(*parameters)
示例6: __init__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def __init__(self, name, inputs, tower_setup, initial_weights, hack_gradient_magnitude=1.0):
super().__init__()
assert len(initial_weights) == len(inputs)
with tf.variable_scope(name):
initializer = tf.constant_initializer(initial_weights)
weights = self.create_bias_variable("linear_combination_weights", len(inputs), tower_setup,
initializer=initializer)
if hack_gradient_magnitude > 1.0:
# https://stackoverflow.com/a/43948872
@tf.custom_gradient
def amplify_gradient_layer(x):
def grad(dy):
return hack_gradient_magnitude * dy
return tf.identity(x), grad
weights = amplify_gradient_layer(weights)
y = inputs[0] * weights[0]
for n in range(1, len(inputs)):
y += inputs[n] * weights[n]
self.outputs.append(y)
for n in range(len(inputs)):
self.add_scalar_summary(weights[n], "linear_combination_weights_" + str(n))
示例7: eager_tensorflow_from_torch
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def eager_tensorflow_from_torch(func):
"""
Wraps a PyTorch function into a TensorFlow eager-mode function (ie can be executed within Tensorflow eager-mode).
:param func: Function that takes PyTorch tensors and returns a PyTorch tensor.
:return: Differentiable Tensorflow eager-mode function.
"""
@tf.custom_gradient
def compute(*inputs):
th_inputs = [th.tensor(tf_input.numpy(), requires_grad=True) for tf_input in inputs]
th_output = func(*th_inputs)
def compute_grad(d_output):
th_d_output = th.tensor(d_output.numpy(), requires_grad=False)
th_gradients = th.autograd.grad([th_output], th_inputs, grad_outputs=[th_d_output], allow_unused=True)
tf_gradients = [tf.convert_to_tensor(th_gradient.numpy()) for th_gradient in th_gradients]
return tf_gradients
return tf.convert_to_tensor(th_output.detach().numpy()), compute_grad
return compute
示例8: blur2d
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def blur2d(x, f, normalize=True):
with tf.variable_scope('Blur2D'):
@tf.custom_gradient
def func(x):
y = _blur2d(x, f, normalize)
@tf.custom_gradient
def grad(dy):
dx = _blur2d(dy, f, normalize, flip=True)
return dx, lambda ddx: _blur2d(ddx, f, normalize)
return y, grad
return func(x)
示例9: upscale2d
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def upscale2d(x, factor=2):
with tf.variable_scope('Upscale2D'):
@tf.custom_gradient
def func(x):
y = _upscale2d(x, factor)
@tf.custom_gradient
def grad(dy):
dx = _downscale2d(dy, factor, gain=factor ** 2)
return dx, lambda ddx: _upscale2d(ddx, factor)
return y, grad
return func(x)
示例10: downscale2d
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def downscale2d(x, factor=2):
with tf.variable_scope('Downscale2D'):
@tf.custom_gradient
def func(x):
y = _downscale2d(x, factor)
@tf.custom_gradient
def grad(dy):
dx = _upscale2d(dy, factor, gain=1 / factor ** 2)
return dx, lambda ddx: _downscale2d(ddx, factor)
return y, grad
return func(x)
示例11: call
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def call(self, input_):
@tf.custom_gradient
def _call(input_):
def reversed_gradient(output_grads):
return self.weight * tf.negative(output_grads)
return input_, reversed_gradient
return _call(input_)
# ## The model function
# The network consists of 3 sub-networks:
#
# * Feature extractor: extracts internal representation for both the source and target distributions.
#
# * Label predictor: predicts label from the extracted features.
#
# * Domain classifier: classifies the origin (`source` or `target`) of the extracted features.
#
#
# Both the label predictor and the domain classifier will try to minimize
# classification loss, but the gradients backpropagated from the domain
# classifier to the feature extractor have their signs reversed.
#
#
# This model function also shows how to use `host_call` to output summaries.
#
示例12: edge_softmax
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def edge_softmax(graph, logits, eids=ALL):
"""Closure for tf.custom_gradient"""
@tf.custom_gradient
def _lambda(logits):
return edge_softmax_real(graph, logits, eids=eids)
return _lambda(logits)
示例13: binary_reduce
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def binary_reduce(reducer, binary_op, graph, lhs, rhs, lhs_data, rhs_data,
out_size, lhs_map=(None, None), rhs_map=(None, None), out_map=(None, None)):
@tf.custom_gradient
def _lambda(lhs_data, rhs_data):
return binary_reduce_real(reducer, binary_op, graph, lhs, rhs, lhs_data, rhs_data,
out_size, lhs_map, rhs_map, out_map)
return _lambda(lhs_data, rhs_data)
示例14: copy_reduce
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def copy_reduce(reducer, graph, target, in_data, out_size, in_map=(None, None),
out_map=(None, None)):
@tf.custom_gradient
def _lambda(in_data):
return copy_reduce_real(reducer, graph, target, in_data, out_size, in_map,
out_map)
return _lambda(in_data)
示例15: ste_sign
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import custom_gradient [as 别名]
def ste_sign(x: tf.Tensor, clip_value: float = 1.0) -> tf.Tensor:
@tf.custom_gradient
def _call(x):
def grad(dy):
return _clipped_gradient(x, dy, clip_value)
return math.sign(x), grad
return _call(x)