def body(tf_content_encodings, level, copy_score, output, i):
                        # find value for write
                        # which is "copy" tensor
                        ##################################
                        current_content = tf_content_encodings[i] # single content sample in batch
                        current_level = level[i] # single level sample in batch
                        current_copy_score_vector = copy_score[i]

                        # find the copy indices to generate the s_t^copy vector
                        c, _ = tf.setdiff1d(current_content, current_level) # test should be content and target should be level
                        output_level_index, true_content_index = tf.setdiff1d(current_content, c) 

                        # true_content_index is the index to extract copy score...
                        # output_level_index is the index to place the copy score

                        # Use the gather and the scatter_add mechanism to obtain the final s_t^copy vector
                        out_value_list = tf.gather(current_copy_score_vector, true_content_index)
                        final_output = tf.scatter_add(
                            # This reference must not be added to the optimizer's backpropagation ops
                            # so turn the trainable property off.
                            # Also, since the initial_value is a lambda, the dtype has to be explicitly mentioned
                            tf.Variable(lambda: tf.zeros(shape=(content_label_vocab_size)), trainable=False, dtype=tf.float32),
                            output_level_index, out_value_list)

                        #################################
                        w_v = final_output

                        output = output.write(i,w_v)
                        return tf_content_encodings, level,copy_score, output, i + 1
def sparse_dropout_row_col(values, mask_inds, shape, rate=0.0, training=True):
    """Apply dropout to rows and columns independently with probability rate."""
    N, M, K = shape
    vals = tf.reshape(values, [-1, K])
    num_vals = tf.shape(vals)[0]

    row_mask = np.random.choice([0, 1], size=N,
                                p=(rate, 1 -
                                   rate))  # Dropout rows where this mask is 0
    row_keep = np.arange(N)[row_mask == 1]
    _, row_inds = tf.setdiff1d(mask_inds[:, 0], row_keep)

    col_mask = np.random.choice([0, 1], size=M,
                                p=(rate, 1 -
                                   rate))  # Dropout cols where this mask is 0
    col_keep = np.arange(M)[col_mask == 1]
    _, col_inds = tf.setdiff1d(mask_inds[:, 1], col_keep)

    inds, _ = tf.unique(tf.concat([row_inds, col_inds], axis=0))

    drop_mask = tf.scatter_nd(tf.expand_dims(inds, axis=1),
                              tf.ones_like(inds),
                              shape=[num_vals])
    drop_mask = tf.cast(drop_mask, tf.bool)

    new_vals = tf.where(drop_mask, tf.zeros_like(vals), vals)
    new_vals = tf.reshape(new_vals, [-1]) / (1 - rate * rate)

    return new_vals
