Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

tf.convert_to_tensor

Converts supported Python and array-like values into a Tensor. The element type is inferred unless dtype is supplied, while dtype_hint provides a fallback preference.

How it works

The function uses TensorFlow's registered conversion for the supplied value and returns a Tensor. It infers the element type unless dtype is provided; dtype_hint supplies a fallback preference only when dtype is omitted and conversion to that type is possible.

Syntax

tf.convert_to_tensor(value, dtype=None, dtype_hint=None, name=None) -> tf.Tensor

Parameters

NameTypeDescription
valueAnyAn object with a registered Tensor conversion function.
dtypetf.DTypeOptional element type for the returned Tensor; if omitted, it is inferred from value.
dtype_hinttf.DTypeOptional element type used as a soft preference when dtype is None.
namestringOptional name used if a new Tensor is created.

Returns

A Tensor based on value.

Key points

  • The function turns a supported tensor-like value into a Tensor.
  • Common accepted inputs include existing Tensors, NumPy arrays, and Python lists.
  • Python scalar values are also valid inputs.
  • Without dtype, TensorFlow determines the element type from the input.
  • name labels a newly created Tensor when a name is supplied.
  • TensorFlow operation constructors use this conversion for tensor-valued inputs, enabling tensor-like arguments.

Common mistakes

  • dtype_hint is only a soft preference: TensorFlow ignores it when conversion to that type cannot be performed.
  • A Tensor with a different dtype can raise ValueError in graph mode when an explicit dtype is requested.
  • Values without a registered conversion function cause TypeError.

Example: Convert a list to a Tensor

import tensorflow as tf

values = tf.convert_to_tensor([1, 2, 3], dtype=tf.float32)
print(values)

Converts a Python list into a float32 Tensor that can be passed to TensorFlow operations.

When to use it

Normalize operation inputs

A custom Python operation receives lists, scalars, or NumPy arrays and needs a consistent Tensor input.

Set the input dtype

Data must enter a TensorFlow computation with a specified element type.

Support tensor-like arguments

An operation should accept tensor-like values without requiring callers to construct Tensors first.

Interview questions

QWhat does tf.convert_to_tensor do?

It converts supported Python values, NumPy arrays, lists, scalars, and existing Tensor objects into a Tensor. TensorFlow operations use this behavior so many operations can accept tensor-like inputs.

Follow-up: What happens when the input type is unsupported? TensorFlow raises TypeError if no conversion function is registered for the value and requested dtype.

QHow are dtype and dtype_hint different?

dtype explicitly specifies the returned tensor's element type. If dtype is absent, TensorFlow infers the type from value.

Follow-up: How does dtype_hint differ? It is used only when dtype is None and acts as a soft preference rather than a required type.

QWhat does the function return, and what does name control?

The function returns a Tensor based on value. The name argument can provide a name when TensorFlow creates a new Tensor.

Follow-up: What other failures can occur? A registered converter returning an invalid result raises RuntimeError, while an incompatible tensor dtype can raise ValueError in graph mode.

  • tf.cast
  • tf.constant
  • tf.Tensor
  • tf.convert_to_tensor_v2

Source: tf.convert_to_tensor | TensorFlow v2.16.1. Checked 2026-10-15.