本文整理汇总了Python中mxnet.nd.sum方法的典型用法代码示例。如果您正苦于以下问题:Python nd.sum方法的具体用法?Python nd.sum怎么用?Python nd.sum使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mxnet.nd
的用法示例。
在下文中一共展示了nd.sum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _spectral_norm
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def _spectral_norm(self):
""" spectral normalization """
w = self.params.get('weight').data(self.ctx)
w_mat = nd.reshape(w, [w.shape[0], -1])
_u = self.u.data(self.ctx)
_v = None
for _ in range(POWER_ITERATION):
_v = nd.L2Normalization(nd.dot(_u, w_mat))
_u = nd.L2Normalization(nd.dot(_v, w_mat.T))
sigma = nd.sum(nd.dot(_u, w_mat) * _v)
if sigma == 0.:
sigma = EPSILON
self.params.setattr('u', _u)
return w / sigma
示例2: hybrid_forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def hybrid_forward(self, F, pred, label, sample_weight=None):
"""Loss forward"""
if not self._from_logits:
pred = F.sigmoid(pred)
if self._sparse_label:
one_hot = F.one_hot(label, self._num_class)
else:
one_hot = label > 0
pt = F.where(one_hot, pred, 1 - pred)
t = F.ones_like(one_hot)
alpha = F.where(one_hot, self._alpha * t, (1 - self._alpha) * t)
loss = -alpha * ((1 - pt) ** self._gamma) * F.log(F.minimum(pt + self._eps, 1))
loss = _apply_weighting(F, loss, self._weight, sample_weight)
if self._size_average:
return F.mean(loss, axis=self._batch_axis, exclude=True)
else:
return F.sum(loss, axis=self._batch_axis, exclude=True)
示例3: Route
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def Route(self, x):
# b_mat = nd.repeat(self.b_mat.data(), repeats=x.shape[0], axis=0)#nd.stop_gradient(nd.repeat(self.b_mat.data(), repeats=x.shape[0], axis=0))
b_mat = nd.zeros((x.shape[0],1,self.num_cap, self.num_locations), ctx=x.context)
x_expand = nd.expand_dims(nd.expand_dims(x, axis=2),2)
w_expand = nd.repeat(nd.expand_dims(self.w_ij.data(x.context),axis=0), repeats=x.shape[0], axis=0)
u_ = w_expand*x_expand
# u_ = nd.abs(w_expand - x_expand)
u = nd.sum(u_, axis = 1)
u_no_gradient = nd.stop_gradient(u)
for i in range(self.route_num):
c_mat = nd.softmax(b_mat, axis=2)
if i == self.route_num -1:
s = nd.sum(u * c_mat, axis=-1)
else:
s = nd.sum(u_no_gradient * c_mat, axis=-1)
v = squash(s, 1)
v1 = nd.expand_dims(v, axis=-1)
if i != self.route_num - 1:
update_term = nd.sum(u_no_gradient*v1, axis=1, keepdims=True)
b_mat = b_mat + update_term
return v
示例4: get_distance_matrix
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def get_distance_matrix(x):
"""Get distance matrix given a matrix. Used in testing."""
square = nd.sum(x ** 2.0, axis=1, keepdims=True)
distance_square = square + square.transpose() - (2.0 * nd.dot(x, x.transpose()))
return nd.sqrt(distance_square)
示例5: __init__
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def __init__(self, mods, aggregate='sum'):
super(HeteroGraphConv, self).__init__()
with self.name_scope():
for name, mod in mods.items():
self.register_child(mod, name)
self.mods = mods
if isinstance(aggregate, str):
self.agg_fn = get_aggregate_fn(aggregate)
else:
self.agg_fn = aggregate
示例6: _mixup_forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def _mixup_forward(self, F, pred, label1, label2, lam, sample_weight=None):
if not self._from_logits:
pred = F.log_softmax(pred, self._axis)
if self._sparse_label:
loss1 = -F.pick(pred, label1, axis=self._axis, keepdims=True)
loss2 = -F.pick(pred, label2, axis=self._axis, keepdims=True)
loss = lam * loss1 + (1 - lam) * loss2
else:
label1 = _reshape_like(F, label1, pred)
label2 = _reshape_like(F, label2, pred)
loss1 = -F.sum(pred*label1, axis=self._axis, keepdims=True)
loss2 = -F.sum(pred*label2, axis=self._axis, keepdims=True)
loss = lam * loss1 + (1 - lam) * loss2
loss = _apply_weighting(F, loss, self._weight, sample_weight)
return F.mean(loss, axis=self._batch_axis, exclude=True)
示例7: weight_l1_loss
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def weight_l1_loss(self, F, pred_loc, label_loc, loss_weight):
"""Compute weight_l1_loss"""
pred_loc = pred_loc.reshape((self.b, 4, -1, self.h, self.w))
diff = F.abs((pred_loc - label_loc))
diff = F.sum(diff, axis=1).reshape((self.b, -1, self.h, self.w))
loss = diff * loss_weight
return F.sum(loss)/self.b
示例8: evaluate_accuracy
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def evaluate_accuracy(data_iterator, net, ctx=[mx.cpu()]):
if isinstance(ctx, mx.Context):
ctx = [ctx]
acc = nd.array([0])
n = 0.
if isinstance(data_iterator, mx.io.MXDataIter):
data_iterator.reset()
for batch in data_iterator:
data, label, batch_size = _get_batch(batch, ctx)
for X, y in zip(data, label):
acc += nd.sum(net(X).argmax(axis=1) == y).copyto(mx.cpu())
n += y.size
acc.wait_to_read() # don't push too many operators into backend
return acc.asscalar() / n
示例9: train
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def train(train_data, test_data, net, loss, trainer, ctx, num_epochs, print_batches=None):
"""Train a network"""
print("Start training on ", ctx)
if isinstance(ctx, mx.Context):
ctx = [ctx]
for epoch in range(num_epochs):
train_loss, train_acc, n, m = 0.0, 0.0, 0.0, 0.0
if isinstance(train_data, mx.io.MXDataIter):
train_data.reset()
start = time()
for i, batch in enumerate(train_data):
data, label, batch_size = _get_batch(batch, ctx)
losses = []
with autograd.record():
outputs = [net(X) for X in data]
losses = [loss(yhat, y) for yhat, y in zip(outputs, label)]
for l in losses:
l.backward()
train_acc += sum([(yhat.argmax(axis=1) == y).sum().asscalar()
for yhat, y in zip(outputs, label)])
train_loss += sum([l.sum().asscalar() for l in losses])
trainer.step(batch_size)
n += batch_size
m += sum([y.size for y in label])
if print_batches and (i + 1) % print_batches == 0:
print("Batch %d. Loss: %f, Train acc %f" % (
n, train_loss / n, train_acc / m
))
test_acc = evaluate_accuracy(test_data, net, ctx)
print("Epoch %d. Loss: %.3f, Train acc %.2f, Test acc %.2f, Time %.1f sec" % (
epoch, train_loss / n, train_acc / m, test_acc, time() - start
))
示例10: grad_clipping
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def grad_clipping(params, clipping_norm, ctx):
"""Gradient clipping."""
if clipping_norm is not None:
norm = nd.array([0.0], ctx)
for p in params:
norm += nd.sum(p.grad ** 2)
norm = nd.sqrt(norm).asscalar()
if norm > clipping_norm:
for p in params:
p.grad[:] *= clipping_norm / norm
示例11: squash
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def squash(x, axis):
s_squared_norm = nd.sum(nd.square(x), axis, keepdims=True)
# if s_squared_norm is really small, we will be in trouble
# so I removed the s_quare terms
# scale = s_squared_norm / ((1 + s_squared_norm) * nd.sqrt(s_squared_norm + 1e-9))
# return x * scale
scale = nd.sqrt(s_squared_norm + 1e-9)
return x / scale
示例12: route
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def route(self, u):
b_mat = nd.zeros((u.shape[0], self.num_cap_in, self.num_cap, 1, u.shape[4], u.shape[5]), ctx=u.context)
for i in range(self.route_num):
c_mat = nd.softmax(b_mat, axis=2)
s = nd.sum(u * c_mat, axis=1)
v = squash(s, 2)
if i != self.route_num - 1:
v1 = nd.expand_dims(v, axis=1)
update_term = nd.sum(u*v1, axis=3, keepdims=True)
b_mat = b_mat + update_term
return v
示例13: forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def forward(self, x):
x = nd.sqrt(nd.sum(nd.square(x), 1))
return x
示例14: update
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def update(self, data, label, mask):
data = self.scaler.inverse_transform(data)
label = self.scaler.inverse_transform(label)
_cnt = nd.sum(mask).as_in_context(mx.cpu())
_loss = nd.sum((data - label) ** 2 * mask).as_in_context(mx.cpu())
if self.cnt is None:
self.cnt = 0
self.loss = 0
self.cnt += _cnt
self.loss += _loss
示例15: update
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import sum [as 别名]
def update(self, data, label):
data = self.scaler.inverse_transform(data)
label = self.scaler.inverse_transform(label)
_cnt = data.size
_loss = nd.sum((data - label) ** 2).as_in_context(mx.cpu())
if self.cnt is None:
self.cnt = 0
self.loss = 0
self.cnt += _cnt
self.loss += _loss