• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Helpers constructing Datasets."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20from tensorflow.python.framework import constant_op
21from tensorflow.python.framework import dtypes
22from tensorflow.python.framework import ops
23from tensorflow.python.framework import tensor_shape
24
25
26def optional_param_to_tensor(argument_name,
27                             argument_value,
28                             argument_default=0,
29                             argument_dtype=dtypes.int64):
30  if argument_value is not None:
31    return ops.convert_to_tensor(
32        argument_value, dtype=argument_dtype, name=argument_name)
33  else:
34    return constant_op.constant(
35        argument_default, dtype=argument_dtype, name=argument_name)
36
37
38def partial_shape_to_tensor(shape_like):
39  """Returns a `tf.Tensor` that represents the given shape.
40
41  Args:
42    shape_like: A value that can be converted to a `tf.TensorShape` or a
43      `tf.Tensor`.
44
45  Returns:
46    A 1-D `tf.Tensor` of `tf.int64` elements representing the given shape, where
47    `-1` is substituted for any unknown dimensions.
48  """
49  try:
50    # First attempt to convert the input to a shape, and return the
51    # "canonical" tensor representation, which uses `-1` in place of
52    # `None`.
53    shape_like = tensor_shape.as_shape(shape_like)
54    return ops.convert_to_tensor(
55        [dim if dim is not None else -1 for dim in shape_like.as_list()],
56        dtype=dtypes.int64)
57  except (TypeError, ValueError):
58    # The argument was not trivially convertible to a
59    # `tf.TensorShape`, so fall back on the conversion to tensor
60    # machinery.
61    ret = ops.convert_to_tensor(shape_like, preferred_dtype=dtypes.int64)
62    if ret.shape.dims is not None and len(ret.shape.dims) != 1:
63      raise ValueError("The given shape %s must be a 1-D tensor of tf.int64 "
64                       "values, but the shape was %s."
65                       % (shape_like, ret.shape))
66    if ret.dtype != dtypes.int64:
67      raise TypeError("The given shape %s must be a 1-D tensor of tf.int64 "
68                      "values, but the element type was %s."
69                      % (shape_like, ret.dtype.name))
70
71    return ret
72
73