本文整理汇总了Python中mxnet.ndarray.log方法的典型用法代码示例。如果您正苦于以下问题:Python ndarray.log方法的具体用法?Python ndarray.log怎么用?Python ndarray.log使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mxnet.ndarray
的用法示例。
在下文中一共展示了ndarray.log方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prepare_sequence
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def prepare_sequence(seq, word2idx):
return nd.array([word2idx[w] for w in seq])
# Compute log sum exp is numerically more stable than multiplying probabilities
示例2: log_sum_exp
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def log_sum_exp(vec):
max_score = nd.max(vec).asscalar()
return nd.log(nd.sum(nd.exp(vec - max_score))) + max_score
# Model
示例3: _forward_alg
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def _forward_alg(self, feats):
# Do the forward algorithm to compute the partition function
alphas = [[-10000.] * self.tagset_size]
alphas[0][self.tag2idx[START_TAG]] = 0.
alphas = nd.array(alphas)
# Iterate through the sentence
for feat in feats:
alphas_t = [] # The forward variables at this timestep
for next_tag in range(self.tagset_size):
# broadcast the emission score: it is the same regardless of
# the previous tag
emit_score = feat[next_tag].reshape((1, -1))
# the ith entry of trans_score is the score of transitioning to
# next_tag from i
trans_score = self.transitions.data()[next_tag].reshape((1, -1))
# The ith entry of next_tag_var is the value for the
# edge (i -> next_tag) before we do log-sum-exp
next_tag_var = alphas + trans_score + emit_score
# The forward variable for this tag is log-sum-exp of all the
# scores.
alphas_t.append(log_sum_exp(next_tag_var))
alphas = nd.concat(*alphas_t, dim=0).reshape((1, -1))
terminal_var = alphas + self.transitions.data()[self.tag2idx[STOP_TAG]]
alpha = log_sum_exp(terminal_var)
return alpha
示例4: _viterbi_decode
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def _viterbi_decode(self, feats):
backpointers = []
# Initialize the viterbi variables in log space
vvars = nd.full((1, self.tagset_size), -10000.)
vvars[0, self.tag2idx[START_TAG]] = 0
for feat in feats:
bptrs_t = [] # holds the backpointers for this step
viterbivars_t = [] # holds the viterbi variables for this step
for next_tag in range(self.tagset_size):
# next_tag_var[i] holds the viterbi variable for tag i at the
# previous step, plus the score of transitioning
# from tag i to next_tag.
# We don't include the emission scores here because the max
# does not depend on them (we add them in below)
next_tag_var = vvars + self.transitions.data()[next_tag]
best_tag_id = argmax(next_tag_var)
bptrs_t.append(best_tag_id)
viterbivars_t.append(next_tag_var[0, best_tag_id])
# Now add in the emission scores, and assign vvars to the set
# of viterbi variables we just computed
vvars = (nd.concat(*viterbivars_t, dim=0) + feat).reshape((1, -1))
backpointers.append(bptrs_t)
# Transition to STOP_TAG
terminal_var = vvars + self.transitions.data()[self.tag2idx[STOP_TAG]]
best_tag_id = argmax(terminal_var)
path_score = terminal_var[0, best_tag_id]
# Follow the back pointers to decode the best path.
best_path = [best_tag_id]
for bptrs_t in reversed(backpointers):
best_tag_id = bptrs_t[best_tag_id]
best_path.append(best_tag_id)
# Pop off the start tag (we dont want to return that to the caller)
start = best_path.pop()
assert start == self.tag2idx[START_TAG] # Sanity check
best_path.reverse()
return path_score, best_path
示例5: get_rmse_log
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def get_rmse_log(net, X_train, y_train):
"""Gets root mse between the logarithms of the prediction and the truth."""
num_train = X_train.shape[0]
clipped_preds = nd.clip(net(X_train), 1, float('inf'))
return np.sqrt(2 * nd.sum(square_loss(
nd.log(clipped_preds), nd.log(y_train))).asscalar() / num_train)
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:8,代码来源:kaggle_k_fold_cross_validation.py
示例6: logsigmoid
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def logsigmoid(val):
max_elem = nd.maximum(0., -val)
z = nd.exp(-max_elem) + nd.exp(-val - max_elem)
return -(max_elem + nd.log(z))
示例7: log_gaussian
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def log_gaussian(x, mean, sigma):
return -0.5 * np.log(2.0 * np.pi) - nd.log(sigma) - (x - mean) ** 2 / (2 * sigma ** 2)
示例8: softplus
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def softplus(x):
return nd.log(1. + nd.exp(x))
示例9: softplus_inv
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def softplus_inv(x):
return nd.log(nd.exp(x) - 1.)
示例10: softplus_inv_numpy
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def softplus_inv_numpy(x):
return np.log(np.exp(x) - 1.)
示例11: KL
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def KL(self, other_prob):
if not self.is_conjugate(other_prob):
raise ValueError("KL cannot be computed in closed form.")
if (not len(self.shapes) == len(other_prob.shapes)) or \
(not np.all(np.array([s == o for s, o in zip(self.shapes, other_prob.shapes)]))):
raise ValueError("KL cannot be computed: The 2 distributions have different support")
raw_params_ext_var_posterior = self._replicate_shared_parameters()
sigmas_var_posterior = transform_rhos(raw_params_ext_var_posterior[RHO])
raw_params_ext_prior = other_prob._replicate_shared_parameters()
out = 0.0
for ii in range(len(self.shapes)):
means_p = raw_params_ext_prior[MEAN][ii]
var_p = raw_params_ext_prior["sigma"][ii] ** 2
means_q = raw_params_ext_var_posterior[MEAN][ii]
var_q = sigmas_var_posterior[ii] ** 2
inc_means = (means_q - means_p)
prec_p = 1.0 / var_p
temp = 0.5 * (var_q*prec_p + ((inc_means ** 2) * prec_p) - 1.0 + nd.log(var_p) - nd.log(var_q))
if temp.shape == (1, 1):
# If parameters are shared, multiply by the number of variables
temp = temp * (self.shapes[ii][0] * self.shapes[ii][1])
out = out + nd.sum(temp)
return out
示例12: log_sum_exp
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def log_sum_exp(vec):
max_score = nd.max(vec).asscalar()
return nd.log(nd.sum(nd.exp(vec - max_score))) + max_score
示例13: _forward_alg
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def _forward_alg(self, feats):
# Do the forward algorithm to compute the partition function
alphas = [[-10000.] * self.tagset_size]
alphas[0][self.tag2idx[START_TAG]] = 0.
alphas = nd.array(alphas)
# Iterate through the sentence
for feat in feats:
alphas_t = [] # The forward variables at this timestep
for next_tag in range(self.tagset_size):
# broadcast the emission score: it is the same regardless of
# the previous tag
emit_score = feat[next_tag].reshape((1, -1))
# the ith entry of trans_score is the score of transitioning to
# next_tag from i
trans_score = self.transitions[next_tag].reshape((1, -1))
# The ith entry of next_tag_var is the value for the
# edge (i -> next_tag) before we do log-sum-exp
next_tag_var = alphas + trans_score + emit_score
# The forward variable for this tag is log-sum-exp of all the
# scores.
alphas_t.append(log_sum_exp(next_tag_var))
alphas = nd.concat(*alphas_t, dim=0).reshape((1, -1))
terminal_var = alphas + self.transitions[self.tag2idx[STOP_TAG]]
alpha = log_sum_exp(terminal_var)
return alpha
示例14: _viterbi_decode
# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import log [as 别名]
def _viterbi_decode(self, feats):
backpointers = []
# Initialize the viterbi variables in log space
vvars = nd.full((1, self.tagset_size), -10000.)
vvars[0, self.tag2idx[START_TAG]] = 0
for feat in feats:
bptrs_t = [] # holds the backpointers for this step
viterbivars_t = [] # holds the viterbi variables for this step
for next_tag in range(self.tagset_size):
# next_tag_var[i] holds the viterbi variable for tag i at the
# previous step, plus the score of transitioning
# from tag i to next_tag.
# We don't include the emission scores here because the max
# does not depend on them (we add them in below)
next_tag_var = vvars + self.transitions[next_tag]
best_tag_id = argmax(next_tag_var)
bptrs_t.append(best_tag_id)
viterbivars_t.append(next_tag_var[0, best_tag_id])
# Now add in the emission scores, and assign vvars to the set
# of viterbi variables we just computed
vvars = (nd.concat(*viterbivars_t, dim=0) + feat).reshape((1, -1))
backpointers.append(bptrs_t)
# Transition to STOP_TAG
terminal_var = vvars + self.transitions[self.tag2idx[STOP_TAG]]
best_tag_id = argmax(terminal_var)
path_score = terminal_var[0, best_tag_id]
# Follow the back pointers to decode the best path.
best_path = [best_tag_id]
for bptrs_t in reversed(backpointers):
best_tag_id = bptrs_t[best_tag_id]
best_path.append(best_tag_id)
# Pop off the start tag (we dont want to return that to the caller)
start = best_path.pop()
assert start == self.tag2idx[START_TAG] # Sanity check
best_path.reverse()
return path_score, best_path