# Conversion of Other Values to Tensor Values

When we are training a model, there are some values that need to be converted into Tensor Values for us to use them. May it be Python values, list, NumPy arrays etc.

We use ‘tf.convert_to_tensor’ to convert these value.

#### Syntax for the above function is…

tf.convert_to_tensor(value, dtype=None, name=None, preferred_dtype=None)

It accepts `Tensor`

objects, Python lists, NumPy arrays and Python scalars.

When we are composing a new operation in Python, this function can be useful. This function allows the operations to accept Lists, Scalars etc in addition to our Tensor Objects, this becomes a reason why some of the standard Python operative constructors apply this function to their tensor valued inputs.

Let’s look at one Python code example to understand this better.

import numpy as np array = [9,8,7,6,5,4,3] x = np.asarray(array, np.float32) y = tf.convert_to_tensor(x, np.float32) session = tf.InteractiveSession() session.run()

As we can see in the above example, we have NumPy array as our data. Using ‘tf.convert_to_tensor’ we are further converting those data values as our tensor values

The above code will return an output based on the provided value.

The arguments for the given function:

: A constant value or list. This value is the value we want for our tensor that is passed to the function.**value**: It is the data type of the passed value. dtype is given to the specify the datatype of the value which is passed to the function. If dtype is not mentioned, then it concludes the type from value, by default.**dtype**: Another optional attribute which assigns name for the tensor, which will contain the value.**name**: Optional data type for the returned tensor and is used when dtype is None. Sometimes a developer can not think of any data type at the moment of converting the value to tensor, so preferred_dtype can be used as a preference. This argument has no effect if the conversion is not done.**preferred_dtype**

You may want to check the below Article:

*Assigning a value to a TensorFlow variable in Python*

## Leave a Reply