Python tensorflow.segment_max() Examples

The following are 7 code examples of tensorflow.segment_max(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: segment_reduction_ops_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testGradient(self):
    shape = [4, 4]
    indices = [0, 1, 2, 2]
    for tf_op in [tf.segment_sum,
                  tf.segment_mean,
                  tf.segment_min,
                  tf.segment_max]:
      with self.test_session():
        tf_x, np_x = self._input(shape, dtype=tf.float64)
        s = tf_op(data=tf_x, segment_ids=indices)
        jacob_t, jacob_n = tf.test.compute_gradient(
            tf_x,
            shape,
            s,
            [3, 4],
            x_init_value=np_x.astype(np.double),
            delta=1)
      self.assertAllClose(jacob_t, jacob_n, rtol=1e-3, atol=1e-3) 
Example #2
Source File: net_utils.py    From scene-graph-TF-release with MIT License 6 votes vote down vote up
def padded_segment_reduce(vecs, segment_inds, num_segments, reduction_mode):
    """
    Reduce the vecs with segment_inds and reduction_mode
    Input:
        vecs: A Tensor of shape (batch_size, vec_dim)
        segment_inds: A Tensor containing the segment index of each
        vec row, should agree with vecs in shape[0]
    Output:
        A tensor of shape (vec_dim)
    """
    if reduction_mode == 'max':
        print('USING MAX POOLING FOR REDUCTION!')
        vecs_reduced = tf.segment_max(vecs, segment_inds)
    elif reduction_mode == 'mean':
        print('USING AVG POOLING FOR REDUCTION!')
        vecs_reduced = tf.segment_mean(vecs, segment_inds)
    vecs_reduced.set_shape([num_segments, vecs.get_shape()[1]])
    return vecs_reduced 
Example #3
Source File: ops.py    From tfdeploy with MIT License 5 votes vote down vote up
def test_SegmentMax(self):
        t = tf.segment_max(self.random(4, 2, 3), np.array([0, 1, 1, 2]))
        self.check(t) 
Example #4
Source File: segment_reduction_ops_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testValues(self):
    dtypes = [tf.float32,
              tf.float64,
              tf.int64,
              tf.int32,
              tf.complex64,
              tf.complex128]

    # Each item is np_op1, np_op2, tf_op
    ops_list = [(np.add, None, tf.segment_sum),
                (self._mean_cum_op, self._mean_reduce_op,
                 tf.segment_mean),
                (np.ndarray.__mul__, None, tf.segment_prod),
                (np.minimum, None, tf.segment_min),
                (np.maximum, None, tf.segment_max)]

    # A subset of ops has been enabled for complex numbers
    complex_ops_list = [(np.add, None, tf.segment_sum),
                        (np.ndarray.__mul__, None, tf.segment_prod)]

    n = 10
    shape = [n, 2]
    indices = [i // 3 for i in range(n)]
    for dtype in dtypes:
      if dtype in (tf.complex64, tf.complex128):
        curr_ops_list = complex_ops_list
      else:
        curr_ops_list = ops_list

      with self.test_session(use_gpu=False):
        tf_x, np_x = self._input(shape, dtype=dtype)
        for np_op1, np_op2, tf_op in curr_ops_list:
          np_ans = self._segmentReduce(indices, np_x, np_op1, np_op2)
          s = tf_op(data=tf_x, segment_ids=indices)
          tf_ans = s.eval()
          self._assertAllClose(indices, np_ans, tf_ans)
          # NOTE(mrry): The static shape inference that computes
          # `tf_ans.shape` can only infer that sizes from dimension 1
          # onwards, because the size of dimension 0 is data-dependent
          # and may therefore vary dynamically.
          self.assertAllEqual(np_ans.shape[1:], tf_ans.shape[1:]) 
Example #5
Source File: math_grad_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testSegmentMaxGradient(self):
    data = tf.constant([1.0, 2.0, 3.0], dtype=tf.float32)
    segment_ids = tf.constant([0, 0, 1], dtype=tf.int64)
    segment_max = tf.segment_max(data, segment_ids)
    with self.test_session():
      error = tf.test.compute_gradient_error(data, [3], segment_max, [2])
      self.assertLess(error, 1e-4) 
Example #6
Source File: math_grad_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testSegmentMaxGradientWithTies(self):
    inputs = tf.constant([1.0], dtype=tf.float32)
    data = tf.concat(0, [inputs, inputs])
    segment_ids = tf.constant([0, 0], dtype=tf.int64)
    segment_max = tf.segment_max(data, segment_ids)
    with self.test_session():
      error = tf.test.compute_gradient_error(inputs, [1], segment_max, [1])
      self.assertLess(error, 1e-4) 
Example #7
Source File: ops.py    From document-qa with Apache License 2.0 5 votes vote down vote up
def segment_logsumexp(xs, segments):
    """ Similar tf.segment_sum but compute logsumexp rather then sum """
    # Stop gradients following the implementation of tf.reduce_logsumexp
    maxs = tf.stop_gradient(tf.reduce_max(xs, axis=1))
    segment_maxes = tf.segment_max(maxs, segments)
    xs -= tf.expand_dims(tf.gather(segment_maxes, segments), 1)
    sums = tf.reduce_sum(tf.exp(xs), axis=1)
    return tf.log(tf.segment_sum(sums, segments)) + segment_maxes