Converts the given `value` to a `Tensor`. This function converts Python objects of various types to `Tensor` objects. It accepts `Tensor` objects, numpy arrays, Python lists, and Python scalars. For example: ```python import numpy as np def my_func(arg): arg = tf.convert_to_tensor
(value,
dtype=None,
name=None,
preferred_dtype=None,
dtype_hint=None)
| 1126 | |
| 1127 | @tf_export(v1=["convert_to_tensor"]) |
| 1128 | def convert_to_tensor(value, |
| 1129 | dtype=None, |
| 1130 | name=None, |
| 1131 | preferred_dtype=None, |
| 1132 | dtype_hint=None): |
| 1133 | """Converts the given `value` to a `Tensor`. |
| 1134 | |
| 1135 | This function converts Python objects of various types to `Tensor` |
| 1136 | objects. It accepts `Tensor` objects, numpy arrays, Python lists, |
| 1137 | and Python scalars. For example: |
| 1138 | |
| 1139 | ```python |
| 1140 | import numpy as np |
| 1141 | |
| 1142 | def my_func(arg): |
| 1143 | arg = tf.convert_to_tensor(arg, dtype=tf.float32) |
| 1144 | return tf.matmul(arg, arg) + arg |
| 1145 | |
| 1146 | # The following calls are equivalent. |
| 1147 | value_1 = my_func(tf.constant([[1.0, 2.0], [3.0, 4.0]])) |
| 1148 | value_2 = my_func([[1.0, 2.0], [3.0, 4.0]]) |
| 1149 | value_3 = my_func(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)) |
| 1150 | ``` |
| 1151 | |
| 1152 | This function can be useful when composing a new operation in Python |
| 1153 | (such as `my_func` in the example above). All standard Python op |
| 1154 | constructors apply this function to each of their Tensor-valued |
| 1155 | inputs, which allows those ops to accept numpy arrays, Python lists, |
| 1156 | and scalars in addition to `Tensor` objects. |
| 1157 | |
| 1158 | Note: This function diverges from default Numpy behavior for `float` and |
| 1159 | `string` types when `None` is present in a Python list or scalar. Rather |
| 1160 | than silently converting `None` values, an error will be thrown. |
| 1161 | |
| 1162 | Args: |
| 1163 | value: An object whose type has a registered `Tensor` conversion function. |
| 1164 | dtype: Optional element type for the returned tensor. If missing, the type |
| 1165 | is inferred from the type of `value`. |
| 1166 | name: Optional name to use if a new `Tensor` is created. |
| 1167 | preferred_dtype: Optional element type for the returned tensor, used when |
| 1168 | dtype is None. In some cases, a caller may not have a dtype in mind when |
| 1169 | converting to a tensor, so preferred_dtype can be used as a soft |
| 1170 | preference. If the conversion to `preferred_dtype` is not possible, this |
| 1171 | argument has no effect. |
| 1172 | dtype_hint: same meaning as preferred_dtype, and overrides it. |
| 1173 | |
| 1174 | Returns: |
| 1175 | A `Tensor` based on `value`. |
| 1176 | |
| 1177 | Raises: |
| 1178 | TypeError: If no conversion function is registered for `value` to `dtype`. |
| 1179 | RuntimeError: If a registered conversion function returns an invalid value. |
| 1180 | ValueError: If the `value` is a tensor not of given `dtype` in graph mode. |
| 1181 | """ |
| 1182 | preferred_dtype = deprecation.deprecated_argument_lookup( |
| 1183 | "dtype_hint", dtype_hint, "preferred_dtype", preferred_dtype) |
| 1184 | return convert_to_tensor_v2(value, dtype, preferred_dtype, name) |
| 1185 |
nothing calls this directly
no test coverage detected