本文整理匯總了Python中mxnet.ndarray.norm方法的典型用法代碼示例。如果您正苦於以下問題:Python ndarray.norm方法的具體用法?Python ndarray.norm怎麽用?Python ndarray.norm使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mxnet.ndarray
的用法示例。
在下文中一共展示了ndarray.norm方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create_neg
# 需要導入模塊: from mxnet import ndarray [as 別名]
# 或者: from mxnet.ndarray import norm [as 別名]
def create_neg(self, neg_head):
gamma = self.gamma
if neg_head:
def fn(heads, relations, tails, num_chunks, chunk_size, neg_sample_size):
relations = relations.reshape(num_chunks, -1, self.relation_dim)
tails = tails - relations
tails = tails.reshape(num_chunks, -1, 1, self.relation_dim)
score = heads - tails
return gamma - nd.norm(score, ord=1, axis=-1)
return fn
else:
def fn(heads, relations, tails, num_chunks, chunk_size, neg_sample_size):
relations = relations.reshape(num_chunks, -1, self.relation_dim)
heads = heads - relations
heads = heads.reshape(num_chunks, -1, 1, self.relation_dim)
score = heads - tails
return gamma - nd.norm(score, ord=1, axis=-1)
return fn
示例2: calc_potential
# 需要導入模塊: from mxnet import ndarray [as 別名]
# 或者: from mxnet.ndarray import norm [as 別名]
def calc_potential(exe, params, label_name, noise_precision, prior_precision):
exe.copy_params_from(params)
exe.forward(is_train=False)
ret = 0.0
ret += (nd.norm(
exe.outputs[0] - exe.arg_dict[label_name]).asscalar() ** 2) / 2.0 * noise_precision
for v in params.values():
ret += (nd.norm(v).asscalar() ** 2) / 2.0 * prior_precision
return ret
示例3: norm_clipping
# 需要導入模塊: from mxnet import ndarray [as 別名]
# 或者: from mxnet.ndarray import norm [as 別名]
def norm_clipping(params_grad, threshold):
assert isinstance(params_grad, dict)
norm_val = numpy.sqrt(sum([nd.norm(grad).asnumpy()[0]**2 for grad in params_grad.values()]))
# print('grad norm: %g' % norm_val)
ratio = 1.0
if norm_val > threshold:
ratio = threshold / norm_val
for grad in params_grad.values():
grad *= ratio
return norm_val
示例4: batched_l2_dist
# 需要導入模塊: from mxnet import ndarray [as 別名]
# 或者: from mxnet.ndarray import norm [as 別名]
def batched_l2_dist(a, b):
a_squared = nd.power(nd.norm(a, axis=-1), 2)
b_squared = nd.power(nd.norm(b, axis=-1), 2)
squared_res = nd.add(nd.linalg_gemm(
a, nd.transpose(b, axes=(0, 2, 1)), nd.broadcast_axes(nd.expand_dims(b_squared, axis=-2), axis=1, size=a.shape[1]), alpha=-2
), nd.expand_dims(a_squared, axis=-1))
res = nd.sqrt(nd.clip(squared_res, 1e-30, np.finfo(np.float32).max))
return res
示例5: batched_l1_dist
# 需要導入模塊: from mxnet import ndarray [as 別名]
# 或者: from mxnet.ndarray import norm [as 別名]
def batched_l1_dist(a, b):
a = nd.expand_dims(a, axis=-2)
b = nd.expand_dims(b, axis=-3)
res = nd.norm(a - b, ord=1, axis=-1)
return res
示例6: edge_func
# 需要導入模塊: from mxnet import ndarray [as 別名]
# 或者: from mxnet.ndarray import norm [as 別名]
def edge_func(self, edges):
head = edges.src['emb']
tail = edges.dst['emb']
rel = edges.data['emb']
score = head + rel - tail
return {'score': self.gamma - nd.norm(score, ord=self.dist_ord, axis=-1)}