<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/ops/core.py#L1019-L1049) |

Convert a NumPy array or Python array to a tensor.

    tf.keras.ops.convert_to_tensor(
        x, dtype=None, sparse=None, ragged=None
    )

Native tensors for the current backend or left unchanged unless the `dtype`,
`sparse` or `ragged` arguments are set.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `x` | A NumPy array, Python array (can be nested) or a backend tensor. |
| `dtype` | The target type. If `None`, the type of `x` is used. |
| `sparse` | Whether to keep sparse tensors. `False` will cause sparse tensors to be densified. The default value of `None` means that sparse tensors are kept only if the backend supports them. |
| `ragged` | Whether to keep ragged tensors. `False` will cause ragged tensors to be densified. The default value of `None` means that ragged tensors are kept only if the backend supports them. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A backend tensor of the specified `dtype` and sparseness. ||

<br />

#### Example:

    x = np.array([1, 2, 3])
    y = keras.ops.convert_to_tensor(x)