コード例 #1
0
 def testSegmentMaxGradient(self):
     data = constant_op.constant([1.0, 2.0, 3.0], dtype=dtypes.float32)
     segment_ids = constant_op.constant([0, 0, 1], dtype=dtypes.int64)
     segment_max = math_ops.segment_max(data, segment_ids)
     with self.test_session():
         error = gradient_checker.compute_gradient_error(data, [3], segment_max, [2])
         self.assertLess(error, 1e-4)
コード例 #2
0
 def testSegmentMaxGradient(self):
   data = constant_op.constant([1.0, 2.0, 3.0], dtype=dtypes.float32)
   segment_ids = constant_op.constant([0, 0, 1], dtype=dtypes.int64)
   segment_max = math_ops.segment_max(data, segment_ids)
   with self.cached_session():
     error = gradient_checker.compute_gradient_error(data, [3], segment_max,
                                                     [2])
     self.assertLess(error, 1e-4)
コード例 #3
0
 def testSegmentMaxGradientWithTies(self):
     inputs = constant_op.constant([1.0], dtype=dtypes.float32)
     data = array_ops.concat_v2([inputs, inputs], 0)
     segment_ids = constant_op.constant([0, 0], dtype=dtypes.int64)
     segment_max = math_ops.segment_max(data, segment_ids)
     with self.test_session():
         error = gradient_checker.compute_gradient_error(inputs, [1], segment_max, [1])
         self.assertLess(error, 1e-4)
コード例 #4
0
 def testSegmentMaxGradientWithTies(self):
   inputs = constant_op.constant([1.0], dtype=dtypes.float32)
   data = array_ops.concat([inputs, inputs], 0)
   segment_ids = constant_op.constant([0, 0], dtype=dtypes.int64)
   segment_max = math_ops.segment_max(data, segment_ids)
   with self.cached_session():
     error = gradient_checker.compute_gradient_error(inputs, [1], segment_max,
                                                     [1])
     self.assertLess(error, 1e-4)
コード例 #5
0
def _sequence_length_from_sparse_tensor(sp_tensor, num_elements=1):
  with ops.name_scope(None, 'sequence_length') as name_scope:
    row_ids = sp_tensor.indices[:, 0]
    column_ids = sp_tensor.indices[:, 1]
    column_ids += array_ops.ones_like(column_ids)
    seq_length = (
        math_ops.segment_max(column_ids, segment_ids=row_ids) / num_elements)
    # If the last n rows do not have ids, seq_length will have shape
    # [batch_size - n]. Pad the remaining values with zeros.
    n_pad = array_ops.shape(sp_tensor)[:1] - array_ops.shape(seq_length)[:1]
    padding = array_ops.zeros(n_pad, dtype=seq_length.dtype)
    return array_ops.concat([seq_length, padding], axis=0, name=name_scope)
コード例 #6
0
def _sequence_length_from_sparse_tensor(sp_tensor, num_elements=1):
  with ops.name_scope(None, 'sequence_length') as name_scope:
    row_ids = sp_tensor.indices[:, 0]
    column_ids = sp_tensor.indices[:, 1]
    column_ids += array_ops.ones_like(column_ids)
    seq_length = math_ops.to_int64(
        math_ops.segment_max(column_ids, segment_ids=row_ids) / num_elements)
    # If the last n rows do not have ids, seq_length will have shape
    # [batch_size - n]. Pad the remaining values with zeros.
    n_pad = array_ops.shape(sp_tensor)[:1] - array_ops.shape(seq_length)[:1]
    padding = array_ops.zeros(n_pad, dtype=seq_length.dtype)
    return array_ops.concat([seq_length, padding], axis=0, name=name_scope)
コード例 #7
0
ファイル: utils.py プロジェクト: adit-chandra/tensorflow
def sequence_length_from_sparse_tensor(sp_tensor, num_elements=1):
  """Returns a [batch_size] Tensor with per-example sequence length."""
  with ops.name_scope(None, 'sequence_length') as name_scope:
    row_ids = sp_tensor.indices[:, 0]
    column_ids = sp_tensor.indices[:, 1]
    # Add one to convert column indices to element length
    column_ids += array_ops.ones_like(column_ids)
    # Get the number of elements we will have per example/row
    seq_length = math_ops.segment_max(column_ids, segment_ids=row_ids)

    # The raw values are grouped according to num_elements;
    # how many entities will we have after grouping?
    # Example: orig tensor [[1, 2], [3]], col_ids = (0, 1, 1),
    # row_ids = (0, 0, 1), seq_length = [2, 1]. If num_elements = 2,
    # these will get grouped, and the final seq_length is [1, 1]
    seq_length = math_ops.cast(
        math_ops.ceil(seq_length / num_elements), dtypes.int64)

    # If the last n rows do not have ids, seq_length will have shape
    # [batch_size - n]. Pad the remaining values with zeros.
    n_pad = array_ops.shape(sp_tensor)[:1] - array_ops.shape(seq_length)[:1]
    padding = array_ops.zeros(n_pad, dtype=seq_length.dtype)
    return array_ops.concat([seq_length, padding], axis=0, name=name_scope)
コード例 #8
0
ファイル: utils.py プロジェクト: xutianming/tensorflow
def sequence_length_from_sparse_tensor(sp_tensor, num_elements=1):
  """Returns a [batch_size] Tensor with per-example sequence length."""
  with ops.name_scope(None, 'sequence_length') as name_scope:
    row_ids = sp_tensor.indices[:, 0]
    column_ids = sp_tensor.indices[:, 1]
    # Add one to convert column indices to element length
    column_ids += array_ops.ones_like(column_ids)
    # Get the number of elements we will have per example/row
    seq_length = math_ops.segment_max(column_ids, segment_ids=row_ids)

    # The raw values are grouped according to num_elements;
    # how many entities will we have after grouping?
    # Example: orig tensor [[1, 2], [3]], col_ids = (0, 1, 1),
    # row_ids = (0, 0, 1), seq_length = [2, 1]. If num_elements = 2,
    # these will get grouped, and the final seq_length is [1, 1]
    seq_length = math_ops.cast(
        math_ops.ceil(seq_length / num_elements), dtypes.int64)

    # If the last n rows do not have ids, seq_length will have shape
    # [batch_size - n]. Pad the remaining values with zeros.
    n_pad = array_ops.shape(sp_tensor)[:1] - array_ops.shape(seq_length)[:1]
    padding = array_ops.zeros(n_pad, dtype=seq_length.dtype)
    return array_ops.concat([seq_length, padding], axis=0, name=name_scope)