def _atan2(y, x):
""" My implementation of atan2 in tensorflow. Returns in -pi .. pi."""
tan = tf.atan(y / (x + 1e-8)) # this returns in -pi/2 .. pi/2
one_map = tf.ones_like(tan)
# correct quadrant error
correction = tf.where(tf.less(x + 1e-8, 0.0), 3.141592653589793*one_map, 0.0*one_map)
tan_c = tan + correction # this returns in -pi/2 .. 3pi/2
# bring to positive values
correction = tf.where(tf.less(tan_c, 0.0), 2*3.141592653589793*one_map, 0.0*one_map)
tan_zero_2pi = tan_c + correction # this returns in 0 .. 2pi
# make symmetric
correction = tf.where(tf.greater(tan_zero_2pi, 3.141592653589793), -2*3.141592653589793*one_map, 0.0*one_map)
tan_final = tan_zero_2pi + correction # this returns in -pi .. pi
return tan_final
python类atan()的实例源码
def atan2(y, x):
""" My implementation of atan2 in tensorflow. Returns in -pi .. pi."""
tan = tf.atan(y / (x + 1e-8)) # this returns in -pi/2 .. pi/2
one_map = tf.ones_like(tan)
# correct quadrant error
correction = tf.where(tf.less(x + 1e-8, 0.0), 3.141592653589793*one_map, 0.0*one_map)
tan_c = tan + correction # this returns in -pi/2 .. 3pi/2
# bring to positive values
correction = tf.where(tf.less(tan_c, 0.0), 2*3.141592653589793*one_map, 0.0*one_map)
tan_zero_2pi = tan_c + correction # this returns in 0 .. 2pi
# make symmetric
correction = tf.where(tf.greater(tan_zero_2pi, 3.141592653589793), -2*3.141592653589793*one_map, 0.0*one_map)
tan_final = tan_zero_2pi + correction # this returns in -pi .. pi
return tan_final
def atan2(x, y, epsilon = 1.0e-12):
"""
A hack until the TensorFlow developers implement a function that can find the angle from an x and y co-
ordinate.
:param x:
:param epsilon:
:return:
"""
# Add a small number to all zeros, to avoid division by zero:
x = tf.where(tf.equal(x, 0.0), x + epsilon, x)
y = tf.where(tf.equal(y, 0.0), y + epsilon, y)
angle = tf.where(tf.greater(x, 0.0), tf.atan(y / x), tf.zeros_like(x))
angle = tf.where(tf.logical_and(tf.less(x, 0.0), tf.greater_equal(y, 0.0)), tf.atan(y / x) + np.pi, angle)
angle = tf.where(tf.logical_and(tf.less(x, 0.0), tf.less(y, 0.0)), tf.atan(y / x) - np.pi, angle)
angle = tf.where(tf.logical_and(tf.equal(x, 0.0), tf.greater(y, 0.0)), 0.5 * np.pi * tf.ones_like(x), angle)
angle = tf.where(tf.logical_and(tf.equal(x, 0.0), tf.less(y, 0.0)), -0.5 * np.pi * tf.ones_like(x), angle)
angle = tf.where(tf.logical_and(tf.equal(x, 0.0), tf.equal(y, 0.0)), tf.zeros_like(x), angle)
return angle
# List of faces for consistent ordering.
def setUp(self):
super(CoreUnaryOpsTest, self).setUp()
self.ops = [
('abs', operator.abs, tf.abs, core.abs_function),
('neg', operator.neg, tf.neg, core.neg),
# TODO(shoyer): add unary + to core TensorFlow
('pos', None, None, None),
('sign', None, tf.sign, core.sign),
('reciprocal', None, tf.reciprocal, core.reciprocal),
('square', None, tf.square, core.square),
('round', None, tf.round, core.round_function),
('sqrt', None, tf.sqrt, core.sqrt),
('rsqrt', None, tf.rsqrt, core.rsqrt),
('log', None, tf.log, core.log),
('exp', None, tf.exp, core.exp),
('log', None, tf.log, core.log),
('ceil', None, tf.ceil, core.ceil),
('floor', None, tf.floor, core.floor),
('cos', None, tf.cos, core.cos),
('sin', None, tf.sin, core.sin),
('tan', None, tf.tan, core.tan),
('acos', None, tf.acos, core.acos),
('asin', None, tf.asin, core.asin),
('atan', None, tf.atan, core.atan),
('lgamma', None, tf.lgamma, core.lgamma),
('digamma', None, tf.digamma, core.digamma),
('erf', None, tf.erf, core.erf),
('erfc', None, tf.erfc, core.erfc),
('lgamma', None, tf.lgamma, core.lgamma),
]
total_size = np.prod([v.size for v in self.original_lt.axes.values()])
self.test_lt = core.LabeledTensor(
tf.cast(self.original_lt, tf.float32) / total_size,
self.original_lt.axes)
def attenuate_rectilinear(self, K, disparity, position):
S, T = lat_long_grid([tf.shape(disparity)[1], tf.shape(disparity)[2]])
_, T_grids = self.expand_grids(S, -T, tf.shape(disparity)[0])
if position == "top":
attenuated_disparity = (1.0 / np.pi) * (tf.atan(disparity / K[1] + tf.tan(T_grids)) - T_grids)
else:
attenuated_disparity = (1.0 / np.pi) * (T_grids - tf.atan(tf.tan(T_grids) - disparity / K[1]))
return tf.clip_by_value(tf.where(tf.is_finite(attenuated_disparity), attenuated_disparity, tf.zeros_like(attenuated_disparity)), 1e-6, 0.75)
def attenuate_equirectangular(self, disparity, position):
S, T = lat_long_grid([tf.shape(disparity)[1], tf.shape(disparity)[2]])
_, T_grids = self.expand_grids(S, -T, tf.shape(disparity)[0])
if position == "top":
attenuated_disparity = (1.0 / np.pi) * (tf.atan(tf.tan(np.pi * disparity) + tf.tan(T_grids)) - T_grids)
else:
attenuated_disparity = (1.0 / np.pi) * (T_grids - tf.atan(tf.tan(T_grids) - tf.tan(np.pi * disparity)))
return tf.clip_by_value(tf.where(tf.is_finite(attenuated_disparity), attenuated_disparity, tf.zeros_like(attenuated_disparity)), 1e-6, 0.75)
def test_Atan(self):
t = tf.atan(self.random(4, 3))
self.check(t)