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, dtype_hint=None, name=None)
| 1186 | |
| 1187 | @tf_export("convert_to_tensor", v1=[]) |
| 1188 | def convert_to_tensor_v2(value, dtype=None, dtype_hint=None, name=None): |
| 1189 | """Converts the given `value` to a `Tensor`. |
| 1190 | |
| 1191 | This function converts Python objects of various types to `Tensor` |
| 1192 | objects. It accepts `Tensor` objects, numpy arrays, Python lists, |
| 1193 | and Python scalars. For example: |
| 1194 | |
| 1195 | ```python |
| 1196 | import numpy as np |
| 1197 | |
| 1198 | def my_func(arg): |
| 1199 | arg = tf.convert_to_tensor(arg, dtype=tf.float32) |
| 1200 | return tf.matmul(arg, arg) + arg |
| 1201 | |
| 1202 | # The following calls are equivalent. |
| 1203 | value_1 = my_func(tf.constant([[1.0, 2.0], [3.0, 4.0]])) |
| 1204 | value_2 = my_func([[1.0, 2.0], [3.0, 4.0]]) |
| 1205 | value_3 = my_func(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)) |
| 1206 | ``` |
| 1207 | |
| 1208 | This function can be useful when composing a new operation in Python |
| 1209 | (such as `my_func` in the example above). All standard Python op |
| 1210 | constructors apply this function to each of their Tensor-valued |
| 1211 | inputs, which allows those ops to accept numpy arrays, Python lists, |
| 1212 | and scalars in addition to `Tensor` objects. |
| 1213 | |
| 1214 | Note: This function diverges from default Numpy behavior for `float` and |
| 1215 | `string` types when `None` is present in a Python list or scalar. Rather |
| 1216 | than silently converting `None` values, an error will be thrown. |
| 1217 | |
| 1218 | Args: |
| 1219 | value: An object whose type has a registered `Tensor` conversion function. |
| 1220 | dtype: Optional element type for the returned tensor. If missing, the type |
| 1221 | is inferred from the type of `value`. |
| 1222 | dtype_hint: Optional element type for the returned tensor, used when dtype |
| 1223 | is None. In some cases, a caller may not have a dtype in mind when |
| 1224 | converting to a tensor, so dtype_hint can be used as a soft preference. |
| 1225 | If the conversion to `dtype_hint` is not possible, this argument has no |
| 1226 | effect. |
| 1227 | name: Optional name to use if a new `Tensor` is created. |
| 1228 | |
| 1229 | Returns: |
| 1230 | A `Tensor` based on `value`. |
| 1231 | |
| 1232 | Raises: |
| 1233 | TypeError: If no conversion function is registered for `value` to `dtype`. |
| 1234 | RuntimeError: If a registered conversion function returns an invalid value. |
| 1235 | ValueError: If the `value` is a tensor not of given `dtype` in graph mode. |
| 1236 | """ |
| 1237 | return internal_convert_to_tensor( |
| 1238 | value=value, |
| 1239 | dtype=dtype, |
| 1240 | name=name, |
| 1241 | preferred_dtype=dtype_hint, |
| 1242 | as_ref=False) |
| 1243 | |
| 1244 | |
| 1245 | def _error_prefix(name): |
no test coverage detected