Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/ops/numpy_ops/np_arrays.py: 58%
12 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2020 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"""ndarray class."""
17# pylint: disable=g-direct-tensorflow-import
19from tensorflow.python.framework import dtypes
20from tensorflow.python.framework import ops
21from tensorflow.python.framework import tensor_conversion
22from tensorflow.python.ops.numpy_ops import np_dtypes
25def convert_to_tensor(value, dtype=None, dtype_hint=None):
26 """Wrapper over `tf.convert_to_tensor`.
28 Args:
29 value: value to convert
30 dtype: (optional) the type we would like it to be converted to.
31 dtype_hint: (optional) soft preference for the type we would like it to be
32 converted to. `tf.convert_to_tensor` will attempt to convert value to this
33 type first, but will not fail if conversion is not possible falling back
34 to inferring the type instead.
36 Returns:
37 Value converted to tf.Tensor.
38 """
39 # A safer version of `tf.convert_to_tensor` to work around b/149876037.
40 # TODO(wangpeng): Remove this function once the bug is fixed.
41 if (dtype is None and isinstance(value, int) and
42 value >= 2**63):
43 dtype = dtypes.uint64
44 elif dtype is None and dtype_hint is None and isinstance(value, float):
45 dtype = np_dtypes.default_float_type()
46 return tensor_conversion.convert_to_tensor_v2_with_dispatch(
47 value, dtype=dtype, dtype_hint=dtype_hint)
50ndarray = ops.Tensor