Python tensorflow.python.ops.math_ops.range() Examples
The following are 30
code examples of tensorflow.python.ops.math_ops.range().
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: mixture.py From lambda-packs with MIT License | 6 votes |
def _mean(self): with ops.control_dependencies(self._assertions): distribution_means = [d.mean() for d in self.components] cat_probs = self._cat_probs(log_probs=False) # This was checked to not be None at construction time. static_event_rank = self.event_shape.ndims # Expand the rank of x up to static_event_rank times so that # broadcasting works correctly. def expand(x): expanded_x = x for _ in range(static_event_rank): expanded_x = array_ops.expand_dims(expanded_x, -1) return expanded_x cat_probs = [expand(c_p) for c_p in cat_probs] partial_means = [ c_p * m for (c_p, m) in zip(cat_probs, distribution_means) ] # These should all be the same shape by virtue of matching # batch_shape and event_shape. return math_ops.add_n(partial_means)
Example #2
Source File: tensor_array_ops.py From lambda-packs with MIT License | 6 votes |
def unstack(self, value, name=None): """Unstack the values of a `Tensor` in the TensorArray. If input value shapes have rank-`R`, then the output TensorArray will contain elements whose shapes are rank-`(R-1)`. Args: value: (N+1)-D. Tensor of type `dtype`. The Tensor to unstack. name: A name for the operation (optional). Returns: A new TensorArray object with flow that ensures the unstack occurs. Use this object all for subsequent operations. Raises: ValueError: if the shape inference fails. """ with ops.name_scope(name, "TensorArrayUnstack", [self._handle, value]): num_elements = array_ops.shape(value)[0] return self.scatter( indices=math_ops.range(0, num_elements), value=value, name=name)
Example #3
Source File: array_grad.py From lambda-packs with MIT License | 6 votes |
def _TileGrad(op, grad): """Sum reduces grad along the tiled dimensions.""" assert isinstance(grad, ops.Tensor) input_shape = array_ops.shape(op.inputs[0]) # We interleave multiples and input_shape to get split_shape, # reshape grad to split_shape, and reduce along all even # dimensions (the tiled dimensions) to get the result # with shape input_shape. For example # input_shape = [20, 30, 40] # multiples = [2, 3, 4] # split_shape = [2, 20, 3, 30, 4, 40] # axes = [0, 2, 4] split_shape = array_ops.reshape( array_ops.transpose(array_ops.stack([op.inputs[1], input_shape])), [-1]) axes = math_ops.range(0, array_ops.size(split_shape), 2) input_grad = math_ops.reduce_sum(array_ops.reshape(grad, split_shape), axes) # Fix shape inference input_grad.set_shape(op.inputs[0].get_shape()) return [input_grad, None]
Example #4
Source File: sharded_mutable_dense_hashtable.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def insert(self, keys, values, name=None): self._check_keys(keys) num_shards = self._num_shards if num_shards == 1: return self._table_shards[0].insert(keys, values, name=name) shard_indices = self._shard_indices(keys) # TODO(andreasst): support 'keys' that are not vectors key_shards = data_flow_ops.dynamic_partition(keys, shard_indices, num_shards) value_shards = data_flow_ops.dynamic_partition(values, shard_indices, num_shards) return_values = [ self._table_shards[i].insert(key_shards[i], value_shards[i], name=name) for i in range(num_shards) ] return control_flow_ops.group(*return_values)
Example #5
Source File: bijector_impl.py From lambda-packs with MIT License | 6 votes |
def _event_dims_tensor(self, sample): """Return a 1D `int32` tensor: `range(rank(sample))[-event_ndims:]`.""" if self.event_ndims is None: raise ValueError("Jacobian cannot be computed with unknown event_ndims") static_event_ndims = tensor_util.constant_value(self.event_ndims) static_rank = sample.get_shape().ndims if static_event_ndims is not None and static_rank is not None: return ops.convert_to_tensor( static_rank + np.arange(-static_event_ndims, 0).astype(np.int32)) if static_event_ndims is not None: event_range = np.arange(-static_event_ndims, 0).astype(np.int32) else: event_range = math_ops.range(-self.event_ndims, 0, dtype=dtypes.int32) if static_rank is not None: return event_range + static_rank else: return event_range + array_ops.rank(sample)
Example #6
Source File: string_ops.py From lambda-packs with MIT License | 6 votes |
def _reduce_join_reduction_dims(x, axis, reduction_indices): """Returns range(rank(x) - 1, 0, -1) if reduction_indices is None.""" # TODO(aselle): Remove this after deprecation if reduction_indices is not None: if axis is not None: raise ValueError("Can't specify both 'axis' and 'reduction_indices'.") axis = reduction_indices if axis is not None: return axis else: # Fast path: avoid creating Rank and Range ops if ndims is known. if isinstance(x, ops.Tensor) and x.get_shape().ndims is not None: return constant_op.constant( np.arange(x.get_shape().ndims - 1, -1, -1), dtype=dtypes.int32) # Otherwise, we rely on Range and Rank to do the right thing at run-time. return math_ops.range(array_ops.rank(x) - 1, -1, -1)
Example #7
Source File: nn_grad.py From lambda-packs with MIT License | 6 votes |
def _BiasAddGradV1(unused_bias_op, received_grad): """Return the gradients for the 2 inputs of bias_op. The first input of unused_bias_op is the tensor t, and its gradient is just the gradient the unused_bias_op received. The second input of unused_bias_op is the bias vector which has one fewer dimension than "received_grad" (the batch dimension.) Its gradient is the received gradient Summed on the batch dimension, which is the first dimension. Args: unused_bias_op: The BiasOp for which we need to generate gradients. received_grad: Tensor. The gradients passed to the BiasOp. Returns: Two tensors, the first one for the "tensor" input of the BiasOp, the second one for the "bias" input of the BiasOp. """ reduction_dim_tensor = math_ops.range(array_ops.rank(received_grad) - 1) return (received_grad, math_ops.reduce_sum(received_grad, reduction_dim_tensor))
Example #8
Source File: sharded_mutable_dense_hashtable.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def __init__(self, key_dtype, value_dtype, default_value, empty_key, num_shards=1, name='ShardedMutableHashTable'): with ops.name_scope(name, 'sharded_mutable_hash_table') as scope: super(ShardedMutableDenseHashTable, self).__init__(key_dtype, value_dtype, scope) table_shards = [] for i in range(num_shards): table_shards.append( lookup.MutableDenseHashTable( key_dtype=key_dtype, value_dtype=value_dtype, default_value=default_value, empty_key=empty_key, name='%s-%d-of-%d' % (name, i + 1, num_shards))) self._table_shards = table_shards # TODO(andreasst): add a value_shape() method to LookupInterface # pylint: disable=protected-access self._value_shape = self._table_shards[0]._value_shape # pylint: enable=protected-access
Example #9
Source File: crf.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _lengths_to_masks(lengths, max_length): """Creates a binary matrix that can be used to mask away padding. Args: lengths: A vector of integers representing lengths. max_length: An integer indicating the maximum length. All values in lengths should be less than max_length. Returns: masks: Masks that can be used to get rid of padding. """ tiled_ranges = array_ops.tile( array_ops.expand_dims(math_ops.range(max_length), 0), [array_ops.shape(lengths)[0], 1]) lengths = array_ops.expand_dims(lengths, 1) masks = math_ops.to_float( math_ops.to_int64(tiled_ranges) < math_ops.to_int64(lengths)) return masks
Example #10
Source File: nn_grad.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _BiasAddGradV1(unused_bias_op, received_grad): """Return the gradients for the 2 inputs of bias_op. The first input of unused_bias_op is the tensor t, and its gradient is just the gradient the unused_bias_op received. The second input of unused_bias_op is the bias vector which has one fewer dimension than "received_grad" (the batch dimension.) Its gradient is the received gradient Summed on the batch dimension, which is the first dimension. Args: unused_bias_op: The BiasOp for which we need to generate gradients. received_grad: Tensor. The gradients passed to the BiasOp. Returns: Two tensors, the first one for the "tensor" input of the BiasOp, the second one for the "bias" input of the BiasOp. """ reduction_dim_tensor = math_ops.range(array_ops.rank(received_grad) - 1) return (received_grad, math_ops.reduce_sum(received_grad, reduction_dim_tensor))
Example #11
Source File: string_ops.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _reduce_join_reduction_dims(x, axis, reduction_indices): """Returns range(rank(x) - 1, 0, -1) if reduction_indices is None.""" # TODO(aselle): Remove this after deprecation if reduction_indices is not None: if axis is not None: raise ValueError("Can't specify both 'axis' and 'reduction_indices'.") axis = reduction_indices if axis is not None: return axis else: # Fast path: avoid creating Rank and Range ops if ndims is known. if isinstance(x, ops.Tensor) and x.get_shape().ndims is not None: return constant_op.constant( np.arange(x.get_shape().ndims - 1, -1, -1), dtype=dtypes.int32) # Otherwise, we rely on Range and Rank to do the right thing at run-time. return math_ops.range(array_ops.rank(x) - 1, -1, -1)
Example #12
Source File: crf.py From lambda-packs with MIT License | 6 votes |
def _lengths_to_masks(lengths, max_length): """Creates a binary matrix that can be used to mask away padding. Args: lengths: A vector of integers representing lengths. max_length: An integer indicating the maximum length. All values in lengths should be less than max_length. Returns: masks: Masks that can be used to get rid of padding. """ tiled_ranges = array_ops.tile( array_ops.expand_dims(math_ops.range(max_length), 0), [array_ops.shape(lengths)[0], 1]) lengths = array_ops.expand_dims(lengths, 1) masks = math_ops.to_float( math_ops.to_int64(tiled_ranges) < math_ops.to_int64(lengths)) return masks
Example #13
Source File: array_grad.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _TileGrad(op, grad): """Sum reduces grad along the tiled dimensions.""" assert isinstance(grad, ops.Tensor) input_shape = array_ops.shape(op.inputs[0]) # We interleave multiples and input_shape to get split_shape, # reshape grad to split_shape, and reduce along all even # dimensions (the tiled dimensions) to get the result # with shape input_shape. For example # input_shape = [20, 30, 40] # multiples = [2, 3, 4] # split_shape = [2, 20, 3, 30, 4, 40] # axes = [0, 2, 4] split_shape = array_ops.reshape( array_ops.transpose(array_ops.stack([op.inputs[1], input_shape])), [-1]) axes = math_ops.range(0, array_ops.size(split_shape), 2) input_grad = math_ops.reduce_sum(array_ops.reshape(grad, split_shape), axes) # Fix shape inference input_grad.set_shape(op.inputs[0].get_shape()) return [input_grad, None]
Example #14
Source File: backend.py From lambda-packs with MIT License | 6 votes |
def count_params(x): """Returns the number of scalars in a Keras variable. Arguments: x: Keras variable. Returns: Integer, the number of scalars in `x`. Example: ```python >>> kvar = K.zeros((2,3)) >>> K.count_params(kvar) 6 >>> K.eval(kvar) array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) ``` """ shape = x.get_shape() return np.prod([shape[i]._value for i in range(len(shape))])
Example #15
Source File: tensor_array_ops.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def unstack(self, value, name=None): """Unstack the values of a `Tensor` in the TensorArray. If input value shapes have rank-`R`, then the output TensorArray will contain elements whose shapes are rank-`(R-1)`. Args: value: (N+1)-D. Tensor of type `dtype`. The Tensor to unstack. name: A name for the operation (optional). Returns: A new TensorArray object with flow that ensures the unstack occurs. Use this object all for subsequent operations. Raises: ValueError: if the shape inference fails. """ with ops.name_scope(name, "TensorArrayUnstack", [self._handle, value]): num_elements = array_ops.shape(value)[0] return self.scatter( indices=math_ops.range(0, num_elements), value=value, name=name)
Example #16
Source File: sharded_mutable_dense_hashtable.py From lambda-packs with MIT License | 6 votes |
def __init__(self, key_dtype, value_dtype, default_value, empty_key, num_shards=1, name='ShardedMutableHashTable'): with ops.name_scope(name, 'sharded_mutable_hash_table') as scope: super(ShardedMutableDenseHashTable, self).__init__(key_dtype, value_dtype, scope) table_shards = [] for i in range(num_shards): table_shards.append( lookup.MutableDenseHashTable( key_dtype=key_dtype, value_dtype=value_dtype, default_value=default_value, empty_key=empty_key, name='%s-%d-of-%d' % (name, i + 1, num_shards))) self._table_shards = table_shards # TODO(andreasst): add a value_shape() method to LookupInterface # pylint: disable=protected-access self._value_shape = self._table_shards[0]._value_shape # pylint: enable=protected-access
Example #17
Source File: sharded_mutable_dense_hashtable.py From lambda-packs with MIT License | 6 votes |
def insert(self, keys, values, name=None): self._check_keys(keys) num_shards = self._num_shards if num_shards == 1: return self._table_shards[0].insert(keys, values, name=name) shard_indices = self._shard_indices(keys) # TODO(andreasst): support 'keys' that are not vectors key_shards = data_flow_ops.dynamic_partition(keys, shard_indices, num_shards) value_shards = data_flow_ops.dynamic_partition(values, shard_indices, num_shards) return_values = [ self._table_shards[i].insert(key_shards[i], value_shards[i], name=name) for i in range(num_shards) ] return control_flow_ops.group(*return_values)
Example #18
Source File: dynamic_decoder.py From tensorflow_end2end_speech_recognition with MIT License | 6 votes |
def _transpose_batch_time(x): """Transpose the batch and time dimensions of a Tensor. Retains as much of the static shape information as possible. Args: x: A tensor of rank 2 or higher. Returns: x transposed along the first two dimensions. Raises: ValueError: if `x` is rank 1 or lower. """ x_static_shape = x.get_shape() if x_static_shape.ndims is not None and x_static_shape.ndims < 2: raise ValueError( "Expected input tensor %s to have rank at least 2, but saw shape: %s" % (x, x_static_shape)) x_rank = array_ops.rank(x) x_t = array_ops.transpose( x, array_ops.concat( ([1, 0], math_ops.range(2, x_rank)), axis=0)) x_t.set_shape( tensor_shape.TensorShape([ x_static_shape[1].value, x_static_shape[0].value ]).concatenate(x_static_shape[2:])) return x_t
Example #19
Source File: rnn.py From lambda-packs with MIT License | 5 votes |
def _transpose_batch_time(x): """Transpose the batch and time dimensions of a Tensor. Retains as much of the static shape information as possible. Args: x: A tensor of rank 2 or higher. Returns: x transposed along the first two dimensions. Raises: ValueError: if `x` is rank 1 or lower. """ x_static_shape = x.get_shape() if x_static_shape.ndims is not None and x_static_shape.ndims < 2: raise ValueError( "Expected input tensor %s to have rank at least 2, but saw shape: %s" % (x, x_static_shape)) x_rank = array_ops.rank(x) x_t = array_ops.transpose( x, array_ops.concat( ([1, 0], math_ops.range(2, x_rank)), axis=0)) x_t.set_shape( tensor_shape.TensorShape([ x_static_shape[1].value, x_static_shape[0].value ]).concatenate(x_static_shape[2:])) return x_t
Example #20
Source File: operator_pd.py From lambda-packs with MIT License | 5 votes |
def _flip_vector_to_matrix_static(vec, batch_shape): """flip_vector_to_matrix with static shapes.""" # Shapes associated with batch_shape batch_rank = batch_shape.ndims # Shapes associated with vec. vec = ops.convert_to_tensor(vec, name="vec") vec_shape = vec.get_shape() vec_rank = len(vec_shape) vec_batch_rank = vec_rank - 1 m = vec_batch_rank - batch_rank # vec_shape_left = [M1,...,Mm] or []. vec_shape_left = vec_shape[:m] # If vec_shape_left = [], then condensed_shape = [1] since reduce_prod([]) = 1 # If vec_shape_left = [M1,...,Mm], condensed_shape = [M1*...*Mm] condensed_shape = [np.prod(vec_shape_left)] k = vec_shape[-1] new_shape = batch_shape.concatenate(k).concatenate(condensed_shape) def _flip_front_dims_to_back(): # Permutation corresponding to [N1,...,Nn] + [k, M1,...,Mm] perm = array_ops.concat((math_ops.range(m, vec_rank), math_ops.range(0, m)), 0) return array_ops.transpose(vec, perm=perm) if 0 < m: x_flipped = _flip_front_dims_to_back() else: x_flipped = array_ops.expand_dims(vec, -1) return array_ops.reshape(x_flipped, new_shape)
Example #21
Source File: operator_pd.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def _flip_vector_to_matrix_static(vec, batch_shape): """flip_vector_to_matrix with static shapes.""" # Shapes associated with batch_shape batch_rank = batch_shape.ndims # Shapes associated with vec. vec = ops.convert_to_tensor(vec, name="vec") vec_shape = vec.get_shape() vec_rank = len(vec_shape) vec_batch_rank = vec_rank - 1 m = vec_batch_rank - batch_rank # vec_shape_left = [M1,...,Mm] or []. vec_shape_left = vec_shape[:m] # If vec_shape_left = [], then condensed_shape = [1] since reduce_prod([]) = 1 # If vec_shape_left = [M1,...,Mm], condensed_shape = [M1*...*Mm] condensed_shape = [np.prod(vec_shape_left)] k = vec_shape[-1] new_shape = batch_shape.concatenate(k).concatenate(condensed_shape) def _flip_front_dims_to_back(): # Permutation corresponding to [N1,...,Nn] + [k, M1,...,Mm] perm = array_ops.concat((math_ops.range(m, vec_rank), math_ops.range(0, m)), 0) return array_ops.transpose(vec, perm=perm) if 0 < m: x_flipped = _flip_front_dims_to_back() else: x_flipped = array_ops.expand_dims(vec, -1) return array_ops.reshape(x_flipped, new_shape)
Example #22
Source File: operator_pd.py From lambda-packs with MIT License | 5 votes |
def _flip_matrix_to_vector_static(mat, static_batch_shape): """Flip matrix to vector with static shapes.""" mat_rank = mat.get_shape().ndims k = mat.get_shape()[-2] final_shape = static_batch_shape.concatenate(k) # mat.shape = matrix_batch_shape + [k, M] # Permutation corresponding to [M] + matrix_batch_shape + [k] perm = [mat_rank - 1] + list(range(0, mat_rank - 1)) mat_with_end_at_beginning = array_ops.transpose(mat, perm=perm) vector = array_ops.reshape(mat_with_end_at_beginning, final_shape) return vector
Example #23
Source File: feature_column.py From lambda-packs with MIT License | 5 votes |
def to_sparse_tensor(self, input_tensor): """Creates a SparseTensor from the bucketized Tensor.""" dimension = self.source_column.dimension batch_size = array_ops.shape(input_tensor, name="shape")[0] if dimension > 1: i1 = array_ops.reshape( array_ops.tile( array_ops.expand_dims( math_ops.range(0, batch_size), 1, name="expand_dims"), [1, dimension], name="tile"), [-1], name="reshape") i2 = array_ops.tile( math_ops.range(0, dimension), [batch_size], name="tile") # Flatten the bucket indices and unique them across dimensions # E.g. 2nd dimension indices will range from k to 2*k-1 with k buckets bucket_indices = array_ops.reshape( input_tensor, [-1], name="reshape") + self.length * i2 else: # Simpler indices when dimension=1 i1 = math_ops.range(0, batch_size) i2 = array_ops.zeros([batch_size], dtype=dtypes.int32, name="zeros") bucket_indices = array_ops.reshape(input_tensor, [-1], name="reshape") indices = math_ops.to_int64(array_ops.transpose(array_ops.stack((i1, i2)))) shape = math_ops.to_int64(array_ops.stack([batch_size, dimension])) sparse_id_values = sparse_tensor_py.SparseTensor( indices, bucket_indices, shape) return sparse_id_values
Example #24
Source File: operator_pd.py From lambda-packs with MIT License | 5 votes |
def _flip_vector_to_matrix_dynamic(vec, batch_shape): """flip_vector_to_matrix with dynamic shapes.""" # Shapes associated with batch_shape batch_rank = array_ops.size(batch_shape) # Shapes associated with vec. vec = ops.convert_to_tensor(vec, name="vec") vec_shape = array_ops.shape(vec) vec_rank = array_ops.rank(vec) vec_batch_rank = vec_rank - 1 m = vec_batch_rank - batch_rank # vec_shape_left = [M1,...,Mm] or []. vec_shape_left = array_ops.strided_slice(vec_shape, [0], [m]) # If vec_shape_left = [], then condensed_shape = [1] since reduce_prod([]) = 1 # If vec_shape_left = [M1,...,Mm], condensed_shape = [M1*...*Mm] condensed_shape = [math_ops.reduce_prod(vec_shape_left)] k = array_ops.gather(vec_shape, vec_rank - 1) new_shape = array_ops.concat((batch_shape, [k], condensed_shape), 0) def _flip_front_dims_to_back(): # Permutation corresponding to [N1,...,Nn] + [k, M1,...,Mm] perm = array_ops.concat((math_ops.range(m, vec_rank), math_ops.range(0, m)), 0) return array_ops.transpose(vec, perm=perm) x_flipped = control_flow_ops.cond( math_ops.less(0, m), _flip_front_dims_to_back, lambda: array_ops.expand_dims(vec, -1)) return array_ops.reshape(x_flipped, new_shape)
Example #25
Source File: data_flow_grad.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def _DynamicPartitionGrads(op, *grads): """Gradients for DynamicPartition.""" data = op.inputs[0] indices = op.inputs[1] num_partitions = op.get_attr("num_partitions") prefix_shape = array_ops.shape(indices) original_indices = array_ops.reshape( math_ops.range(math_ops.reduce_prod(prefix_shape)), prefix_shape) partitioned_indices = data_flow_ops.dynamic_partition( original_indices, indices, num_partitions) reconstructed = data_flow_ops.dynamic_stitch(partitioned_indices, grads) reconstructed = array_ops.reshape(reconstructed, array_ops.shape(data)) return [reconstructed, None]
Example #26
Source File: operator_pd.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def _flip_vector_to_matrix_dynamic(vec, batch_shape): """flip_vector_to_matrix with dynamic shapes.""" # Shapes associated with batch_shape batch_rank = array_ops.size(batch_shape) # Shapes associated with vec. vec = ops.convert_to_tensor(vec, name="vec") vec_shape = array_ops.shape(vec) vec_rank = array_ops.rank(vec) vec_batch_rank = vec_rank - 1 m = vec_batch_rank - batch_rank # vec_shape_left = [M1,...,Mm] or []. vec_shape_left = array_ops.strided_slice(vec_shape, [0], [m]) # If vec_shape_left = [], then condensed_shape = [1] since reduce_prod([]) = 1 # If vec_shape_left = [M1,...,Mm], condensed_shape = [M1*...*Mm] condensed_shape = [math_ops.reduce_prod(vec_shape_left)] k = array_ops.gather(vec_shape, vec_rank - 1) new_shape = array_ops.concat((batch_shape, [k], condensed_shape), 0) def _flip_front_dims_to_back(): # Permutation corresponding to [N1,...,Nn] + [k, M1,...,Mm] perm = array_ops.concat((math_ops.range(m, vec_rank), math_ops.range(0, m)), 0) return array_ops.transpose(vec, perm=perm) x_flipped = control_flow_ops.cond( math_ops.less(0, m), _flip_front_dims_to_back, lambda: array_ops.expand_dims(vec, -1)) return array_ops.reshape(x_flipped, new_shape)
Example #27
Source File: clip_ops.py From auto-alt-text-lambda-api 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 #28
Source File: sparse_grad.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def _SparseReorderGrad(op, unused_output_indices_grad, output_values_grad): """Gradients for the SparseReorder op. Args: op: the SparseReorder op unused_output_indices_grad: the incoming gradients of the output indices output_values_grad: the incoming gradients of the output values Returns: Gradient for each of the 3 input tensors: (input_indices, input_values, input_shape) The gradients for input_indices and input_shape is None. """ input_indices = op.inputs[0] input_shape = op.inputs[2] num_entries = array_ops.shape(input_indices)[0] entry_indices = math_ops.range(num_entries) sp_unordered = sparse_tensor.SparseTensor( input_indices, entry_indices, input_shape) sp_ordered = sparse_ops.sparse_reorder(sp_unordered) inverted_permutation = array_ops.invert_permutation(sp_ordered.values) return (None, array_ops.gather(output_values_grad, inverted_permutation), None)
Example #29
Source File: feature_column.py From lambda-packs with MIT License | 5 votes |
def __new__(cls, source_column, boundaries): if not isinstance(source_column, _RealValuedColumn): raise TypeError("source_column must be an instance of _RealValuedColumn. " "source_column: {}".format(source_column)) if source_column.dimension is None: raise ValueError("source_column must have a defined dimension. " "source_column: {}".format(source_column)) if (not isinstance(boundaries, list) and not isinstance(boundaries, tuple)) or not boundaries: raise ValueError("boundaries must be a non-empty list or tuple. " "boundaries: {}".format(boundaries)) # We allow bucket boundaries to be monotonically increasing # (ie a[i+1] >= a[i]). When two bucket boundaries are the same, we # de-duplicate. sanitized_boundaries = [] for i in range(len(boundaries) - 1): if boundaries[i] == boundaries[i + 1]: continue elif boundaries[i] < boundaries[i + 1]: sanitized_boundaries.append(boundaries[i]) else: raise ValueError("boundaries must be a sorted list. " "boundaries: {}".format(boundaries)) sanitized_boundaries.append(boundaries[len(boundaries) - 1]) return super(_BucketizedColumn, cls).__new__(cls, source_column, tuple(sanitized_boundaries))
Example #30
Source File: operator_pd.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def _flip_matrix_to_vector_dynamic(mat, batch_shape): """Flip matrix to vector with dynamic shapes.""" mat_rank = array_ops.rank(mat) k = array_ops.gather(array_ops.shape(mat), mat_rank - 2) final_shape = array_ops.concat((batch_shape, [k]), 0) # mat.shape = matrix_batch_shape + [k, M] # Permutation corresponding to [M] + matrix_batch_shape + [k] perm = array_ops.concat(([mat_rank - 1], math_ops.range(0, mat_rank - 1)), 0) mat_with_end_at_beginning = array_ops.transpose(mat, perm=perm) vector = array_ops.reshape(mat_with_end_at_beginning, final_shape) return vector