本文整理匯總了Python中math.isnan方法的典型用法代碼示例。如果您正苦於以下問題:Python math.isnan方法的具體用法?Python math.isnan怎麽用?Python math.isnan使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類math
的用法示例。
在下文中一共展示了math.isnan方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _compute_delta
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def _compute_delta(self, log_moments, eps):
"""Compute delta for given log_moments and eps.
Args:
log_moments: the log moments of privacy loss, in the form of pairs
of (moment_order, log_moment)
eps: the target epsilon.
Returns:
delta
"""
min_delta = 1.0
for moment_order, log_moment in log_moments:
if math.isinf(log_moment) or math.isnan(log_moment):
sys.stderr.write("The %d-th order is inf or Nan\n" % moment_order)
continue
if log_moment < moment_order * eps:
min_delta = min(min_delta,
math.exp(log_moment - moment_order * eps))
return min_delta
示例2: _compute_delta
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def _compute_delta(log_moments, eps):
"""Compute delta for given log_moments and eps.
Args:
log_moments: the log moments of privacy loss, in the form of pairs
of (moment_order, log_moment)
eps: the target epsilon.
Returns:
delta
"""
min_delta = 1.0
for moment_order, log_moment in log_moments:
if moment_order == 0:
continue
if math.isinf(log_moment) or math.isnan(log_moment):
sys.stderr.write("The %d-th order is inf or Nan\n" % moment_order)
continue
if log_moment < moment_order * eps:
min_delta = min(min_delta,
math.exp(log_moment - moment_order * eps))
return min_delta
示例3: GenerateBinomialTable
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def GenerateBinomialTable(m):
"""Generate binomial table.
Args:
m: the size of the table.
Returns:
A two dimensional array T where T[i][j] = (i choose j),
for 0<= i, j <=m.
"""
table = numpy.zeros((m + 1, m + 1), dtype=numpy.float64)
for i in range(m + 1):
table[i, 0] = 1
for i in range(1, m + 1):
for j in range(1, m + 1):
v = table[i - 1, j] + table[i - 1, j -1]
assert not math.isnan(v) and not math.isinf(v)
table[i, j] = v
return tf.convert_to_tensor(table)
示例4: backward
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def backward(self, loss):
'''
Standard backward step with self.timer and debugger
Arguments
loss - the loss to perform loss.backward()
'''
self.timer.set()
loss.backward()
grad_norm = torch.nn.utils.clip_grad_norm_(
self.model.parameters(), self.GRAD_CLIP)
if math.isnan(grad_norm):
self.verbose('Error : grad norm is NaN @ step '+str(self.step))
else:
self.optimizer.step()
self.timer.cnt('bw')
return grad_norm
示例5: write_log
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def write_log(self, log_name, log_dict):
'''
Write log to TensorBoard
log_name - <str> Name of tensorboard variable
log_value - <dict>/<array> Value of variable (e.g. dict of losses), passed if value = None
'''
if type(log_dict) is dict:
log_dict = {key: val for key, val in log_dict.items() if (
val is not None and not math.isnan(val))}
if log_dict is None:
pass
elif len(log_dict) > 0:
if 'align' in log_name or 'spec' in log_name:
img, form = log_dict
self.log.add_image(
log_name, img, global_step=self.step, dataformats=form)
elif 'text' in log_name or 'hyp' in log_name:
self.log.add_text(log_name, log_dict, self.step)
else:
self.log.add_scalars(log_name, log_dict, self.step)
示例6: from_float
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def from_float(cls, f):
"""Converts a finite float to a rational number, exactly.
Beware that Fraction.from_float(0.3) != Fraction(3, 10).
"""
if isinstance(f, numbers.Integral):
return cls(f)
elif not isinstance(f, float):
raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
(cls.__name__, f, type(f).__name__))
if math.isnan(f):
raise ValueError("Cannot convert %r to %s." % (f, cls.__name__))
if math.isinf(f):
raise OverflowError("Cannot convert %r to %s." % (f, cls.__name__))
return cls(*f.as_integer_ratio())
示例7: __eq__
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def __eq__(a, b):
"""a == b"""
if isinstance(b, numbers.Rational):
return (a._numerator == b.numerator and
a._denominator == b.denominator)
if isinstance(b, numbers.Complex) and b.imag == 0:
b = b.real
if isinstance(b, float):
if math.isnan(b) or math.isinf(b):
# comparisons with an infinity or nan should behave in
# the same way for any finite a, so treat a as zero.
return 0.0 == b
else:
return a == a.from_float(b)
else:
# Since a doesn't know how to compare with b, let's give b
# a chance to compare itself with a.
return NotImplemented
示例8: _richcmp
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def _richcmp(self, other, op):
"""Helper for comparison operators, for internal use only.
Implement comparison between a Rational instance `self`, and
either another Rational instance or a float `other`. If
`other` is not a Rational instance or a float, return
NotImplemented. `op` should be one of the six standard
comparison operators.
"""
# convert other to a Rational instance where reasonable.
if isinstance(other, numbers.Rational):
return op(self._numerator * other.denominator,
self._denominator * other.numerator)
if isinstance(other, float):
if math.isnan(other) or math.isinf(other):
return op(0.0, other)
else:
return op(self, self.from_float(other))
else:
return NotImplemented
示例9: draw_setpoint
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def draw_setpoint(self, *args):
# draw a setpoint
if self.setpoint_canvas:
self.canvas.after.remove(self.setpoint_canvas)
self.setpoint_canvas = None
if math.isnan(self.setpoint_value) or math.isinf(self.setpoint_value):
return
v = self.value_to_angle(self.setpoint_value)
length = self.dial_diameter / 2.0 - self.tic_length if not self.setpoint_length else self.setpoint_length
self.setpoint_canvas = InstructionGroup()
self.setpoint_canvas.add(PushMatrix())
self.setpoint_canvas.add(Color(*self.setpoint_color))
self.setpoint_canvas.add(Rotate(angle=v, axis=(0, 0, -1), origin=self.dial_center))
self.setpoint_canvas.add(Translate(self.dial_center[0], self.dial_center[1]))
self.setpoint_canvas.add(Line(points=[0, 0, 0, length], width=self.setpoint_thickness, cap='none'))
# self.setpoint_canvas.add(SmoothLine(points=[0, 0, 0, length], width=self.setpoint_thickness))
self.setpoint_canvas.add(PopMatrix())
self.canvas.after.add(self.setpoint_canvas)
示例10: testParseFloat
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def testParseFloat(self):
engine, = PFAEngine.fromYaml('''
input: string
output: float
action: {parse.float: input}
''')
self.assertEqual(engine.action(" 123 "), 123.0)
self.assertEqual(engine.action(" -123 "), -123.0)
self.assertEqual(engine.action(" 3.4028234e38 "), 3.4028234e38)
self.assertEqual(engine.action(" -3.4028234e38 "), -3.4028234e38)
self.assertEqual(engine.action(" 3.4028236e38 "), float("inf"))
self.assertEqual(engine.action(" -3.4028236e38 "), float("-inf"))
self.assertEqual(engine.action(" 1.4e-45 "), 1.4e-45)
self.assertEqual(engine.action(" -1.4e-45 "), -1.4e-45)
self.assertEqual(engine.action(" 1e-46 "), 0.0)
self.assertEqual(engine.action(" -1e-46 "), 0.0)
self.assertTrue(math.isnan(engine.action(" nAN ")))
self.assertEqual(engine.action(" inf "), float("inf"))
self.assertEqual(engine.action(" +inf "), float("inf"))
self.assertEqual(engine.action(" -inf "), float("-inf"))
示例11: testParseDouble
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def testParseDouble(self):
engine, = PFAEngine.fromYaml('''
input: string
output: double
action: {parse.double: input}
''')
self.assertEqual(engine.action(" 123 "), 123.0)
self.assertEqual(engine.action(" -123 "), -123.0)
self.assertEqual(engine.action(" 1.7976931348623157e308 "), 1.7976931348623157e308)
self.assertEqual(engine.action(" -1.7976931348623157e308 "), -1.7976931348623157e308)
self.assertEqual(engine.action(" 1.7976931348623159e308 "), float("inf"))
self.assertEqual(engine.action(" -1.7976931348623159e308 "), float("-inf"))
self.assertEqual(engine.action(" 4.9e-324 "), 4.9e-324)
self.assertEqual(engine.action(" -4.9e-324 "), -4.9e-324)
self.assertEqual(engine.action(" 1e-324 "), 0.0)
self.assertEqual(engine.action(" 1e-324 "), 0.0)
self.assertTrue(math.isnan(engine.action(" nAN ")))
self.assertEqual(engine.action(" inf "), float("inf"))
self.assertEqual(engine.action(" +inf "), float("inf"))
self.assertEqual(engine.action(" -inf "), float("-inf"))
示例12: __call__
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def __call__(self, state, scope, pos, paramTypes, x, *args):
if len(args) == 3:
numbins, low, high = args
if low >= high or math.isnan(low) or math.isnan(high):
raise PFARuntimeException("bad histogram range", self.errcodeBase + 0, self.name, pos)
if numbins < 1:
raise PFARuntimeException("bad histogram scale", self.errcodeBase + 1, self.name, pos)
if math.isnan(x) or x < low or x >= high:
raise PFARuntimeException("x out of range", self.errcodeBase + 2, self.name, pos)
out = int(math.floor(numbins * div((x - low), (high - low))))
if out < 0 or out >= numbins:
raise PFARuntimeException("x out of range", self.errcodeBase + 2, self.name, pos)
return out
else:
origin, width = args
if math.isnan(origin) or math.isinf(origin):
raise PFARuntimeException("bad histogram range", self.errcodeBase + 0, self.name, pos)
if width <= 0.0 or math.isnan(width):
raise PFARuntimeException("bad histogram scale", self.errcodeBase + 1, self.name, pos)
if math.isnan(x) or math.isinf(x):
raise PFARuntimeException("x out of range", self.errcodeBase + 2, self.name, pos)
else:
return int(math.floor(div((x - origin), width)))
示例13: __call__
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def __call__(self, state, scope, pos, paramTypes, str):
try:
out = float(str)
except ValueError:
raise PFARuntimeException("not a single-precision float", self.errcodeBase + 0, self.name, pos)
if math.isnan(out):
return out
elif math.isinf(out):
return out
elif out > FLOAT_MAX_VALUE:
return float("inf")
elif -out > FLOAT_MAX_VALUE:
return float("-inf")
elif abs(out) < FLOAT_MIN_VALUE:
return 0.0
else:
return out
示例14: __call__
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def __call__(self, state, scope, pos, paramTypes, x, y):
if y == 0:
if paramTypes[-1] == "int" or paramTypes[-1] == "long":
raise PFARuntimeException("integer division by zero", self.errcodeBase + 0, self.name, pos)
else:
return float("nan")
else:
if not math.isnan(x) and not math.isinf(x) and math.isinf(y):
return x
else:
out = x % y
if x < 0 and out > 0:
return out - abs(y)
elif x > 0 and out < 0:
return out + abs(y)
else:
return out
示例15: __call__
# 需要導入模塊: import math [as 別名]
# 或者: from math import isnan [as 別名]
def __call__(self, state, scope, pos, paramTypes, a, p):
if len(a) == 0:
raise PFARuntimeException("empty array", self.errcodeBase + 0, self.name, pos)
if math.isnan(p):
raise PFARuntimeException("p not a number", self.errcodeBase + 1, self.name, pos)
if p <= 0.0:
return lowestN(a, 1, lambda x, y: compare(jsonNodeToAvroType(paramTypes[0]).items, x, y) < 0)[0]
if p >= 1.0:
return highestN(a, 1, lambda x, y: compare(jsonNodeToAvroType(paramTypes[0]).items, x, y) < 0)[0]
sa = sorted(a, lambda x, y: compare(jsonNodeToAvroType(paramTypes[0]).items, x, y))
k = (len(a) - 1.0)*p
f = math.floor(k)
dataType = paramTypes[-1]
if (dataType is "float") or (dataType is "double"):
c = math.ceil(k)
if f == c:
return sa[int(k)]
d0 = sa[int(f)] * (c - k)
d1 = sa[int(c)] * (k - f)
return d0 + d1
else:
if len(sa) % 2 == 1:
return sa[int(f)]
else:
return sa[int(k)]