Ejemplo n.º 1
0
  def testDenseToSparseBatchDatasetWithUnknownShape(self):
    components = np.random.randint(5, size=(40,)).astype(np.int32)
    iterator = (
        dataset_ops.Dataset.from_tensor_slices(components)
        .map(lambda x: array_ops.fill([x, x], x)).apply(
            batching.dense_to_sparse_batch(
                4, [5, None])).make_initializable_iterator())
    init_op = iterator.initializer
    get_next = iterator.get_next()

    with self.cached_session() as sess:
      sess.run(init_op)

      for start in range(0, len(components), 4):
        results = sess.run(get_next)
        self.assertAllEqual([[i, j, z]
                             for i, c in enumerate(components[start:start + 4])
                             for j in range(c)
                             for z in range(c)], results.indices)
        self.assertAllEqual([
            c
            for c in components[start:start + 4] for _ in range(c)
            for _ in range(c)
        ], results.values)
        self.assertAllEqual([
            min(4,
                len(components) - start), 5,
            np.max(components[start:start + 4])
        ], results.dense_shape)

      with self.assertRaises(errors.OutOfRangeError):
        sess.run(get_next)
Ejemplo n.º 2
0
  def testDenseToSparseBatchDatasetWithUnknownShape(self):
    components = np.random.randint(5, size=(40,)).astype(np.int32)
    iterator = (
        dataset_ops.Dataset.from_tensor_slices(components)
        .map(lambda x: array_ops.fill([x, x], x)).apply(
            batching.dense_to_sparse_batch(
                4, [5, None])).make_initializable_iterator())
    init_op = iterator.initializer
    get_next = iterator.get_next()

    with self.test_session() as sess:
      sess.run(init_op)

      for start in range(0, len(components), 4):
        results = sess.run(get_next)
        self.assertAllEqual([[i, j, z]
                             for i, c in enumerate(components[start:start + 4])
                             for j in range(c)
                             for z in range(c)], results.indices)
        self.assertAllEqual([
            c
            for c in components[start:start + 4] for _ in range(c)
            for _ in range(c)
        ], results.values)
        self.assertAllEqual([
            min(4,
                len(components) - start), 5,
            np.max(components[start:start + 4])
        ], results.dense_shape)

      with self.assertRaises(errors.OutOfRangeError):
        sess.run(get_next)
    def testDenseToSparseBatchDatasetWithInvalidShape(self):
        input_tensor = array_ops.constant([[1]])
        iterator = (dataset_ops.Dataset.from_tensors(input_tensor).apply(
            batching.dense_to_sparse_batch(
                4, [-2])).make_initializable_iterator())
        init_op = iterator.initializer

        with self.test_session() as sess:
            with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                         "Dimension -2 must be >= -1"):
                sess.run(init_op)
Ejemplo n.º 4
0
  def testDenseToSparseBatchDatasetWithInvalidShape(self):
    input_tensor = array_ops.constant([[1]])
    iterator = (dataset_ops.Dataset.from_tensors(input_tensor)
                .apply(batching.dense_to_sparse_batch(4, [-2]))
                .make_initializable_iterator())
    init_op = iterator.initializer

    with self.test_session() as sess:
      with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                   "Dimension -2 must be >= -1"):
        sess.run(init_op)
Ejemplo n.º 5
0
  def testDenseToSparseBatchDatasetShapeErrors(self):
    input_tensor = array_ops.placeholder(dtypes.int32)
    iterator = (dataset_ops.Dataset.from_tensors(input_tensor).apply(
        batching.dense_to_sparse_batch(4, [12])).make_initializable_iterator())
    init_op = iterator.initializer
    get_next = sparse_tensor.SparseTensor(*iterator.get_next())

    with self.test_session() as sess:
      # Initialize with an input tensor of incompatible rank.
      sess.run(init_op, feed_dict={input_tensor: [[1]]})
      with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                   "incompatible with the row shape"):
        sess.run(get_next)

      # Initialize with an input tensor that is larger than `row_shape`.
      sess.run(init_op, feed_dict={input_tensor: range(13)})
      with self.assertRaisesRegexp(errors.DataLossError,
                                   "larger than the row shape"):
        sess.run(get_next)
Ejemplo n.º 6
0
  def testDenseToSparseBatchDatasetShapeErrors(self):
    input_tensor = array_ops.placeholder(dtypes.int32)
    iterator = (dataset_ops.Dataset.from_tensors(input_tensor).apply(
        batching.dense_to_sparse_batch(4, [12])).make_initializable_iterator())
    init_op = iterator.initializer
    get_next = sparse_tensor.SparseTensor(*iterator.get_next())

    with self.test_session() as sess:
      # Initialize with an input tensor of incompatible rank.
      sess.run(init_op, feed_dict={input_tensor: [[1]]})
      with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                   "incompatible with the row shape"):
        sess.run(get_next)

      # Initialize with an input tensor that is larger than `row_shape`.
      sess.run(init_op, feed_dict={input_tensor: range(13)})
      with self.assertRaisesRegexp(errors.DataLossError,
                                   "larger than the row shape"):
        sess.run(get_next)
Ejemplo n.º 7
0
    def dense_to_sparse_batch(self, batch_size, row_shape):
        """Use: `Dataset.apply(tf.contrib.data.dense_to_sparse_batch(...))`."""

        return self.apply(batching.dense_to_sparse_batch(
            batch_size, row_shape))
Ejemplo n.º 8
0
 def testDenseToSparseBatchDatasetWithInvalidShape(self):
   input_tensor = array_ops.constant([[1]])
   with self.assertRaisesRegexp(ValueError, "Dimension -2 must be >= 0"):
     dataset_ops.Dataset.from_tensors(input_tensor).apply(
         batching.dense_to_sparse_batch(4, [-2])).make_initializable_iterator()
Ejemplo n.º 9
0
 def _build_dataset_dense_to_sparse(self, components):
   return dataset_ops.Dataset.from_tensor_slices(components).map(
       lambda x: array_ops.fill([x], x)).apply(
           batching.dense_to_sparse_batch(4, [12]))
Ejemplo n.º 10
0
 def testDenseToSparseBatchDatasetWithInvalidShape(self):
   input_tensor = array_ops.constant([[1]])
   with self.assertRaisesRegexp(ValueError, "Dimension -2 must be >= 0"):
     dataset_ops.Dataset.from_tensors(input_tensor).apply(
         batching.dense_to_sparse_batch(4, [-2])).make_initializable_iterator()
Ejemplo n.º 11
0
  def dense_to_sparse_batch(self, batch_size, row_shape):
    """Use: `Dataset.apply(tf.contrib.data.dense_to_sparse_batch(...))`."""

    return self.apply(batching.dense_to_sparse_batch(batch_size, row_shape))