當前位置: 首頁>>代碼示例>>Python>>正文


Python tensorflow.custom_gradient方法代碼示例

本文整理匯總了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) 
開發者ID:larq,項目名稱:larq,代碼行數:21,代碼來源:quantizers.py

示例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
############################### 
開發者ID:cc-hpc-itwm,項目名稱:TensorQuant,代碼行數:23,代碼來源:Quantizers.py

示例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
############################### 
開發者ID:cc-hpc-itwm,項目名稱:TensorQuant,代碼行數:20,代碼來源:Quantizers.py

示例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) 
開發者ID:cvxgrp,項目名稱:cvxpylayers,代碼行數:24,代碼來源:cvxpylayer.py

示例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)) 
開發者ID:VisualComputingInstitute,項目名稱:TrackR-CNN,代碼行數:23,代碼來源:UtilLayers.py

示例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 
開發者ID:BlackHC,項目名稱:tfpyth,代碼行數:24,代碼來源:__init__.py

示例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) 
開發者ID:taki0112,項目名稱:StyleGAN-Tensorflow,代碼行數:16,代碼來源:ops.py

示例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) 
開發者ID:taki0112,項目名稱:StyleGAN-Tensorflow,代碼行數:16,代碼來源:ops.py

示例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) 
開發者ID:taki0112,項目名稱:StyleGAN-Tensorflow,代碼行數:16,代碼來源:ops.py

示例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.
# 
開發者ID:GoogleCloudPlatform,項目名稱:cloudml-samples,代碼行數:30,代碼來源:trainer.py

示例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) 
開發者ID:dmlc,項目名稱:dgl,代碼行數:10,代碼來源:softmax.py

示例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) 
開發者ID:dmlc,項目名稱:dgl,代碼行數:10,代碼來源:tensor.py

示例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) 
開發者ID:dmlc,項目名稱:dgl,代碼行數:9,代碼來源:tensor.py

示例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) 
開發者ID:larq,項目名稱:larq,代碼行數:11,代碼來源:quantizers.py


注:本文中的tensorflow.custom_gradient方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。