Python tensorflow.python.ops.math_ops.minimum() Examples
The following are 30
code examples of tensorflow.python.ops.math_ops.minimum().
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.math_ops
, or try the search function
.
Example #1
Source File: tf_image.py From HUAWEIOCR-2019 with MIT License | 6 votes |
def rotate90(bboxes, xs, ys, k): # bboxes = tf.Print(bboxes, [bboxes], 'before rotate',summarize = 100) ymin, xmin, ymax, xmax = [bboxes[:, i] for i in range(4)] xmin, ymin = tf_rotate_point_by_90(xmin, ymin, k) xmax, ymax = tf_rotate_point_by_90(xmax, ymax, k) new_xmin = tf.minimum(xmin, xmax) new_xmax = tf.maximum(xmin, xmax) new_ymin = tf.minimum(ymin, ymax) new_ymax = tf.maximum(ymin, ymax) bboxes = tf.stack([new_ymin, new_xmin, new_ymax, new_xmax]) bboxes = tf.transpose(bboxes) xs, ys = tf_rotate_point_by_90(xs, ys, k) return bboxes, xs, ys
Example #2
Source File: odes.py From lambda-packs with MIT License | 6 votes |
def _optimal_step_size(last_step, error_ratio, safety=0.9, ifactor=10.0, dfactor=0.2, order=5, name=None): """Calculate the optimal size for the next Runge-Kutta step.""" with ops.name_scope( name, 'optimal_step_size', [last_step, error_ratio]) as scope: error_ratio = math_ops.cast(error_ratio, last_step.dtype) exponent = math_ops.cast(1 / order, last_step.dtype) # this looks more complex than necessary, but importantly it keeps # error_ratio in the numerator so we can't divide by zero: factor = math_ops.maximum( 1 / ifactor, math_ops.minimum(error_ratio ** exponent / safety, 1 / dfactor)) return math_ops.div(last_step, factor, name=scope)
Example #3
Source File: utils.py From Transformer-in-generating-dialogue with Apache License 2.0 | 6 votes |
def LRSchedule(global_step, d_model, warmup_steps=4000): if global_step is None: raise ValueError("global_step is required for learning_rate_schedule.") def deal_lr(global_step, d_model, warmup_steps): d_model = ops.convert_to_tensor(d_model, dtype=tf.float32) dtype = d_model.dtype warmup_steps = math_ops.cast(warmup_steps, dtype) global_step_recomp = math_ops.cast(global_step, dtype) arg1 = math_ops.rsqrt(global_step_recomp) arg2 = math_ops.multiply(global_step_recomp, math_ops.pow(warmup_steps, -1.5)) return math_ops.multiply(math_ops.rsqrt(d_model), math_ops.minimum(arg1, arg2)) return functools.partial(deal_lr, global_step, d_model, warmup_steps)
Example #4
Source File: tf_image.py From pixel_link with MIT License | 6 votes |
def rotate90(bboxes, xs, ys, k): # bboxes = tf.Print(bboxes, [bboxes], 'before rotate',summarize = 100) ymin, xmin, ymax, xmax = [bboxes[:, i] for i in range(4)] xmin, ymin = tf_rotate_point_by_90(xmin, ymin, k) xmax, ymax = tf_rotate_point_by_90(xmax, ymax, k) new_xmin = tf.minimum(xmin, xmax) new_xmax = tf.maximum(xmin, xmax) new_ymin = tf.minimum(ymin, ymax) new_ymax = tf.maximum(ymin, ymax) bboxes = tf.stack([new_ymin, new_xmin, new_ymax, new_xmax]) bboxes = tf.transpose(bboxes) xs, ys = tf_rotate_point_by_90(xs, ys, k) return bboxes, xs, ys
Example #5
Source File: odes.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _optimal_step_size(last_step, error_ratio, safety=0.9, ifactor=10.0, dfactor=0.2, order=5, name=None): """Calculate the optimal size for the next Runge-Kutta step.""" with ops.name_scope( name, 'optimal_step_size', [last_step, error_ratio]) as scope: error_ratio = math_ops.cast(error_ratio, last_step.dtype) exponent = math_ops.cast(1 / order, last_step.dtype) # this looks more complex than necessary, but importantly it keeps # error_ratio in the numerator so we can't divide by zero: factor = math_ops.maximum( 1 / ifactor, math_ops.minimum(error_ratio ** exponent / safety, 1 / dfactor)) return math_ops.div(last_step, factor, name=scope)
Example #6
Source File: deep_rnn_model_huber_loss.py From deep-quant with MIT License | 6 votes |
def _huber_loss(labels, predictions, mask, config): """ Huber loss according to masking""" delta = config.huber_delta predictions = math_ops.to_float(predictions) labels = math_ops.to_float(labels) predictions.get_shape().assert_is_compatible_with(labels.get_shape()) error = math_ops.subtract(predictions, labels) abs_error = math_ops.abs(error) quadratic = math_ops.minimum(abs_error, delta) # The following expression is the same in value as # tf.maximum(abs_error - delta, 0), but importantly the gradient for the # expression when abs_error == delta is 0 (for tf.maximum it would be 1). # This is necessary to avoid doubling the gradient, since there is already a # nonzero contribution to the gradient from the quadratic term. linear = math_ops.subtract(abs_error, quadratic) losses = math_ops.add( math_ops.multiply( ops.convert_to_tensor(0.5, dtype=quadratic.dtype), math_ops.multiply(quadratic, quadratic)), math_ops.multiply(delta, linear)) huber_loss = tf.reduce_sum(losses) / tf.reduce_sum(mask) return huber_loss
Example #7
Source File: deep_loglikelihood_uq_model.py From deep-quant with MIT License | 6 votes |
def _huber_loss(labels, predictions, config): """ Huber loss tensor""" delta = config.huber_delta predictions = math_ops.to_float(predictions) labels = math_ops.to_float(labels) predictions.get_shape().assert_is_compatible_with(labels.get_shape()) error = math_ops.subtract(predictions, labels) abs_error = math_ops.abs(error) quadratic = math_ops.minimum(abs_error, delta) # The following expression is the same in value as # tf.maximum(abs_error - delta, 0), but importantly the gradient for the # expression when abs_error == delta is 0 (for tf.maximum it would be 1). # This is necessary to avoid doubling the gradient, since there is already a # nonzero contribution to the gradient from the quadratic term. linear = math_ops.subtract(abs_error, quadratic) losses = math_ops.add( math_ops.multiply( ops.convert_to_tensor(0.5, dtype=quadratic.dtype), math_ops.multiply(quadratic, quadratic)), math_ops.multiply(delta, linear)) return losses
Example #8
Source File: deep_mlp_uq_model.py From deep-quant with MIT License | 6 votes |
def _huber_loss(labels, predictions, config): """ Huber loss tensor""" delta = config.huber_delta predictions = math_ops.to_float(predictions) labels = math_ops.to_float(labels) predictions.get_shape().assert_is_compatible_with(labels.get_shape()) error = math_ops.subtract(predictions, labels) abs_error = math_ops.abs(error) quadratic = math_ops.minimum(abs_error, delta) # The following expression is the same in value as # tf.maximum(abs_error - delta, 0), but importantly the gradient for the # expression when abs_error == delta is 0 (for tf.maximum it would be 1). # This is necessary to avoid doubling the gradient, since there is already a # nonzero contribution to the gradient from the quadratic term. linear = math_ops.subtract(abs_error, quadratic) losses = math_ops.add( math_ops.multiply( ops.convert_to_tensor(0.5, dtype=quadratic.dtype), math_ops.multiply(quadratic, quadratic)), math_ops.multiply(delta, linear)) return losses
Example #9
Source File: odes.py From deep_image_model with Apache License 2.0 | 6 votes |
def _optimal_step_size(last_step, error_ratio, safety=0.9, ifactor=10.0, dfactor=0.2, order=5, name=None): """Calculate the optimal size for the next Runge-Kutta step.""" with ops.name_scope( name, 'optimal_step_size', [last_step, error_ratio]) as scope: error_ratio = math_ops.cast(error_ratio, last_step.dtype) exponent = math_ops.cast(1 / order, last_step.dtype) # this looks more complex than necessary, but importantly it keeps # error_ratio in the numerator so we can't divide by zero: factor = math_ops.maximum( 1 / ifactor, math_ops.minimum(error_ratio ** exponent / safety, 1 / dfactor)) return math_ops.div(last_step, factor, name=scope)
Example #10
Source File: optimizers.py From deep_image_model with Apache License 2.0 | 6 votes |
def _adaptive_max_norm(norm, std_factor, decay, global_step, epsilon, name): """Find max_norm given norm and previous average.""" with vs.variable_scope(name, "AdaptiveMaxNorm", [norm]): log_norm = math_ops.log(norm + epsilon) def moving_average(name, value, decay): moving_average_variable = vs.get_variable( name, shape=value.get_shape(), dtype=value.dtype, initializer=init_ops.zeros_initializer, trainable=False) return moving_averages.assign_moving_average( moving_average_variable, value, decay, zero_debias=False) # quicker adaptation at the beginning if global_step is not None: n = math_ops.to_float(global_step) decay = math_ops.minimum(decay, n / (n + 1.)) # update averages mean = moving_average("mean", log_norm, decay) sq_mean = moving_average("sq_mean", math_ops.square(log_norm), decay) variance = sq_mean - math_ops.square(mean) std = math_ops.sqrt(math_ops.maximum(epsilon, variance)) max_norms = math_ops.exp(mean + std_factor*std) return max_norms, mean
Example #11
Source File: odes.py From keras-lambda with MIT License | 6 votes |
def _optimal_step_size(last_step, error_ratio, safety=0.9, ifactor=10.0, dfactor=0.2, order=5, name=None): """Calculate the optimal size for the next Runge-Kutta step.""" with ops.name_scope( name, 'optimal_step_size', [last_step, error_ratio]) as scope: error_ratio = math_ops.cast(error_ratio, last_step.dtype) exponent = math_ops.cast(1 / order, last_step.dtype) # this looks more complex than necessary, but importantly it keeps # error_ratio in the numerator so we can't divide by zero: factor = math_ops.maximum( 1 / ifactor, math_ops.minimum(error_ratio ** exponent / safety, 1 / dfactor)) return math_ops.div(last_step, factor, name=scope)
Example #12
Source File: backend.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def minimum(x, y): """Element-wise minimum of two tensors. Arguments: x: Tensor or variable. y: Tensor or variable. Returns: A tensor. """ return math_ops.minimum(x, y)
Example #13
Source File: clip_ops.py From deep_image_model with Apache License 2.0 | 5 votes |
def clip_by_average_norm(t, clip_norm, name=None): """Clips tensor values to a maximum average L2-norm. Given a tensor `t`, and a maximum clip value `clip_norm`, this operation normalizes `t` so that its average L2-norm is less than or equal to `clip_norm`. Specifically, if the average L2-norm is already less than or equal to `clip_norm`, then `t` is not modified. If the average L2-norm is greater than `clip_norm`, then this operation returns a tensor of the same type and shape as `t` with its values set to: `t * clip_norm / l2norm_avg(t)` In this case, the average L2-norm of the output tensor is `clip_norm`. This operation is typically used to clip gradients before applying them with an optimizer. Args: t: A `Tensor`. clip_norm: A 0-D (scalar) `Tensor` > 0. A maximum clipping value. name: A name for the operation (optional). Returns: A clipped `Tensor`. """ with ops.name_scope(name, "clip_by_average_norm", [t, clip_norm]) as name: t = ops.convert_to_tensor(t, name="t") # Calculate L2-norm per element, clip elements by ratio of clip_norm to # L2-norm per element n_element = math_ops.cast(array_ops.size(t), dtypes.float32) l2norm_inv = math_ops.rsqrt( math_ops.reduce_sum(t * t, math_ops.range(array_ops.rank(t)))) tclip = array_ops.identity( t * clip_norm * math_ops.minimum( l2norm_inv * n_element, constant_op.constant(1.0) / clip_norm), name=name) return tclip
Example #14
Source File: backend.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def argmin(x, axis=-1): """Returns the index of the minimum value along an axis. Arguments: x: Tensor or variable. axis: axis along which to perform the reduction. Returns: A tensor. """ return math_ops.argmin(x, axis)
Example #15
Source File: clip_ops.py From keras-lambda with MIT License | 5 votes |
def clip_by_value(t, clip_value_min, clip_value_max, name=None): """Clips tensor values to a specified min and max. Given a tensor `t`, this operation returns a tensor of the same type and shape as `t` with its values clipped to `clip_value_min` and `clip_value_max`. Any values less than `clip_value_min` are set to `clip_value_min`. Any values greater than `clip_value_max` are set to `clip_value_max`. Args: t: A `Tensor`. clip_value_min: A 0-D (scalar) `Tensor`. The minimum value to clip by. clip_value_max: A 0-D (scalar) `Tensor`. The maximum value to clip by. name: A name for the operation (optional). Returns: A clipped `Tensor`. """ with ops.name_scope(name, "clip_by_value", [t, clip_value_min, clip_value_max]) as name: t = ops.convert_to_tensor(t, name="t") # Go through list of tensors, for each value in each tensor clip t_min = math_ops.minimum(t, clip_value_max) t_max = math_ops.maximum(t_min, clip_value_min, name=name) return t_max
Example #16
Source File: clip_ops.py From keras-lambda with MIT License | 5 votes |
def clip_by_average_norm(t, clip_norm, name=None): """Clips tensor values to a maximum average L2-norm. Given a tensor `t`, and a maximum clip value `clip_norm`, this operation normalizes `t` so that its average L2-norm is less than or equal to `clip_norm`. Specifically, if the average L2-norm is already less than or equal to `clip_norm`, then `t` is not modified. If the average L2-norm is greater than `clip_norm`, then this operation returns a tensor of the same type and shape as `t` with its values set to: `t * clip_norm / l2norm_avg(t)` In this case, the average L2-norm of the output tensor is `clip_norm`. This operation is typically used to clip gradients before applying them with an optimizer. Args: t: A `Tensor`. clip_norm: A 0-D (scalar) `Tensor` > 0. A maximum clipping value. name: A name for the operation (optional). Returns: A clipped `Tensor`. """ with ops.name_scope(name, "clip_by_average_norm", [t, clip_norm]) as name: t = ops.convert_to_tensor(t, name="t") # Calculate L2-norm per element, clip elements by ratio of clip_norm to # L2-norm per element n_element = math_ops.cast(array_ops.size(t), dtypes.float32) l2norm_inv = math_ops.rsqrt( math_ops.reduce_sum(t * t, math_ops.range(array_ops.rank(t)))) tclip = array_ops.identity( t * clip_norm * math_ops.minimum( l2norm_inv * n_element, constant_op.constant(1.0) / clip_norm), name=name) return tclip
Example #17
Source File: metrics_impl.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def _num_relevant(labels, k): """Computes number of relevant values for each row in labels. For labels with shape [D1, ... DN, num_labels], this is the minimum of `num_labels` and `k`. Args: labels: `int64` `Tensor` or `SparseTensor` with shape [D1, ... DN, num_labels], where N >= 1 and num_labels is the number of target classes for the associated prediction. Commonly, N=1 and `labels` has shape [batch_size, num_labels]. k: Integer, k for @k metric. Returns: Integer `Tensor` of shape [D1, ... DN], where each value is the number of relevant values for that row. Raises: ValueError: if inputs have invalid dtypes or values. """ if k < 1: raise ValueError('Invalid k=%s.' % k) with ops.name_scope(None, 'num_relevant', (labels,)) as scope: # For SparseTensor, calculate separate count for each row. labels = sparse_tensor.convert_to_tensor_or_sparse_tensor(labels) if isinstance(labels, sparse_tensor.SparseTensor): return math_ops.minimum(sets.set_size(labels), k, name=scope) # For dense Tensor, calculate scalar count based on last dimension, and # tile across labels shape. labels_shape = array_ops.shape(labels) labels_size = labels_shape[-1] num_relevant_scalar = math_ops.minimum(labels_size, k) return array_ops.fill(labels_shape[0:-1], num_relevant_scalar, name=scope)
Example #18
Source File: image_ops_impl.py From keras-lambda with MIT License | 5 votes |
def per_image_standardization(image): """Linearly scales `image` to have zero mean and unit norm. This op computes `(x - mean) / adjusted_stddev`, where `mean` is the average of all values in image, and `adjusted_stddev = max(stddev, 1.0/sqrt(image.NumElements()))`. `stddev` is the standard deviation of all values in `image`. It is capped away from zero to protect against division by 0 when handling uniform images. Args: image: 3-D tensor of shape `[height, width, channels]`. Returns: The standardized image with same shape as `image`. Raises: ValueError: if the shape of 'image' is incompatible with this function. """ image = ops.convert_to_tensor(image, name='image') _Check3DImage(image, require_static=False) num_pixels = math_ops.reduce_prod(array_ops.shape(image)) image = math_ops.cast(image, dtype=dtypes.float32) image_mean = math_ops.reduce_mean(image) variance = (math_ops.reduce_mean(math_ops.square(image)) - math_ops.square(image_mean)) variance = gen_nn_ops.relu(variance) stddev = math_ops.sqrt(variance) # Apply a minimum normalization that protects us against uniform images. min_stddev = math_ops.rsqrt(math_ops.cast(num_pixels, dtypes.float32)) pixel_value_scale = math_ops.maximum(stddev, min_stddev) pixel_value_offset = image_mean image = math_ops.subtract(image, pixel_value_offset) image = math_ops.div(image, pixel_value_scale) return image
Example #19
Source File: topn.py From keras-lambda with MIT License | 5 votes |
def get_best(self, n): """Return the indices and values of the n highest scores in the TopN.""" def refresh_shortlist(): """Update the shortlist with the highest scores in id_to_score.""" new_scores, new_ids = nn_ops.top_k(self.id_to_score, self.shortlist_size) smallest_new_score = math_ops.reduce_min(new_scores) new_length = math_ops.reduce_sum( math_ops.to_int32(math_ops.greater(new_scores, dtypes.float32.min))) u1 = self.sl_ids.assign( math_ops.to_int64(array_ops.concat([[new_length], new_ids], 0))) u2 = self.sl_scores.assign( array_ops.concat([[smallest_new_score], new_scores], 0)) self.last_ops = [u1, u2] return control_flow_ops.group(u1, u2) # We only need to refresh the shortlist if n is greater than the # current shortlist size (which is stored in sl_ids[0]). with ops.control_dependencies(self.last_ops): cond_op = control_flow_ops.cond(n > self.sl_ids[0], refresh_shortlist, control_flow_ops.no_op) with ops.control_dependencies([cond_op]): topk_values, topk_indices = nn_ops.top_k( self.sl_scores, math_ops.minimum(n, math_ops.to_int32(self.sl_ids[0]))) # topk_indices are the indices into the shortlist, we want to return # the indices into id_to_score gathered_indices = array_ops.gather(self.sl_ids, topk_indices) return gathered_indices, topk_values
Example #20
Source File: metric_ops.py From keras-lambda with MIT License | 5 votes |
def num_relevant(labels, k): """Computes number of relevant values for each row in labels. For labels with shape [D1, ... DN, num_labels], this is the minimum of `num_labels` and `k`. Args: labels: `int64` `Tensor` or `SparseTensor` with shape [D1, ... DN, num_labels], where N >= 1 and num_labels is the number of target classes for the associated prediction. Commonly, N=1 and `labels` has shape [batch_size, num_labels]. k: Integer, k for @k metric. Returns: Integer `Tensor` of shape [D1, ... DN], where each value is the number of relevant values for that row. Raises: ValueError: if inputs have invalid dtypes or values. """ if k < 1: raise ValueError('Invalid k=%s.' % k) with ops.name_scope(None, 'num_relevant', (labels,)) as scope: # For SparseTensor, calculate separate count for each row. if isinstance( labels, (sparse_tensor.SparseTensor, sparse_tensor.SparseTensorValue)): labels_sizes = set_ops.set_size(labels) return math_ops.minimum(labels_sizes, k, name=scope) # For dense Tensor, calculate scalar count based on last dimension, and # tile across labels shape. labels_shape = array_ops.shape(labels) labels_size = labels_shape[-1] num_relevant_scalar = math_ops.minimum(labels_size, k) return array_ops.fill(labels_shape[0:-1], num_relevant_scalar, name=scope)
Example #21
Source File: optimizers.py From keras-lambda with MIT License | 5 votes |
def _adaptive_max_norm(norm, std_factor, decay, global_step, epsilon, name): """Find max_norm given norm and previous average.""" with vs.variable_scope(name, "AdaptiveMaxNorm", [norm]): log_norm = math_ops.log(norm + epsilon) def moving_average(name, value, decay): moving_average_variable = vs.get_variable( name, shape=value.get_shape(), dtype=value.dtype, initializer=init_ops.zeros_initializer(), trainable=False) return moving_averages.assign_moving_average( moving_average_variable, value, decay, zero_debias=False) # quicker adaptation at the beginning if global_step is not None: n = math_ops.to_float(global_step) decay = math_ops.minimum(decay, n / (n + 1.)) # update averages mean = moving_average("mean", log_norm, decay) sq_mean = moving_average("sq_mean", math_ops.square(log_norm), decay) variance = sq_mean - math_ops.square(mean) std = math_ops.sqrt(math_ops.maximum(epsilon, variance)) max_norms = math_ops.exp(mean + std_factor * std) return max_norms, mean
Example #22
Source File: official_tf_image.py From X-Detector with Apache License 2.0 | 5 votes |
def per_image_standardization(image): """Linearly scales `image` to have zero mean and unit norm. This op computes `(x - mean) / adjusted_stddev`, where `mean` is the average of all values in image, and `adjusted_stddev = max(stddev, 1.0/sqrt(image.NumElements()))`. `stddev` is the standard deviation of all values in `image`. It is capped away from zero to protect against division by 0 when handling uniform images. Args: image: 3-D tensor of shape `[height, width, channels]`. Returns: The standardized image with same shape as `image`. Raises: ValueError: if the shape of 'image' is incompatible with this function. """ image = ops.convert_to_tensor(image, name='image') image = control_flow_ops.with_dependencies( _Check3DImage(image, require_static=False), image) num_pixels = math_ops.reduce_prod(array_ops.shape(image)) image = math_ops.cast(image, dtype=dtypes.float32) image_mean = math_ops.reduce_mean(image) variance = (math_ops.reduce_mean(math_ops.square(image)) - math_ops.square(image_mean)) variance = gen_nn_ops.relu(variance) stddev = math_ops.sqrt(variance) # Apply a minimum normalization that protects us against uniform images. min_stddev = math_ops.rsqrt(math_ops.cast(num_pixels, dtypes.float32)) pixel_value_scale = math_ops.maximum(stddev, min_stddev) pixel_value_offset = image_mean image = math_ops.subtract(image, pixel_value_offset) image = math_ops.div(image, pixel_value_scale) return image
Example #23
Source File: metrics_impl.py From keras-lambda with MIT License | 5 votes |
def _num_relevant(labels, k): """Computes number of relevant values for each row in labels. For labels with shape [D1, ... DN, num_labels], this is the minimum of `num_labels` and `k`. Args: labels: `int64` `Tensor` or `SparseTensor` with shape [D1, ... DN, num_labels], where N >= 1 and num_labels is the number of target classes for the associated prediction. Commonly, N=1 and `labels` has shape [batch_size, num_labels]. k: Integer, k for @k metric. Returns: Integer `Tensor` of shape [D1, ... DN], where each value is the number of relevant values for that row. Raises: ValueError: if inputs have invalid dtypes or values. """ if k < 1: raise ValueError('Invalid k=%s.' % k) with ops.name_scope(None, 'num_relevant', (labels,)) as scope: # For SparseTensor, calculate separate count for each row. labels = sparse_tensor.convert_to_tensor_or_sparse_tensor(labels) if isinstance(labels, sparse_tensor.SparseTensor): return math_ops.minimum(sets.set_size(labels), k, name=scope) # For dense Tensor, calculate scalar count based on last dimension, and # tile across labels shape. labels_shape = array_ops.shape(labels) labels_size = labels_shape[-1] num_relevant_scalar = math_ops.minimum(labels_size, k) return array_ops.fill(labels_shape[0:-1], num_relevant_scalar, name=scope)
Example #24
Source File: test_forward.py From incubator-tvm with Apache License 2.0 | 5 votes |
def _test_minimum(data): """ One iteration of minimum """ return _test_elemwise(math_ops.minimum, data) ####################################################################### # Greater # -------
Example #25
Source File: test_forward.py From incubator-tvm with Apache License 2.0 | 5 votes |
def with_fused_activation_function(input_tensor, fn_name): if fn_name is None or fn_name == "NONE": return input_tensor if fn_name == "RELU": return nn_ops.relu(input_tensor) if fn_name == "RELU6": return nn_ops.relu6(input_tensor) if fn_name == "RELU_N1_TO_1": return math_ops.maximum(-1, math_ops.minimum(input_tensor, 1)) if fn_name == "TANH": return math_ops.tanh(input_tensor) raise AssertionError("Unknown fused_activation_function {}".format(fn_name))
Example #26
Source File: optimizer.py From noisy-K-FAC with Apache License 2.0 | 5 votes |
def _update_clip_coeff(self, grads_and_vars, precon_grads_and_vars): sq_norm_grad = self._squared_fisher_norm(grads_and_vars, precon_grads_and_vars) sq_norm_up = sq_norm_grad * self._learning_rate**2 return math_ops.minimum(1., math_ops.sqrt(self._norm_constraint / sq_norm_up))
Example #27
Source File: IPWrank_model.py From Unbiased-Learning-to-Rank-with-Unbiased-Propensity-Estimation with Apache License 2.0 | 5 votes |
def clip_by_each_value(self, t_list, clip_max_value = None, clip_min_value = None, name=None): if (not isinstance(t_list, collections.Sequence) or isinstance(t_list, six.string_types)): raise TypeError("t_list should be a sequence") t_list = list(t_list) with ops.name_scope(name, "clip_by_each_value",t_list + [clip_norm]) as name: values = [ ops.convert_to_tensor( t.values if isinstance(t, ops.IndexedSlices) else t, name="t_%d" % i) if t is not None else t for i, t in enumerate(t_list)] values_clipped = [] for i, v in enumerate(values): if v is None: values_clipped.append(None) else: t = None if clip_value_max != None: t = math_ops.minimum(v, clip_value_max) if clip_value_min != None: t = math_ops.maximum(t, clip_value_min, name=name) with ops.colocate_with(t): values_clipped.append( tf.identity(t, name="%s_%d" % (name, i))) list_clipped = [ ops.IndexedSlices(c_v, t.indices, t.dense_shape) if isinstance(t, ops.IndexedSlices) else c_v for (c_v, t) in zip(values_clipped, t_list)] return list_clipped
Example #28
Source File: DLA_model.py From Unbiased-Learning-to-Rank-with-Unbiased-Propensity-Estimation with Apache License 2.0 | 5 votes |
def clip_by_each_value(self, t_list, clip_max_value = None, clip_min_value = None, name=None): if (not isinstance(t_list, collections.Sequence) or isinstance(t_list, six.string_types)): raise TypeError("t_list should be a sequence") t_list = list(t_list) with ops.name_scope(name, "clip_by_each_value",t_list + [clip_norm]) as name: values = [ ops.convert_to_tensor( t.values if isinstance(t, ops.IndexedSlices) else t, name="t_%d" % i) if t is not None else t for i, t in enumerate(t_list)] values_clipped = [] for i, v in enumerate(values): if v is None: values_clipped.append(None) else: t = None if clip_value_max != None: t = math_ops.minimum(v, clip_value_max) if clip_value_min != None: t = math_ops.maximum(t, clip_value_min, name=name) with ops.colocate_with(t): values_clipped.append( tf.identity(t, name="%s_%d" % (name, i))) list_clipped = [ ops.IndexedSlices(c_v, t.indices, t.dense_shape) if isinstance(t, ops.IndexedSlices) else c_v for (c_v, t) in zip(values_clipped, t_list)] return list_clipped
Example #29
Source File: metric_ops.py From deep_image_model with Apache License 2.0 | 5 votes |
def num_relevant(labels, k): """Computes number of relevant values for each row in labels. For labels with shape [D1, ... DN, num_labels], this is the minimum of `num_labels` and `k`. Args: labels: `int64` `Tensor` or `SparseTensor` with shape [D1, ... DN, num_labels], where N >= 1 and num_labels is the number of target classes for the associated prediction. Commonly, N=1 and `labels` has shape [batch_size, num_labels]. k: Integer, k for @k metric. Returns: Integer `Tensor` of shape [D1, ... DN], where each value is the number of relevant values for that row. Raises: ValueError: if inputs have invalid dtypes or values. """ if k < 1: raise ValueError('Invalid k=%s.' % k) with ops.name_scope(None, 'num_relevant', (labels,)) as scope: # For SparseTensor, calculate separate count for each row. if isinstance( labels, (sparse_tensor.SparseTensor, sparse_tensor.SparseTensorValue)): labels_sizes = set_ops.set_size(labels) return math_ops.minimum(labels_sizes, k, name=scope) # For dense Tensor, calculate scalar count based on last dimension, and # tile across labels shape. labels_shape = array_ops.shape(labels) labels_size = labels_shape[-1] num_relevant_scalar = math_ops.minimum(labels_size, k) return array_ops.fill(labels_shape[0:-1], num_relevant_scalar, name=scope)
Example #30
Source File: optimizers.py From tensornets with MIT License | 5 votes |
def _adaptive_max_norm(norm, std_factor, decay, global_step, epsilon, name): """Find max_norm given norm and previous average.""" with vs.variable_scope(name, "AdaptiveMaxNorm", [norm]): log_norm = math_ops.log(norm + epsilon) def moving_average(name, value, decay): moving_average_variable = vs.get_variable( name, shape=value.get_shape(), dtype=value.dtype, initializer=init_ops.zeros_initializer(), trainable=False) return moving_averages.assign_moving_average( moving_average_variable, value, decay, zero_debias=False) # quicker adaptation at the beginning if global_step is not None: n = math_ops.cast(global_step, dtypes.float32) decay = math_ops.minimum(decay, n / (n + 1.)) # update averages mean = moving_average("mean", log_norm, decay) sq_mean = moving_average("sq_mean", math_ops.square(log_norm), decay) variance = sq_mean - math_ops.square(mean) std = math_ops.sqrt(math_ops.maximum(epsilon, variance)) max_norms = math_ops.exp(mean + std_factor * std) return max_norms, mean