1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""This module defines tensor utilities not found in TensorFlow.
16
17The reason these utilities are not defined in TensorFlow is because they may
18not be not fully robust, although they work in the vast majority of cases. So
19we define them here in order for their behavior to be consistently verified.
20"""
21
22from __future__ import absolute_import
23from __future__ import division
24from __future__ import print_function
25
26from tensorflow.python.framework import dtypes
27from tensorflow.python.framework import sparse_tensor
28from tensorflow.python.framework import tensor_util
29from tensorflow.python.ops import tensor_array_ops
30
31
32def is_dense_tensor(t):
33  # TODO(mdan): Resolve this inconsistency.
34  return (tensor_util.is_tf_type(t) and
35          not isinstance(t, sparse_tensor.SparseTensor))
36
37
38def is_tensor_array(t):
39  return isinstance(t, tensor_array_ops.TensorArray)
40
41
42def is_tensor_list(t):
43  # TODO(mdan): This is just a heuristic.
44  # With TF lacking support for templated types, this is unfortunately the
45  # closest we can get right now. A dedicated op ought to be possible to
46  # construct.
47  return (tensor_util.is_tf_type(t) and t.dtype == dtypes.variant and
48          not t.shape.ndims)
49
50
51def is_range_tensor(t):
52  """Returns True if a tensor is the result of a tf.range op. Best effort."""
53  return tensor_util.is_tf_type(t) and hasattr(t, 'op') and t.op.type == 'Range'
54