예제 #1
0
 def test_tf_tensor_list_new_empty(self):
     l = data_structures.tf_tensor_list_new([],
                                            element_dtype=dtypes.int32,
                                            element_shape=())
     t = list_ops.tensor_list_stack(l, element_dtype=dtypes.int32)
     with self.cached_session() as sess:
         self.assertAllEqual(self.evaluate(t), [])
def tensor_list(elements,
                element_dtype=None,
                element_shape=None,
                use_tensor_array=False):
  """Creates an tensor list and populates it with the given elements.

  This function provides a more uniform access to tensor lists and tensor
  arrays, and allows optional initialization.

  Note: this function is a simplified wrapper. If you need greater control,
  it is recommended to use the underlying implementation directly.

  Args:
    elements: Iterable[tf.Tensor, ...], the elements to initially fill the list
        with
    element_dtype: Optional[tf.DType], data type for the elements in the list;
        required if the list is empty
    element_shape: Optional[tf.TensorShape], shape for the elements in the list;
        required if the list is empty
    use_tensor_array: bool, whether to use the more compatible but restrictive
        tf.TensorArray implementation
  Returns:
    Union[tf.Tensor, tf.TensorArray], the new list.
  Raises:
    ValueError: for invalid arguments
  """
  _validate_list_constructor(elements, element_dtype, element_shape)
  if use_tensor_array:
    return data_structures.tf_tensor_array_new(elements, element_dtype,
                                               element_shape)
  else:
    return data_structures.tf_tensor_list_new(elements, element_dtype,
                                              element_shape)
예제 #3
0
 def test_tf_tensor_list_new_empty(self):
   l = data_structures.tf_tensor_list_new([],
                                          element_dtype=dtypes.int32,
                                          element_shape=())
   t = list_ops.tensor_list_stack(l, element_dtype=dtypes.int32)
   with self.cached_session() as sess:
     self.assertAllEqual(sess.run(t), [])
예제 #4
0
def tensor_list(elements,
                element_dtype=None,
                element_shape=None,
                use_tensor_array=False):
    """Creates an tensor list and populates it with the given elements.

  This function provides a more uniform access to tensor lists and tensor
  arrays, and allows optional initialization.

  Note: this function is a simplified wrapper. If you need greater control,
  it is recommended to use the underlying implementation directly.

  Args:
    elements: Iterable[tf.Tensor, ...], the elements to initially fill the list
        with
    element_dtype: Optional[tf.DType], data type for the elements in the list;
        required if the list is empty
    element_shape: Optional[tf.TensorShape], shape for the elements in the list;
        required if the list is empty
    use_tensor_array: bool, whether to use the more compatible but restrictive
        tf.TensorArray implementation
  Returns:
    Union[tf.Tensor, tf.TensorArray], the new list.
  Raises:
    ValueError: for invalid arguments
  """
    _validate_list_constructor(elements, element_dtype, element_shape)
    if use_tensor_array:
        return data_structures.tf_tensor_array_new(elements, element_dtype,
                                                   element_shape)
    else:
        return data_structures.tf_tensor_list_new(elements, element_dtype,
                                                  element_shape)
 def test_tf_tensor_list_new_illegal_input(self):
   with self.assertRaises(ValueError):
     data_structures.tf_tensor_list_new([3, 4.0])
   # TODO(mdan): It might make more sense to type cast in this case.
   with self.assertRaises(ValueError):
     data_structures.tf_tensor_list_new([3, 4], element_dtype=dtypes.float32)
   # Tensor lists do support heterogeneous lists.
   self.assertIsNot(data_structures.tf_tensor_list_new([3, [4, 5]]), None)
   with self.assertRaises(ValueError):
     data_structures.tf_tensor_list_new([3, 4], element_shape=(2,))
   with self.assertRaises(ValueError):
     data_structures.tf_tensor_list_new(
         constant_op.constant([1, 2, 3]), element_shape=[1])
예제 #6
0
 def test_tf_tensor_list_new_illegal_input(self):
   with self.assertRaises(ValueError):
     data_structures.tf_tensor_list_new([3, 4.0])
   # TODO(mdan): It might make more sense to type cast in this case.
   with self.assertRaises(ValueError):
     data_structures.tf_tensor_list_new([3, 4], element_dtype=dtypes.float32)
   # Tensor lists do support heterogeneous lists.
   self.assertIsNot(data_structures.tf_tensor_list_new([3, [4, 5]]), None)
   with self.assertRaises(ValueError):
     data_structures.tf_tensor_list_new([3, 4], element_shape=(2,))
   with self.assertRaises(ValueError):
     data_structures.tf_tensor_list_new(
         constant_op.constant([1, 2, 3]), element_shape=[1])
예제 #7
0
 def test_len(self):
   self.assertEqual(py_builtins.len_([1, 2, 3]), 3)
   with self.test_session() as sess:
     t = py_builtins.len_(constant_op.constant([[1], [2], [3]]))
     self.assertEqual(t, 3)
     ta = py_builtins.len_(tensor_array_ops.TensorArray(dtypes.int32, size=5))
     self.assertEqual(sess.run(ta), 5)
     tl = py_builtins.len_(data_structures.tf_tensor_list_new([3, 4, 5]))
     self.assertEqual(sess.run(tl), 3)
예제 #8
0
 def test_len(self):
   self.assertEqual(py_builtins.len_([1, 2, 3]), 3)
   with self.cached_session() as sess:
     t = py_builtins.len_(constant_op.constant([[1], [2], [3]]))
     self.assertEqual(t, 3)
     ta = py_builtins.len_(tensor_array_ops.TensorArray(dtypes.int32, size=5))
     self.assertEqual(self.evaluate(ta), 5)
     tl = py_builtins.len_(data_structures.tf_tensor_list_new([3, 4, 5]))
     self.assertEqual(self.evaluate(tl), 3)
 def test_tf_tensor_list_new_from_tensor(self):
     l = data_structures.tf_tensor_list_new(constant_op.constant([3, 4, 5]))
     t = list_ops.tensor_list_stack(l, element_dtype=dtypes.int32)
     with self.cached_session() as sess:
         self.assertAllEqual(sess.run(t), [3, 4, 5])
예제 #10
0
 def test_tf_tensor_list_new_from_tensor(self):
   l = data_structures.tf_tensor_list_new(constant_op.constant([3, 4, 5]))
   t = list_ops.tensor_list_stack(l, element_dtype=dtypes.int32)
   with self.cached_session() as sess:
     self.assertAllEqual(sess.run(t), [3, 4, 5])
 def test_tf_tensor_list_new(self):
   l = data_structures.tf_tensor_list_new([3, 4, 5])
   t = list_ops.tensor_list_stack(l, element_dtype=dtypes.int32)
   with self.cached_session() as sess:
     self.assertAllEqual(self.evaluate(t), [3, 4, 5])