本文整理汇总了Python中tensorflow.acos方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.acos方法的具体用法?Python tensorflow.acos怎么用?Python tensorflow.acos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.acos方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: angle_error
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def angle_error(gt, pred):
"""
Average angular error computed by cosine difference
:param gt: list of ground truth label
:param pred: list of predicted label
:return: Average angular error
"""
vec_gt = angles2vector(gt)
vec_pred = angles2vector(pred)
x = K.np.multiply(vec_gt[:, 0], vec_pred[:, 0])
y = K.np.multiply(vec_gt[:, 1], vec_pred[:, 1])
z = K.np.multiply(vec_gt[:, 2], vec_pred[:, 2])
dif = K.np.sum([x, y, z], axis=0) / (tf.norm(vec_gt, axis=1) * tf.norm(vec_pred, axis=1))
clipped_dif = K.clip(dif, np.float(-1.0), np.float(1.0))
loss = (tf.acos(clipped_dif) * 180) / np.pi
return K.mean(loss, axis=-1)
示例2: ypr_from_campos
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def ypr_from_campos(cx, cy, cz):
camDist = math.sqrt(cx * cx + cy * cy + cz * cz)
cx = cx / camDist
cy = cy / camDist
cz = cz / camDist
t = math.sqrt(cx * cx + cy * cy)
tx = cx / t
ty = cy / t
yaw = math.acos(tx)
if ty > 0:
yaw = 2 * math.pi - yaw
roll = 0
pitch = math.asin(cz)
return yaw, pitch, roll
示例3: compare_euler_angle_error
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def compare_euler_angle_error(self, net_roll,net_pitch, tar_roll, tar_pitch):
cx1 = tf.cos(net_roll)
sx1 = tf.sin(net_roll)
cy1 = tf.cos(net_pitch)
sy1 = tf.sin(net_pitch)
cx2 = tf.cos(tar_roll)
sx2 = tf.sin(tar_roll)
cy2 = tf.cos(tar_pitch)
sy2 = tf.sin(tar_pitch)
m00 = cy1*cy2 + sy1*sy2
m11 = cx1*cx2 + cy1*cy2*sx1*sx2 + sx1*sx2*sy1*sy2
m22 = sx1*sx2 + cx1*cx2*sy1*sy2 + cx1*cx2*cy1*cy2
self.acos = ( m00 + m11 + m22 - 1)/2.0 * 0.99999
return tf.acos(self.acos)
示例4: call
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def call(self, inputs):
x, y = inputs
c = K.shape(x)[-1]
# normalize feature
x = tf.nn.l2_normalize(x, axis=1)
# normalize weights
W = tf.nn.l2_normalize(self.W, axis=0)
# dot product
logits = x @ W
# add margin
# clip logits to prevent zero division when backward
theta = tf.acos(K.clip(logits, -1.0 + K.epsilon(), 1.0 - K.epsilon()))
target_logits = tf.cos(theta + self.m)
# sin = tf.sqrt(1 - logits**2)
# cos_m = tf.cos(logits)
# sin_m = tf.sin(logits)
# target_logits = logits * cos_m - sin * sin_m
#
logits = logits * (1 - y) + target_logits * y
# feature re-scale
logits *= self.s
out = tf.nn.softmax(logits)
return out
示例5: ExactQabArcCos
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def ExactQabArcCos(self, var_aa, corr_ab):
"""Exact integration result from Cho & Saul (2009).
Specifically:
qaa = 0.5*qaa
qab = (qaa/2*pi)*(sin angle + (pi-angle)*cos angle),
where cos angle = corr_ab.
Args:
var_aa: 1d tensor of variance grid points.
corr_ab: 1d tensor of correlation grid points.
Returns:
qab_exact: tensor, exact covariance matrix.
"""
angle = tf.acos(corr_ab)
jtheta = tf.sin(angle) + (np.pi - angle) * tf.cos(angle)
term1 = tf.tile(tf.expand_dims(var_aa, 1), [1, corr_ab.shape[0]])
term2 = tf.tile(tf.expand_dims(jtheta, 0), [var_aa.shape[0], 1])
qab_exact = (1 / (2 * np.pi)) * term1 * term2
return qab_exact
示例6: arcface_loss
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def arcface_loss(x, normx_cos, labels, m1, m2, m3, s):
norm_x = tf.norm(x, axis=1, keepdims=True)
cos_theta = normx_cos / norm_x
theta = tf.acos(cos_theta)
mask = tf.one_hot(labels, depth=normx_cos.shape[-1])
zeros = tf.zeros_like(mask)
cond = tf.where(tf.greater(theta * m1 + m3, math.pi), zeros, mask)
cond = tf.cast(cond, dtype=tf.bool)
m1_theta_plus_m3 = tf.where(cond, theta * m1 + m3, theta)
cos_m1_theta_plus_m3 = tf.cos(m1_theta_plus_m3)
prelogits = tf.where(cond, cos_m1_theta_plus_m3 - m2, cos_m1_theta_plus_m3) * s
cce = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) # do softmax
loss = cce(labels, prelogits)
return loss
示例7: test_forward_unary
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def test_forward_unary():
def _test_forward_unary(op, a_min=1, a_max=5, dtype=np.float32):
"""test unary operators"""
np_data = np.random.uniform(a_min, a_max, size=(2, 3, 5)).astype(dtype)
tf.reset_default_graph()
with tf.Graph().as_default():
in_data = tf.placeholder(dtype, (2, 3, 5), name="in_data")
out = op(in_data)
compare_tf_with_tvm([np_data], ['in_data:0'], out.name)
_test_forward_unary(tf.acos, -1, 1)
_test_forward_unary(tf.asin, -1, 1)
_test_forward_unary(tf.atanh, -1, 1)
_test_forward_unary(tf.sinh)
_test_forward_unary(tf.cosh)
_test_forward_unary(tf.acosh)
_test_forward_unary(tf.asinh)
_test_forward_unary(tf.atan)
_test_forward_unary(tf.sin)
_test_forward_unary(tf.cos)
_test_forward_unary(tf.tan)
_test_forward_unary(tf.tanh)
_test_forward_unary(tf.erf)
_test_forward_unary(tf.log)
_test_forward_unary(tf.log1p)
示例8: call
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def call(self, inputs, mask=None):
# Import graph tensors
# scalar_features = (samples, max_atoms, atom_feat)
# vector_features = (samples, max_atoms, coor_dims, atom_feat)
scalar_features, vector_features = inputs
# Get parameters
coor_dims = int(vector_features.shape[2])
atom_feat = int(vector_features.shape[-1])
# Integrate over atom axis
if self.pooling == "sum":
scalar_features = tf.reduce_sum(scalar_features, axis=1)
vector_features = tf.reduce_sum(vector_features, axis=1)
elif self.pooling == "max":
scalar_features = tf.reduce_max(scalar_features, axis=1)
vector_features = tf.transpose(vector_features, perm=[0, 2, 3, 1])
size = tf.sqrt(tf.reduce_sum(tf.square(vector_features), axis=1))
idx = tf.reshape(tf.argmax(size, axis=-1, output_type=tf.int32), [-1, 1, atom_feat, 1])
idx = tf.tile(idx, [1, coor_dims, 1, 1])
vector_features = tf.reshape(tf.batch_gather(vector_features, idx), [-1, coor_dims, atom_feat])
# Activation
scalar_features = self.activation(scalar_features)
vector_features = self.activation(vector_features)
if self.system == "spherical":
x, y, z = tf.unstack(vector_features, axis=1)
r = tf.sqrt(tf.square(x) + tf.square(y) + tf.square(z))
t = tf.acos(tf.divide(z, r + tf.cast(tf.equal(r, 0), dtype=float)))
p = tf.atan(tf.divide(y, x + tf.cast(tf.equal(x, 0), dtype=float)))
vector_features = tf.stack([r, t, p], axis=1)
return [scalar_features, vector_features]
示例9: arcface_loss
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def arcface_loss(self, labels, x, normx_cos, m1=1.0, m2=0.2, m3=0.3, s=64.0):
norm_x = tf.norm(x, axis=1, keepdims=True)
cos_theta = normx_cos / norm_x
theta = tf.acos(cos_theta)
mask = tf.one_hot(labels, depth=normx_cos.shape[-1])
zeros = tf.zeros_like(mask)
cond = tf.where(tf.greater(theta * m1 + m3, math.pi), zeros, mask)
cond = tf.cast(cond, dtype=tf.bool)
m1_theta_plus_m3 = tf.where(cond, theta * m1 + m3, theta)
cos_m1_theta_plus_m3 = tf.cos(m1_theta_plus_m3)
prelogits = tf.where(cond, cos_m1_theta_plus_m3 - m2, cos_m1_theta_plus_m3) * s
loss = self.sparse(mask, prelogits)
return loss, prelogits
示例10: cartesian_to_spherical_coordinates
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def cartesian_to_spherical_coordinates(point_cartesian, eps=None, name=None):
"""Function to transform Cartesian coordinates to spherical coordinates.
This function assumes a right handed coordinate system with `z` pointing up.
When `x` and `y` are both `0`, the function outputs `0` for `phi`. Note that
the function is not smooth when `x = y = 0`.
Note:
In the following, A1 to An are optional batch dimensions.
Args:
point_cartesian: A tensor of shape `[A1, ..., An, 3]`. In the last
dimension, the data follows the `x`, `y`, `z` order.
eps: A small `float`, to be added to the denominator. If left as `None`,
its value is automatically selected using `point_cartesian.dtype`.
name: A name for this op. Defaults to `cartesian_to_spherical_coordinates`.
Returns:
A tensor of shape `[A1, ..., An, 3]`. The last dimensions contains
(`r`,`theta`,`phi`), where `r` is the sphere radius, `theta` is the polar
angle and `phi` is the azimuthal angle.
"""
with tf.compat.v1.name_scope(name, "cartesian_to_spherical_coordinates",
[point_cartesian]):
point_cartesian = tf.convert_to_tensor(value=point_cartesian)
shape.check_static(
tensor=point_cartesian,
tensor_name="point_cartesian",
has_dim_equals=(-1, 3))
x, y, z = tf.unstack(point_cartesian, axis=-1)
radius = tf.norm(tensor=point_cartesian, axis=-1)
theta = tf.acos(safe_ops.safe_unsigned_div(z, radius, eps))
phi = tf.atan2(y, x)
return tf.stack((radius, theta, phi), axis=-1)
示例11: square_to_spherical_coordinates
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def square_to_spherical_coordinates(point_2d, name=None):
"""Maps points from a unit square to a unit sphere.
Note:
In the following, A1 to An are optional batch dimensions.
Args:
point_2d: A tensor of shape `[A1, ..., An, 2]` with values in [0,1].
name: A name for this op. Defaults to
"math_square_to_spherical_coordinates".
Returns:
A tensor of shape `[A1, ..., An, 2]` with [..., 0] having values in
[0.0, pi] and [..., 1] with values in [0.0, 2pi].
Raises:
ValueError: if the shape of `point_2d` is not supported.
InvalidArgumentError: if at least an element of `point_2d` is outside of
[0,1].
"""
with tf.compat.v1.name_scope(name, "math_square_to_spherical_coordinates",
[point_2d]):
point_2d = tf.convert_to_tensor(value=point_2d)
shape.check_static(
tensor=point_2d, tensor_name="point_2d", has_dim_equals=(-1, 2))
point_2d = asserts.assert_all_in_range(
point_2d, 0.0, 1.0, open_bounds=False)
x, y = tf.unstack(point_2d, axis=-1)
theta = 2.0 * tf.acos(tf.sqrt(1.0 - x))
phi = 2.0 * np.pi * y
return tf.stack((tf.ones_like(theta), theta, phi), axis=-1)
# API contains all public functions and classes.
示例12: test_safe_shrink_exception_not_raised
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def test_safe_shrink_exception_not_raised(self, dtype):
"""Checks whether safe shrinking makes tensor safe for tf.acos(x)."""
tensor = tf.convert_to_tensor(value=_pick_random_vector(), dtype=dtype)
tensor = tensor * tensor
norm_tensor = tensor / tf.reduce_max(
input_tensor=tensor, axis=-1, keepdims=True)
eps = asserts.select_eps_for_addition(dtype)
norm_tensor += eps
safe_tensor = safe_ops.safe_shrink(norm_tensor, -1.0, 1.0)
self.assert_exception_is_not_raised(tf.acos, shapes=[], x=safe_tensor)
示例13: test_Acos
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def test_Acos(self):
t = tf.acos(self.random(4, 3))
self.check(t)
示例14: to_degrees
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def to_degrees(log_quaternion_loss):
"""Converts a log quaternion distance to an angle.
Args:
log_quaternion_loss: The log quaternion distance between two
unit quaternions (or a batch of pairs of quaternions).
Returns:
The angle in degrees of the implied angle-axis representation.
"""
return tf.acos(-(tf.exp(log_quaternion_loss) - 1)) * 2 * 180 / math.pi
示例15: tf_quaternion_to_angle
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import acos [as 别名]
def tf_quaternion_to_angle(q):
return tf.acos(q[3]) * 2.0
# From frustum pointnets