# The following calls are equivalent.value_1=my_func(tf.constant([[1.0,2.0],[3.0,4.0]]))print(value_1)tf.Tensor([[1.2.][3.4.]],shape=(2,2),dtype=float32)value_2=my_func([[1.0,2.0],[3.0,4.0]])print(value_2)tf.Tensor([[1.2.][3.4.]],shape=(2,2),dtype=float32)value_3=my_func(np.array([[1.0,2.0],[3.0,4.0]],dtype=np.float32))print(value_3)tf.Tensor([[1.2.][3.4.]],shape=(2,2),dtype=float32)
This function can be useful when composing a new operation in Python
(such as my_func in the example above). All standard Python op
constructors apply this function to each of their Tensor-valued
inputs, which allows those ops to accept numpy arrays, Python lists,
and scalars in addition to Tensor objects.
Args
value
An object whose type has a registered Tensor conversion function.
dtype
Optional element type for the returned tensor. If missing, the type
is inferred from the type of value.
dtype_hint
Optional element type for the returned tensor, used when dtype
is None. In some cases, a caller may not have a dtype in mind when
converting to a tensor, so dtype_hint can be used as a soft preference. If
the conversion to dtype_hint is not possible, this argument has no
effect.
name
Optional name to use if a new Tensor is created.
Returns
A Tensor based on value.
Raises
TypeError
If no conversion function is registered for value to dtype.
RuntimeError
If a registered conversion function returns an invalid value.
ValueError
If the value is a tensor not of given dtype in graph mode.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.convert_to_tensor\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/tensor_conversion.py#L96-L163) |\n\nConverts the given `value` to a `Tensor`. \n\n tf.convert_to_tensor(\n value, dtype=None, dtype_hint=None, name=None\n ) -\u003e ../tf/Tensor\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Logistic regression for binary classification with Core APIs](https://www.tensorflow.org/guide/core/logistic_regression_core) - [Extension types](https://www.tensorflow.org/guide/extension_type) - [Matrix approximation with Core APIs](https://www.tensorflow.org/guide/core/matrix_core) - [TensorFlow basics](https://www.tensorflow.org/guide/basics) - [Distributed training with Core APIs and DTensor](https://www.tensorflow.org/guide/core/distribution) | - [DeepDream](https://www.tensorflow.org/tutorials/generative/deepdream) - [Custom training: walkthrough](https://www.tensorflow.org/tutorials/customization/custom_training_walkthrough) - [Distributed training with DTensors](https://www.tensorflow.org/tutorials/distribute/dtensor_ml_tutorial) - [Load a pandas DataFrame](https://www.tensorflow.org/tutorials/load_data/pandas_dataframe) - [Scalable model compression](https://www.tensorflow.org/tutorials/optimization/compression) |\n\nThis function converts Python objects of various types to `Tensor`\nobjects. It accepts `Tensor` objects, numpy arrays, Python lists,\nand Python scalars.\n\n#### For example:\n\n import numpy as np\n def my_func(arg):\n arg = tf.convert_to_tensor(arg, dtype=tf.float32)\n return arg\n\n # The following calls are equivalent.\n\n value_1 = my_func(tf.constant([[1.0, 2.0], [3.0, 4.0]]))\n print(value_1)\n tf.Tensor(\n [[1. 2.]\n [3. 4.]], shape=(2, 2), dtype=float32)\n value_2 = my_func([[1.0, 2.0], [3.0, 4.0]])\n print(value_2)\n tf.Tensor(\n [[1. 2.]\n [3. 4.]], shape=(2, 2), dtype=float32)\n value_3 = my_func(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32))\n print(value_3)\n tf.Tensor(\n [[1. 2.]\n [3. 4.]], shape=(2, 2), dtype=float32)\n\nThis function can be useful when composing a new operation in Python\n(such as `my_func` in the example above). All standard Python op\nconstructors apply this function to each of their Tensor-valued\ninputs, which allows those ops to accept numpy arrays, Python lists,\nand scalars in addition to `Tensor` objects.\n| **Note:** This function diverges from default Numpy behavior for `float` and `string` types when `None` is present in a Python list or scalar. Rather than silently converting `None` values, an error will be thrown.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `value` | An object whose type has a registered `Tensor` conversion function. |\n| `dtype` | Optional element type for the returned tensor. If missing, the type is inferred from the type of `value`. |\n| `dtype_hint` | Optional element type for the returned tensor, used when dtype is None. In some cases, a caller may not have a dtype in mind when converting to a tensor, so dtype_hint can be used as a soft preference. If the conversion to `dtype_hint` is not possible, this argument has no effect. |\n| `name` | Optional name to use if a new `Tensor` is created. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` based on `value`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|----------------|-----------------------------------------------------------------|\n| `TypeError` | If no conversion function is registered for `value` to `dtype`. |\n| `RuntimeError` | If a registered conversion function returns an invalid value. |\n| `ValueError` | If the `value` is a tensor not of given `dtype` in graph mode. |\n\n\u003cbr /\u003e"]]