def body(list_, m): list_ = control_flow_ops.cond( math_ops.equal(list_ops.tensor_list_length(list_), 0), lambda: list_ops.empty_tensor_list(m.shape, m.dtype), lambda: list_) list_ = list_ops.tensor_list_push_back(list_, m) return list_, m
def body(i, m, t1): t1 = control_flow_ops.cond( math_ops.equal(list_ops.tensor_list_length(t1), 0), lambda: list_ops.empty_tensor_list(m.shape, m.dtype), lambda: t1) t1 = list_ops.tensor_list_push_back(t1, m * i) i += 1.0 return i, m, t1
def testTensorListFromTensor(self): t = constant_op.constant([1.0, 2.0]) l = list_ops.tensor_list_from_tensor(t, element_shape=[]) l, e = list_ops.tensor_list_pop_back(l, element_dtype=dtypes.float32) self.assertAllEqual(self.evaluate(e), 2.0) l, e = list_ops.tensor_list_pop_back(l, element_dtype=dtypes.float32) self.assertAllEqual(self.evaluate(e), 1.0) self.assertAllEqual(self.evaluate(list_ops.tensor_list_length(l)), 0)
def body(i, m, t1): t1 = control_flow_ops.cond( math_ops.equal(list_ops.tensor_list_length(t1), 0), lambda: list_ops.empty_tensor_list(m.shape, m.dtype), lambda: t1) t1 = list_ops.tensor_list_push_back(t1, m * i) i += 1.0 return i, m, t1
def testTensorListFromTensor(self): t = constant_op.constant([1.0, 2.0]) l = list_ops.tensor_list_from_tensor(t, element_shape=scalar_shape()) l, e = list_ops.tensor_list_pop_back(l, element_dtype=dtypes.float32) self.assertAllEqual(self.evaluate(e), 2.0) l, e = list_ops.tensor_list_pop_back(l, element_dtype=dtypes.float32) self.assertAllEqual(self.evaluate(e), 1.0) self.assertAllEqual(self.evaluate(list_ops.tensor_list_length(l)), 0)
def build_graph(parameters): """Build the TensorListLength op testing graph.""" data = tf.placeholder(dtype=parameters["element_dtype"], shape=[parameters["num_elements"]] + parameters["element_shape"]) tensor_list = list_ops.tensor_list_from_tensor( data, parameters["element_shape"]) out = list_ops.tensor_list_length(tensor_list) return [data], [out]
def testListFromTensor(self): with self.cached_session(), self.test_scope(): t = constant_op.constant([1.0, 2.0]) l = list_ops.tensor_list_from_tensor(t, element_shape=[]) e = list_ops.tensor_list_get_item(l, 0, element_dtype=dtypes.float32) self.assertAllEqual(e, 1.0) l, e0 = list_ops.tensor_list_pop_back(l, element_dtype=dtypes.float32) self.assertAllEqual(e0, 2.0) l, e1 = list_ops.tensor_list_pop_back(l, element_dtype=dtypes.float32) self.assertAllEqual(e1, 1.0) self.assertAllEqual(list_ops.tensor_list_length(l), 0)
def testListFromTensor(self): with self.cached_session(), self.test_scope(): t = constant_op.constant([1.0, 2.0]) l = list_ops.tensor_list_from_tensor(t, element_shape=[]) e = list_ops.tensor_list_get_item(l, 0, element_dtype=dtypes.float32) self.assertAllEqual(e, 1.0) l, e0 = list_ops.tensor_list_pop_back(l, element_dtype=dtypes.float32) self.assertAllEqual(e0, 2.0) l, e1 = list_ops.tensor_list_pop_back(l, element_dtype=dtypes.float32) self.assertAllEqual(e1, 1.0) self.assertAllEqual(list_ops.tensor_list_length(l), 0)
def dynamic_len(list_or_tensor): """Implementation of len using dynamic dispatch.""" if _is_tensor_list(list_or_tensor): return list_ops.tensor_list_length(list_or_tensor) elif tensor_util.is_tensor(list_or_tensor): shape = list_or_tensor.shape if not shape.ndims: raise ValueError( 'len requires non-zero rank for tensor "%s"' % list_or_tensor) return array_ops.shape(list_or_tensor)[0] return len(list_or_tensor)
def testUnevenSplit(self): l = list_ops.tensor_list_split([1., 2., 3., 4., 5], element_shape=None, lengths=[3, 2]) self.assertAllEqual(list_ops.tensor_list_length(l), 2) self.assertAllEqual( list_ops.tensor_list_get_item(l, 0, element_dtype=dtypes.float32), [1., 2., 3.]) self.assertAllEqual( list_ops.tensor_list_get_item(l, 1, element_dtype=dtypes.float32), [4., 5.])
def _tf_tensor_list_append(list_, x): """Overload of list_append that stages a Tensor list write.""" def empty_list_of_elements_like_x(): tensor_x = ops.convert_to_tensor(x) return list_ops.empty_tensor_list( element_shape=array_ops.shape(tensor_x), element_dtype=tensor_x.dtype) list_ = control_flow_ops.cond( list_ops.tensor_list_length(list_) > 0, lambda: list_, empty_list_of_elements_like_x, ) return list_ops.tensor_list_push_back(list_, x)
def body(list_, m): list_ = control_flow_ops.cond( math_ops.equal(list_ops.tensor_list_length(list_), 0), lambda: list_ops.empty_tensor_list(m.shape, m.dtype), lambda: list_) list_ = list_ops.tensor_list_push_back(list_, m) return list_, m
def count(self): return list_ops.tensor_list_length(self.list_)
def _tf_tensor_list_len(s): return list_ops.tensor_list_length(s)
def _tf_tensor_list_len(s): return list_ops.tensor_list_length(s)
def size(self, name=None): """See TensorArray.""" return list_ops.tensor_list_length(input_handle=self._flow, name=name)
def size(self, name=None): """See TensorArray.""" return list_ops.tensor_list_length(input_handle=self._flow, name=name)
def count(self): return list_ops.tensor_list_length(self.list_)