Python tensorflow.compat.v1.gather_nd() Examples
The following are 30
code examples of tensorflow.compat.v1.gather_nd().
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.compat.v1
, or try the search function
.
Example #1
Source File: expert_utils.py From tensor2tensor with Apache License 2.0 | 6 votes |
def remove(self, x): """Remove padding from the given tensor. Args: x (tf.Tensor): of shape [dim_origin,...] Returns: a tensor of shape [dim_compressed,...] with dim_compressed <= dim_origin """ with tf.name_scope("pad_reduce/remove"): x_shape = x.get_shape().as_list() x = tf.gather_nd( x, indices=self.nonpad_ids, ) if not tf.executing_eagerly(): # This is a hack but for some reason, gather_nd return a tensor of # undefined shape, so the shape is set up manually x.set_shape([None] + x_shape[1:]) return x
Example #2
Source File: common_layers.py From tensor2tensor with Apache License 2.0 | 6 votes |
def argmax_with_score(logits, axis=None): """Argmax along with the value.""" axis = axis or len(logits.get_shape()) - 1 predictions = tf.argmax(logits, axis=axis) logits_shape = shape_list(logits) prefix_shape, vocab_size = logits_shape[:-1], logits_shape[-1] prefix_size = 1 for d in prefix_shape: prefix_size *= d # Flatten to extract scores flat_logits = tf.reshape(logits, [prefix_size, vocab_size]) flat_predictions = tf.reshape(predictions, [prefix_size]) flat_indices = tf.stack( [tf.range(tf.to_int64(prefix_size)), tf.to_int64(flat_predictions)], axis=1) flat_scores = tf.gather_nd(flat_logits, flat_indices) # Unflatten scores = tf.reshape(flat_scores, prefix_shape) return predictions, scores
Example #3
Source File: target_assigner.py From models with Apache License 2.0 | 6 votes |
def get_batch_predictions_from_indices(batch_predictions, indices): """Gets the values of predictions in a batch at the given indices. The indices are expected to come from the offset targets generation functions in this library. The returned value is intended to be used inside a loss function. Args: batch_predictions: A tensor of shape [batch_size, height, width, channels] or [batch_size, height, width, class, channels] for class-specific features (e.g. keypoint joint offsets). indices: A tensor of shape [num_instances, 3] for single class features or [num_instances, 4] for multiple classes features. Returns: values: A tensor of shape [num_instances, channels] holding the predicted values at the given indices. """ return tf.gather_nd(batch_predictions, indices)
Example #4
Source File: model.py From ocrd_anybaseocr with Apache License 2.0 | 6 votes |
def rpn_class_loss_graph(rpn_match, rpn_class_logits): """RPN anchor classifier loss. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for BG/FG. """ # Squeeze last dim to simplify rpn_match = tf.squeeze(rpn_match, -1) # Get anchor classes. Convert the -1/+1 match to 0/1 values. anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32) # Positive and Negative anchors contribute to the loss, # but neutral anchors (match value = 0) don't. indices = tf.where(K.not_equal(rpn_match, 0)) # Pick rows that contribute to the loss and filter out the rest. rpn_class_logits = tf.gather_nd(rpn_class_logits, indices) anchor_class = tf.gather_nd(anchor_class, indices) # Cross entropy loss loss = K.sparse_categorical_crossentropy(target=anchor_class, output=rpn_class_logits, from_logits=True) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #5
Source File: dataloader.py From Object_Detection_Tracking with Apache License 2.0 | 6 votes |
def resize_and_crop_boxes(self): """Resize boxes and crop it to the self._output dimension.""" boxlist = preprocessor.box_list.BoxList(self._boxes) boxes = preprocessor.box_list_scale( boxlist, self._scaled_height, self._scaled_width).get() # Adjust box coordinates based on the offset. box_offset = tf.stack([self._crop_offset_y, self._crop_offset_x, self._crop_offset_y, self._crop_offset_x,]) boxes -= tf.cast(tf.reshape(box_offset, [1, 4]), tf.float32) # Clip the boxes. boxes = self.clip_boxes(boxes) # Filter out ground truth boxes that are all zeros. indices = tf.where(tf.not_equal(tf.reduce_sum(boxes, axis=1), 0)) boxes = tf.gather_nd(boxes, indices) classes = tf.gather_nd(self._classes, indices) return boxes, classes
Example #6
Source File: multi_head_dqn_agent.py From batch_rl with Apache License 2.0 | 6 votes |
def _build_train_op(self): """Builds a training op. Returns: train_op: An op performing one step of training from replay data. """ actions = self._replay.actions indices = tf.stack([tf.range(actions.shape[0]), actions], axis=-1) replay_chosen_q = tf.gather_nd( self._replay_net_outputs.q_heads, indices=indices) target = tf.stop_gradient(self._build_target_q_op()) loss = tf.losses.huber_loss( target, replay_chosen_q, reduction=tf.losses.Reduction.NONE) q_head_losses = tf.reduce_mean(loss, axis=0) final_loss = tf.reduce_mean(q_head_losses) if self.summary_writer is not None: with tf.variable_scope('Losses'): tf.summary.scalar('HuberLoss', final_loss) return self.optimizer.minimize(final_loss)
Example #7
Source File: quantile_agent.py From batch_rl with Apache License 2.0 | 6 votes |
def _build_target_distribution(self): batch_size = tf.shape(self._replay.rewards)[0] # size of rewards: batch_size x 1 rewards = self._replay.rewards[:, None] # size of tiled_support: batch_size x num_atoms is_terminal_multiplier = 1. - tf.cast(self._replay.terminals, tf.float32) # Incorporate terminal state to discount factor. # size of gamma_with_terminal: batch_size x 1 gamma_with_terminal = self.cumulative_gamma * is_terminal_multiplier gamma_with_terminal = gamma_with_terminal[:, None] # size of next_qt_argmax: 1 x batch_size next_qt_argmax = tf.argmax( self._replay_next_target_net_outputs.q_values, axis=1)[:, None] batch_indices = tf.range(tf.to_int64(batch_size))[:, None] # size of next_qt_argmax: batch_size x 2 batch_indexed_next_qt_argmax = tf.concat( [batch_indices, next_qt_argmax], axis=1) # size of next_logits (next quantiles): batch_size x num_atoms next_logits = tf.gather_nd( self._replay_next_target_net_outputs.logits, batch_indexed_next_qt_argmax) return rewards + gamma_with_terminal * next_logits
Example #8
Source File: model_fns.py From language with Apache License 2.0 | 6 votes |
def sparse_dense_mul(sp_mat, dense_mat): """Element-wise multiplication between sparse and dense tensors. Returns a sparse tensor. Limited broadcasting of dense_mat is supported. If rank(dense_mat) < rank(sparse_mat), then dense_mat is broadcasted on the rightmost dimensions to match sparse_mat. Args: sp_mat: SparseTensor. dense_mat: DenseTensor with rank <= sp_mat. Returns: SparseTensor. """ rank = dense_mat.get_shape().ndims indices = sp_mat.indices[:, :rank] dense_values = tf.gather_nd(dense_mat, indices) return tf.SparseTensor(sp_mat.indices, sp_mat.values * dense_values, sp_mat.dense_shape)
Example #9
Source File: test_forward.py From incubator-tvm with Apache License 2.0 | 5 votes |
def _test_gather_nd(ip_shape, indice_value, dtype): """test operator GatherNd""" np_data = np.random.uniform(1, 100, size=ip_shape).astype(dtype) tf.reset_default_graph() with tf.Graph().as_default(): in_data = tf.placeholder(dtype, ip_shape, name="in_data") tf.gather_nd(in_data, indices=indice_value, name="gather_nd") compare_tf_with_tvm([np_data], ['in_data:0'], 'gather_nd:0')
Example #10
Source File: test_forward.py From incubator-tvm with Apache License 2.0 | 5 votes |
def _test_gather_nd(data, indices): """ One iteration of GATHER_ND """ with tf.Graph().as_default(): in_data = tf.placeholder(shape=data.shape, dtype=data.dtype, name="data") indices_data = tf.placeholder(shape=indices.shape, dtype=indices.dtype, name="indices") out = tf.gather_nd(in_data, indices_data) compare_tflite_with_tvm([data, indices], ['data:0', 'indices:0'], [in_data, indices_data], [out])
Example #11
Source File: beam_search_v1.py From models with Apache License 2.0 | 5 votes |
def _gather_beams(nested, beam_indices, batch_size, new_beam_size): """Gather beams from nested structure of tensors. Each tensor in nested represents a batch of beams, where beam refers to a single search state (beam search involves searching through multiple states in parallel). This function is used to gather the top beams, specified by beam_indices, from the nested tensors. Args: nested: Nested structure (tensor, list, tuple or dict) containing tensors with shape [batch_size, beam_size, ...]. beam_indices: int32 tensor with shape [batch_size, new_beam_size]. Each value in beam_indices must be between [0, beam_size), and are not necessarily unique. batch_size: int size of batch new_beam_size: int number of beams to be pulled from the nested tensors. Returns: Nested structure containing tensors with shape [batch_size, new_beam_size, ...] """ # Computes the i'th coodinate that contains the batch index for gather_nd. # Batch pos is a tensor like [[0,0,0,0,],[1,1,1,1],..]. batch_pos = tf.range(batch_size * new_beam_size) // new_beam_size batch_pos = tf.reshape(batch_pos, [batch_size, new_beam_size]) # Create coordinates to be passed to tf.gather_nd. Stacking creates a tensor # with shape [batch_size, beam_size, 2], where the last dimension contains # the (i, j) gathering coordinates. coordinates = tf.stack([batch_pos, beam_indices], axis=2) return nest.map_structure( lambda state: tf.gather_nd(state, coordinates), nested)
Example #12
Source File: model.py From ocrd_anybaseocr with Apache License 2.0 | 5 votes |
def mrcnn_bbox_loss_graph(target_bbox, target_class_ids, pred_bbox): """Loss for Mask R-CNN bounding box refinement. target_bbox: [batch, num_rois, (dy, dx, log(dh), log(dw))] target_class_ids: [batch, num_rois]. Integer class IDs. pred_bbox: [batch, num_rois, num_classes, (dy, dx, log(dh), log(dw))] """ # Reshape to merge batch and roi dimensions for simplicity. target_class_ids = K.reshape(target_class_ids, (-1,)) target_bbox = K.reshape(target_bbox, (-1, 4)) pred_bbox = K.reshape(pred_bbox, (-1, K.int_shape(pred_bbox)[2], 4)) # Only positive ROIs contribute to the loss. And only # the right class_id of each ROI. Get their indices. positive_roi_ix = tf.where(target_class_ids > 0)[:, 0] positive_roi_class_ids = tf.cast( tf.gather(target_class_ids, positive_roi_ix), tf.int64) indices = tf.stack([positive_roi_ix, positive_roi_class_ids], axis=1) # Gather the deltas (predicted and true) that contribute to loss target_bbox = tf.gather(target_bbox, positive_roi_ix) pred_bbox = tf.gather_nd(pred_bbox, indices) # Smooth-L1 Loss loss = K.switch(tf.size(target_bbox) > 0, smooth_l1_loss(y_true=target_bbox, y_pred=pred_bbox), tf.constant(0.0)) loss = K.mean(loss) return loss
Example #13
Source File: model.py From ocrd_anybaseocr with Apache License 2.0 | 5 votes |
def rpn_bbox_loss_graph(config, target_bbox, rpn_match, rpn_bbox): """Return the RPN bounding box loss graph. config: the model config object. target_bbox: [batch, max positive anchors, (dy, dx, log(dh), log(dw))]. Uses 0 padding to fill in unsed bbox deltas. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_bbox: [batch, anchors, (dy, dx, log(dh), log(dw))] """ # Positive anchors contribute to the loss, but negative and # neutral anchors (match value of 0 or -1) don't. rpn_match = K.squeeze(rpn_match, -1) indices = tf.where(K.equal(rpn_match, 1)) # Pick bbox deltas that contribute to the loss rpn_bbox = tf.gather_nd(rpn_bbox, indices) # Trim target bounding box deltas to the same length as rpn_bbox. batch_counts = K.sum(K.cast(K.equal(rpn_match, 1), tf.int32), axis=1) target_bbox = batch_pack_graph(target_bbox, batch_counts, config.IMAGES_PER_GPU) loss = smooth_l1_loss(target_bbox, rpn_bbox) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
Example #14
Source File: common.py From language with Apache License 2.0 | 5 votes |
def sparse_batched_matmul(x, y): """Batch multiply sparse tensor x with dense tensor y. Args: x: <tf.float32>[B, M, N] SparseTensor. y: <tf.float32>[B, N, K] DenseTensor. Returns: <tf.float32>[B, M, K] DenseTensor. """ sp_indices = x.indices sp_values = x.values batch_size, num_row = x.dense_shape[0], x.dense_shape[1] num_col = tf.cast(tf.shape(y)[2], tf.int64) # Number of non-zero entries. num_nz = tf.cast(tf.shape(sp_indices)[0], tf.int64) # Fetch relevant values from y. # <tf.float32>[num_nz, num_col] lookup = tf.gather_nd(y, tf.stack( [sp_indices[:, 0], sp_indices[:, 2]], axis=1)) # Reshape first two dimensions of x into a new SparseTensor of rank 2. # <tf.int32>[num_nz, 2] x_i = tf.stack([sp_indices[:, 0] * num_row + sp_indices[:, 1], tf.cast(tf.range(num_nz), tf.int64)], axis=1) # <tf.float32>[batch_size * num_row, num_nz] sparse_2d = tf.SparseTensor(x_i, sp_values, tf.stack([batch_size * num_row, num_nz], 0)) # Multiply the new sparse tensor with the gathered values. # <tf.float32>[batch_size * num_row, num_col] dense_2d = tf.sparse_tensor_dense_matmul(sparse_2d, lookup) # Reshape back [batch_size, num_row, num_col] dense_3d = tf.reshape(dense_2d, tf.stack([batch_size, num_row, num_col], 0)) return dense_3d
Example #15
Source File: model_fns.py From language with Apache License 2.0 | 5 votes |
def batch_gather(params, indices): """Gather a batch of indices from a batch of params.""" batch_size = tf.shape(indices)[0] num_idx = tf.shape(indices)[1] brange = tf.cast(tf.range(batch_size), indices.dtype) bindices = tf.tile(tf.expand_dims(brange, 1), (1, num_idx)) gather_indices = tf.stack([bindices, indices], axis=2) return tf.gather_nd(params, gather_indices)
Example #16
Source File: attacks.py From interval-bound-propagation with Apache License 2.0 | 5 votes |
def find_worst_attack(self, objective_fn, adversarial_input, batch_size, input_shape): """Returns the attack that maximizes objective_fn.""" adversarial_objective = objective_fn(adversarial_input) adversarial_objective = tf.reshape(adversarial_objective, [-1, batch_size]) adversarial_input = tf.reshape(adversarial_input, [-1, batch_size] + input_shape) i = tf.argmax(adversarial_objective, axis=0) j = tf.cast(tf.range(tf.shape(adversarial_objective)[1]), i.dtype) ij = tf.stack([i, j], axis=1) return tf.gather_nd(adversarial_input, ij)
Example #17
Source File: specification.py From interval-bound-propagation with Apache License 2.0 | 5 votes |
def evaluate(self, logits): if len(logits.shape) == 2: correct_class_logit = tf.gather_nd(logits, self._correct_idx) correct_class_logit = tf.expand_dims(correct_class_logit, -1) wrong_class_logits = tf.gather_nd(logits, self._wrong_idx) elif len(logits.shape) == 3: # [num_restarts, batch_size, num_classes] to # [num_restarts, batch_size, num_specs] logits = tf.transpose(logits, [1, 2, 0]) # Put restart dimension last. correct_class_logit = tf.gather_nd(logits, self._correct_idx) correct_class_logit = tf.transpose(correct_class_logit) correct_class_logit = tf.expand_dims(correct_class_logit, -1) wrong_class_logits = tf.gather_nd(logits, self._wrong_idx) wrong_class_logits = tf.transpose(wrong_class_logits, [2, 0, 1]) else: assert len(logits.shape) == 4 # [num_restarts, num_specs, batch_size, num_classes] to # [num_restarts, batch_size, num_specs]. logits = tf.transpose(logits, [2, 3, 1, 0]) correct_class_logit = tf.gather_nd(logits, self._correct_idx) correct_class_logit = tf.transpose(correct_class_logit, [2, 0, 1]) batch_size = tf.shape(logits)[0] wrong_idx = tf.concat([ self._wrong_idx, tf.tile(tf.reshape(tf.range(self.num_specifications, dtype=tf.int32), [1, self.num_specifications, 1]), [batch_size, 1, 1])], axis=-1) wrong_class_logits = tf.gather_nd(logits, wrong_idx) wrong_class_logits = tf.transpose(wrong_class_logits, [2, 0, 1]) return wrong_class_logits - correct_class_logit
Example #18
Source File: specification.py From interval-bound-propagation with Apache License 2.0 | 5 votes |
def _build(self, modules): if not (self.collapse and isinstance(modules[-1], verifiable_wrapper.LinearFCWrapper)): logging.info('Elision of last layer disabled.') bounds = modules[-1].output_bounds bounds = bounds_lib.IntervalBounds.convert(bounds) correct_class_logit = tf.gather_nd(bounds.lower, self._correct_idx) wrong_class_logits = tf.gather_nd(bounds.upper, self._wrong_idx) return wrong_class_logits - tf.expand_dims(correct_class_logit, 1) logging.info('Elision of last layer active.') bounds = modules[-1].input_bounds bounds = bounds_lib.IntervalBounds.convert(bounds) batch_size = tf.shape(bounds.lower)[0] w = modules[-1].module.w b = modules[-1].module.b w_t = tf.tile(tf.expand_dims(tf.transpose(w), 0), [batch_size, 1, 1]) b_t = tf.tile(tf.expand_dims(b, 0), [batch_size, 1]) w_correct = tf.expand_dims(tf.gather_nd(w_t, self._correct_idx), -1) b_correct = tf.expand_dims(tf.gather_nd(b_t, self._correct_idx), 1) w_wrong = tf.transpose(tf.gather_nd(w_t, self._wrong_idx), [0, 2, 1]) b_wrong = tf.gather_nd(b_t, self._wrong_idx) w = w_wrong - w_correct b = b_wrong - b_correct # Maximize z * w + b s.t. lower <= z <= upper. c = (bounds.lower + bounds.upper) / 2. r = (bounds.upper - bounds.lower) / 2. c = tf.einsum('ij,ijk->ik', c, w) if b is not None: c += b r = tf.einsum('ij,ijk->ik', r, tf.abs(w)) return c + r
Example #19
Source File: multi_network_dqn_agent.py From batch_rl with Apache License 2.0 | 5 votes |
def _build_train_op(self): """Builds a training op. Returns: train_op: An op performing one step of training from replay data. """ actions = self._replay.actions indices = tf.stack([tf.range(actions.shape[0]), actions], axis=-1) replay_chosen_q = tf.gather_nd( self._replay_net_outputs.q_networks, indices=indices) target = tf.stop_gradient(self._build_target_q_op()) loss = tf.losses.huber_loss( target, replay_chosen_q, reduction=tf.losses.Reduction.NONE) q_head_losses = tf.reduce_mean(loss, axis=0) final_loss = tf.reduce_mean(q_head_losses) if self.summary_writer is not None: with tf.variable_scope('Losses'): tf.summary.scalar('HuberLoss', final_loss) self.optimizers = [copy.deepcopy(self.optimizer) for _ in range(self.num_networks)] train_ops = [] for i in range(self.num_networks): var_list = tf.trainable_variables(scope='Online/subnet_{}'.format(i)) train_op = self.optimizers[i].minimize(final_loss, var_list=var_list) train_ops.append(train_op) return tf.group(*train_ops, name='merged_train_op')
Example #20
Source File: sequence_example_lib.py From magenta with Apache License 2.0 | 5 votes |
def flatten_maybe_padded_sequences(maybe_padded_sequences, lengths=None): """Flattens the batch of sequences, removing padding (if applicable). Args: maybe_padded_sequences: A tensor of possibly padded sequences to flatten, sized `[N, M, ...]` where M = max(lengths). lengths: Optional length of each sequence, sized `[N]`. If None, assumes no padding. Returns: flatten_maybe_padded_sequences: The flattened sequence tensor, sized `[sum(lengths), ...]`. """ def flatten_unpadded_sequences(): # The sequences are equal length, so we should just flatten over the first # two dimensions. return tf.reshape(maybe_padded_sequences, [-1] + maybe_padded_sequences.shape.as_list()[2:]) if lengths is None: return flatten_unpadded_sequences() def flatten_padded_sequences(): indices = tf.where(tf.sequence_mask(lengths)) return tf.gather_nd(maybe_padded_sequences, indices) return tf.cond( tf.equal(tf.reduce_min(lengths), tf.shape(maybe_padded_sequences)[1]), flatten_unpadded_sequences, flatten_padded_sequences)
Example #21
Source File: lstm_utils.py From magenta with Apache License 2.0 | 5 votes |
def get_final(sequence, sequence_length, time_major=True): """Get the final item in a batch of sequences.""" final_index = _get_final_index(sequence_length, time_major) return tf.gather_nd(sequence, final_index)
Example #22
Source File: evaluator.py From graphics with Apache License 2.0 | 5 votes |
def _batch_slice(self, ary, start_ijk, w, batch_size): """Batched slicing of original grid. Args: ary: tensor, rank = 3. start_ijk: [batch_size, 3] tensor, starting index. w: width of cube to extract. batch_size: int, batch size. Returns: batched_slices: [batch_size, w, w, w] tensor, batched slices of ary. """ batch_size = start_ijk.shape[0] ijk = tf.range(w, dtype=tf.int32) slice_idx = tf.meshgrid(ijk, ijk, ijk, indexing='ij') slice_idx = tf.stack( slice_idx, axis=-1) # [in_grid_res, in_grid_res, in_grid_res, 3] slice_idx = tf.broadcast_to(slice_idx[tf.newaxis], [batch_size, w, w, w, 3]) offset = tf.broadcast_to( start_ijk[:, tf.newaxis, tf.newaxis, tf.newaxis, :], [batch_size, w, w, w, 3]) slice_idx += offset # [batch_size, in_grid_res, in_grid_res, in_grid_res, 3] batched_slices = tf.gather_nd(ary, slice_idx) # [batch_size, in_grid_res, in_grid_res, in_grid_res] return batched_slices
Example #23
Source File: common_video.py From tensor2tensor with Apache License 2.0 | 5 votes |
def extract_random_video_patch(videos, num_frames=-1): """For every video, extract a random consecutive patch of num_frames. Args: videos: 5-D Tensor, (NTHWC) num_frames: Integer, if -1 then the entire video is returned. Returns: video_patch: 5-D Tensor, (NTHWC) with T = num_frames. Raises: ValueError: If num_frames is greater than the number of total frames in the video. """ if num_frames == -1: return videos batch_size, num_total_frames, h, w, c = common_layers.shape_list(videos) if num_total_frames < num_frames: raise ValueError("Expected num_frames <= %d, got %d" % (num_total_frames, num_frames)) # Randomly choose start_inds for each video. frame_start = tf.random_uniform( shape=(batch_size,), minval=0, maxval=num_total_frames - num_frames + 1, dtype=tf.int32) # [start[0], start[0] + 1, ... start[0] + num_frames - 1] + ... # [start[batch_size-1], ... start[batch_size-1] + num_frames - 1] range_inds = tf.expand_dims(tf.range(num_frames), axis=0) frame_inds = range_inds + tf.expand_dims(frame_start, axis=1) frame_inds = tf.reshape(frame_inds, [-1]) # [0]*num_frames + [1]*num_frames + ... [batch_size-1]*num_frames batch_inds = tf.expand_dims(tf.range(batch_size), axis=1) batch_inds = tf.tile(batch_inds, [1, num_frames]) batch_inds = tf.reshape(batch_inds, [-1]) gather_inds = tf.stack((batch_inds, frame_inds), axis=1) video_patches = tf.gather_nd(videos, gather_inds) return tf.reshape(video_patches, (batch_size, num_frames, h, w, c))
Example #24
Source File: beam_search.py From language with Apache License 2.0 | 4 votes |
def compute_topk_scores_and_seq(sequences, scores, scores_to_gather, flags, beam_size, batch_size): """Given sequences and scores, will gather the top k=beam size sequences. This function is used to grow alive, and finished. It takes sequences, scores, and flags, and returns the top k from sequences, scores_to_gather, and flags based on the values in scores. Args: sequences: Tensor of sequences that we need to gather from. [batch_size, beam_size, decode_length + 1] scores: Tensor of scores for each sequence in sequences. [batch_size, beam_size]. We will use these to compute the topk. scores_to_gather: Tensor of scores for each sequence in sequences. [batch_size, beam_size]. We will return the gathered scores from here. Scores to gather is different from scores because for _grow_alive, we will need to return log_probs, while for _grow_finished, we will need to return the length penalized scors. flags: Tensor of bools for sequences that say whether a sequence has reached EOS or not beam_size: int batch_size: int Returns: Tuple of (topk_seq [batch_size, beam_size, decode_length], topk_gathered_scores [batch_size, beam_size], topk_finished_flags[batch_size, beam_size]) """ _, topk_indexes = tf.nn.top_k(scores, beam_size) # The next three steps are to create coordinates for tf.gather_nd to pull # out the topk sequences from sequences based on scores. # batch pos is a tensor like [[0,0,0,0,],[1,1,1,1],..]. It says which # batch the beam item is in. This will create the i of the i,j coordinate # needed for the gather batch_pos = _compute_batch_indices(batch_size, beam_size) # top coordinates will give us the actual coordinates to do the gather. # stacking will create a tensor of dimension batch * beam * 2, where the # last dimension contains the i,j gathering coordinates. top_coordinates = tf.stack([batch_pos, topk_indexes], axis=2) # Gather up the highest scoring sequences topk_seq = tf.gather_nd(sequences, top_coordinates) topk_flags = tf.gather_nd(flags, top_coordinates) topk_gathered_scores = tf.gather_nd(scores_to_gather, top_coordinates) return topk_seq, topk_gathered_scores, topk_flags
Example #25
Source File: seq2seq.py From magenta with Apache License 2.0 | 4 votes |
def next_inputs(self, time, outputs, state, sample_ids, name=None): with tf.name_scope(name, "ScheduledOutputTrainingHelperNextInputs", [time, outputs, state, sample_ids]): (finished, base_next_inputs, state) = ( super(ScheduledOutputTrainingHelper, self).next_inputs( time=time, outputs=outputs, state=state, sample_ids=sample_ids, name=name)) sample_ids = tf.cast(sample_ids, tf.bool) def maybe_sample(): """Perform scheduled sampling.""" def maybe_concatenate_auxiliary_inputs(outputs_, indices=None): """Concatenate outputs with auxiliary inputs, if they exist.""" if self._auxiliary_input_tas is None: return outputs_ next_time = time + 1 auxiliary_inputs = tf.nest.map_structure( lambda ta: ta.read(next_time), self._auxiliary_input_tas) if indices is not None: auxiliary_inputs = tf.gather_nd(auxiliary_inputs, indices) return tf.nest.map_structure( lambda x, y: tf.concat((x, y), -1), outputs_, auxiliary_inputs) if self._next_inputs_fn is None: return tf.where( sample_ids, maybe_concatenate_auxiliary_inputs(outputs), base_next_inputs) where_sampling = tf.cast( tf.where(sample_ids), tf.int32) where_not_sampling = tf.cast( tf.where(tf.logical_not(sample_ids)), tf.int32) outputs_sampling = tf.gather_nd(outputs, where_sampling) inputs_not_sampling = tf.gather_nd(base_next_inputs, where_not_sampling) sampled_next_inputs = maybe_concatenate_auxiliary_inputs( self._next_inputs_fn(outputs_sampling), where_sampling) base_shape = tf.shape(base_next_inputs) return (tf.scatter_nd(indices=where_sampling, updates=sampled_next_inputs, shape=base_shape) + tf.scatter_nd(indices=where_not_sampling, updates=inputs_not_sampling, shape=base_shape)) all_finished = tf.reduce_all(finished) no_samples = tf.logical_not(tf.reduce_any(sample_ids)) next_inputs = tf.cond( tf.logical_or(all_finished, no_samples), lambda: base_next_inputs, maybe_sample) return (finished, next_inputs, state)
Example #26
Source File: local_implicit_grid_layer.py From graphics with Apache License 2.0 | 4 votes |
def _eval_net(self, lat, weights, xloc, training=False): """Evaluate function values by querying shared dense network. Args: lat: `[batch_size, num_points, 2**dim, in_features]` tensor, neighbor latent codes for each input point. weights: `[batch_size, num_points, 2**dim]` tensor, bi/tri-linear interpolation weights for each neighbor. xloc: `[batch_size, num_points, 2**dim, dim]`tensor, relative coordinates. training: bool, flag indicating training phase. Returns: values: `[batch_size, num_point, out_features]` tensor, query values. """ nb, np, nn, nc = lat.get_shape().as_list() nd = self.dim if self.method == "linear": inputs = tf.concat([xloc, lat], axis=-1) # `[batch_size, num_points, 2**dim, dim+in_features]` inputs = tf.reshape(inputs, [-1, nc+nd]) values = self.net(inputs, training=training) values = tf.reshape(values, [nb, np, nn, self.cout]) # `[batch_size, num_points, 2**dim, out_features]` if self.interp: values = tf.reduce_sum(tf.expand_dims(weights, axis=-1)*values, axis=2) # `[batch_size, num_points out_features]` else: values = (values, weights) else: # nearest neighbor nid = tf.cast(tf.argmax(weights, axis=-1), tf.int32) # [batch_size, num_points] bid = tf.broadcast_to(tf.range(nb, dtype=tf.int32)[:, tf.newaxis], [nb, np]) pid = tf.broadcast_to(tf.range(np, dtype=tf.int32)[tf.newaxis, :], [nb, np]) gather_id = tf.stack((bid, pid, nid), axis=-1) lat_ = tf.gather_nd(lat, gather_id) # [batch_size, num_points, in_feat] xloc_ = tf.gather_nd(xloc, gather_id) # [batch_size, num_points, dim] inputs = tf.concat([xloc_, lat_], axis=-1) inputs = tf.reshape(inputs, [-1, nc+nd]) values = self.net(inputs, training=training) values = tf.reshape(values, [nb, np, self.cout]) # `[batch_size, num_points, out_features]` return values
Example #27
Source File: model.py From ocrd_anybaseocr with Apache License 2.0 | 4 votes |
def mrcnn_mask_loss_graph(target_masks, target_class_ids, pred_masks): """Mask binary cross-entropy loss for the masks head. target_masks: [batch, num_rois, height, width]. A float32 tensor of values 0 or 1. Uses zero padding to fill array. target_class_ids: [batch, num_rois]. Integer class IDs. Zero padded. pred_masks: [batch, proposals, height, width, num_classes] float32 tensor with values from 0 to 1. """ # Reshape for simplicity. Merge first two dimensions into one. target_class_ids = K.reshape(target_class_ids, (-1,)) mask_shape = tf.shape(target_masks) target_masks = K.reshape(target_masks, (-1, mask_shape[2], mask_shape[3])) pred_shape = tf.shape(pred_masks) pred_masks = K.reshape(pred_masks, (-1, pred_shape[2], pred_shape[3], pred_shape[4])) # Permute predicted masks to [N, num_classes, height, width] pred_masks = tf.transpose(pred_masks, [0, 3, 1, 2]) # Only positive ROIs contribute to the loss. And only # the class specific mask of each ROI. positive_ix = tf.where(target_class_ids > 0)[:, 0] positive_class_ids = tf.cast( tf.gather(target_class_ids, positive_ix), tf.int64) indices = tf.stack([positive_ix, positive_class_ids], axis=1) # Gather the masks (predicted and true) that contribute to loss y_true = tf.gather(target_masks, positive_ix) y_pred = tf.gather_nd(pred_masks, indices) # Compute binary cross entropy. If no positive ROIs, then return 0. # shape: [batch, roi, num_classes] loss = K.switch(tf.size(y_true) > 0, K.binary_crossentropy(target=y_true, output=y_pred), tf.constant(0.0)) loss = K.mean(loss) return loss ############################################################ # Data Generator ############################################################
Example #28
Source File: vqa_attention.py From tensor2tensor with Apache License 2.0 | 4 votes |
def question_encoder(question, hparams, name="encoder"): """Question encoder, run LSTM encoder and get the last output as encoding.""" with tf.variable_scope(name, "encoder", values=[question]): question = common_layers.flatten4d3d(question) padding = common_attention.embedding_to_padding(question) length = common_attention.padding_to_length(padding) max_question_length = hparams.max_question_length question = question[:, :max_question_length, :] actual_question_length = common_layers.shape_list(question)[1] length = tf.minimum(length, max_question_length) padding = [[0, 0], [0, max_question_length-actual_question_length], [0, 0]] question = tf.pad(question, padding) question_shape = question.get_shape().as_list() question_shape[1] = max_question_length question.set_shape(question_shape) # apply tanh dropout on question embedding question = tf.tanh(question) question = tf.nn.dropout(question, keep_prob=1.-hparams.dropout) question = [question[:, i, :] for i in range(max_question_length)] # rnn_layers = [_get_rnn_cell(hparams) # for _ in range(hparams.num_rnn_layers)] # rnn_multi_cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layers) rnn_cell = _get_rnn_cell(hparams) # outputs, _ = tf.nn.dynamic_rnn( # rnn_cell, question, length, dtype=tf.float32) _, state = tf.nn.static_rnn(rnn_cell, question, sequence_length=length, dtype=tf.float32) # outputs = [tf.expand_dims(output, axis=1) for output in outputs] # outputs = tf.concat(outputs, axis=1) # utils.collect_named_outputs("vqa_attention_debug", "question_output", # outputs) # utils.collect_named_outputs("vqa_attention_debug", "question_state", # state.h) # batch_size = common_layers.shape_list(outputs)[0] # row_indices = tf.range(batch_size) # # length - 1 as index # indices = tf.transpose([row_indices, tf.maximum(length-1, 0)]) # last_output = tf.gather_nd(outputs, indices) # utils.collect_named_outputs("vqa_attention_debug", # "question_final_output", last_output) return state.h
Example #29
Source File: beam_search.py From tensor2tensor with Apache License 2.0 | 4 votes |
def fast_tpu_gather(params, indices, name=None): """Fast gather implementation for models running on TPU. This function use one_hot and batch matmul to do gather, which is faster than gather_nd on TPU. For params that have dtype of int32 (sequences to gather from), batch_gather is used to keep accuracy. Args: params: A tensor from which to gather values. [batch_size, original_size, ...] indices: A tensor used as the index to gather values. [batch_size, selected_size]. name: A string, name of the operation (optional). Returns: gather_result: A tensor that has the same rank as params. [batch_size, selected_size, ...] """ with tf.name_scope(name): dtype = params.dtype def _gather(params, indices): """Fast gather using one_hot and batch matmul.""" if dtype != tf.float32: params = tf.to_float(params) shape = common_layers.shape_list(params) indices_shape = common_layers.shape_list(indices) ndims = params.shape.ndims # Adjust the shape of params to match one-hot indices, which is the # requirement of Batch MatMul. if ndims == 2: params = tf.expand_dims(params, axis=-1) if ndims > 3: params = tf.reshape(params, [shape[0], shape[1], -1]) gather_result = tf.matmul( tf.one_hot(indices, shape[1], dtype=params.dtype), params) if ndims == 2: gather_result = tf.squeeze(gather_result, axis=-1) if ndims > 3: shape[1] = indices_shape[1] gather_result = tf.reshape(gather_result, shape) if dtype != tf.float32: gather_result = tf.cast(gather_result, dtype) return gather_result # If the dtype is int, use the gather instead of one_hot matmul to avoid # precision loss. The max int value can be represented by bfloat16 in MXU is # 256, which is smaller than the possible id values. Encoding/decoding can # potentially used to make it work, but the benenfit is small right now. if dtype.is_integer: gather_result = tf.batch_gather(params, indices) else: gather_result = _gather(params, indices) return gather_result
Example #30
Source File: metrics.py From tensor2tensor with Apache License 2.0 | 4 votes |
def sequence_edit_distance(predictions, labels, weights_fn=common_layers.weights_nonzero): """Average edit distance, ignoring padding 0s. The score returned is the edit distance divided by the total length of reference truth and the weight returned is the total length of the truth. Args: predictions: Tensor of shape [`batch_size`, `length`, 1, `num_classes`] and type tf.float32 representing the logits, 0-padded. labels: Tensor of shape [`batch_size`, `length`, 1, 1] and type tf.int32 representing the labels of same length as logits and 0-padded. weights_fn: ignored. The weights returned are the total length of the ground truth labels, excluding 0-paddings. Returns: (edit distance / reference length, reference length) Raises: ValueError: if weights_fn is not common_layers.weights_nonzero. """ if weights_fn is not common_layers.weights_nonzero: raise ValueError("Only weights_nonzero can be used for this metric.") with tf.variable_scope("edit_distance", values=[predictions, labels]): # Transform logits into sequence classes by taking max at every step. predictions = tf.to_int32( tf.squeeze(tf.argmax(predictions, axis=-1), axis=(2, 3))) nonzero_idx = tf.where(tf.not_equal(predictions, 0)) sparse_outputs = tf.SparseTensor(nonzero_idx, tf.gather_nd(predictions, nonzero_idx), tf.shape(predictions, out_type=tf.int64)) labels = tf.squeeze(labels, axis=(2, 3)) nonzero_idx = tf.where(tf.not_equal(labels, 0)) label_sparse_outputs = tf.SparseTensor(nonzero_idx, tf.gather_nd(labels, nonzero_idx), tf.shape(labels, out_type=tf.int64)) distance = tf.reduce_sum( tf.edit_distance(sparse_outputs, label_sparse_outputs, normalize=False)) reference_length = tf.to_float(common_layers.shape_list(nonzero_idx)[0]) return distance / reference_length, reference_length