def iou(boxlist1, boxlist2, scope=None): """Computes pairwise intersection-over-union between box collections. Args: boxlist1: BoxList holding N boxes boxlist2: BoxList holding M boxes scope: name scope. Returns: a tensor with shape [N, M] representing pairwise iou scores. """ with tf.name_scope(scope, 'IOU'): intersections = intersection(boxlist1, boxlist2) areas1 = area(boxlist1) areas2 = area(boxlist2) unions = (tf.expand_dims(areas1, 1) + tf.expand_dims(areas2, 0) - intersections) return tf.where(tf.equal(intersections, 0.0), tf.zeros_like(intersections), tf.truediv(intersections, unions))
def labels_of_top_ranked_predictions_in_batch(labels, predictions): """Applying tf.metrics.mean to this gives precision at 1. Args: labels: minibatch of dense 0/1 labels, shape [batch_size rows, num_classes] predictions: minibatch of predictions of the same shape Returns: one-dimension tensor top_labels, where top_labels[i]=1.0 iff the top-scoring prediction for batch element i has label 1.0 """ indices_of_top_preds = tf.cast(tf.argmax(input=predictions, axis=1), tf.int32) batch_size = tf.reduce_sum(input_tensor=tf.ones_like(indices_of_top_preds)) row_indices = tf.range(batch_size) thresholded_labels = tf.where(labels > 0.0, tf.ones_like(labels), tf.zeros_like(labels)) label_indices_to_gather = tf.transpose( a=tf.stack([row_indices, indices_of_top_preds])) return tf.gather_nd(thresholded_labels, label_indices_to_gather)
def _make_intrinsics_matrix(fx, fy, cx, cy): """Constructs a batch of intrinsics matrices given arguments.. Args: fx: <float32>[B] tensor containing horizontal focal length. fy: <float32>[B] tensor containing vertical focal length. cx: <float32>[B] tensor containing horizontal principal offset. cy: <float32>[B] tensor containing vertical principal offset. Returns: <float32>[B, 3, 3] tensor containing batch of intrinsics matrices. """ # fx, fy, cx, cy: [B] zeros = tf.zeros_like(fx) ones = tf.ones_like(fx) r1 = tf.stack([fx, zeros, cx], axis=-1) r2 = tf.stack([zeros, fy, cy], axis=-1) r3 = tf.stack([zeros, zeros, ones], axis=-1) intrinsics = tf.stack([r1, r2, r3], axis=1) return intrinsics
def _remove_empty_timesteps(sp_tensor): """Creates a 3D SparseTensor skipping empty time steps. Args: sp_tensor: A SparseTensor with at least 2 dimensions (subsequent ones will be ignored and simply flattened into the 2nd dimension). Returns: A 3D SparseTensor with index 0 for dimension 3 and a series from [0,..k] for dimension 1 for each batch entry. """ batch_size = tf.to_int32(sp_tensor.dense_shape[0]) indices, max_len = _example_index_to_sparse_index( tf.to_int32(sp_tensor.indices[:, 0]), batch_size) indices = tf.concat([indices, tf.zeros_like(indices[:, 0:1])], axis=1) return tf.SparseTensor( indices=indices, values=sp_tensor.values, dense_shape=[batch_size, max_len, 1])
def __init__(self, posts, **kwargs): FactorisedPosterior.__init__(self, posts, **kwargs) # The full covariance matrix is formed from the Cholesky decomposition # to ensure that it remains positive definite. # # To achieve this, we have to create PxP tensor variables for # each parameter vertex, but we then extract only the lower triangular # elements and train only on these. The diagonal elements # are constructed by the FactorisedPosterior if kwargs.get("init", None): # We are initializing from an existing posterior. # The FactorizedPosterior will already have extracted the mean and # diagonal of the covariance matrix - we need the Cholesky decomposition # of the covariance to initialize the off-diagonal terms self.log.info(" - Initializing posterior covariance from input posterior") _mean, cov = kwargs["init"] covar_init = tf.cholesky(cov) else: covar_init = tf.zeros([self.nvertices, self.nparams, self.nparams], dtype=tf.float32) self.off_diag_vars_base = self.log_tf(tf.Variable(covar_init, validate_shape=False, name='%s_off_diag_vars' % self.name)) if kwargs.get("suppress_nan", True): self.off_diag_vars = tf.where(tf.is_nan(self.off_diag_vars_base), tf.zeros_like(self.off_diag_vars_base), self.off_diag_vars_base) else: self.off_diag_vars = self.off_diag_vars_base self.off_diag_cov_chol = tf.matrix_set_diag(tf.matrix_band_part(self.off_diag_vars, -1, 0), tf.zeros([self.nvertices, self.nparams]), name='%s_off_diag_cov_chol' % self.name) # Combine diagonal and off-diagonal elements into full matrix self.cov_chol = tf.add(tf.matrix_diag(self.std), self.off_diag_cov_chol, name='%s_cov_chol' % self.name) # Form the covariance matrix from the chol decomposition self.cov = tf.matmul(tf.transpose(self.cov_chol, perm=(0, 2, 1)), self.cov_chol, name='%s_cov' % self.name) self.cov_chol = self.log_tf(self.cov_chol) self.cov = self.log_tf(self.cov)
def __call__(self, logits, labels): _, height, width, num_classes = logits.get_shape().as_list() # Use bilinear resizing because nearest neighbor is not supported in # tensorflow 1.14 with TPU. Once the environment is updated, it should be # change back to nearest neighbor. For now, it is tested and the performance # should be similar. if self._use_groundtruth_dimension: logits = tf.image.resize_bilinear(logits, tf.shape(labels)[1:3], align_corners=False) else: labels = tf.image.resize_images( labels, (height, width), method=tf.image.ResizeMethod.BILINEAR) valid_mask = tf.not_equal(labels, self._ignore_label) normalizer = tf.reduce_sum(tf.to_float(valid_mask)) # Assign pixel with ignore label to class 0 (background). The loss on the # pixel will later be masked out. labels = tf.where(valid_mask, labels, tf.zeros_like(labels)) labels = tf.squeeze(tf.cast(labels, tf.int32), axis=3) valid_mask = tf.squeeze(tf.cast(valid_mask, tf.float32), axis=3) cross_entropy_loss = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=labels, logits=logits) if not self._class_weights: class_weights = [1] * num_classes else: class_weights = self._class_weights if num_classes != len(class_weights): raise ValueError( 'Length of class_weights should be {}'.format(num_classes)) tf.logging.info('Using class weights: %s', class_weights) weight_mask = tf.einsum( '...y,y->...', tf.one_hot(labels, num_classes, dtype=tf.float32), tf.constant(class_weights, tf.float32)) valid_mask *= weight_mask cross_entropy_loss *= tf.to_float(valid_mask) loss = tf.reduce_sum(cross_entropy_loss) / normalizer return loss
def _set_padding_to_sentinel(padded_representations, sequence_lengths, sentinel): """Set padding on batch of padded representations to a sentinel value. Useful for preparing a batch of sequence representations for max or average pooling. Args: padded_representations: float32 tensor, shape (batch, longest_sequence, d), where d is some arbitrary embedding dimension. E.g. the output of tf.data.padded_batch. sequence_lengths: tensor, shape (batch,). Each entry corresponds to the original length of the sequence (before padding) of that sequence within the batch. sentinel: float32 tensor, shape: broadcastable to padded_representations. Returns: tensor of same shape as padded_representations, where all entries in the sequence dimension that came from padding (i.e. are beyond index sequence_length[i]) are set to sentinel. """ sequence_dimension = 1 embedding_dimension = 2 with tf.variable_scope('set_padding_to_sentinel', reuse=False): longest_sequence_length = tf.shape( padded_representations)[sequence_dimension] embedding_size = tf.shape(padded_representations)[embedding_dimension] seq_mask = tf.sequence_mask(sequence_lengths, longest_sequence_length) seq_mask = tf.expand_dims(seq_mask, [embedding_dimension]) is_not_padding = tf.tile(seq_mask, [1, 1, embedding_size]) full_sentinel = tf.zeros_like(padded_representations) full_sentinel = full_sentinel + tf.convert_to_tensor(sentinel) per_location_representations = tf.where(is_not_padding, padded_representations, full_sentinel) return per_location_representations
def lstm_decoder(self, inputs, sequence_length, hparams, clss, train, initial_state=None, bottleneck=None): # NOT IN PREDICT MODE. JUST RUN TEACHER-FORCED RNN: layers = contrib_rnn.MultiRNNCell([ self.lstm_cell(hparams, train) for _ in range(hparams.num_hidden_layers) ]) # append one-hot class to bottleneck, which will be given per step clss = tf.reshape(clss, [-1]) if not hparams.use_cls: clss = tf.zeros_like(clss) if hparams.condition_on_sln: sln = tf.reshape(sequence_length, [-1]) bottleneck = tf.concat((bottleneck, tf.one_hot(clss, hparams.num_categories), tf.one_hot(sln, 51)), -1) else: bottleneck = tf.concat((bottleneck, tf.one_hot(clss, hparams.num_categories)), -1) # tile and append the bottleneck to inputs sln_offset = 0 if hparams.condition_on_sln: sln_offset = 51 pre_tile_y = tf.reshape( bottleneck, [common_layers.shape_list(bottleneck)[0], 1, hparams.bottleneck_bits + hparams.num_categories + sln_offset]) overlay_x = tf.tile(pre_tile_y, [1, common_layers.shape_list(inputs)[1], 1]) inputs = tf.concat([inputs, overlay_x], -1) with tf.variable_scope('pre_decoder', reuse=tf.AUTO_REUSE): inputs = tf.layers.dense(inputs, hparams.hidden_size, name='bottom') inputs = tf.nn.tanh(inputs) # print(inputs) # print(initial_state) # input() with tf.variable_scope('lstm_decoder', reuse=tf.AUTO_REUSE): return tf.nn.dynamic_rnn( layers, inputs, sequence_length, initial_state=initial_state, dtype=tf.float32, time_major=False)
def test_static(self): with self.test_session(): x = tf.reshape(tf.range(6), [2, 3]) reconstruct = lambda v: v + 10 # test full mask mask = tf.ones_like(x, dtype=tf.int32) x_r = masked_reconstruct(reconstruct, x, mask) np.testing.assert_equal(x_r.eval(), [[10, 11, 12], [13, 14, 15]]) # test empty mask mask = tf.zeros_like(x, dtype=tf.int32) x_r = masked_reconstruct(reconstruct, x, mask) np.testing.assert_equal(x_r.eval(), [[0, 1, 2], [3, 4, 5]]) # test partial mask mask = tf.constant([[0, 1, 0], [1, 1, 0]], dtype=tf.int32) x_r = masked_reconstruct(reconstruct, x, mask) np.testing.assert_equal(x_r.eval(), [[0, 11, 2], [13, 14, 5]]) # test broadcast mask mask = tf.constant([0, 1, 1], dtype=tf.int32) x_r = masked_reconstruct(reconstruct, x, mask) np.testing.assert_equal(x_r.eval(), [[0, 11, 12], [3, 14, 15]]) mask = tf.constant([[0], [1]], dtype=tf.int32) x_r = masked_reconstruct(reconstruct, x, mask) np.testing.assert_equal(x_r.eval(), [[0, 1, 2], [13, 14, 15]]) # test non-broadcastable with pytest.raises(ValueError, match='Shape of `mask` cannot broadcast ' 'into the shape of `x`'): mask = tf.constant([1, 0, 0, 0]) _ = masked_reconstruct(reconstruct, x, mask) with pytest.raises(ValueError, match='Shape of `mask` cannot broadcast ' 'into the shape of `x`'): mask = tf.constant([[[0]]]) _ = masked_reconstruct(reconstruct, x, mask)
def dot_interact(concat_features, params=None): """Performs feature interaction operation between dense and sparse. Input tensors represent dense and sparse features. Pre-condition: The tensors have been stacked along dimension 1. Args: concat_features: Tensor of features with shape [B, n_features, feature_dim]. params: Model params. Returns: activations: Tensor representing interacted features. """ batch_size = concat_features.shape[0] if not params: params = {} # Interact features, select lower-triangular portion, and re-shape. xactions = tf.matmul(concat_features, concat_features, transpose_b=True) tf.logging.info("Model_FN: xactions shape: %s", xactions.get_shape()) ones = tf.ones_like(xactions) upper_tri_mask = tf.linalg.band_part(ones, 0, -1) feature_dim = xactions.shape[-1] if params["opt_skip_gather"]: upper_tri_bool = tf.cast(upper_tri_mask, tf.bool) activations = tf.where(condition=upper_tri_bool, x=tf.zeros_like(xactions), y=xactions) tf.logging.info("Model_FN: activations shape: %s", activations.get_shape()) out_dim = feature_dim * feature_dim else: lower_tri_mask = ones - upper_tri_mask activations = tf.boolean_mask(xactions, lower_tri_mask) tf.logging.info("Model_FN: activations shape: %s", activations.get_shape()) out_dim = feature_dim * (feature_dim - 1) // 2 activations = tf.reshape(activations, (batch_size, out_dim)) return activations
def _build_image(image): """Helper function to create a result for each image on the fly.""" # Expand the first dimension as batch_size = 1. images = tf.expand_dims(image, axis=0) # Tile the image num_domains times, so we can get all transformed together. images = tf.tile(images, [num_domains, 1, 1, 1]) # Create the targets to 0, 1, 2, ..., num_domains-1. targets = tf.one_hot(list(range(num_domains)), num_domains) with tf.variable_scope(stargan_model.generator_scope, reuse=True): # Add the original image. output_images_list = [image] # Generate the image and add to the list. gen_images = stargan_model.generator_fn(images, targets) gen_images_list = tf.split(gen_images, num_domains) gen_images_list = [ tf.squeeze(img, axis=0) for img in gen_images_list ] output_images_list.extend(gen_images_list) # Display diffs. if display_diffs: diff_images = gen_images - images diff_images_list = tf.split(diff_images, num_domains) diff_images_list = [ tf.squeeze(img, axis=0) for img in diff_images_list ] output_images_list.append(tf.zeros_like(image)) output_images_list.extend(diff_images_list) # Create the final image. final_image = eval_utils.image_reshaper(output_images_list, num_cols=num_domains + 1) # Reduce the first rank. return tf.squeeze(final_image, axis=0)
def compute_masked_example_loss( label_ids, per_example_loss, ): """Computes a mask that denotes whether the ith example has an answer. Args: label_ids: <int32>[batch_size, seq_length] per_example_loss: <float32>[batch_size] Returns: label_mask: <float32>[batch_size]. """ is_cell_supervision_available = tf.reduce_sum(label_ids, axis=1) > 0 mask = tf.where( is_cell_supervision_available, tf.ones_like(per_example_loss), tf.zeros_like(per_example_loss), ) mask = tf.stop_gradient(mask) return per_example_loss * mask
def sp_precision(y_true, y_pred): def precision(y_true, y_pred): true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1))) precision = true_positives / (predicted_positives + K.epsilon()) return precision def recall(y_true, y_pred): true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) possible_positives = K.sum(K.round(K.clip(y_true, 0, 1))) recall = true_positives / (possible_positives + K.epsilon()) return recall if exp_output != 'rnr': return 0 starts_pred = y_pred[:, :, :1] starts_true = y_true - tf.concat( [tf.zeros_like(y_true[:, :1, :]), y_true[:, 1:, :]], axis=1) starts_true = tf.clip_by_value(starts_true, 0., 1.) precision = precision(y_true, y_pred) return precision
def scaffold_fn(): """Scaffold function to restore non-logits vars from checkpoint.""" tf.train.init_from_checkpoint( FLAGS.checkpoint, {v.op.name: v.op.name for v in tf.global_variables(FLAGS.variable_schema)}) if FLAGS.zero_init_logits_layer: # Init op that initializes output layer parameters to zeros. output_layer_parameters = [ var for var in tf.trainable_variables() if var.name.startswith( 'head_supervised')] tf.logging.info('Initializing output layer parameters %s to zero', [x.op.name for x in output_layer_parameters]) with tf.control_dependencies([tf.global_variables_initializer()]): init_op = tf.group([ tf.assign(x, tf.zeros_like(x)) for x in output_layer_parameters]) return tf.train.Scaffold(init_op=init_op) else: return tf.train.Scaffold()
def landmark_ohem(landmark_pred, landmark_target, label): ''' :param landmark_pred: :param landmark_target: :param label: :return: mean euclidean loss ''' # keep label =-2 then do landmark detection ones = tf.ones_like(label, dtype=tf.float32) zeros = tf.zeros_like(label, dtype=tf.float32) valid_inds = tf.where(tf.equal(label, -2), ones, zeros) square_error = tf.square(landmark_pred - landmark_target) square_error = tf.reduce_sum(square_error, axis=1) num_valid = tf.reduce_sum(valid_inds) # keep_num = tf.cast(num_valid*num_keep_radio,dtype=tf.int32) keep_num = tf.cast(num_valid, dtype=tf.int32) square_error = square_error * valid_inds _, k_index = tf.nn.top_k(square_error, k=keep_num) square_error = tf.gather(square_error, k_index) return tf.reduce_mean(square_error)
def __call__(self, logits, labels): _, height, width, _ = logits.get_shape().as_list() # Use bilinear resizing because nearest neighbor is not supported in # tensorflow 1.14 with TPU. Once the environment is updated, it should be # change back to nearest neighbor. For now, it is tested and the performance # should be similar. labels = tf.image.resize_images(labels, (height, width), method=tf.image.ResizeMethod.BILINEAR) valid_mask = tf.not_equal(labels, self._ignore_label) normalizer = tf.reduce_sum(tf.to_float(valid_mask)) # Assign pixel with ignore label to class 0 (background). The loss on the # pixel will later be masked out. labels = tf.where(valid_mask, labels, tf.zeros_like(labels)) labels = tf.squeeze(tf.cast(labels, tf.int32), axis=3) valid_mask = tf.squeeze(tf.cast(valid_mask, tf.float32), axis=3) cross_entropy_loss = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=labels, logits=logits) cross_entropy_loss *= tf.to_float(valid_mask) loss = tf.reduce_sum(cross_entropy_loss) / normalizer return loss
def padded_accuracy_topk(predictions, labels, k, weights_fn=common_layers.weights_nonzero): """Percentage of times that top-k predictions matches labels on non-0s.""" with tf.variable_scope("padded_accuracy_topk", values=[predictions, labels]): padded_predictions, padded_labels = common_layers.pad_with_zeros( predictions, labels) weights = weights_fn(padded_labels) effective_k = tf.minimum( k, common_layers.shape_list(padded_predictions)[-1]) _, outputs = tf.nn.top_k(padded_predictions, k=effective_k) outputs = tf.to_int32(outputs) padded_labels = tf.to_int32(padded_labels) padded_labels = tf.expand_dims(padded_labels, axis=-1) padded_labels += tf.zeros_like(outputs) # Pad to same shape. same = tf.to_float(tf.equal(outputs, padded_labels)) same_topk = tf.reduce_sum(same, axis=-1) return same_topk, weights
def indicators_to_id(*indicators: tf.Tensor) -> tf.Tensor: """Returns the integer id resulting from crossing the (binary) indicators. Args: *indicators: int32 Tensors containing only `1` and `0` values. All tensors must have the same shape. Returns: An int32 Tensor of the same shape as the inputs, with values in the range from 0, inclusive, to 2**len(indicators), exclusive. Raises: ValueError: If `indicators` is empty. """ if not indicators: raise ValueError('`indicators` must not be empty.') result = tf.zeros_like(indicators[0]) for i, tensor in enumerate(reversed(indicators)): result += tensor * 2**i return result
def model_loss(input_real, input_z, output_channel_dim): g_model = generator(input_z, output_channel_dim, True) noisy_input_real = input_real + tf.random_normal( shape=tf.shape(input_real), mean=0.0, stddev=random.uniform(0.0, 0.1), dtype=tf.float32) d_model_real, d_logits_real = discriminator(noisy_input_real, reuse=False) d_model_fake, d_logits_fake = discriminator(g_model, reuse=True) d_loss_real = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits( logits=d_logits_real, labels=tf.ones_like(d_model_real) * random.uniform(0.9, 1.0))) d_loss_fake = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits( logits=d_logits_fake, labels=tf.zeros_like(d_model_fake))) d_loss = tf.reduce_mean(0.5 * (d_loss_real + d_loss_fake)) g_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits( logits=d_logits_fake, labels=tf.ones_like(d_model_fake))) return d_loss, g_loss
def pad_caption_to_input(caption, max_caption_len=64): # clip long captions caption = caption[0:max_caption_len] caption = tf.cast(caption, tf.int32) caption = tf.where(tf.equal(caption, -1), tf.zeros_like(caption), caption) # pad short captions up caption_len = tf.maximum(1, tf.shape(caption)[0]) caption = tf.pad(caption, [(0, tf.maximum(max_caption_len - caption_len, 0))]) input_seq = caption[0:-1] target_seq = caption[1:] indicator = tf.pad(tf.ones(caption_len - 1), [(0, tf.maximum(max_caption_len - caption_len, 0))]) input_seq.set_shape(max_caption_len - 1) target_seq.set_shape(max_caption_len - 1) indicator.set_shape(max_caption_len - 1) return input_seq, target_seq, indicator
def _make_image_summary(tensor, name=None): assert isinstance(tensor, (tf.Tensor, tf.Variable)) if name is None: name = tensor.name shape = tensor.shape.as_list() # Currently tensors of 2-D are supported only assert len(shape) == 2 axis, div, total = None, 9, 0 edge = int((div - 1) / 2) if shape[0] == 1: axis, total = 0, shape[1] elif shape[1] == 1: axis, total = 1, shape[0] if axis is not None and total > div: share = int(total / div) pad = tf.zeros_like(tensor) tensor = tf.concat([pad] * edge * share + [tensor] * share + [pad] * edge * share, axis=axis) shape = tensor.shape.as_list() image = tf.reshape(tensor, [1] + shape + [1]) # Initiate an image summary for image tensor image_summary = tf.summary.image(name, image, max_outputs=1) return image_summary
def create_model(self, optimizer): """Model function for Logistic Regression.""" features = tf.placeholder(tf.float32, shape=[None, 100], name='features') labels = tf.placeholder(tf.float32, shape=[None, 1], name='labels') W = tf.Variable(tf.zeros([100, 1])) b = tf.Variable(tf.zeros([1])) y_pred = tf.matmul(features, W) + b loss = 0.01 * tf.reduce_sum(tf.square(W)) + tf.reduce_mean( tf.maximum(tf.zeros_like(labels), 1 - labels * y_pred)) grads_and_vars = optimizer.compute_gradients(loss) grads, _ = zip(*grads_and_vars) train_op = optimizer.apply_gradients( grads_and_vars, global_step=tf.train.get_global_step()) eval_metric_ops = tf.count_nonzero(tf.equal(labels, tf.sign(y_pred))) return features, labels, train_op, grads, eval_metric_ops, loss, tf.sign( y_pred)
def batch_get_targets(batch_match, groundtruth_tensor_list, groundtruth_weights_list, unmatched_value, unmatched_weight): """Returns targets based on anchor-groundtruth box matching results. Args: batch_match: An int32 tensor of shape [batch, num_anchors] containing the result of target assignment returned by TargetAssigner.assign(..). groundtruth_tensor_list: A list of groundtruth tensors of shape [num_groundtruth, d_1, d_2, ..., d_k]. The tensors can be of any type. groundtruth_weights_list: A list of weights, one per groundtruth tensor, of shape [num_groundtruth]. unmatched_value: A tensor of shape [d_1, d_2, ..., d_k] of the same type as groundtruth tensor containing target value for anchors that remain unmatched. unmatched_weight: Scalar weight to assign to anchors that remain unmatched. Returns: targets: A tensor of shape [batch, num_anchors, d_1, d_2, ..., d_k] containing targets for anchors. weights: A float tensor of shape [batch, num_anchors] containing the weights to assign to each target. """ match_list = tf.unstack(batch_match) targets_list = [] weights_list = [] for match_tensor, groundtruth_tensor, groundtruth_weight in zip( match_list, groundtruth_tensor_list, groundtruth_weights_list): match_object = mat.Match(match_tensor) targets = match_object.gather_based_on_match( groundtruth_tensor, unmatched_value=unmatched_value, ignored_value=unmatched_value) targets_list.append(targets) weights = match_object.gather_based_on_match( groundtruth_weight, unmatched_value=unmatched_weight, ignored_value=tf.zeros_like(unmatched_weight)) weights_list.append(weights) return tf.stack(targets_list), tf.stack(weights_list)
def testUpdateVelocities(self): with tf.Graph().as_default(), self.test_session() as sess: layers = lc.LayerCollection() layers.register_categorical_predictive_distribution( tf.constant([1.0])) opt = optimizer.KfacOptimizer(0.1, 0.2, layers, 0.3, momentum=0.5, momentum_type='regular') x = tf.get_variable('x', initializer=tf.ones((2, 2))) y = tf.get_variable('y', initializer=tf.ones((2, 2)) * 2) vec1 = tf.ones((2, 2)) * 3 vec2 = tf.ones((2, 2)) * 4 model_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) update_op = opt._update_velocities([(vec1, x), (vec2, y)], 0.5) opt_vars = [ v for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) if v not in model_vars ] sess.run(tf.global_variables_initializer()) old_opt_vars = sess.run(opt_vars) # Optimizer vars start out at 0. for opt_var in old_opt_vars: self.assertAllEqual(sess.run(tf.zeros_like(opt_var)), opt_var) sess.run(update_op) new_opt_vars = sess.run(opt_vars) # After one update, the velocities are equal to the vectors. for vec, opt_var in zip([vec1, vec2], new_opt_vars): self.assertAllEqual(sess.run(vec), opt_var) sess.run(update_op) final_opt_vars = sess.run(opt_vars) for first, second in zip(new_opt_vars, final_opt_vars): self.assertFalse(np.equal(first, second).all())
def construct_lmcost(self, input_tensor_fw, input_tensor_bw, sentence_lengths, target_ids, lmcost_type, name): with tf.variable_scope(name): lmcost_max_vocab_size = min(len(self.word2id), self.config["lmcost_max_vocab_size"]) target_ids = tf.where( tf.greater_equal(target_ids, lmcost_max_vocab_size - 1), x=(lmcost_max_vocab_size - 1) + tf.zeros_like(target_ids), y=target_ids) cost = 0.0 if lmcost_type == "separate": lmcost_fw_mask = tf.sequence_mask( sentence_lengths, maxlen=tf.shape(target_ids)[1])[:, 1:] lmcost_bw_mask = tf.sequence_mask( sentence_lengths, maxlen=tf.shape(target_ids)[1])[:, :-1] lmcost_fw = self._construct_lmcost(input_tensor_fw[:, :-1, :], lmcost_max_vocab_size, lmcost_fw_mask, target_ids[:, 1:], name=name + "_fw") lmcost_bw = self._construct_lmcost(input_tensor_bw[:, 1:, :], lmcost_max_vocab_size, lmcost_bw_mask, target_ids[:, :-1], name=name + "_bw") cost += lmcost_fw + lmcost_bw elif lmcost_type == "joint": joint_input_tensor = tf.concat( [input_tensor_fw[:, :-2, :], input_tensor_bw[:, 2:, :]], axis=-1) lmcost_mask = tf.sequence_mask( sentence_lengths, maxlen=tf.shape(target_ids)[1])[:, 1:-1] cost += self._construct_lmcost(joint_input_tensor, lmcost_max_vocab_size, lmcost_mask, target_ids[:, 1:-1], name=name + "_joint") else: raise ValueError("Unknown lmcost_type: " + str(lmcost_type)) return cost
def calculate_generalized_advantage_estimator( reward, value, done, gae_gamma, gae_lambda): # pylint: disable=g-doc-args """Generalized advantage estimator. Returns: GAE estimator. It will be one element shorter than the input; this is because to compute GAE for [0, ..., N-1] one needs V for [1, ..., N]. """ # pylint: enable=g-doc-args next_value = value[1:, :] next_not_done = 1 - tf.cast(done[1:, :], tf.float32) delta = (reward[:-1, :] + gae_gamma * next_value * next_not_done - value[:-1, :]) return_ = tf.reverse(tf.scan( lambda agg, cur: cur[0] + cur[1] * gae_gamma * gae_lambda * agg, [tf.reverse(delta, [0]), tf.reverse(next_not_done, [0])], tf.zeros_like(delta[0, :]), parallel_iterations=1), [0]) return tf.check_numerics(return_, "return")
def img2mpi(self, img, depth, planedepths): """Compute ground truth MPI of visible content using depth map.""" height = tf.shape(img)[1] width = tf.shape(img)[2] num_depths = planedepths.shape[0] depth_inds = (tf.to_float(num_depths) - 1) * ( (1.0 / depth) - (1.0 / planedepths[0])) / ((1.0 / planedepths[-1]) - (1.0 / planedepths[0])) depth_inds = tf.round(depth_inds) depth_inds_tile = tf.to_int32( tf.tile(depth_inds[:, :, :, tf.newaxis], [1, 1, 1, num_depths])) _, _, d = tf.meshgrid( tf.range(height), tf.range(width), tf.range(num_depths), indexing='ij') mpi_colors = tf.to_float( tf.tile(img[:, :, :, tf.newaxis, :], [1, 1, 1, num_depths, 1])) mpi_alphas = tf.to_float( tf.where( tf.equal(depth_inds_tile, d), tf.ones_like(depth_inds_tile), tf.zeros_like(depth_inds_tile))) mpi = tf.concat([mpi_colors, mpi_alphas[Ellipsis, tf.newaxis]], axis=4) return mpi
def _coverage_loss(attn_dists, padding_mask): """Calculates the coverage loss from the attention distributions. Args: attn_dists: The attention distributions for each decoder timestep. A list length max_dec_steps containing shape (batch_size, attn_length) padding_mask: shape (batch_size, max_dec_steps). Returns: coverage_loss: scalar """ coverage = tf.zeros_like( attn_dists[0] ) # shape (batch_size, attn_length). Initial coverage is zero. covlosses = [ ] # Coverage loss per decoder timestep. Will be list length max_dec_steps containing shape (batch_size). for a in attn_dists: covloss = tf.reduce_sum(tf.minimum( a, coverage), [1]) # calculate the coverage loss for this step covlosses.append(covloss) coverage += a # update the coverage vector coverage_loss = _mask_and_avg(covlosses, padding_mask) return coverage_loss
def _calculate_aggregation_loss_known(logits_aggregation, aggregate_mask, aggregation_function_id, config): """Calculates aggregation loss when its type is known during training. In the weakly supervised setting, the only known information is that for cell selection examples, "no aggregation" should be predicted. For other examples (those that require aggregation), no loss is accumulated. In the setting where aggregation type is always known, standard cross entropy loss is accumulated for all examples. Args: logits_aggregation: <float32>[batch_size, num_aggregation_labels] aggregate_mask: <float32>[batch_size] aggregation_function_id: <int32>[batch_size] config: Configuration for Tapas model. Returns: aggregation_loss_known: <float32>[batch_size, num_aggregation_labels] """ if config.use_answer_as_supervision: # Prepare "no aggregation" targets for cell selection examples. target_aggregation = tf.zeros_like(aggregate_mask, dtype=tf.int32) else: # Use aggregation supervision as the target. target_aggregation = aggregation_function_id one_hot_labels = tf.one_hot(target_aggregation, depth=config.num_aggregation_labels, dtype=tf.float32) log_probs = tf.nn.log_softmax(logits_aggregation, axis=-1) # <float32>[batch_size] per_example_aggregation_intermediate = -tf.reduce_sum( one_hot_labels * log_probs, axis=-1) if config.use_answer_as_supervision: # Accumulate loss only for examples requiring cell selection # (no aggregation). return per_example_aggregation_intermediate * (1 - aggregate_mask) else: return per_example_aggregation_intermediate
def preprocess_targets(targets, i): targets = self._shard_features({'targets': targets})['targets'] modality_name = hparams.name.get( 'targets', modalities.get_name(target_modality))(hparams, target_vocab_size) with tf.variable_scope(modality_name + '/targets'): bottom = hparams.bottom.get( 'targets', modalities.get_targets_bottom(target_modality)) targets = dp(bottom, targets, hparams, target_vocab_size)[0] targets = common_layers.flatten4d3d(targets) if not self.get_decode_start_id(): targets = tf.cond( tf.equal(i, 0), lambda: tf.zeros_like(targets), lambda: targets, ) if positional_encoding is not None: targets += positional_encoding[:, i:i + 1] return targets