本文整理汇总了Python中tensorflow.compat.v2.convert_to_tensor方法的典型用法代码示例。如果您正苦于以下问题:Python v2.convert_to_tensor方法的具体用法?Python v2.convert_to_tensor怎么用?Python v2.convert_to_tensor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v2
的用法示例。
在下文中一共展示了v2.convert_to_tensor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_conjugate_preset
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def test_conjugate_preset(self):
"""Tests if the conjugate function is providing correct results."""
x_init = test_helpers.generate_preset_test_dual_quaternions()
x = tf.convert_to_tensor(value=x_init)
y = tf.convert_to_tensor(value=x_init)
x = dual_quaternion.conjugate(x)
x_real, x_dual = tf.split(x, (4, 4), axis=-1)
y_real, y_dual = tf.split(y, (4, 4), axis=-1)
xyz_y_real, w_y_real = tf.split(y_real, (3, 1), axis=-1)
xyz_y_dual, w_y_dual = tf.split(y_dual, (3, 1), axis=-1)
y_real = tf.concat((-xyz_y_real, w_y_real), axis=-1)
y_dual = tf.concat((-xyz_y_dual, w_y_dual), axis=-1)
self.assertAllEqual(x_real, y_real)
self.assertAllEqual(x_dual, y_dual)
示例2: generate_preset_test_dual_quaternions
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def generate_preset_test_dual_quaternions():
"""Generates pre-set test quaternions."""
angles = generate_preset_test_euler_angles()
preset_quaternion_real = quaternion.from_euler(angles)
translations = generate_preset_test_translations()
translations = np.concatenate(
(translations / 2.0, np.zeros((np.ma.size(translations, 0), 1))), axis=1)
preset_quaternion_translation = tf.convert_to_tensor(value=translations)
preset_quaternion_dual = quaternion.multiply(preset_quaternion_translation,
preset_quaternion_real)
preset_dual_quaternion = tf.concat(
(preset_quaternion_real, preset_quaternion_dual), axis=-1)
return preset_dual_quaternion
示例3: generate_random_test_dual_quaternions
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def generate_random_test_dual_quaternions():
"""Generates random test dual quaternions."""
angles = generate_random_test_euler_angles()
random_quaternion_real = quaternion.from_euler(angles)
min_translation = -3.0
max_translation = 3.0
translations = np.random.uniform(min_translation, max_translation,
angles.shape)
translations_quaternion_shape = np.asarray(translations.shape)
translations_quaternion_shape[-1] = 1
translations = np.concatenate(
(translations / 2.0, np.zeros(translations_quaternion_shape)), axis=-1)
random_quaternion_translation = tf.convert_to_tensor(value=translations)
random_quaternion_dual = quaternion.multiply(random_quaternion_translation,
random_quaternion_real)
random_dual_quaternion = tf.concat(
(random_quaternion_real, random_quaternion_dual), axis=-1)
return random_dual_quaternion
示例4: testJitNoUnnecessaryTracing
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def testJitNoUnnecessaryTracing(self):
def num_traces(f):
return len(f.tf_function._list_all_concrete_functions_for_serialization())
def check_trace_only_once(arg1, arg2):
@extensions.jit
def f(a):
return a + 1
self.assertAllEqual(0, num_traces(f))
f(arg1)
self.assertAllEqual(1, num_traces(f))
f(arg2)
self.assertAllEqual(1, num_traces(f))
check_trace_only_once(1, 2)
check_trace_only_once(1.1, 2.1)
check_trace_only_once(tf_np.asarray(1), tf_np.asarray(2))
check_trace_only_once(
tf.convert_to_tensor(value=1), tf.convert_to_tensor(value=2))
示例5: testEvalOnShapesNoUnnecessaryTracing
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def testEvalOnShapesNoUnnecessaryTracing(self):
def num_traces(f):
return len(
f._tf_function._list_all_concrete_functions_for_serialization())
def check_trace_only_once(arg1, arg2):
@extensions.eval_on_shapes
def f(a):
return a + 1
self.assertAllEqual(0, num_traces(f))
f(arg1)
self.assertAllEqual(1, num_traces(f))
f(arg2)
self.assertAllEqual(1, num_traces(f))
check_trace_only_once(1, 2)
check_trace_only_once(1.1, 2.1)
check_trace_only_once(tf_np.asarray(1), tf_np.asarray(2))
check_trace_only_once(
tf.convert_to_tensor(value=1), tf.convert_to_tensor(value=2))
示例6: zeros_like
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def zeros_like(a, dtype=None):
"""Returns an array of zeros with the shape and type of the input array.
Args:
a: array_like. Could be an ndarray, a Tensor or any object that can be
converted to a Tensor using `tf.convert_to_tensor`.
dtype: Optional, defaults to dtype of the input array. The type of the
resulting ndarray. Could be a python type, a NumPy type or a TensorFlow
`DType`.
Returns:
An ndarray.
"""
if isinstance(a, arrays_lib.ndarray):
a = a.data
if dtype is None:
# We need to let utils.result_type decide the dtype, not tf.zeros_like
dtype = utils.result_type(a)
else:
# TF and numpy has different interpretations of Python types such as
# `float`, so we let `utils.result_type` decide.
dtype = utils.result_type(dtype)
dtype = tf.as_dtype(dtype) # Work around b/149877262
return arrays_lib.tensor_to_ndarray(tf.zeros_like(a, dtype))
示例7: ones_like
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def ones_like(a, dtype=None):
"""Returns an array of ones with the shape and type of the input array.
Args:
a: array_like. Could be an ndarray, a Tensor or any object that can be
converted to a Tensor using `tf.convert_to_tensor`.
dtype: Optional, defaults to dtype of the input array. The type of the
resulting ndarray. Could be a python type, a NumPy type or a TensorFlow
`DType`.
Returns:
An ndarray.
"""
if isinstance(a, arrays_lib.ndarray):
a = a.data
if dtype is None:
dtype = utils.result_type(a)
else:
dtype = utils.result_type(dtype)
return arrays_lib.tensor_to_ndarray(tf.ones_like(a, dtype))
示例8: diagflat
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def diagflat(v, k=0):
"""Returns a 2-d array with flattened `v` as diagonal.
Args:
v: array_like of any rank. Gets flattened when setting as diagonal. Could be
an ndarray, a Tensor or any object that can be converted to a Tensor using
`tf.convert_to_tensor`.
k: Position of the diagonal. Defaults to 0, the main diagonal. Positive
values refer to diagonals shifted right, negative values refer to
diagonals shifted left.
Returns:
2-d ndarray.
"""
v = asarray(v)
return diag(tf.reshape(v.data, [-1]), k)
示例9: any
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def any(a, axis=None, keepdims=None): # pylint: disable=redefined-builtin
"""Whether any element in the entire array or in an axis evaluates to true.
Casts the array to bool type if it is not already and uses `tf.reduce_any` to
compute the result.
Args:
a: array_like. Could be an ndarray, a Tensor or any object that can
be converted to a Tensor using `tf.convert_to_tensor`.
axis: Optional. Could be an int or a tuple of integers. If not specified,
the reduction is performed over all array indices.
keepdims: If true, retains reduced dimensions with length 1.
Returns:
An ndarray. Note that unlike NumPy this does not return a scalar bool if
`axis` is None.
"""
a = asarray(a, dtype=bool)
return utils.tensor_to_ndarray(
tf.reduce_any(input_tensor=a.data, axis=axis, keepdims=keepdims))
示例10: imag
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def imag(a):
"""Returns imaginary parts of all elements in `a`.
Uses `tf.imag`.
Args:
a: array_like. Could be an ndarray, a Tensor or any object that can
be converted to a Tensor using `tf.convert_to_tensor`.
Returns:
An ndarray with the same shape as `a`.
"""
a = asarray(a)
# TODO(srbs): np.imag returns a scalar if a is a scalar, whereas we always
# return an ndarray.
return utils.tensor_to_ndarray(tf.math.imag(a.data))
示例11: real
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def real(val):
"""Returns real parts of all elements in `a`.
Uses `tf.real`.
Args:
val: array_like. Could be an ndarray, a Tensor or any object that can
be converted to a Tensor using `tf.convert_to_tensor`.
Returns:
An ndarray with the same shape as `a`.
"""
val = asarray(val)
# TODO(srbs): np.real returns a scalar if val is a scalar, whereas we always
# return an ndarray.
return utils.tensor_to_ndarray(tf.math.real(val.data))
示例12: transpose
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def transpose(a, axes=None):
"""Permutes dimensions of the array.
Args:
a: array_like. Could be an ndarray, a Tensor or any object that can
be converted to a Tensor using `tf.convert_to_tensor`.
axes: array_like. A list of ints with length rank(a) or None specifying the
order of permutation. The i'th dimension of the output array corresponds
to axes[i]'th dimension of the `a`. If None, the axes are reversed.
Returns:
An ndarray.
"""
a = asarray(a)
if axes is not None:
axes = asarray(axes)
return utils.tensor_to_ndarray(tf.transpose(a=a.data, perm=axes))
示例13: _scalar
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def _scalar(tf_fn, x, promote_to_float=False):
"""Computes the tf_fn(x) for each element in `x`.
Args:
tf_fn: function that takes a single Tensor argument.
x: array_like. Could be an ndarray, a Tensor or any object that can
be converted to a Tensor using `tf.convert_to_tensor`.
promote_to_float: whether to cast the argument to a float dtype
(`dtypes.default_float_type`) if it is not already.
Returns:
An ndarray with the same shape as `x`. The default output dtype is
determined by `dtypes.default_float_type`, unless x is an ndarray with a
floating point type, in which case the output type is same as x.dtype.
"""
x = array_ops.asarray(x)
if promote_to_float and not np.issubdtype(x.dtype, np.inexact):
x = x.astype(dtypes.default_float_type())
return utils.tensor_to_ndarray(tf_fn(x.data))
示例14: _testBinOp
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def _testBinOp(self, a, b, out, f, types=None):
a = t2a(tf.convert_to_tensor(value=a, dtype=np.int32))
b = t2a(tf.convert_to_tensor(value=b, dtype=np.int32))
if not isinstance(out, arrays.ndarray):
out = t2a(tf.convert_to_tensor(value=out, dtype=np.int32))
if types is None:
types = [[np.int32, np.int32, np.int32],
[np.int64, np.int32, np.int64],
[np.int32, np.int64, np.int64],
[np.float32, np.int32, np.float64],
[np.int32, np.float32, np.float64],
[np.float32, np.float32, np.float32],
[np.float64, np.float32, np.float64],
[np.float32, np.float64, np.float64]]
for a_type, b_type, out_type in types:
o = f(a.astype(a_type), b.astype(b_type))
self.assertIs(o.dtype.type, out_type)
self.assertAllEqual(out.astype(out_type), o)
示例15: setUp
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import convert_to_tensor [as 别名]
def setUp(self):
super(LogicTest, self).setUp()
self.array_transforms = [
lambda x: x, # Identity,
tf.convert_to_tensor,
np.array,
lambda x: np.array(x, dtype=np.int32),
lambda x: np.array(x, dtype=np.int64),
lambda x: np.array(x, dtype=np.float32),
lambda x: np.array(x, dtype=np.float64),
array_ops.array,
lambda x: array_ops.array(x, dtype=tf.int32),
lambda x: array_ops.array(x, dtype=tf.int64),
lambda x: array_ops.array(x, dtype=tf.float32),
lambda x: array_ops.array(x, dtype=tf.float64),
]