Converts the given value
to a Tensor
.
Aliases:
tf.compat.v2.convert_to_tensor
tf.convert_to_tensor(
value,
dtype=None,
dtype_hint=None,
name=None
)
Used in the guide:
E
ag
er
e
xe
cu
ti
on
Used in the tutorials:
A
ut
om
at
ic
d
if
fe
re
nt
ia
ti
on
a
nd
g
ra
di
en
tt
a
pe
C
us
to
mt
r
ai
ni
ng
:w
a
lk
th
ro
ug
h``D
ee
pD
re
am
N
eu
ra
lm
a
ch
in
et
r
an
sl
at
io
nw
i
th
a
tt
en
ti
on
This function converts Python objects of various types toTensor
objects. It acceptsTensor
objects, numpy arrays, Python lists, and Python scalars. For example:
import numpy as np
def my_func(arg):
arg = tf.convert_to_tensor(arg, dtype=tf.float32)
return tf.matmul(arg, arg) + arg
# The following calls are equivalent.
value_1 = my_func(tf.constant([[1.0, 2.0], [3.0, 4.0]]))
value_2 = my_func([[1.0, 2.0], [3.0, 4.0]])
value_3 = my_func(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.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 registeredTensor
conversion function.dtype
: Optional element type for the returned tensor. If missing, the type is inferred from the type ofvalue
.dtype
_hint: Optional element type for the returned tensor, used whendtype
is None. In some cases, a caller may not have adtype
in mind when converting to a tensor, sodtype
_hint can be used as a soft preference. If the conversion todtype
_hint is not possible, this argument has no effect.name
: Optionalname
to use if a newTensor
is created.
Returns:
A Tensor
based on value
.
Raises:
TypeError
: If no conversion function is registered forvalue
todtype
.RuntimeError
: If a registered conversion function returns an invalidvalue
.ValueError
: If thevalue
is a tensor not of givendtype
in graph mode.