本文整理汇总了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)}