def _class_weights_map_fn(*data): """Convert `class_weight` to `sample_weight`.""" x, y, sw = unpack_x_y_sample_weight(data) if nest.is_sequence(y): raise ValueError( "`class_weight` is only supported for Models with a single output.") if y.shape.rank > 2: raise ValueError("`class_weight` not supported for " "3+ dimensional targets.") y_classes = smart_cond.smart_cond( y.shape.rank == 2 and backend.shape(y)[1] > 1, lambda: backend.argmax(y, axis=1), lambda: math_ops.cast(backend.reshape(y, (-1,)), dtypes.int64)) cw = array_ops.gather_v2(class_weight_tensor, y_classes) if sw is not None: cw = math_ops.cast(cw, sw.dtype) sw, cw = expand_1d((sw, cw)) # `class_weight` and `sample_weight` are multiplicative. sw = sw * cw else: sw = cw return x, y, sw
def f(x): return array_ops.gather_v2(x, constant_op.constant( [[[1, 0], [0, 0]]], dtype=dtypes.int32), axis=2, batch_dims=2)
def _create_simple_tf1_gather_model( self, use_variable_for_filter=False) -> Tuple[core.Tensor, core.Tensor]: """Creates a basic gather model. This is intended to be used for TF1 (graph mode) tests. Args: use_variable_for_filter: Setting this to `True` makes the filter for the gather operation a `tf.Variable`. Returns: in_placeholder: Input tensor placeholder. output_tensor: The resulting tensor of the gather operation. """ in_placeholder = array_ops.placeholder(dtypes.int64, shape=(6)) filters = random_ops.random_uniform(shape=(64, 512), minval=-1., maxval=1.) if use_variable_for_filter: filters = variables.Variable(filters) output_tensor = array_ops.gather_v2(filters, in_placeholder) return in_placeholder, output_tensor
def _gather_result_by_index(input_tensor, batch_index): """Handle the data element gather for different type of tensor.""" if isinstance(input_tensor, sparse_tensor.SparseTensor): # For sparse tensor, both the index and value component should be gathered. return sparse_tensor.SparseTensor( indices=array_ops.gather_v2(input_tensor.indices, batch_index), values=array_ops.gather_v2(input_tensor.values, batch_index), dense_shape=input_tensor.dense_shape) # For both ragged tensor or eager tensor or np array, tf.gather should do the # correct thing. elif isinstance(input_tensor, ragged_tensor.RaggedTensor): return array_ops.gather_v2(input_tensor, batch_index) elif isinstance(input_tensor, (ops.EagerTensor, np.ndarray)): return array_ops.gather_v2(input_tensor, batch_index).numpy() else: raise ValueError('Unexpected type {} encountered when gathering ' 'batch slices.'.format(input_tensor))
def _ragged_substr(text_input, begin, end): text_input_flat = None if ragged_tensor.is_ragged(text_input): text_input_flat = text_input.flat_values else: text_input_flat = text_input broadcasted_text = array_ops.gather_v2(text_input_flat, begin.nested_value_rowids()[-1]) size = math_ops.sub(end.flat_values, begin.flat_values) new_tokens = string_ops.substr_v2(broadcasted_text, begin.flat_values, size) return begin.with_flat_values(new_tokens)
def gather(params, indices, axis=None, batch_dims=0, name=None): return array_ops.gather_v2( params, indices, axis=axis, batch_dims=batch_dims, name=name)
def merge_summaries(prev_summary, next_summary, epsilon): """Weighted merge sort of summaries. Given two summaries of distinct data, this function merges (and compresses) them to stay within `epsilon` error tolerance. Args: prev_summary: 2-D `np.ndarray` summary to be merged with `next_summary`. next_summary: 2-D `np.ndarray` summary to be merged with `prev_summary`. epsilon: A float that determines the approxmiate desired precision. Returns: A 2-D `np.ndarray` that is a merged summary. First column is the interpolated partition values, the second is the weights (counts). """ merged = array_ops.concat((prev_summary, next_summary), axis=1) merged = array_ops.gather_v2(merged, sort_ops.argsort(merged[0]), axis=1) return compress(merged, epsilon)
def _ragged_substr(text_input, begin, size): if not (isinstance(text_input, ragged_tensor.RaggedTensor) or isinstance(begin, ragged_tensor.RaggedTensor) or isinstance(size, ragged_tensor.RaggedTensor)): return string_ops.substr_v2(text_input, begin, size) # TODO(edloper) Update this to use ragged_tensor_shape.broadcast_dynamic_shape # once it's been updated to handle uniform_row_lengths correctly. if ragged_tensor.is_ragged(text_input): if text_input.ragged_rank != 1 or text_input.shape.rank != 2: return None # Test only works for `shape=[N, None]` text_input_flat = text_input.flat_values else: text_input_flat = array_ops.reshape(text_input, [-1]) broadcasted_text = array_ops.gather_v2(text_input_flat, begin.nested_value_rowids()[-1]) new_tokens = string_ops.substr_v2(broadcasted_text, begin.flat_values, size.flat_values) return begin.with_flat_values(new_tokens)
def _RaggedSubstr(text_input, begin, end): text_input_flat = None if ragged_tensor.is_ragged(text_input): text_input_flat = text_input.flat_values else: text_input_flat = ops.convert_to_tensor(text_input) if ragged_tensor.is_ragged(begin): broadcasted_text = array_ops.gather_v2(text_input_flat, begin.nested_value_rowids()[-1]) # convert boardcasted_text into a 1D tensor. broadcasted_text = array_ops.reshape(broadcasted_text, [-1]) size = math_ops.sub(end.flat_values, begin.flat_values) new_tokens = string_ops.substr_v2(broadcasted_text, begin.flat_values, size) return begin.with_flat_values(new_tokens) else: assert begin.shape.ndims == 1 assert text_input_flat.shape.ndims == 0 size = math_ops.sub(end, begin) new_tokens = string_ops.substr_v2(text_input_flat, begin, size) return new_tokens
def _tf_sorted(iterable, key, reverse): """Overload of sorted_ for Tensor iterable.""" if reverse is UNSPECIFIED: direction = 'ASCENDING' else: direction = 'DESCENDING' if key is not UNSPECIFIED: mapped = parallel_ops.vectorized_map(key, iterable) if mapped.shape.rank is not None and mapped.shape.rank != 1: raise ValueError('sort only supports only 1D tensors') with ops.control_dependencies([ check_ops.assert_rank_v2(mapped, 1, 'sort only supports only 1D tensors') ]): order = sort_ops.argsort(mapped, direction=direction) return array_ops.gather_v2(iterable, order) if iterable.shape.rank is not None and iterable.shape.rank != 1: raise ValueError('sort only supports only 1D tensors') with ops.control_dependencies([ check_ops.assert_rank_v2(iterable, 1, 'sort only supports only 1D tensors') ]): return sort_ops.sort(iterable, direction=direction)
def _split(t, indices): if t is None: return t t = ops.convert_to_tensor_v2(t) return array_ops.gather_v2(t, indices)
def f(x): return array_ops.gather_v2(x, constant_op.constant( [1, 0], dtype=dtypes.int32), axis=1)
def f(x): return array_ops.gather_v2( x, constant_op.constant([[2, 0], [2, 4]], dtype=dtypes.int32))
def __call__( self, input_tensor: core.Tensor) -> Mapping[str, core.Tensor]: """Performs a gather operation.""" out = array_ops.gather_v2(self.w, input_tensor) return {'output': out}