本文整理汇总了Python中math.log方法的典型用法代码示例。如果您正苦于以下问题:Python math.log方法的具体用法?Python math.log怎么用?Python math.log使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math
的用法示例。
在下文中一共展示了math.log方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: entropy
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def entropy(self, subset, attr, value, base=False):
"""
Calculate the entropy of the given attribute/value pair from the
given subset.
Args:
subset: the subset with which to calculate entropy.
attr: the attribute of the value.
value: the value used in calculation.
base: whether or not to calculate base entropy based solely on the
dependent value (default False).
Returns:
A float of the entropy of the given value.
"""
counts = self.value_counts(subset, attr, value, base)
total = float(sum(counts.values())) # Coerce to float division
entropy = 0
for dv in counts: # For each dependent value
proportion = counts[dv] / total
entropy += -(proportion*math.log(proportion, 2))
return entropy
示例2: get_similarity
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def get_similarity(word_list1, word_list2):
"""默认的用于计算两个句子相似度的函数。
Keyword arguments:
word_list1, word_list2 -- 分别代表两个句子,都是由单词组成的列表
"""
words = list(set(word_list1 + word_list2))
vector1 = [float(word_list1.count(word)) for word in words]
vector2 = [float(word_list2.count(word)) for word in words]
vector3 = [vector1[x]*vector2[x] for x in xrange(len(vector1))]
vector4 = [1 for num in vector3 if num > 0.]
co_occur_num = sum(vector4)
if abs(co_occur_num) <= 1e-12:
return 0.
denominator = math.log(float(len(word_list1))) + math.log(float(len(word_list2))) # 分母
if abs(denominator) < 1e-12:
return 0.
return co_occur_num / denominator
示例3: draw
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def draw(self, true_classes):
"""Draw samples from log uniform distribution and returns sampled candidates,
expected count for true classes and sampled classes."""
range_max = self.range_max
num_sampled = self.num_sampled
ctx = true_classes.context
log_range = math.log(range_max + 1)
num_tries = 0
true_classes = true_classes.reshape((-1,))
sampled_classes, num_tries = self.sampler.sample_unique(num_sampled)
true_cls = true_classes.as_in_context(ctx).astype('float64')
prob_true = ((true_cls + 2.0) / (true_cls + 1.0)).log() / log_range
count_true = self._prob_helper(num_tries, num_sampled, prob_true)
sampled_classes = ndarray.array(sampled_classes, ctx=ctx, dtype='int64')
sampled_cls_fp64 = sampled_classes.astype('float64')
prob_sampled = ((sampled_cls_fp64 + 2.0) / (sampled_cls_fp64 + 1.0)).log() / log_range
count_sampled = self._prob_helper(num_tries, num_sampled, prob_sampled)
return [sampled_classes, count_true, count_sampled]
示例4: _compute_delta
# 需要导入模块: import math [as 别名]
# 或者: from math import log [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
示例5: _compute_delta
# 需要导入模块: import math [as 别名]
# 或者: from math import log [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
示例6: _compute_eps
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def _compute_eps(log_moments, delta):
"""Compute epsilon for given log_moments and delta.
Args:
log_moments: the log moments of privacy loss, in the form of pairs
of (moment_order, log_moment)
delta: the target delta.
Returns:
epsilon
"""
min_eps = float("inf")
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
min_eps = min(min_eps, (log_moment - math.log(delta)) / moment_order)
return min_eps
示例7: get_privacy_spent
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def get_privacy_spent(log_moments, target_eps=None, target_delta=None):
"""Compute delta (or eps) for given eps (or delta) from log moments.
Args:
log_moments: array of (moment_order, log_moment) pairs.
target_eps: if not None, the epsilon for which we would like to compute
corresponding delta value.
target_delta: if not None, the delta for which we would like to compute
corresponding epsilon value. Exactly one of target_eps and target_delta
is None.
Returns:
eps, delta pair
"""
assert (target_eps is None) ^ (target_delta is None)
assert not ((target_eps is None) and (target_delta is None))
if target_eps is not None:
return (target_eps, _compute_delta(log_moments, target_eps))
else:
return (_compute_eps(log_moments, target_delta), target_delta)
示例8: testCodeLength
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def testCodeLength(self):
shape = [2, 4]
proba_feed = [[0.65, 0.25, 0.70, 0.10],
[0.28, 0.20, 0.44, 0.54]]
symbol_feed = [[1.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]]
mean_code_length = - (
(math.log(0.65) + math.log(0.75) + math.log(0.70) + math.log(0.90) +
math.log(0.72) + math.log(0.80) + math.log(0.56) + math.log(0.54)) /
math.log(2.0)) / (shape[0] * shape[1])
symbol = tf.placeholder(dtype=tf.float32, shape=shape)
proba = tf.placeholder(dtype=tf.float32, shape=shape)
code_length_calculator = blocks_entropy_coding.CodeLength()
code_length = code_length_calculator(symbol, proba)
with self.test_session():
tf.global_variables_initializer().run()
code_length_eval = code_length.eval(
feed_dict={symbol: symbol_feed, proba: proba_feed})
self.assertAllClose(mean_code_length, code_length_eval)
示例9: __init__
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def __init__(self, T, opts):
super(LOOLoss, self).__init__()
self.gpu = opts.gpu
self.loo = opts.loo if 'LOO' in opts.method else 0.
self.label_smooth = opts.label_smooth
self.kld_u_const = math.log(len(T['wnids']))
self.relevant = [torch.from_numpy(rel) for rel in T['relevant']]
self.labels_relevant = torch.from_numpy(T['labels_relevant'].astype(np.uint8))
ch_slice = T['ch_slice']
if opts.class_wise:
num_children = T['num_children']
num_supers = len(num_children)
self.class_weight = torch.zeros(ch_slice[-1])
for m, num_ch in enumerate(num_children):
self.class_weight[ch_slice[m]:ch_slice[m+1]] = 1. / (num_ch * num_supers)
else:
self.class_weight = torch.ones(ch_slice[-1]) / ch_slice[-1]
示例10: get_timing_signal
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def get_timing_signal(length,
min_timescale=1,
max_timescale=1e4,
num_timescales=16):
"""Create Tensor of sinusoids of different frequencies.
Args:
length: Length of the Tensor to create, i.e. Number of steps.
min_timescale: a float
max_timescale: a float
num_timescales: an int
Returns:
Tensor of shape (length, 2*num_timescales)
"""
positions = tf.to_float(tf.range(length))
log_timescale_increment = (
math.log(max_timescale / min_timescale) / (num_timescales - 1))
inv_timescales = min_timescale * tf.exp(
tf.to_float(tf.range(num_timescales)) * -log_timescale_increment)
scaled_time = tf.expand_dims(positions, 1) * tf.expand_dims(inv_timescales, 0)
return tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=1)
示例11: __init__
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def __init__(
self,
classes,
alpha,
p=0.9,
from_normx=False,
weight=None,
size_average=None,
ignore_index=-100,
reduce=None,
reduction='mean'):
super(L2Softmax, self).__init__(
weight, size_average, reduce, reduction)
alpha_low = math.log(p * (classes - 2) / (1 - p))
assert alpha > alpha_low, "For given probability of p={}, alpha should higher than {}.".format(
p, alpha_low)
self.ignore_index = ignore_index
self.alpha = alpha
self.from_normx = from_normx
示例12: log2
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def log2(x):
try:
return math.log(x, 2)
except ValueError:
return float("nan")
示例13: corpus_bleu
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def corpus_bleu(hypothesis, references, max_n=4):
assert(len(hypothesis) == len(references))
clip_count, count, total_len_hyp, total_len_ref = bleu_count(hypothesis, references, max_n=max_n)
brevity_penalty = 1.0
bleu_scores = []
bleu = 0
for n in range(max_n):
if count[n]>0:
bleu_scores.append(clip_count[n]/count[n])
else:
bleu_scores.append(0)
if total_len_hyp < total_len_ref:
if total_len_hyp==0:
brevity_penalty = 0.0
else:
brevity_penalty = math.exp(1 - total_len_ref/total_len_hyp)
def my_log(x):
if x == 0:
return -9999999999.0
elif x < 0:
raise Exception("Value Error")
return math.log(x)
log_bleu = 0.0
for n in range(max_n):
log_bleu += my_log(bleu_scores[n])
bleu = brevity_penalty*math.exp(log_bleu / float(max_n))
return [bleu]+bleu_scores, [brevity_penalty, total_len_hyp/total_len_ref, total_len_hyp, total_len_ref]
示例14: incremental_sent_bleu
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def incremental_sent_bleu(hypothesis, references, max_n=4):
clip_count, count, total_len_hyp, total_len_ref = incremental_bleu_count([hypothesis], [references], max_n=max_n)
clip_count = clip_count[0]
count = count[0]
total_len_hyp = total_len_hyp[0]
total_len_ref = total_len_ref[0]
n_len = len(clip_count)
ret = []
for i in range(n_len):
brevity_penalty = 1.0
bleu_scores = []
bleu = 0
for n in range(max_n):
if count[i][n]>0:
bleu_scores.append(clip_count[i][n]/count[i][n])
else:
bleu_scores.append(0)
if total_len_hyp[i] < total_len_ref[i]:
if total_len_hyp[i]==0:
brevity_penalty = 0.0
else:
brevity_penalty = math.exp(1 - total_len_ref[i]/total_len_hyp[i])
def my_log(x):
if x == 0:
return -9999999999.0
elif x < 0:
raise Exception("Value Error")
return math.log(x)
log_bleu = 0.0
for n in range(max_n):
log_bleu += my_log(bleu_scores[n])
bleu = brevity_penalty*math.exp(log_bleu / float(max_n))
ret.append(bleu)
return ret
示例15: incremental_test_corpus_bleu
# 需要导入模块: import math [as 别名]
# 或者: from math import log [as 别名]
def incremental_test_corpus_bleu(hypothesis, references, max_n=4):
assert(len(hypothesis) == len(references))
tmp_clip_count, tmp_count, tmp_total_len_hyp, tmp_total_len_ref = incremental_bleu_count(hypothesis, references, max_n=max_n)
clip_count = [0]*4
count = [0]*4
total_len_hyp = 0
total_len_ref = 0
for i in range(len(hypothesis)):
for n in range(4):
clip_count[n]+=tmp_clip_count[i][-1][n]
count[n] += tmp_count[i][-1][n]
total_len_hyp += tmp_total_len_hyp[i][-1]
total_len_ref += tmp_total_len_ref[i][-1]
brevity_penalty = 1.0
bleu_scores = []
bleu = 0
for n in range(max_n):
if count[n]>0:
bleu_scores.append(clip_count[n]/count[n])
else:
bleu_scores.append(0)
if total_len_hyp < total_len_ref:
if total_len_hyp==0:
brevity_penalty = 0.0
else:
brevity_penalty = math.exp(1 - total_len_ref/total_len_hyp)
def my_log(x):
if x == 0:
return -9999999999.0
elif x < 0:
raise Exception("Value Error")
return math.log(x)
log_bleu = 0.0
for n in range(max_n):
log_bleu += my_log(bleu_scores[n])
bleu = brevity_penalty*math.exp(log_bleu / float(max_n))
return [bleu]+bleu_scores, [brevity_penalty, total_len_hyp/total_len_ref, total_len_hyp, total_len_ref]