Exemple #3
0
def dataset_fft_to_mel_multi_with_files(fn, tfrecord, num_timesteps, num_freqs, linear_to_mel_weight_matrix, all_labels):
    """ Takes consequtive samples with step size out of each tfr"""
    context_out, feat_list_out = tf.parse_single_sequence_example(
        tfrecord,
        context_features={
            "label": tf.FixedLenFeature((1,), dtype=tf.string)
        },
        sequence_features={
            "spectrogram": tf.FixedLenSequenceFeature((num_freqs,), tf.float32),
        }
    )
    spectrogram_all = feat_list_out['spectrogram']

    mel_spect = tf.tensordot(spectrogram_all, linear_to_mel_weight_matrix, 1)
    mel_spect = tf.math.log(mel_spect + 0.00001)

    mel_spect = tf.expand_dims(mel_spect, -1)

    spectrogram_frames = tf.transpose(shape_ops.frame(mel_spect, num_timesteps, num_timesteps//4, axis=0),[0,2,1,3])

    lbl = context_out['label']

    _, idx = tf.setdiff1d(tf.constant(all_labels), lbl)
    idx, _ = tf.setdiff1d(tf.range(len(all_labels)), idx)

    lblIndex = tf.fill([tf.shape(spectrogram_frames)[0]], tf.to_int32(idx[0]))
    files = tf.fill([tf.shape(spectrogram_frames)[0]], fn)
    return spectrogram_frames, lblIndex, files
def closest_class_prediction(pairwise_distances, labels):
    """
    Helper function to gather predictions for top-1 accuracy calculation

    :param pairwise_distances: nxn matrix with cosine distances within a batch
    :param labels: nx1 ids of identities
    :return:
    """

    max_values = tf.reduce_max(pairwise_distances)
    diag_replacer = tf.tile(
        tf.reduce_max(pairwise_distances)[None, ...],
        [tf.shape(pairwise_distances)[0]])

    # The distance of embedding to itself will be 0, so we're replacing it with the max value
    replaced_diag = tf.linalg.set_diag(pairwise_distances, diag_replacer)

    indecies_min = tf.arg_min(replaced_diag, 1)

    predictions_raw = tf.gather(labels, indecies_min)

    # Filter classes with one instance only
    classes, _, counts = tf.unique_with_counts(labels)
    classes_not_for_acuracy = classes[tf.equal(counts, 1)]

    _, indecies_to_keep = tf.setdiff1d(labels, classes_not_for_acuracy)

    labels_selected = tf.gather(labels, indecies_to_keep)

    predictions = tf.gather(predictions_raw, indecies_to_keep)

    return predictions, labels_selected
    def run(self):
        x = self.convert(
            np.array([[2, 2, 1, 3], [4, 5, 6, 1], [0, 1, 1, -2], [6, 2, 3,
                                                                  0]]))

        y = self.convert(np.array([1, 2, 3, 5, 7]))

        z = self.convert((np.array([1, 0, 4, 6, 2])))

        arg_min = tf.argmin(x, 1)
        arg_max = tf.argmax(x, 1)
        unique = tf.unique(y)
        diff = tf.setdiff1d(y, z)

        with tf.Session() as session:
            print("Sequence utilities examples:")
            print("argmin and argmax input matrix: ")
            print(session.run(x))
            print("Argmin axis 1: ", session.run(arg_min))
            print("Argmax axis 1: ", session.run(arg_max))
            print("")
            print("Unique input array: ", session.run(y))
            print("Unique: ", session.run(unique))
            print("")
            print("Diff input arays: ")
            print(session.run(y))
            print(session.run(z))
            print("Diff: ", session.run(diff))
Exemple #6
0
 def _tensordot_reshape(a, axes, flipped=False):
     """Helper method to perform transpose and reshape for contraction op.
     This method is helpful in reducing `math_tf.tensordot` to `math_tf.matmul`
     using `tf.transpose` and `tf.reshape`. The method takes a
     tensor and performs the correct transpose and reshape operation for a given
     set of indices. It returns the reshaped tensor as well as a list of indices
     necessary to reshape the tensor again after matrix multiplication.
     Args:
         a: `Tensor`.
         axes: List or `int32` `Tensor` of unique indices specifying valid axes of
          `a`.
         flipped: An optional `bool`. Defaults to `False`. If `True`, the method
             assumes that `a` is the second argument in the contraction operation.
     Returns:
         A tuple `(reshaped_a, free_dims, free_dims_static)` where `reshaped_a` is
         the tensor `a` reshaped to allow contraction via `matmul`, `free_dims` is
         either a list of integers or an `int32` `Tensor`, depending on whether
         the shape of a is fully specified, and free_dims_static is either a list
         of integers and None values, or None, representing the inferred
         static shape of the free dimensions
     """
     if a.get_shape().is_fully_defined() and isinstance(axes, (list, tuple)):
         shape_a = a.get_shape().as_list()
         axes = [i if i >= 0 else i + len(shape_a) for i in axes]
         free = [i for i in range(len(shape_a)) if i not in axes]
         free_dims = [shape_a[i] for i in free]
         prod_free = int(np.prod([shape_a[i] for i in free]))
         prod_axes = int(np.prod([shape_a[i] for i in axes]))
         perm = list(axes) + free if flipped else free + list(axes)
         new_shape = [prod_axes, prod_free] if flipped else [prod_free, prod_axes]
         reshaped_a = tf.reshape(tf.transpose(a, perm), new_shape)
         return reshaped_a, free_dims, free_dims
     else:
         if a.get_shape().ndims is not None and isinstance(axes, (list, tuple)):
             shape_a = a.get_shape().as_list()
             axes = [i if i >= 0 else i + len(shape_a) for i in axes]
             free = [i for i in range(len(shape_a)) if i not in axes]
             free_dims_static = [shape_a[i] for i in free]
         else:
             free_dims_static = None
         shape_a = tf.shape(a)
         rank_a = tf.rank(a)
         axes = tf.convert_to_tensor(axes, dtype=tf.int32, name="axes")
         axes = tf.cast(axes >= 0, tf.int32) * axes + tf.cast(
                 axes < 0, tf.int32) * (
                         axes + rank_a)
         free, _ = tf.setdiff1d(tf.range(rank_a), axes)
         free_dims = tf.gather(shape_a, free)
         axes_dims = tf.gather(shape_a, axes)
         prod_free_dims = tf.reduce_prod(free_dims)
         prod_axes_dims = tf.reduce_prod(axes_dims)
         perm = tf.concat([axes_dims, free_dims], 0)
         if flipped:
             perm = tf.concat([axes, free], 0)
             new_shape = tf.stack([prod_axes_dims, prod_free_dims])
         else:
             perm = tf.concat([free, axes], 0)
             new_shape = tf.stack([prod_free_dims, prod_axes_dims])
         reshaped_a = tf.reshape(tf.transpose(a, perm), new_shape)
         return reshaped_a, free_dims, free_dims_static
Exemple #7
0
 def forward_tensor(self, x):
     y = tf.reshape(x, [-1])
     total_size = tf.shape(y)[0] + self.fixed_inds.shape[0]
     nonfixed_inds = tf.setdiff1d(tf.range(total_size), self.fixed_inds)[0]
     y = tf.reshape(
         tf.dynamic_stitch([self.fixed_inds, nonfixed_inds],
                           [self.fixed_vals, y]), [tf.shape(x)[0], -1])
     return y
Exemple #8
0
def variable_set_accuracy_1d(y_pred, y_true):
    mask = tf.not_equal(tf.zeros_like(y_true), y_true)
    diff = tf.setdiff1d(tf.boolean_mask(y_pred, mask),
                        tf.boolean_mask(y_true, mask))
    #diff = tf.listdiff(tf.boolean_mask(y_pred,mask),tf.boolean_mask(y_true,mask))
    accuracy = 1 - tf.reduce_sum(tf.to_float(tf.ones_like(
        diff.idx))) / tf.reduce_sum(tf.to_float(mask))
    return accuracy
 def func_body_augmented(iteration, chosen_ids):
     # find a new facility location to add
     #  based on the clustering score and the NMI score
     candidate_ids = tf.setdiff1d(all_ids, chosen_ids)[0]
     new_chosen_idx = _find_loss_augmented_facility_idx(
         pairwise_distances, labels, chosen_ids, candidate_ids,
         margin_multiplier, margin_type)
     chosen_ids = tf.concat([chosen_ids, [new_chosen_idx]], 0)
     return iteration + 1, chosen_ids
def _squeeze(x, axis):
  """A version of squeeze that works with dynamic axis."""
  x = tf.convert_to_tensor(x, name='x')
  if axis is None:
    return tf.squeeze(x, axis=None)
  axis = tf.convert_to_tensor(axis, name='axis', dtype=tf.int32)
  axis += tf.zeros([1], dtype=axis.dtype)  # Make axis at least 1d.
  keep_axis, _ = tf.setdiff1d(tf.range(0, tf.rank(x)), axis)
  return tf.reshape(x, tf.gather(tf.shape(x), keep_axis))
def _squeeze(x, axis):
    """A version of squeeze that works with dynamic axis."""
    x = tf.convert_to_tensor(x, name='x')
    if axis is None:
        return tf.squeeze(x, axis=None)
    axis = tf.convert_to_tensor(axis, name='axis')
    axis += tf.zeros([1], dtype=axis.dtype)  # Make axis at least 1d.
    keep_axis, _ = tf.setdiff1d(tf.range(0, tf.rank(x)), axis)
    return tf.reshape(x, tf.gather(tf.shape(x), keep_axis))
Exemple #12
0
        def _match_when_rows_are_non_empty():
            """Performs matching when the rows of similarity matrix are non empty.

            Returns:
              matches:  int32 tensor indicating the row each column matches to.
            """
            # Matches for each column
            matches = tf.argmax(similarity_matrix, 0)

            # Deal with matched and unmatched threshold
            if self._matched_threshold is not None:
                # Get logical indices of ignored and unmatched columns as tf.int64
                matched_vals = tf.reduce_max(similarity_matrix, 0)
                below_unmatched_threshold = tf.greater(self._unmatched_threshold,
                                                       matched_vals)
                between_thresholds = tf.logical_and(
                    tf.greater_equal(matched_vals, self._unmatched_threshold),
                    tf.greater(self._matched_threshold, matched_vals))

                if self._negatives_lower_than_unmatched:
                    matches = self._set_values_using_indicator(matches,
                                                               below_unmatched_threshold,
                                                               -1)
                    matches = self._set_values_using_indicator(matches,
                                                               between_thresholds,
                                                               -2)
                else:
                    matches = self._set_values_using_indicator(matches,
                                                               below_unmatched_threshold,
                                                               -2)
                    matches = self._set_values_using_indicator(matches,
                                                               between_thresholds,
                                                               -1)

                if self._ignore_sim_less_than_zero:
                    # If a minimum value is less than zero and a maximum value is zero, the columns are ignored
                    ignore_vals = tf.reduce_min(similarity_matrix, 0)
                    ignore_indicator = tf.logical_and(tf.less(ignore_vals, 0), tf.equal(matched_vals, 0))
                    matches = self._set_values_using_indicator(matches, ignore_indicator, -2)

            if self._force_match_for_each_row:
                forced_matches_ids = tf.cast(tf.argmax(similarity_matrix, 1), tf.int32)

                # Set matches[forced_matches_ids] = [0, ..., R], R is number of rows.
                row_range = tf.range(tf.shape(similarity_matrix)[0])
                col_range = tf.range(tf.shape(similarity_matrix)[1])
                forced_matches_values = tf.cast(row_range, matches.dtype)
                keep_matches_ids, _ = tf.setdiff1d(col_range, forced_matches_ids)
                keep_matches_values = tf.gather(matches, keep_matches_ids)
                matches = tf.dynamic_stitch([forced_matches_ids, keep_matches_ids],
                                            [forced_matches_values, keep_matches_values])

            return tf.cast(matches, tf.int32)
Exemple #13
0
def dataset_fft_to_mel_single(tfrecord, num_timesteps, num_freqs, linear_to_mel_weight_matrix, all_labels, augment):
    """ Takes a single random sample out of each tfr"""
    context_out, feat_list_out = tf.parse_single_sequence_example(
        tfrecord,
        context_features={
            "label": tf.FixedLenFeature((1,), dtype=tf.string)
        },
        sequence_features={
            "spectrogram": tf.FixedLenSequenceFeature((num_freqs,), tf.float32),
        }
    )
    spectrogram_all = feat_list_out['spectrogram']
    shp = tf.shape(spectrogram_all)

    if augment:
        # augment by scaling and shifting
        shift=tf.random_uniform([1,1],1-0.3,1+0.3)
        scale=tf.random_uniform([1,1],1-0.3,1+0.3)
        shift_scale=tf.concat([shift, tf.zeros([1,3]),scale, tf.zeros([1, 3])],axis=1,name='shiftscale')
        spectrogram_aug = tf.cond(tf.random_uniform([],0,1) < 0.5, lambda: tf.contrib.image.transform(spectrogram_all,shift_scale,interpolation='BILINEAR',name='augment'), lambda: spectrogram_all )
    else:
        spectrogram_aug=spectrogram_all

    # random offset into the file
    frame_offset=tf.cond(  shp[0]>num_timesteps, lambda: tf.random_uniform([1],minval=0,maxval=tf.subtract(shp[0],num_timesteps),dtype=tf.int32,seed=42),lambda: tf.zeros([1], dtype=tf.int32))
    slice_start = tf.concat([frame_offset,[0]],0)

    slice = tf.slice(spectrogram_aug, slice_start, [num_timesteps, num_freqs])

    mel_slice = tf.tensordot(slice, linear_to_mel_weight_matrix, 1)
    mel_slice = tf.math.log(mel_slice + 0.00001)

    mel_slice = tf.expand_dims(tf.transpose(mel_slice), -1)

    lbl = context_out['label']

    _, idx = tf.setdiff1d(tf.constant(all_labels), lbl)
    idx, _ = tf.setdiff1d(tf.range(len(all_labels)), idx)

    return mel_slice , idx[0]
                    def body(tf_content_encodings, level, copy_score, output,
                             i):
                        # find value for write
                        # which is "copy" tensor
                        ##################################
                        current_content = tf_content_encodings[
                            i]  # single content sample in batch
                        current_level = level[
                            i]  # single level sample in batch
                        current_copy_score_vector = copy_score[i]

                        # find the copy indices to generate the s_t^copy vector
                        c, _ = tf.setdiff1d(
                            current_content, current_level
                        )  # test should be content and target should be level
                        output_level_index, true_content_index = tf.setdiff1d(
                            current_content, c)

                        # true_content_index is the index to extract copy score...
                        # output_level_index is the index to place the copy score

                        # Use the gather and the scatter_add mechanism to obtain the final s_t^copy vector
                        out_value_list = tf.gather(current_copy_score_vector,
                                                   true_content_index)
                        final_output = tf.scatter_add(
                            # This reference must not be added to the optimizer's backpropagation ops
                            # so turn the trainable property off.
                            # Also, since the initial_value is a lambda, the dtype has to be explicitly mentioned
                            tf.Variable(lambda: tf.zeros(shape=(
                                content_label_vocab_size)),
                                        trainable=False,
                                        dtype=tf.float32),
                            output_level_index,
                            out_value_list)

                        #################################
                        w_v = final_output

                        output = output.write(i, w_v)
                        return tf_content_encodings, level, copy_score, output, i + 1
Exemple #15
0
def predict(itemSet, true_target):
    precision = []
    itemSets = tf.reshape(tf.tile(itemSet, [numItems]),
                          [numItems, tf.size(itemSet)])
    #itemSets = [itemSet for i in range(numItems)]
    targets = list(range(numItems))
    scores = unnormalized_predict(itemSets, targets)
    _, indices = tf.nn.top_k(scores, k=numItems, sorted=True)
    percentile_rank = tf.where(tf.equal(indices, true_target))
    for K in Ks:
        _, indices = tf.nn.top_k(scores, k=K, sorted=True)
        out, idx = tf.setdiff1d(indices, true_target)
        precision.append(K - tf.shape(out))
    return precision, percentile_rank
Exemple #16
0
def find_dup(a):
    """ Find the duplicated elements in 1-D a tensor.
  Args:
    a: 1-D tensor.
    
  Return:
    more_than_one_vals: duplicated value in a.
    indexes_in_a: duplicated value's index in a.
    dups_in_a: duplicated value with duplicate in a.
  """
    unique_a_vals, unique_idx = tf.unique(a)
    count_a_unique = tf.unsorted_segment_sum(tf.ones_like(a), unique_idx,
                                             tf.shape(a)[0])

    more_than_one = tf.greater(count_a_unique, 1)
    more_than_one_idx = tf.squeeze(tf.where(more_than_one))
    more_than_one_vals = tf.squeeze(tf.gather(unique_a_vals,
                                              more_than_one_idx))

    not_duplicated, _ = tf.setdiff1d(a, more_than_one_vals)
    dups_in_a, indexes_in_a = tf.setdiff1d(a, not_duplicated)

    return more_than_one_vals, indexes_in_a, dups_in_a
Exemple #17
0
def find_dup(a):
  """ Find the duplicated elements in 1-D a tensor.
  Args:
    a: 1-D tensor.
    
  Return:
    more_than_one_vals: duplicated value in a.
    indexes_in_a: duplicated value's index in a.
    dups_in_a: duplicated value with duplicate in a.
  """
  unique_a_vals, unique_idx = tf.unique(a)
  count_a_unique = tf.unsorted_segment_sum(tf.ones_like(a),
                                           unique_idx,
                                           tf.shape(a)[0])

  more_than_one = tf.greater(count_a_unique, 1)
  more_than_one_idx = tf.squeeze(tf.where(more_than_one))
  more_than_one_vals = tf.squeeze(tf.gather(unique_a_vals, more_than_one_idx))

  not_duplicated, _ = tf.setdiff1d(a, more_than_one_vals)
  dups_in_a, indexes_in_a = tf.setdiff1d(a, not_duplicated)

  return more_than_one_vals, indexes_in_a, dups_in_a
 def _sparse_tensordot_reshape(a, axes, flipped=False):
     if a.get_shape().is_fully_defined() and isinstance(
             axes, (list, tuple)):
         shape_a = a.get_shape().as_list()
         axes = [i if i >= 0 else i + len(shape_a) for i in axes]
         free = [i for i in tf.range(len(shape_a)) if i not in axes]
         free_dims = [shape_a[i] for i in free]
         # prod_free = int(np.prod(np.array([shape_a[i] for i in free])))
         # prod_axes = int(np.prod(np.array([shape_a[i] for i in axes])))
         prod_free = tf.math.reduce_prod(
             tf.constant([shape_a[i] for i in free]))
         prod_axes = tf.math.reduce_prod(
             tf.constant([shape_a[i] for i in axes]))
         perm = list(axes) + free if flipped else free + list(axes)
         new_shape = [prod_axes, prod_free
                      ] if flipped else [prod_free, prod_axes]
         reshaped_a = tf.sparse.reshape(tf.sparse.transpose(a, perm),
                                        new_shape)
         return reshaped_a, free_dims, free_dims
     else:
         if a.get_shape().ndims is not None and isinstance(
                 axes, (list, tuple)):
             shape_a = a.get_shape().as_list()
             axes = [i if i >= 0 else i + len(shape_a) for i in axes]
             free = [i for i in tf.range(len(shape_a)) if i not in axes]
             free_dims_static = [shape_a[i] for i in free]
         else:
             free_dims_static = None
         shape_a = tf.shape(a)
         rank_a = tf.rank(a)
         axes = tf.convert_to_tensor(axes, dtype=tf.int32, name="axes")
         axes = tf.cast(axes >= 0, tf.int32) * axes + tf.cast(
             axes < 0, tf.int32) * (axes + rank_a)
         # print(sess.run(rank_a), sess.run(axes))
         free, _ = tf.setdiff1d(tf.range(rank_a), axes)
         free_dims = tf.gather(shape_a, free)
         axes_dims = tf.gather(shape_a, axes)
         prod_free_dims = tf.reduce_prod(free_dims)
         prod_axes_dims = tf.reduce_prod(axes_dims)
         # perm = tf.concat([axes_dims, free_dims], 0)
         if flipped:
             perm = tf.concat([axes, free], 0)
             new_shape = tf.stack([prod_axes_dims, prod_free_dims])
         else:
             perm = tf.concat([free, axes], 0)
             new_shape = tf.stack([prod_free_dims, prod_axes_dims])
         reshaped_a = tf.sparse_reshape(tf.sparse_transpose(a, perm),
                                        new_shape)
         return reshaped_a, free_dims, free_dims_static
    def _match_when_rows_are_non_empty():
      """Performs matching when the rows of similarity matrix are non empty.

      Returns:
        matches:  int32 tensor indicating the row each column matches to.
      """
      # Matches for each column
      matches = tf.argmax(similarity_matrix, 0)

      # Deal with matched and unmatched threshold
      if self._matched_threshold is not None:
        # Get logical indices of ignored and unmatched columns as tf.int64
        matched_vals = tf.reduce_max(similarity_matrix, 0)
        below_unmatched_threshold = tf.greater(self._unmatched_threshold,
                                               matched_vals)
        between_thresholds = tf.logical_and(
            tf.greater_equal(matched_vals, self._unmatched_threshold),
            tf.greater(self._matched_threshold, matched_vals))

        if self._negatives_lower_than_unmatched:
          matches = self._set_values_using_indicator(matches,
                                                     below_unmatched_threshold,
                                                     -1)
          matches = self._set_values_using_indicator(matches,
                                                     between_thresholds,
                                                     -2)
        else:
          matches = self._set_values_using_indicator(matches,
                                                     below_unmatched_threshold,
                                                     -2)
          matches = self._set_values_using_indicator(matches,
                                                     between_thresholds,
                                                     -1)

      if self._force_match_for_each_row:
        forced_matches_ids = tf.cast(tf.argmax(similarity_matrix, 1), tf.int32)

        # Set matches[forced_matches_ids] = [0, ..., R], R is number of rows.
        row_range = tf.range(tf.shape(similarity_matrix)[0])
        col_range = tf.range(tf.shape(similarity_matrix)[1])
        forced_matches_values = tf.cast(row_range, matches.dtype)
        keep_matches_ids, _ = tf.setdiff1d(col_range, forced_matches_ids)
        keep_matches_values = tf.gather(matches, keep_matches_ids)
        matches = tf.dynamic_stitch(
            [forced_matches_ids,
             keep_matches_ids], [forced_matches_values, keep_matches_values])

      return tf.cast(matches, tf.int32)
Exemple #20
0
def wrapperSummary(url,nSent=4,nRelevance=10):
    urlTarget = url
    sess = tf.Session()
    nl = NaturalLanguageUnderstandingV1(
      username='******',
      password='******',
      version='2017-02-27')
    #print(nRelevance)
    response = nl.analyze(
      url=urlTarget,
      #text="Different categories of gender very in the context they are in.",
      features=Features(
        #entities=EntitiesOptions(emotion=True,sentiment=True,limit=2),
        keywords=KeywordsOptions(limit=nRelevance+1)),
      return_analyzed_text=True)

    stopword = set(corpus.stopwords.words("english"));
    article = tk.sent_tokenize(response["analyzed_text"])
    keywords = response['keywords']
    print(keywords)
    sentRelevance = []
    s = stem.PorterStemmer();
    for sent in article:
        score = [k['relevance'] for k in keywords if stemmedPunctuationTokenized(sent,stopword).lower().find(stemmedPunctuationTokenized(k['text'],stopword).lower())!=-1]
        #print(score)
        floatscore = [float(number) for number in score ]
        sentRelevance.append(np.sum(floatscore))
    totalSent = np.size(sentRelevance)
    #nSentActual = math.floor(totalSent*(1-compressionRate))
    nSentActual = min(nSent,totalSent)
    relevantV,indicies =tf.nn.top_k(tf.constant(sentRelevance),k=nSentActual)
    indexZeros = tf.reshape(tf.where(tf.equal(relevantV,0)),[-1])
    indexAll = tf.expand_dims(tf.range(0,tf.shape(indicies)[0]),0)
    flatIndexAll = tf.reduce_sum(indexAll,0)
    
    validSet= tf.setdiff1d(flatIndexAll,tf.cast(indexZeros,dtype=tf.int32))
    indexNonZero = sess.run(tf.gather(indicies,validSet[0]))

    relevantSentences = np.take(article,np.sort(indexNonZero))
    joinedRelevance = " ".join(relevantSentences)
    return joinedRelevance
Exemple #21
0
        def func():
            rank = tf.constant(3, dtype=tf.int32, name='rank')
            start = tf.constant(0, dtype=tf.int32, name='start')
            delta = tf.constant(1, dtype=tf.int32, name='delta')
            tensor_dot_range = tf.range(start, rank, delta)

            axes = tf.constant([2], dtype=tf.int32, name='axes')
            ge_y = tf.constant(0, dtype=tf.int32, name='ge_y')
            ge = tf.greater_equal(axes, ge_y)
            cast = tf.cast(ge, tf.int32)
            mul_1 = tf.multiply(cast, axes)

            less_y = tf.constant(0, dtype=tf.int32)
            less = tf.less(axes, less_y)
            cast_2 = tf.cast(less, tf.int32)
            add_1 = tf.add(axes, rank)
            mul_2 = tf.multiply(cast_2, add_1)

            add_2 = tf.add(mul_1, mul_2)
            out, _ = tf.setdiff1d(tensor_dot_range, add_2)
            return tf.identity(out, name="output_0")
Exemple #22
0
    def test_bahdanau_attention_memory_layer_tensordot(self):
        rank = tf.constant(3, dtype=tf.int32, name='rank')
        start = tf.constant(0, dtype=tf.int32, name='start')
        delta = tf.constant(1, dtype=tf.int32, name='delta')
        tensor_dot_range = tf.range(start, rank, delta)

        axes = tf.constant([2], dtype=tf.int32, name='axes')
        ge_y = tf.constant(0, dtype=tf.int32, name='ge_y')
        ge = tf.greater_equal(axes, ge_y)
        cast = tf.cast(ge, tf.int32)
        mul_1 = tf.multiply(cast, axes)

        less_y = tf.constant(0, dtype=tf.int32)
        less = tf.less(axes, less_y)
        cast_2 = tf.cast(less, tf.int32)
        add_1 = tf.add(axes, rank)
        mul_2 = tf.multiply(cast_2, add_1)

        add_2 = tf.add(mul_1, mul_2)
        out, _ = tf.setdiff1d(tensor_dot_range, add_2)
        _ = tf.identity(out, name="output_0")
        self._run_test_case(["output_0:0"], {})
>array([13,  2,  7,  4], dtype=int32)
"""

tf.segment_mean(data, segment_ids, name=None)
tf.segment_max(data, segment_ids, name=None)
tf.segment_min(data, segment_ids, name=None)
tf.segment_prod(data, segment_ids, name=None)
 
# 其它
tf.unsorted_segment_sum
tf.sparse_segment_sum
tf.sparse_segment_mean
tf.sparse_segment_sqrt_n

# 比较两个 list 或者 string 的不同,并返回不同的值和索引
tf.setdiff1d(x, y, index_dtype=tf.int32, name=None)
 
# 返回 x 中的唯一值所组成的tensor 和原 tensor 中元素在现 tensor 中的索引
tf.unique(x, out_idx=None, name=None)
 
# x if condition else y, condition 为 bool 类型的,可用tf.equal()等来表示
# x 和 y 的形状和数据类型必须一致
tf.where(condition, x=None, y=None, name=None)
 
# 返回沿着坐标轴方向的最大/最小值的索引
tf.argmax(input, axis=None, name=None, output_type=tf.int64)
tf.argmin(input, axis=None, name=None, output_type=tf.int64)
 
# x 的值当作 y 的索引,range(len(x)) 索引当作 y 的值
# y[x[i]] = i for i in [0, 1, ..., len(x) - 1]
tf.invert_permutation(x, name=None)
Exemple #24
0
    def one_step(self, current_state, previous_kernel_results):
        """Takes one step of the TransitionKernel.

    Args:
      current_state: `Tensor` or Python `list` of `Tensor`s representing the
        current state(s) of the Markov chain(s).
      previous_kernel_results: A (possibly nested) `tuple`, `namedtuple` or
        `list` of `Tensor`s representing internal calculations made within the
        previous call to this function (or as returned by `bootstrap_results`).

    Returns:
      next_state: `Tensor` or Python `list` of `Tensor`s representing the
        next state(s) of the Markov chain(s).
      kernel_results: A (possibly nested) `tuple`, `namedtuple` or `list` of
        `Tensor`s representing internal calculations made within this function.
        This inculdes replica states.
    """
        # Key difficulty:  The type of exchanges differs from one call to the
        # next...even the number of exchanges can differ.
        # As a result, exchanges must happen dynamically, in while loops.
        with tf.name_scope(name=mcmc_util.make_name(self.name, 'remc',
                                                    'one_step'),
                           values=[current_state, previous_kernel_results]):

            # Each replica does `one_step` to get pre-exchange states/KernelResults.
            sampled_replica_states, sampled_replica_results = zip(*[
                rk.one_step(previous_kernel_results.replica_states[i],
                            previous_kernel_results.replica_results[i])
                for i, rk in enumerate(self.replica_kernels)
            ])
            sampled_replica_states = list(sampled_replica_states)
            sampled_replica_results = list(sampled_replica_results)

            states_are_lists = mcmc_util.is_list_like(
                sampled_replica_states[0])

            if not states_are_lists:
                sampled_replica_states = [[s] for s in sampled_replica_states]
            num_state_parts = len(sampled_replica_states[0])

            dtype = sampled_replica_states[0][0].dtype

            # Must put states into TensorArrays.  Why?  We will read/write states
            # dynamically with Tensor index `i`, and you cannot do this with lists.
            # old_states[k][i] is Tensor of (old) state part k, for replica i.
            # The `k` will be known statically, and `i` is a Tensor.
            old_states = [
                tf.TensorArray(
                    dtype,
                    size=self.num_replica,
                    dynamic_size=False,
                    clear_after_read=False,
                    tensor_array_name='old_states',
                    # State part k has same shape, regardless of replica.  So use 0.
                    element_shape=sampled_replica_states[0][k].shape)
                for k in range(num_state_parts)
            ]
            for k in range(num_state_parts):
                for i in range(self.num_replica):
                    old_states[k] = old_states[k].write(
                        i, sampled_replica_states[i][k])

            exchange_proposed = self.exchange_proposed_fn(
                self.num_replica, seed=self._seed_stream())
            exchange_proposed_n = tf.shape(exchange_proposed)[0]

            exchanged_states = self._get_exchanged_states(
                old_states, exchange_proposed, exchange_proposed_n,
                sampled_replica_states, sampled_replica_results)

            no_exchange_proposed, _ = tf.setdiff1d(
                tf.range(self.num_replica), tf.reshape(exchange_proposed,
                                                       [-1]))

            exchanged_states = self._insert_old_states_where_no_exchange_was_proposed(
                no_exchange_proposed, old_states, exchanged_states)

            next_replica_states = []
            for i in range(self.num_replica):
                next_replica_states_i = []
                for k in range(num_state_parts):
                    next_replica_states_i.append(exchanged_states[k].read(i))
                next_replica_states.append(next_replica_states_i)

            if not states_are_lists:
                next_replica_states = [s[0] for s in next_replica_states]
                sampled_replica_states = [s[0] for s in sampled_replica_states]

            # Now that states are/aren't exchanged, bootstrap next kernel_results.
            # The viewpoint is that after each exchange, we are starting anew.
            next_replica_results = [
                rk.bootstrap_results(state)
                for rk, state in zip(self.replica_kernels, next_replica_states)
            ]

            next_state = next_replica_states[
                0]  # Replica 0 is the returned state(s).

            kernel_results = ReplicaExchangeMCKernelResults(
                replica_states=next_replica_states,
                replica_results=next_replica_results,
                sampled_replica_states=sampled_replica_states,
                sampled_replica_results=sampled_replica_results,
            )

            return next_state, kernel_results
def covariance(x,
               y=None,
               sample_axis=0,
               event_axis=-1,
               keepdims=False,
               name=None):
    """Estimate covariance between members of `event_axis`.

  Sample covariance for scalars is defined as:

  ```none
  Cov[X, Y] := N^{-1} sum_{n=1}^N (X_n - Xbar) Conj{(Y_n - Ybar)}
  Xbar := N^{-1} sum_{n=1}^N X_n
  Ybar := N^{-1} sum_{n=1}^N Y_n
  ```

  For vectors `X = (X1, ..., XN)`, `Y = (Y1, ..., YN)`, one is often interested
  in the covariance matrix, `C_{ij} := Cov[Xi, Yj]`.

  ```python
  x = tf.random_normal(shape=(100, 2, 3))
  y = tf.random_normal(shape=(100, 2, 3))

  # cov[i, j] is the sample covariance between x[:, i, j] and y[:, i, j].
  cov = covariance(x, y, sample_axis=0, event_axis=None)

  # cov_matrix[i, m, n] is the sample covariance of x[:, i, m] and y[:, i, n]
  cov_matrix = covariance(x, y, sample_axis=0, event_axis=-1)
  ```

  Notice we divide by `N` (the numpy default), which does not create `NaN`
  when `N = 1`, but is slightly biased.

  Args:
    x:  A numeric `Tensor` holding samples.
    y:  Optional `Tensor` with same `dtype` and `shape` as `x`.
      Default value: `None` (`y` is effectively set to `x`).
    sample_axis: Scalar or vector `Tensor` designating axis holding samples, or
      `None` (meaning all axis hold samples).
      Default value: `0` (leftmost dimension).
    event_axis:  Scalar or vector `Tensor`, or `None`. Axis holding random
      events, whose covariance we are interested in. If a vector, entries must
      form a contiguous block of dims. `sample_axis` and `event_axis` should not
      intersect.
      Default value: `-1` (rightmost axis holds events).
    keepdims:  Boolean.  Whether to keep the sample axis as singletons.
    name: Python `str` name prefixed to Ops created by this function.
          Default value: `None` (i.e., `'covariance'`).

  Returns:
    cov: A `Tensor` of same `dtype` as the `x`, and rank equal to
      `rank(x) - len(sample_axis) + 2 * len(event_axis)`.

  Raises:
    AssertionError:  If `x` and `y` are found to have different shape.
    ValueError:  If `sample_axis` and `event_axis` are found to overlap.
    ValueError:  If `event_axis` is found to not be contiguous.
  """

    with tf.name_scope(name,
                       'covariance',
                       values=[x, y, event_axis, sample_axis]):
        x = tf.convert_to_tensor(x, name='x')
        # Covariance *only* uses the centered versions of x (and y).
        x -= tf.reduce_mean(x, axis=sample_axis, keepdims=True)

        if y is None:
            y = x
        else:
            y = tf.convert_to_tensor(y, name='y', dtype=x.dtype)
            # If x and y have different shape, sample_axis and event_axis will likely
            # be wrong for one of them!
            x.shape.assert_is_compatible_with(y.shape)
            y -= tf.reduce_mean(y, axis=sample_axis, keepdims=True)

        if event_axis is None:
            return tf.reduce_mean(x * tf.conj(y),
                                  axis=sample_axis,
                                  keepdims=keepdims)

        if sample_axis is None:
            raise ValueError(
                'sample_axis was None, which means all axis hold events, and this '
                'overlaps with event_axis ({})'.format(event_axis))

        event_axis = _make_positive_axis(event_axis, tf.rank(x))
        sample_axis = _make_positive_axis(sample_axis, tf.rank(x))

        # If we get lucky and axis is statically defined, we can do some checks.
        if _is_list_like(event_axis) and _is_list_like(sample_axis):
            if set(event_axis).intersection(sample_axis):
                raise ValueError(
                    'sample_axis ({}) and event_axis ({}) overlapped'.format(
                        sample_axis, event_axis))
            if (np.diff(sorted(event_axis)) > 1).any():
                raise ValueError(
                    'event_axis must be contiguous. Found: {}'.format(
                        event_axis))
            batch_axis = list(
                sorted(
                    set(range(x.shape.ndims)).difference(sample_axis +
                                                         event_axis)))
        else:
            batch_axis, _ = tf.setdiff1d(
                tf.range(0, tf.rank(x)), tf.concat((sample_axis, event_axis),
                                                   0))

        event_axis = tf.convert_to_tensor(event_axis, name='event_axis')
        sample_axis = tf.convert_to_tensor(sample_axis, name='sample_axis')
        batch_axis = tf.convert_to_tensor(batch_axis, name='batch_axis')

        # Permute x/y until shape = B + E + S
        perm_for_xy = tf.concat((batch_axis, event_axis, sample_axis), 0)
        x_permed = tf.transpose(x, perm=perm_for_xy)
        y_permed = tf.transpose(y, perm=perm_for_xy)

        batch_ndims = tf.size(batch_axis)
        batch_shape = tf.shape(x_permed)[:batch_ndims]
        event_ndims = tf.size(event_axis)
        event_shape = tf.shape(x_permed)[batch_ndims:batch_ndims + event_ndims]
        sample_shape = tf.shape(x_permed)[batch_ndims + event_ndims:]
        sample_ndims = tf.size(sample_shape)
        n_samples = tf.reduce_prod(sample_shape)
        n_events = tf.reduce_prod(event_shape)

        # Flatten sample_axis into one long dim.
        x_permed_flat = tf.reshape(
            x_permed, tf.concat((batch_shape, event_shape, [n_samples]), 0))
        y_permed_flat = tf.reshape(
            y_permed, tf.concat((batch_shape, event_shape, [n_samples]), 0))
        # Do the same for event_axis.
        x_permed_flat = tf.reshape(
            x_permed, tf.concat((batch_shape, [n_events], [n_samples]), 0))
        y_permed_flat = tf.reshape(
            y_permed, tf.concat((batch_shape, [n_events], [n_samples]), 0))

        # After matmul, cov.shape = batch_shape + [n_events, n_events]
        cov = tf.matmul(x_permed_flat, y_permed_flat,
                        adjoint_b=True) / tf.cast(n_samples, x.dtype)

        # Insert some singletons to make
        # cov.shape = batch_shape + event_shape**2 + [1,...,1]
        # This is just like x_permed.shape, except the sample_axis is all 1's, and
        # the [n_events] became event_shape**2.
        cov = tf.reshape(
            cov,
            tf.concat(
                (
                    batch_shape,
                    # event_shape**2 used here because it is the same length as
                    # event_shape, and has the same number of elements as one
                    # batch of covariance.
                    event_shape**2,
                    tf.ones([sample_ndims], tf.int32)),
                0))
        # Permuting by the argsort inverts the permutation, making
        # cov.shape have ones in the position where there were samples, and
        # [n_events * n_events] in the event position.
        cov = tf.transpose(cov, perm=tf.invert_permutation(perm_for_xy))

        # Now expand event_shape**2 into event_shape + event_shape.
        # We here use (for the first time) the fact that we require event_axis to be
        # contiguous.
        e_start = event_axis[0]
        e_len = 1 + event_axis[-1] - event_axis[0]
        cov = tf.reshape(
            cov,
            tf.concat((tf.shape(cov)[:e_start], event_shape, event_shape,
                       tf.shape(cov)[e_start + e_len:]), 0))

        # tf.squeeze requires python ints for axis, not Tensor.  This is enough to
        # require our axis args to be constants.
        if not keepdims:
            squeeze_axis = tf.where(sample_axis < e_start, sample_axis,
                                    sample_axis + e_len)
            cov = _squeeze(cov, axis=squeeze_axis)

        return cov
Exemple #26
0
  def get_predictions_and_loss(self, tokens, context_word_emb, head_word_emb, lm_emb, char_index, text_len, speaker_ids, genre, is_training, gold_starts, gold_ends, cluster_ids, scene_emb, genders, fpronouns):
    self.dropout = self.get_dropout(self.config["dropout_rate"], is_training)
    self.lexical_dropout = self.get_dropout(self.config["lexical_dropout_rate"], is_training)
    self.lstm_dropout = self.get_dropout(self.config["lstm_dropout_rate"], is_training)

    num_sentences = tf.shape(context_word_emb)[0]
    max_sentence_length = tf.shape(context_word_emb)[1]

    context_emb_list = [context_word_emb]
    head_emb_list = [head_word_emb]

    if self.config["char_embedding_size"] > 0:
      char_emb = tf.gather(tf.get_variable("char_embeddings", [len(self.char_dict), self.config["char_embedding_size"]]), char_index) # [num_sentences, max_sentence_length, max_word_length, emb]
      flattened_char_emb = tf.reshape(char_emb, [num_sentences * max_sentence_length, util.shape(char_emb, 2), util.shape(char_emb, 3)]) # [num_sentences * max_sentence_length, max_word_length, emb]
      flattened_aggregated_char_emb = util.cnn(flattened_char_emb, self.config["filter_widths"], self.config["filter_size"]) # [num_sentences * max_sentence_length, emb]
      aggregated_char_emb = tf.reshape(flattened_aggregated_char_emb, [num_sentences, max_sentence_length, util.shape(flattened_aggregated_char_emb, 1)]) # [num_sentences, max_sentence_length, emb]
      context_emb_list.append(aggregated_char_emb)
      head_emb_list.append(aggregated_char_emb)

    if not self.lm_file:
      elmo_module = hub.Module("https://tfhub.dev/google/elmo/2")
      lm_embeddings = elmo_module(
          inputs={"tokens": tokens, "sequence_len": text_len},
          signature="tokens", as_dict=True)
      word_emb = lm_embeddings["word_emb"]  # [num_sentences, max_sentence_length, 512]
      lm_emb = tf.stack([tf.concat([word_emb, word_emb], -1),
                         lm_embeddings["lstm_outputs1"],
                         lm_embeddings["lstm_outputs2"]], -1)  # [num_sentences, max_sentence_length, 1024, 3]
    lm_emb_size = util.shape(lm_emb, 2)
    lm_num_layers = util.shape(lm_emb, 3)
    with tf.variable_scope("lm_aggregation"):
      self.lm_weights = tf.nn.softmax(tf.get_variable("lm_scores", [lm_num_layers], initializer=tf.constant_initializer(0.0)))
      self.lm_scaling = tf.get_variable("lm_scaling", [], initializer=tf.constant_initializer(1.0))
    flattened_lm_emb = tf.reshape(lm_emb, [num_sentences * max_sentence_length * lm_emb_size, lm_num_layers])
    flattened_aggregated_lm_emb = tf.matmul(flattened_lm_emb, tf.expand_dims(self.lm_weights, 1)) # [num_sentences * max_sentence_length * emb, 1]
    aggregated_lm_emb = tf.reshape(flattened_aggregated_lm_emb, [num_sentences, max_sentence_length, lm_emb_size])
    aggregated_lm_emb *= self.lm_scaling
    context_emb_list.append(aggregated_lm_emb)

    context_emb = tf.concat(context_emb_list, 2) # [num_sentences, max_sentence_length, emb]
    head_emb = tf.concat(head_emb_list, 2) # [num_sentences, max_sentence_length, emb]
    context_emb = tf.nn.dropout(context_emb, self.lexical_dropout) # [num_sentences, max_sentence_length, emb]
    head_emb = tf.nn.dropout(head_emb, self.lexical_dropout) # [num_sentences, max_sentence_length, emb]

    text_len_mask = tf.sequence_mask(text_len, maxlen=max_sentence_length) # [num_sentence, max_sentence_length]

    context_outputs = self.lstm_contextualize(context_emb, text_len, text_len_mask) # [num_words, emb]
    num_words = util.shape(context_outputs, 0)

    genre_emb = tf.gather(tf.get_variable("genre_embeddings", [len(self.genres), self.config["feature_size"]]), genre) # [emb]

    sentence_indices = tf.tile(tf.expand_dims(tf.range(num_sentences), 1), [1, max_sentence_length]) # [num_sentences, max_sentence_length]
    flattened_sentence_indices = self.flatten_emb_by_sentence(sentence_indices, text_len_mask) # [num_words]
    flattened_head_emb = self.flatten_emb_by_sentence(head_emb, text_len_mask) # [num_words]

    candidate_starts = tf.tile(tf.expand_dims(tf.range(num_words), 1), [1, self.max_span_width]) # [num_words, max_span_width]
    candidate_ends = candidate_starts + tf.expand_dims(tf.range(self.max_span_width), 0) # [num_words, max_span_width]
    
    #debug
    prev_can_st = candidate_starts
    prev_can_ends = candidate_ends
    #debug

    candidate_start_sentence_indices = tf.gather(flattened_sentence_indices, candidate_starts) # [num_words, max_span_width]
    candidate_end_sentence_indices = tf.gather(flattened_sentence_indices, tf.minimum(candidate_ends, num_words - 1)) # [num_words, max_span_width]
    candidate_mask = tf.logical_and(candidate_ends < num_words, tf.equal(candidate_start_sentence_indices, candidate_end_sentence_indices)) # [num_words, max_span_width]
    flattened_candidate_mask = tf.reshape(candidate_mask, [-1]) # [num_words * max_span_width]
    candidate_starts = tf.boolean_mask(tf.reshape(candidate_starts, [-1]), flattened_candidate_mask) # [num_candidates]
    candidate_ends = tf.boolean_mask(tf.reshape(candidate_ends, [-1]), flattened_candidate_mask) # [num_candidates]

    combined_candidate_st = candidate_starts*10000 + candidate_ends
    combined_gold_st = gold_starts*10000 + gold_ends

    _, non_top_span_list = tf.setdiff1d(combined_candidate_st, combined_gold_st) #[num_candidate - num_gold_mentions]
    whole_candidate_indices_list = tf.range(util.shape(candidate_starts,0)) # [num_candidates]
    gold_span_indices, _ = tf.setdiff1d(whole_candidate_indices_list, non_top_span_list) #[num_gold_mentions]


    candidate_sentence_indices = tf.boolean_mask(tf.reshape(candidate_start_sentence_indices, [-1]), flattened_candidate_mask) # [num_candidates]

    candidate_cluster_ids = self.get_candidate_labels(candidate_starts, candidate_ends, gold_starts, gold_ends, cluster_ids) # [num_candidates]

    candidate_span_emb = self.get_span_emb(flattened_head_emb, context_outputs, candidate_starts, candidate_ends) # [num_candidates, emb]


    #Video Scene Emb
    ffnn_scene_emb = util.ffnn(scene_emb, num_hidden_layers=self.config["ffnn_depth"], hidden_size=400, output_size=128, dropout=self.dropout) # [num_words, 100]
    candidate_scene_emb = self.get_scene_emb(ffnn_scene_emb, candidate_starts) #[num_candidates, 100]

    '''
    #Comment : This part is for calculating mention scores and prnunign metnion
    #It is not used for this task, because mention boundary are given.

    candidate_mention_scores =  self.get_mention_scores(candidate_span_emb) # [k, 1]
    candidate_mention_scores = tf.squeeze(candidate_mention_scores, 1) # [k]

    k = tf.to_int32(tf.floor(tf.to_float(tf.shape(context_outputs)[0]) * self.config["top_span_ratio"]))
    top_span_indices = coref_ops.extract_spans(tf.expand_dims(candidate_mention_scores, 0),
                                               tf.expand_dims(candidate_starts, 0),
                                               tf.expand_dims(candidate_ends, 0),
                                               tf.expand_dims(k, 0),
                                               util.shape(context_outputs, 0),
                                               True) # [1, k]
    top_span_indices.set_shape([1, None])
    top_span_indices = tf.squeeze(top_span_indices, 0) # [k]
    '''

    ######## Only Using Gold Span Indices #####
    k = tf.to_int32(util.shape(gold_span_indices,0))
    top_span_indices = gold_span_indices
    ############

    top_span_starts = tf.gather(candidate_starts, top_span_indices) # [k]
    top_span_ends = tf.gather(candidate_ends, top_span_indices) # [k]
    top_span_emb = tf.gather(candidate_span_emb, top_span_indices) # [k, emb]
    top_scene_emb = tf.gather(candidate_scene_emb, top_span_indices) # [k, emb-scene]

    top_span_cluster_ids = tf.gather(candidate_cluster_ids, top_span_indices) # [k]
    #top_span_mention_scores = tf.gather(candidate_mention_scores, top_span_indices) # [k]
    top_span_sentence_indices = tf.gather(candidate_sentence_indices, top_span_indices) # [k]
    top_span_speaker_ids = tf.gather(speaker_ids, top_span_starts) # [k]
    top_span_genders = tf.gather(genders, top_span_ends)
    top_span_fpronouns = tf.gather(fpronouns, top_span_ends)

    # k : total number of candidates span (M in paper)
    # c : how many antecedents we check (K in paper)
    c = tf.minimum(self.config["max_top_antecedents"], k)

    if self.config["coarse_to_fine"]:
      top_antecedents, top_antecedents_mask, top_fast_antecedent_scores, top_antecedent_offsets = self.coarse_to_fine_pruning(top_span_emb, top_span_mention_scores, c)
    else:
      #top_antecedents, top_antecedents_mask, top_fast_antecedent_scores, top_antecedent_offsets = self.distance_pruning(top_span_emb, top_span_mention_scores, c)
      top_antecedents, top_antecedents_mask, top_fast_antecedent_scores, top_antecedent_offsets = self.distance_prnuing_wo_mention_score(top_span_emb, c)

    dummy_scores = tf.zeros([k, 1]) # [k, 1]
    for i in range(self.config["coref_depth"]):
      with tf.variable_scope("coref_layer", reuse=(i > 0)):
        top_antecedent_emb = tf.gather(top_span_emb, top_antecedents) # [k, c, emb]
        top_antecedent_scene_emb = tf.gather(top_scene_emb, top_antecedents) # [k, c, emb-scene]
        top_antecedent_scores = top_fast_antecedent_scores + self.get_slow_antecedent_scores(top_span_emb, top_antecedents, top_antecedent_emb, top_antecedent_offsets, top_span_speaker_ids, genre_emb, top_scene_emb, top_antecedent_scene_emb, top_span_genders, top_span_fpronouns) # [k, c]
        top_antecedent_weights = tf.nn.softmax(tf.concat([dummy_scores, top_antecedent_scores], 1)) # [k, c + 1]
        top_antecedent_emb = tf.concat([tf.expand_dims(top_span_emb, 1), top_antecedent_emb], 1) # [k, c + 1, emb]
        attended_span_emb = tf.reduce_sum(tf.expand_dims(top_antecedent_weights, 2) * top_antecedent_emb, 1) # [k, emb]
        with tf.variable_scope("f"):
          f = tf.sigmoid(util.projection(tf.concat([top_span_emb, attended_span_emb], 1), util.shape(top_span_emb, -1))) # [k, emb]
          top_span_emb = f * attended_span_emb + (1 - f) * top_span_emb # [k, emb]

    top_antecedent_scores = tf.concat([dummy_scores, top_antecedent_scores], 1) # [k, c + 1]

    top_antecedent_cluster_ids = tf.gather(top_span_cluster_ids, top_antecedents) # [k, c]
    top_antecedent_cluster_ids += tf.to_int32(tf.log(tf.to_float(top_antecedents_mask))) # [k, c]
    same_cluster_indicator = tf.equal(top_antecedent_cluster_ids, tf.expand_dims(top_span_cluster_ids, 1)) # [k, c]
    non_dummy_indicator = tf.expand_dims(top_span_cluster_ids > 0, 1) # [k, 1]
    pairwise_labels = tf.logical_and(same_cluster_indicator, non_dummy_indicator) # [k, c]집단사기범
    dummy_labels = tf.logical_not(tf.reduce_any(pairwise_labels, 1, keepdims=True)) # [k, 1]
    top_antecedent_labels = tf.concat([dummy_labels, pairwise_labels], 1) # [k, c + 1]

    top_antecedent_prob = tf.nn.softmax(top_antecedent_scores, 1) # [k, c + 1]
    if (self.config["use_gender_logic_rule"]):
      top_antecedent_prob_with_logic = self.project_logic_rule(top_antecedent_prob, top_span_genders, top_span_fpronouns, top_span_speaker_ids, top_antecedents, k)
      '''
      marginal_prob = tf.reduce_sum(top_antecedent_prob*tf.to_float(top_antecedent_labels),axis=1)
      gold_loss = -1 * tf.reduce_sum(tf.log(marginal_prob))
      top_antecedent_scores = top_antecedent_prob      
      '''
      origin_loss = self.softmax_loss(top_antecedent_scores, top_antecedent_labels) # [k]
      origin_loss = tf.reduce_sum(origin_loss)

      # cross_entropy : -1 * ground_truth * log(prediction)
      #teacher_loss = tf.reduce_min(tf.nn. (labels=top_antecedent_prob_with_logic, logits=top_antecedent_scores))
      teacher_loss = tf.reduce_sum(-tf.reduce_sum(top_antecedent_prob_with_logic * tf.log(top_antecedent_prob + 1e-10), reduction_indices=[1]))

      pi = tf.minimum(self.config["logic_rule_pi_zero"], 1.0 - tf.pow(self.config["logic_rule_imitation_alpha"], tf.to_float(self.global_step)+1.0)) 

      # For Validation Loss
      marginal_prob = tf.reduce_sum(top_antecedent_prob_with_logic*tf.to_float(top_antecedent_labels),axis=1)
      validation_loss = -1 * tf.reduce_sum(tf.log(marginal_prob))
      
      #loss = teacher_loss + origin_loss
      loss = tf.where(is_training, pi*teacher_loss + (1.0-pi)*origin_loss, validation_loss)

      top_antecedent_scores = top_antecedent_prob_with_logic
    else:
      loss = self.softmax_loss(top_antecedent_scores, top_antecedent_labels) # [k]
      loss = tf.reduce_sum(loss) # []
      teacher_loss = loss
      origin_loss = loss

    return [candidate_starts, candidate_ends, top_span_starts, top_span_ends, top_antecedents, top_antecedent_scores, teacher_loss, origin_loss], loss
import tensorflow as tf
import numpy as np

input_a = np.array([1, 0, 2, 5])
input_b = np.array([2, 3, 4, 5])
input_c = np.array([[True, False], [True, True]])
input_d = np.array([1, 1, 2, 2, 3, 5, 6])
input_e = np.array([0, 2, 3, 1, 4])

a_argmin = tf.argmin(input_a)
a_argmax = tf.argmax(input_a)
a_listdiff = tf.setdiff1d(input_a, input_b)
c_where = tf.where(input_c)
d_unique = tf.unique(input_d)
e_invert_permutation = tf.invert_permutation(input_e)

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(a_argmin), '\n', sess.run(a_argmax), '\n', sess.run(a_listdiff), '\n', sess.run(c_where))
    print(sess.run(d_unique), '\n', sess.run(e_invert_permutation))
Exemple #28
0
    def _tensordot_reshape(
        a: tf.Tensor,
        axes: Union[Sequence[int], tf.Tensor],
        is_right_term=False
    ) -> Tuple[tf.Tensor, Union[List[int], tf.Tensor], Optional[List[int]],
               bool]:
        """Helper method to perform transpose and reshape for contraction op.
    This method is helpful in reducing `math_ops.tensordot` to `math_ops.matmul`
    using `array_ops.transpose` and `array_ops.reshape`. The method takes a
    tensor and performs the correct transpose and reshape operation for a given
    set of indices. It returns the reshaped tensor as well as a list of indices
    necessary to reshape the tensor again after matrix multiplication.
    Args:
      a: `Tensor`.
      axes: List or `int32` `Tensor` of unique indices specifying valid axes of
       `a`.
      is_right_term: Whether `a` is the right (second) argument to `matmul`.
    Returns:
      A tuple `(reshaped_a, free_dims, free_dims_static, transpose_needed)`
      where `reshaped_a` is the tensor `a` reshaped to allow contraction via
      `matmul`, `free_dims` is either a list of integers or an `int32`
      `Tensor`, depending on whether the shape of a is fully specified, and
      free_dims_static is either a list of integers and None values, or None,
      representing the inferred static shape of the free dimensions. 
      `transpose_needed` indicates whether `reshaped_a` must be transposed,
      or not, when calling `matmul`.
    """
        if a.get_shape().is_fully_defined() and isinstance(
                axes, (list, tuple)):
            shape_a = a.get_shape().as_list()
            # NOTE: This will fail if axes contains any tensors
            axes = [i if i >= 0 else i + len(shape_a) for i in axes]
            free = [i for i in range(len(shape_a)) if i not in axes]
            flipped = _tensordot_should_flip(axes, free)

            free_dims = [shape_a[i] for i in free]
            prod_free = int(np.prod([shape_a[i] for i in free]))
            prod_axes = int(np.prod([shape_a[i] for i in axes]))
            perm = axes + free if flipped else free + axes
            new_shape = [prod_axes, prod_free
                         ] if flipped else [prod_free, prod_axes]
            transposed_a = _tranpose_if_necessary(a, perm)
            reshaped_a = _reshape_if_necessary(transposed_a, new_shape)
            transpose_needed = (not flipped) if is_right_term else flipped
            return reshaped_a, free_dims, free_dims, transpose_needed
        if a.get_shape().ndims is not None and isinstance(axes, (list, tuple)):
            shape_a = a.get_shape().as_list()
            axes = [i if i >= 0 else i + len(shape_a) for i in axes]
            free = [i for i in range(len(shape_a)) if i not in axes]
            flipped = _tensordot_should_flip(axes, free)
            perm = axes + free if flipped else free + axes

            axes_dims = [shape_a[i] for i in axes]
            free_dims = [shape_a[i] for i in free]
            free_dims_static = free_dims
            axes = tf.convert_to_tensor(axes,
                                        dtype=tf.dtypes.int32,
                                        name="axes")
            free = tf.convert_to_tensor(free,
                                        dtype=tf.dtypes.int32,
                                        name="free")
            shape_a = tf.shape(a)
            transposed_a = _tranpose_if_necessary(a, perm)
        else:
            free_dims_static = None
            shape_a = tf.shape(a)
            rank_a = tf.rank(a)
            axes = tf.convert_to_tensor(axes,
                                        dtype=tf.dtypes.int32,
                                        name="axes")
            axes = tf.where(axes >= 0, axes, axes + rank_a)
            free, _ = tf.setdiff1d(tf.range(rank_a), axes)
            # Matmul does not accept tensors for its transpose arguments, so fall
            # back to the previous, fixed behavior.
            # NOTE(amilsted): With a suitable wrapper for `matmul` using e.g. `case`
            #   to match transpose arguments to tensor values, we could also avoid
            #   unneeded tranposes in this case at the expense of a somewhat more
            #   complicated graph. Unclear whether this would be beneficial overall.
            flipped = is_right_term
            perm = (tf.concat([axes, free], 0)
                    if flipped else tf.concat([free, axes], 0))
            transposed_a = tf.transpose(a, perm)

        free_dims = tf.gather(shape_a, free)
        axes_dims = tf.gather(shape_a, axes)
        prod_free_dims = tf.reduce_prod(free_dims)
        prod_axes_dims = tf.reduce_prod(axes_dims)

        if flipped:
            new_shape = tf.stack([prod_axes_dims, prod_free_dims])
        else:
            new_shape = tf.stack([prod_free_dims, prod_axes_dims])
        reshaped_a = tf.reshape(transposed_a, new_shape)
        transpose_needed = (not flipped) if is_right_term else flipped
        return reshaped_a, free_dims, free_dims_static, transpose_needed
def covariance(x,
               y=None,
               sample_axis=0,
               event_axis=-1,
               keepdims=False,
               name=None):
  """Sample covariance between observations indexed by `event_axis`.

  Given `N` samples of scalar random variables `X` and `Y`, covariance may be
  estimated as

  ```none
  Cov[X, Y] := N^{-1} sum_{n=1}^N (X_n - Xbar) Conj{(Y_n - Ybar)}
  Xbar := N^{-1} sum_{n=1}^N X_n
  Ybar := N^{-1} sum_{n=1}^N Y_n
  ```

  For vector-variate random variables `X = (X1, ..., Xd)`, `Y = (Y1, ..., Yd)`,
  one is often interested in the covariance matrix, `C_{ij} := Cov[Xi, Yj]`.

  ```python
  x = tf.random_normal(shape=(100, 2, 3))
  y = tf.random_normal(shape=(100, 2, 3))

  # cov[i, j] is the sample covariance between x[:, i, j] and y[:, i, j].
  cov = tfp.stats.covariance(x, y, sample_axis=0, event_axis=None)

  # cov_matrix[i, m, n] is the sample covariance of x[:, i, m] and y[:, i, n]
  cov_matrix = tfp.stats.covariance(x, y, sample_axis=0, event_axis=-1)
  ```

  Notice we divide by `N` (the numpy default), which does not create `NaN`
  when `N = 1`, but is slightly biased.

  Args:
    x:  A numeric `Tensor` holding samples.
    y:  Optional `Tensor` with same `dtype` and `shape` as `x`.
      Default value: `None` (`y` is effectively set to `x`).
    sample_axis: Scalar or vector `Tensor` designating axis holding samples, or
      `None` (meaning all axis hold samples).
      Default value: `0` (leftmost dimension).
    event_axis:  Scalar or vector `Tensor`, or `None` (scalar events).
      Axis indexing random events, whose covariance we are interested in.
      If a vector, entries must form a contiguous block of dims. `sample_axis`
      and `event_axis` should not intersect.
      Default value: `-1` (rightmost axis holds events).
    keepdims:  Boolean.  Whether to keep the sample axis as singletons.
    name: Python `str` name prefixed to Ops created by this function.
          Default value: `None` (i.e., `'covariance'`).

  Returns:
    cov: A `Tensor` of same `dtype` as the `x`, and rank equal to
      `rank(x) - len(sample_axis) + 2 * len(event_axis)`.

  Raises:
    AssertionError:  If `x` and `y` are found to have different shape.
    ValueError:  If `sample_axis` and `event_axis` are found to overlap.
    ValueError:  If `event_axis` is found to not be contiguous.
  """

  with tf.name_scope(
      name, 'covariance', values=[x, y, event_axis, sample_axis]):
    x = tf.convert_to_tensor(x, name='x')
    # Covariance *only* uses the centered versions of x (and y).
    x -= tf.reduce_mean(x, axis=sample_axis, keepdims=True)

    if y is None:
      y = x
    else:
      y = tf.convert_to_tensor(y, name='y', dtype=x.dtype)
      # If x and y have different shape, sample_axis and event_axis will likely
      # be wrong for one of them!
      x.shape.assert_is_compatible_with(y.shape)
      y -= tf.reduce_mean(y, axis=sample_axis, keepdims=True)

    if event_axis is None:
      return tf.reduce_mean(x * tf.conj(y), axis=sample_axis, keepdims=keepdims)

    if sample_axis is None:
      raise ValueError(
          'sample_axis was None, which means all axis hold events, and this '
          'overlaps with event_axis ({})'.format(event_axis))

    event_axis = _make_positive_axis(event_axis, tf.rank(x))
    sample_axis = _make_positive_axis(sample_axis, tf.rank(x))

    # If we get lucky and axis is statically defined, we can do some checks.
    if _is_list_like(event_axis) and _is_list_like(sample_axis):
      if set(event_axis).intersection(sample_axis):
        raise ValueError(
            'sample_axis ({}) and event_axis ({}) overlapped'.format(
                sample_axis, event_axis))
      if (np.diff(sorted(event_axis)) > 1).any():
        raise ValueError(
            'event_axis must be contiguous. Found: {}'.format(event_axis))
      batch_axis = list(
          sorted(
              set(range(x.shape.ndims)).difference(sample_axis + event_axis)))
    else:
      batch_axis, _ = tf.setdiff1d(
          tf.range(0, tf.rank(x)), tf.concat((sample_axis, event_axis), 0))

    event_axis = tf.convert_to_tensor(
        event_axis, name='event_axis', dtype=tf.int32)
    sample_axis = tf.convert_to_tensor(
        sample_axis, name='sample_axis', dtype=tf.int32)
    batch_axis = tf.convert_to_tensor(
        batch_axis, name='batch_axis', dtype=tf.int32)

    # Permute x/y until shape = B + E + S
    perm_for_xy = tf.concat((batch_axis, event_axis, sample_axis), 0)
    x_permed = tf.transpose(x, perm=perm_for_xy)
    y_permed = tf.transpose(y, perm=perm_for_xy)

    batch_ndims = tf.size(batch_axis)
    batch_shape = tf.shape(x_permed)[:batch_ndims]
    event_ndims = tf.size(event_axis)
    event_shape = tf.shape(x_permed)[batch_ndims:batch_ndims + event_ndims]
    sample_shape = tf.shape(x_permed)[batch_ndims + event_ndims:]
    sample_ndims = tf.size(sample_shape)
    n_samples = tf.reduce_prod(sample_shape)
    n_events = tf.reduce_prod(event_shape)

    # Flatten sample_axis into one long dim.
    x_permed_flat = tf.reshape(
        x_permed, tf.concat((batch_shape, event_shape, [n_samples]), 0))
    y_permed_flat = tf.reshape(
        y_permed, tf.concat((batch_shape, event_shape, [n_samples]), 0))
    # Do the same for event_axis.
    x_permed_flat = tf.reshape(
        x_permed, tf.concat((batch_shape, [n_events], [n_samples]), 0))
    y_permed_flat = tf.reshape(
        y_permed, tf.concat((batch_shape, [n_events], [n_samples]), 0))

    # After matmul, cov.shape = batch_shape + [n_events, n_events]
    cov = tf.matmul(
        x_permed_flat, y_permed_flat, adjoint_b=True) / tf.cast(
            n_samples, x.dtype)

    # Insert some singletons to make
    # cov.shape = batch_shape + event_shape**2 + [1,...,1]
    # This is just like x_permed.shape, except the sample_axis is all 1's, and
    # the [n_events] became event_shape**2.
    cov = tf.reshape(
        cov,
        tf.concat(
            (
                batch_shape,
                # event_shape**2 used here because it is the same length as
                # event_shape, and has the same number of elements as one
                # batch of covariance.
                event_shape**2,
                tf.ones([sample_ndims], tf.int32)),
            0))
    # Permuting by the argsort inverts the permutation, making
    # cov.shape have ones in the position where there were samples, and
    # [n_events * n_events] in the event position.
    cov = tf.transpose(cov, perm=tf.invert_permutation(perm_for_xy))

    # Now expand event_shape**2 into event_shape + event_shape.
    # We here use (for the first time) the fact that we require event_axis to be
    # contiguous.
    e_start = event_axis[0]
    e_len = 1 + event_axis[-1] - event_axis[0]
    cov = tf.reshape(
        cov,
        tf.concat((tf.shape(cov)[:e_start], event_shape, event_shape,
                   tf.shape(cov)[e_start + e_len:]), 0))

    # tf.squeeze requires python ints for axis, not Tensor.  This is enough to
    # require our axis args to be constants.
    if not keepdims:
      squeeze_axis = tf.where(sample_axis < e_start, sample_axis,
                              sample_axis + e_len)
      cov = _squeeze(cov, axis=squeeze_axis)

    return cov
Exemple #30
0
 def range_not_j(j, m):
     return tf.setdiff1d(tf.range(m), [j])
Exemple #31
0
import tensorflow as tf 
sess = tf.InteractiveSession()
x = tf.constant([[2, 5, 3, -5], 
                 [0, 3,-2,  5], 
                 [4, 3, 5,  3], 
                 [6, 1, 4,  0]]) 
listx = tf.constant([1,2,3,4,5,6,7,8])
listy = tf.constant([4,5,8,9])

boolx = tf.constant([[True,False], [False,True]])

tf.argmin(x, 1).eval() # Position of the maximum value of columns
tf.argmax(x, 1).eval() # Position of the minimum value of rows
tf.setdiff1d(listx, listy)[0].eval() # List differences
tf.where(boolx).eval() # Show true values
tf.unique(listx)[0].eval() # Unique values in list
    def compute_target_actions_values(self):
        next_targets = self.forward_rewards[
            0] + self.gamma * self.compute_slow_target_action_values(
                0, self.choose_best_actions(0))

        violations_penalties = tf.identity(next_targets)

        forward_targets = np.zeros(self.transition_length)
        backward_targets = np.zeros(self.transition_length)
        for i in np.arange(1, self.transition_length):
            forward_targets[i] = self.forward_rewards[i] + (
                self.gamma**i) * self.compute_slow_target_action_values(
                    i, self.choose_best_actions(i))

        for i in np.arange(0, self.transition_length):
            backward_targets[i] = self.backward_rewards[i] + (
                self.gamma**i) * self.compute_slow_target_action_values(
                    i, self.choose_best_actions(i))

        max_q_value_indices = tf.argmax(backward_targets, axis=1)
        min_q_value_indices = tf.argmax(forward_targets, axis=1)

        flattened_forward_targets = tf.reshape(forward_targets, [-1])
        flattened_backward_targets = tf.reshape(backward_targets, [-1])

        flattened_backward_ind_max = max_q_value_indices + tf.cast(
            tf.range(tf.shape(backward_targets)[0]) *
            tf.shape(backward_targets)[1], tf.int64)
        flattened_forward_ind_max = min_q_value_indices + tf.cast(
            tf.range(tf.shape(forward_targets)[0]) *
            tf.shape(forward_targets)[1], tf.int64)

        min_q_values = tf.gather(flattened_forward_targets,
                                 flattened_forward_ind_max)
        max_q_values = tf.gather(flattened_backward_targets,
                                 flattened_backward_ind_max)

        upper_violation = tf.where(tf.greater(max_q_values - 20, next_targets))
        lower_violation = tf.where(tf.greater(next_targets, min_q_values + 20))

        upper_violation_range = tf.range(tf.shape(upper_violation)[0])
        lower_violation_range = tf.range(tf.shape(lower_violation)[0])

        flattened_upper_violation = tf.gather_nd(
            upper_violation,
            tf.stack((upper_violation_range,
                      tf.fill([tf.shape(upper_violation)[0]], 0)),
                     axis=1))
        flattened_lower_violation = tf.gather_nd(
            lower_violation,
            tf.stack((lower_violation_range,
                      tf.fill([tf.shape(lower_violation)[0]], 0)),
                     axis=1))

        diff = tf.setdiff1d(flattened_upper_violation,
                            flattened_lower_violation)
        diff_2 = tf.setdiff1d(flattened_lower_violation,
                              flattened_upper_violation)
        diff_3 = tf.setdiff1d(flattened_upper_violation, diff[0])

        only_upper_bound_violation_indices = tf.stack(
            (diff[0], tf.fill(tf.shape(diff[0]), tf.constant(0,
                                                             dtype=tf.int64))),
            axis=1)
        only_lower_bound_violation_indices = tf.stack(
            (diff_2[0],
             tf.fill(tf.shape(diff_2[0]), tf.constant(0, dtype=tf.int64))),
            axis=1)
        both_bounds_violations_indices = tf.stack(
            (diff_3[0],
             tf.fill(tf.shape(diff_3[0]), tf.constant(0, dtype=tf.int64))),
            axis=1)

        violations_penalties += tf.scatter_nd(
            only_upper_bound_violation_indices,
            tf.gather(max_q_values, only_upper_bound_violation_indices),
            [self.minibatch_size, 1])
        violations_penalties += tf.scatter_nd(
            only_lower_bound_violation_indices,
            tf.gather(min_q_values, only_lower_bound_violation_indices),
            [self.minibatch_size, 1])
        violations_penalties += tf.scatter_nd(
            both_bounds_violations_indices,
            0.5 * tf.gather(min_q_values, both_bounds_violations_indices) +
            tf.gather(max_q_values, both_bounds_violations_indices),
            [self.minibatch_size, 1])

        final_targets = 0.8 * next_targets + 0.2 * violations_penalties

        return final_targets
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tensorflow as tf

sess = tf.InteractiveSession()
x = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [4, 3, 5, 3], [6, 1, 4, 0]])
listx = tf.constant([1, 2, 3, 4, 5, 6, 7, 8])
listy = tf.constant([4, 5, 8, 9])

print("\nx=\n", x.eval())
print("\nlistx=", listx.eval())
print("\nlisty=", listy.eval())

boolx = tf.constant([[True, False], [False, True]])

print("\ntf.argmin(x, 1).eval() ")  # Position of the min value of columns
print(tf.argmin(x, 1).eval())  # Position of the min value of columns

print("\ntf.argmax(x, 1).eval() ")  # Position of the max value of rows
print(tf.argmax(x, 1).eval())  # Position of the max value of rows

print("\ntf.setdiff1d(listx, listy)[0].eval() ")  # List differences
print(tf.setdiff1d(listx, listy)[0].eval())  # List differences

print(tf.where(boolx).eval())  # Show true values

print(tf.unique(listx)[0].eval())  # Unique values in list
def detect_targets_graph(gt_boxes, gt_class_ids, proposals,
                         train_rois_per_image, roi_positive_ratio):
    """
    每个图像生成检测网络的分类和回归目标
    IoU>=0.5的为正样本;IoU<0.5的为负样本
    :param gt_boxes: GT 边框坐标 [MAX_GT_BOXs, (y1,x1,y2,x2,tag)] ,tag=0 为padding
    :param gt_class_ids: GT 类别 [MAX_GT_BOXs, 1+1] ;最后一位为tag, tag=0 为padding
    :param proposals: [N,(y1,x1,y2,x2,tag)] ,tag=0 为padding
    :param train_rois_per_image: 每张图像训练的proposal数量
    :param roi_positive_ratio: proposal正负样本比
    :return:
    """
    # 去除padding
    gt_boxes = tf_utils.remove_pad(gt_boxes)
    gt_class_ids = tf_utils.remove_pad(gt_class_ids)[:, 0]  # 从[N,1]变为[N]
    proposals = tf_utils.remove_pad(proposals)
    # 计算iou
    iou = compute_iou(gt_boxes, proposals)

    # 每个GT边框IoU最大的proposal为正
    # gt_iou_argmax = tf.argmax(iou, axis=1)
    gt_iou_argmax = tf.cond(  # 需要考虑proposal个数为0的情况
        tf.greater(tf.shape(proposals)[0], 0),
        true_fn=lambda: tf.argmax(iou, axis=1),
        false_fn=lambda: tf.cast(tf.constant([]), tf.int64))

    # GT和对应的proposal
    # gt_boxes_pos_1 = tf.identity(gt_boxes)
    # gt_class_ids_pos_1 = tf.identity(gt_class_ids)
    # proposal_pos_1 = tf.gather(proposals, gt_iou_argmax)

    # 在接下来的操作之前提出已经被选中的proposal
    indices = tf.unique(gt_iou_argmax)[0]  # 被选中的索引
    all_indices = tf.range(tf.shape(proposals)[0])  # 所有的索引
    remainder_indices = tf.setdiff1d(all_indices,
                                     tf.cast(indices, tf.int32))[0]  # 剩余的索引
    # 剩余的proposals和iou
    proposals = tf.gather(proposals, remainder_indices)
    iou = tf.gather(iou, remainder_indices, axis=1)

    # 正样本每个proposal 最大的iou,且iou>=0.5
    proposal_iou_max = tf.reduce_max(iou, axis=0)
    proposal_pos_idx = tf.where(
        proposal_iou_max >= 0.5)  # 正样本proposal对应的索引号,二维

    # proposal_iou_argmax = tf.argmax(iou, axis=0)
    proposal_iou_argmax = tf.cond(  # 需要考虑GT个数为0的情况
        tf.greater(tf.shape(gt_boxes)[0], 0),
        true_fn=lambda: tf.argmax(iou, axis=0),
        false_fn=lambda: tf.cast(tf.constant([]), tf.int64))
    gt_pos_idx = tf.gather_nd(proposal_iou_argmax,
                              proposal_pos_idx)  # 对应的GT 索引号,一维的

    gt_boxes_pos_2 = tf.gather(gt_boxes, gt_pos_idx)
    gt_class_ids_pos_2 = tf.gather(gt_class_ids, gt_pos_idx)
    proposal_pos_2 = tf.gather_nd(proposals, proposal_pos_idx)

    # 合并两部分正样本
    # gt_boxes_pos = tf.concat([gt_boxes_pos_1, gt_boxes_pos_2], axis=0)
    # class_ids = tf.concat([gt_class_ids_pos_1, gt_class_ids_pos_2], axis=0)
    # proposal_pos = tf.concat([proposal_pos_1, proposal_pos_2], axis=0)
    gt_boxes_pos = gt_boxes_pos_2
    class_ids = gt_class_ids_pos_2
    proposal_pos = proposal_pos_2

    # 根据正负样本比确定最终的正样本
    positive_num = tf.minimum(
        tf.shape(proposal_pos)[0],
        int(train_rois_per_image * roi_positive_ratio))
    gt_boxes_pos, class_ids, proposal_pos = shuffle_sample(
        [gt_boxes_pos, class_ids, proposal_pos],
        tf.shape(proposal_pos)[0], positive_num)

    # 计算回归目标
    deltas = regress_target(proposal_pos, gt_boxes_pos)

    # 负样本:与所有GT的iou<0.5
    proposal_neg_idx = tf.where(proposal_iou_max < 0.5)
    # 确定负样本数量
    negative_num = tf.minimum(train_rois_per_image - positive_num,
                              tf.shape(proposal_neg_idx)[0])
    proposal_neg_idx = tf.random_shuffle(proposal_neg_idx)[:negative_num]
    # 收集负样本
    proposal_neg = tf.gather_nd(proposals, proposal_neg_idx)
    class_ids_neg = tf.zeros(shape=[negative_num])  # 背景类,类别id为0
    deltas_neg = tf.zeros(shape=[negative_num, 4])

    # 合并正负样本
    train_rois = tf.concat([proposal_pos, proposal_neg], axis=0)
    deltas = tf.concat([deltas, deltas_neg], axis=0)
    class_ids = tf.concat([class_ids, class_ids_neg], axis=0)

    # 计算padding
    class_ids, train_rois = tf_utils.pad_list_to_fixed_size(
        [tf.expand_dims(class_ids, axis=1), train_rois],
        train_rois_per_image)  # class_ids分类扩一维
    # 为后续处理方便负样本tag设置为-1
    deltas = tf_utils.pad_to_fixed_size_with_negative(
        deltas, train_rois_per_image, negative_num=negative_num)
    # 其它统计指标
    gt_num = tf.shape(gt_class_ids)[0]  # GT数
    miss_match_gt_num = gt_num - tf.shape(
        tf.unique(gt_pos_idx)[0])[0]  # 未分配anchor的GT
    return [
        deltas, class_ids, train_rois,
        tf_utils.scalar_to_1d_tensor(miss_match_gt_num)
    ]
import tensorflow as tf


sess = tf.InteractiveSession() 
x = tf.constant([[2, 5, 3, -5], 
              [0, 3,-2,  5], 
              [4, 3, 5,  3], 
              [6, 1, 4,  0]]) 
listx = tf.constant([1,2,3,4,5,6,7,8]) 
listy = tf.constant([4,5,8,9]) 
 
print("\nx=\n", x.eval())
print("\nlistx=", listx.eval())
print("\nlisty=", listy.eval())
 
boolx = tf.constant([[True,False], [False,True]]) 
 
print("\ntf.argmin(x, 1).eval() ")# Position of the min value of columns
print(tf.argmin(x, 1).eval() )# Position of the min value of columns

print("\ntf.argmax(x, 1).eval() ")# Position of the max value of rows 
print(tf.argmax(x, 1).eval() )# Position of the max value of rows 

print("\ntf.setdiff1d(listx, listy)[0].eval() ")# List differences 
print(tf.setdiff1d(listx, listy)[0].eval() )# List differences 

print(tf.where(boolx).eval() )# Show true values  

print(tf.unique(listx)[0].eval() )# Unique values in list 
    def __init__(self,
                 base,
                 inputs,
                 k_transfer,
                 positive_feature_weights=False,
                 readout_sparsity=0.017,
                 init_masks='sta',
                 scope='readout',
                 reuse=False,
                 **kwargs):
        with base.tf_session.graph.as_default():
            with tf.variable_scope(scope, reuse=reuse):
                data = base.data
                _, num_px_y, num_px_x, num_features = inputs.shape.as_list()
                num_neurons = data.num_neurons

                # masks
                if init_masks == 'sta':
                    images_train, responses_train = data.train()
                    k = (images_train.shape[1] - num_px_y) // 2
                    mask_init = sta_init(images_train,
                                         responses_train,
                                         max_val=0.01,
                                         sd=0.001)[:, k:-k, k:-k]
                    mask_init = tf.constant_initializer(mask_init)
                else:
                    mask_init = tf.truncated_normal_initializer(mean=0.0,
                                                                stddev=0.01)
                self.masks = tf.get_variable(
                    'masks',
                    shape=[num_neurons, num_px_y, num_px_x],
                    initializer=mask_init)
                self.masks = tf.abs(self.masks, name='positive_masks')

                # split masks into neurons used for training the core and neurons
                # used for transfer learning (i.e. only readout weights are trained,
                # but they do not affect the core). We then insert a stop_gradient
                # for the units used only for transfer learning, apply the masks
                # and put things back together. This way, there is no gradient flow
                # from the units used for transfer learning back into the core.
                idx_train = tf.range(num_neurons, delta=k_transfer)
                idx_transfer, _ = tf.setdiff1d(tf.range(num_neurons),
                                               idx_train)
                idx_all = tf.invert_permutation(
                    tf.concat([idx_train, idx_transfer], axis=0))
                masks_train = tf.gather(self.masks, idx_train)
                masks_transfer = tf.gather(self.masks, idx_transfer)
                masked_train = tf.tensordot(masks_train,
                                            inputs, [[1, 2], [1, 2]],
                                            name='masked_train')
                masked_transfer = tf.tensordot(masks_transfer,
                                               tf.stop_gradient(inputs),
                                               [[1, 2], [1, 2]],
                                               name='masked_transfer')
                self.masked = tf.transpose(
                    tf.gather(tf.concat([masked_train, masked_transfer],
                                        axis=0),
                              idx_all,
                              name='masked'), [1, 2, 0])

                # feature weights
                self.feature_weights = tf.get_variable(
                    'feature_weights',
                    shape=[num_neurons, num_features],
                    initializer=tf.truncated_normal_initializer(mean=0.0,
                                                                stddev=0.01))
                if positive_feature_weights:
                    self.feature_weights = tf.abs(
                        self.feature_weights, name='positive_feature_weights')
                self.h = tf.reduce_sum(
                    self.masked * tf.transpose(self.feature_weights), 1)

                # L1 regularization for readout layer
                self.readout_reg = readout_sparsity * tf.reduce_sum(
                    tf.reduce_sum(tf.abs(self.masks), [1, 2]) * \
                    tf.reduce_sum(tf.abs(self.feature_weights), 1))
                tf.losses.add_loss(self.readout_reg,
                                   tf.GraphKeys.REGULARIZATION_LOSSES)

                # bias and output nonlinearity
                _, responses = data.train()
                bias_init = 0.5 * inv_soft_threshold(responses.mean(axis=0))
                self.biases = tf.get_variable(
                    'biases',
                    shape=[num_neurons],
                    initializer=tf.constant_initializer(bias_init))
                self.output = tf.identity(soft_threshold(self.h + self.biases),
                                          name='output')
Exemple #37
0
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [4, 3, 5, 3], [6, 1, 4, 0]])
listx = tf.constant([1, 2, 3, 4, 5, 6, 7, 8])
listy = tf.constant([4, 5, 8, 9])

boolx = tf.constant([[True, False], [False, True]])

tf.argmin(x, 1).eval()  # Position of the maximum value of columns
tf.argmax(x, 1).eval()  # Position of the minimum value of rows
tf.setdiff1d(listx, listy)[0].eval()  # List differences
tf.where(boolx).eval()  # Show true values
tf.unique(listx)[0].eval()  # Unique values in list
Exemple #38
0
        def _match_when_rows_are_non_empty():
            """Performs matching when the rows of similarity matrix are non empty.

      Returns:
        matches:  int32 tensor indicating the row each column matches to.
      """
            # Matches for each column
            matches = tf.argmax(similarity_matrix, 0)

            # Deal with matched and unmatched threshold
            if self._matched_threshold is not None:
                # Get logical indices of ignored and unmatched columns as tf.int64
                matched_vals = tf.reduce_max(similarity_matrix, 0)
                below_unmatched_threshold = tf.greater(
                    self._unmatched_threshold, matched_vals)
                between_thresholds = tf.logical_and(
                    tf.greater_equal(matched_vals, self._unmatched_threshold),
                    tf.greater(self._matched_threshold, matched_vals))
                tfprint.matches_raw = tf.Print(
                    matches,
                    ["raw matches", tf.shape(matches), matches])
                if self._negatives_lower_than_unmatched:
                    matches = self._set_values_using_indicator(
                        matches, below_unmatched_threshold, -1)
                    matches = self._set_values_using_indicator(
                        matches, between_thresholds, -2)
                else:
                    matches = self._set_values_using_indicator(
                        matches, below_unmatched_threshold, -2)
                    matches = self._set_values_using_indicator(
                        matches, between_thresholds, -1)
                tfprint.matches_thresh = tf.Print(
                    matches,
                    ["after thresh matches",
                     tf.shape(matches), matches])

            if self._force_match_for_each_row:
                forced_matches_ids = tf.cast(tf.argmax(similarity_matrix, 1),
                                             tf.int32)

                # Set matches[forced_matches_ids] = [0, ..., R], R is number of rows.
                row_range = tf.range(tf.shape(similarity_matrix)[0])
                col_range = tf.range(tf.shape(similarity_matrix)[1])
                forced_matches_values = tf.cast(row_range, matches.dtype)
                keep_matches_ids, _ = tf.setdiff1d(col_range,
                                                   forced_matches_ids)
                keep_matches_values = tf.gather(matches, keep_matches_ids)
                matches = tf.dynamic_stitch(
                    [forced_matches_ids, keep_matches_ids],
                    [forced_matches_values, keep_matches_values])
                tfprint.argmax_row_range = tf.Print(
                    row_range,
                    ["row_range", tf.shape(row_range), row_range])
                tfprint.argmax_col_range = tf.Print(
                    col_range,
                    ["col_range", tf.shape(col_range), col_range])
                tfprint.forced_matches_values = tf.Print(
                    forced_matches_values, [
                        "forced_matches_values",
                        tf.shape(forced_matches_values), forced_matches_values
                    ])
                tfprint.keep_matches_ids = tf.Print(keep_matches_ids, [
                    "keep_matches_ids",
                    tf.shape(keep_matches_ids), keep_matches_ids
                ])
                tfprint.keep_matches_values = tf.Print(keep_matches_values, [
                    "keep_matches_values",
                    tf.shape(keep_matches_values), keep_matches_values
                ])

            return tf.cast(matches, tf.int32)