Beispiel #1
0
  def test_ragged_value_concatenation(self):
    tensor_1 = ragged_tensor_value.RaggedTensorValue(
        np.array([0, 1, 2]), np.array([0, 1, 3], dtype=np.int64))
    tensor_2 = ragged_tensor_value.RaggedTensorValue(
        np.array([3, 4, 5]), np.array([0, 2, 3], dtype=np.int64))
    concatenated_tensor = composite_tensor_utils.append_composite_tensor(
        tensor_1, tensor_2)

    self.assertAllEqual(concatenated_tensor.values, [0, 1, 2, 3, 4, 5])
    self.assertAllEqual(concatenated_tensor.row_splits, [0, 1, 3, 5, 6])
Beispiel #2
0
 def _eval_tensor(self, tensor):
     if ragged_tensor.is_ragged(tensor):
         return ragged_tensor_value.RaggedTensorValue(
             self._eval_tensor(tensor.values),
             self._eval_tensor(tensor.row_splits))
     else:
         return test_util.TensorFlowTestCase._eval_tensor(self, tensor)
Beispiel #3
0
 def _eager_value(self):
     """Returns a RaggedTensorValue for self.  Requires self._is_eager()=true."""
     value = self.inner_values.numpy()
     for row_splits in reversed(self.nested_row_splits):
         value = ragged_tensor_value.RaggedTensorValue(
             value, row_splits.numpy())
     return value
Beispiel #4
0
  def testPaddedBatchRaggedError(self):

    rt = ragged_tensor_value.RaggedTensorValue(
        np.array([0, 42]), np.array([0, 2], dtype=np.int64))

    with self.assertRaises(TypeError):
      _ = dataset_ops.Dataset.from_tensors(rt).repeat(10).padded_batch(10)
Beispiel #5
0
def _append_ragged_tensor_value(target, to_append):
    """Append ragged tensor value objects."""
    # Make sure the ragged tensors are of the same size (save for the 0th dim).
    if len(target.shape) != len(to_append.shape):
        raise RuntimeError('Unable to concatenate %s and %s' %
                           (target, to_append))

    if target.shape[1:] != to_append.shape[1:]:
        raise RuntimeError('Unable to concatenate %s and %s' %
                           (target, to_append))

    adjusted_row_splits = to_append.row_splits[1:] + target.row_splits[-1]
    new_row_splits = np.append(target.row_splits, adjusted_row_splits)
    if isinstance(target.values, ragged_tensor_value.RaggedTensorValue):
        new_values = _append_ragged_tensor_value(target.values,
                                                 to_append.values)
    else:
        new_values = np.concatenate((target.values, to_append.values), axis=0)

    return ragged_tensor_value.RaggedTensorValue(new_values, new_row_splits)
Beispiel #6
0
  def test_is_composite(self):
    # Validate that all composite tensor and value types return true.
    self.assertTrue(
        training_utils_v1.is_composite_or_composite_value(
            sparse_tensor.SparseTensor([[0, 0]], [1], [1, 1])))
    self.assertTrue(
        training_utils_v1.is_composite_or_composite_value(
            sparse_tensor.SparseTensorValue([[0, 0]], [1], [1, 1])))
    self.assertTrue(
        training_utils_v1.is_composite_or_composite_value(
            ragged_tensor.RaggedTensor.from_row_splits(
                np.array([0, 1, 2]), np.array([0, 1, 3], dtype=np.int64))))
    self.assertTrue(
        training_utils_v1.is_composite_or_composite_value(
            ragged_tensor_value.RaggedTensorValue(
                np.array([0, 1, 2]), np.array([0, 1, 3], dtype=np.int64))))

    # Test that numpy arrays and tensors return false.
    self.assertFalse(
        training_utils_v1.is_composite_or_composite_value(np.ndarray([0, 1])))
    self.assertFalse(
        training_utils_v1.is_composite_or_composite_value(
            ops.convert_to_tensor_v2_with_dispatch([3, 1])))
 def _eval_tensor(self, tensor):
     if tensor is None:
         return None
     elif callable(tensor):
         return self._eval_helper(tensor())
     else:
         try:
             if sparse_tensor.is_sparse(tensor):
                 return sparse_tensor.SparseTensorValue(
                     tensor.indices.numpy(), tensor.values.numpy(),
                     tensor.dense_shape.numpy())
             elif ragged_tensor.is_ragged(tensor):
                 return ragged_tensor_value.RaggedTensorValue(
                     self._eval_tensor(tensor.values),
                     self._eval_tensor(tensor.row_splits))
             elif isinstance(tensor, ops.IndexedSlices):
                 return ops.IndexedSlicesValue(
                     values=tensor.values.numpy(),
                     indices=tensor.indices.numpy(),
                     dense_shape=tensor.dense_shape.numpy())
             return tensor.numpy()
         except AttributeError as e:
             ValueError("Unsupported type %s." % type(tensor)), e
Beispiel #8
0
 def _ragged_factory(values, row_splits):
     row_splits = np.array(row_splits, dtype=row_splits_dtype)
     return ragged_tensor_value.RaggedTensorValue(values, row_splits)
Beispiel #9
0
 def testRaggedElement(self):
     ds = dataset_ops.Dataset.from_tensors(
         ragged_tensor_value.RaggedTensorValue(
             np.array([0, 1, 2]), np.array([0, 1, 3], dtype=np.int64)))
     with self.assertRaises(TypeError):
         ds.as_numpy_iterator()
Beispiel #10
0
 def testRaggedElement(self):
     self._testInvalidElement(
         ragged_tensor_value.RaggedTensorValue(
             np.array([0, 1, 2]), np.array([0, 1, 3], dtype=np.int64)))
Beispiel #11
0
def _ragged_tensor_value_from_components(components):
    components = list(components)
    value = components.pop()
    while components:
        value = ragged_tensor_value.RaggedTensorValue(value, components.pop())
    return value