本文整理匯總了Python中mxnet.nd.abs方法的典型用法代碼示例。如果您正苦於以下問題:Python nd.abs方法的具體用法?Python nd.abs怎麽用?Python nd.abs使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mxnet.nd
的用法示例。
在下文中一共展示了nd.abs方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _reg_loss
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import abs [as 別名]
def _reg_loss(regr, gt_regr, mask):
''' L1 regression loss
Arguments:
regr (batch x max_objects x dim)
gt_regr (batch x max_objects x dim)
mask (batch x max_objects)
'''
num = mask.astype('float32').sum()
mask = mask.expand_dims(2).broadcast_like(gt_regr).astype('float32')
regr = regr * mask
gt_regr = gt_regr * mask
t = nd.abs(regr - gt_regr)
regt_loss = smooth_l1(t).sum()
regr_loss = regr_loss / (num + 1e-4)
return regr_loss
示例2: Route
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import abs [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
示例3: hybrid_forward
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import abs [as 別名]
def hybrid_forward(self, F, pred, label, mask, sample_weight=None):
label = _reshape_like(F, label, pred)
loss = F.abs(label * mask - pred * mask)
loss = _apply_weighting(F, loss, self._weight, sample_weight)
norm = F.sum(mask).clip(1, 1e30)
return F.sum(loss) / norm
示例4: weight_l1_loss
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import abs [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
示例5: extract_multi_position_matrix_nd
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import abs [as 別名]
def extract_multi_position_matrix_nd(bbox):
bbox = nd.transpose(bbox, axes=(1, 0, 2))
xmin, ymin, xmax, ymax = nd.split(data=bbox, num_outputs=4, axis=2)
# [num_fg_classes, num_boxes, 1]
bbox_width = xmax - xmin + 1.
bbox_height = ymax - ymin + 1.
center_x = 0.5 * (xmin + xmax)
center_y = 0.5 * (ymin + ymax)
# [num_fg_classes, num_boxes, num_boxes]
delta_x = nd.broadcast_minus(lhs=center_x,
rhs=nd.transpose(center_x, axes=(0, 2, 1)))
delta_x = nd.broadcast_div(delta_x, bbox_width)
delta_x = nd.log(nd.maximum(nd.abs(delta_x), 1e-3))
delta_y = nd.broadcast_minus(lhs=center_y,
rhs=nd.transpose(center_y, axes=(0, 2, 1)))
delta_y = nd.broadcast_div(delta_y, bbox_height)
delta_y = nd.log(nd.maximum(nd.abs(delta_y), 1e-3))
delta_width = nd.broadcast_div(lhs=bbox_width,
rhs=nd.transpose(bbox_width, axes=(0, 2, 1)))
delta_width = nd.log(delta_width)
delta_height = nd.broadcast_div(lhs=bbox_height,
rhs=nd.transpose(bbox_height, axes=(0, 2, 1)))
delta_height = nd.log(delta_height)
concat_list = [delta_x, delta_y, delta_width, delta_height]
for idx, sym in enumerate(concat_list):
concat_list[idx] = nd.expand_dims(sym, axis=3)
position_matrix = nd.concat(*concat_list, dim=3)
return position_matrix
示例6: _slow_reg_loss
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import abs [as 別名]
def _slow_reg_loss(regr, gt_regr, mask):
num = mask.float().sum()
mask = mask.unsqueeze(2).expand_as(gt_regr)
regr = regr[mask]
gt_regr = gt_regr[mask]
t = nd.abs(regr - gt_regr)
regt_loss = smooth_l1(t).sum()
regr_loss = regr_loss / (num + 1e-4)
return regr_loss
示例7: forward
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import abs [as 別名]
def forward(self, output, mask, ind, target):
pred = _tranpose_and_gather_feat(output, ind)
pred = pred.swapaxes(dim1 = 0, dim2 = 1)
mask = mask.expand_dims(axis = 2).broadcast_like(pred).astype('float32')
loss = nd.abs(pred*mask - target*mask).sum()
loss = loss / (mask.sum() + 1e-4)
return loss
示例8: compute_res_loss
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import abs [as 別名]
def compute_res_loss(output, target, maski):
diff = nd.abs(output * maski - target * maski)
return nd.smooth_l1(diff).mean()
示例9: forward
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import abs [as 別名]
def forward(self, cls_pred, box_pred, cls_target, box_target):
"""Compute loss in entire batch across devices."""
# require results across different devices at this time
cls_pred, box_pred, cls_target, box_target = [_as_list(x) \
for x in (cls_pred, box_pred, cls_target, box_target)]
# cross device reduction to obtain positive samples in entire batch
num_pos = []
for cp, bp, ct, bt in zip(*[cls_pred, box_pred, cls_target, box_target]):
pos_samples = (ct > 0)
num_pos.append(pos_samples.sum())
num_pos_all = sum([p.asscalar() for p in num_pos])
if num_pos_all < 1:
# no positive samples found, return dummy losses
return nd.zeros((1,)), nd.zeros((1,)), nd.zeros((1,))
# compute element-wise cross entropy loss and sort, then perform negative mining
cls_losses = []
box_losses = []
sum_losses = []
for cp, bp, ct, bt in zip(*[cls_pred, box_pred, cls_target, box_target]):
pred = nd.log_softmax(cp, axis=-1)
pos = ct > 0
cls_loss = -nd.pick(pred, ct, axis=-1, keepdims=False)
rank = (cls_loss * (pos - 1)).argsort(axis=1).argsort(axis=1)
hard_negative = rank < (pos.sum(axis=1) * self._negative_mining_ratio).expand_dims(-1)
# mask out if not positive or negative
cls_loss = nd.where((pos + hard_negative) > 0, cls_loss, nd.zeros_like(cls_loss))
cls_losses.append(nd.sum(cls_loss, axis=0, exclude=True) / num_pos_all)
bp = _reshape_like(nd, bp, bt)
box_loss = nd.abs(bp - bt)
box_loss = nd.where(box_loss > self._rho, box_loss - 0.5 * self._rho,
(0.5 / self._rho) * nd.square(box_loss))
# box loss only apply to positive samples
box_loss = box_loss * pos.expand_dims(axis=-1)
box_losses.append(nd.sum(box_loss, axis=0, exclude=True) / num_pos_all)
sum_losses.append(cls_losses[-1] + self._lambd * box_losses[-1])
return sum_losses, cls_losses, box_losses
示例10: forward
# 需要導入模塊: from mxnet import nd [as 別名]
# 或者: from mxnet.nd import abs [as 別名]
def forward(self, cls_pred, box_pred, cls_target, box_target):
"""Compute loss in entire batch across devices."""
# require results across different devices at this time
cls_pred, box_pred, cls_target, box_target = [_as_list(x) \
for x in (cls_pred, box_pred, cls_target, box_target)]
# cross device reduction to obtain positive samples in entire batch
num_pos = []
for cp, bp, ct, bt in zip(*[cls_pred, box_pred, cls_target, box_target]):
pos_samples = (ct > 0)
num_pos.append(pos_samples.sum())
num_pos_all = sum([p.asscalar() for p in num_pos])
if num_pos_all < 1 and self._min_hard_negatives < 1:
# no positive samples and no hard negatives, return dummy losses
cls_losses = [nd.sum(cp * 0) for cp in cls_pred]
box_losses = [nd.sum(bp * 0) for bp in box_pred]
sum_losses = [nd.sum(cp * 0) + nd.sum(bp * 0) for cp, bp in zip(cls_pred, box_pred)]
return sum_losses, cls_losses, box_losses
# compute element-wise cross entropy loss and sort, then perform negative mining
cls_losses = []
box_losses = []
sum_losses = []
for cp, bp, ct, bt in zip(*[cls_pred, box_pred, cls_target, box_target]):
pred = nd.log_softmax(cp, axis=-1)
pos = ct > 0
cls_loss = -nd.pick(pred, ct, axis=-1, keepdims=False)
rank = (cls_loss * (pos - 1)).argsort(axis=1).argsort(axis=1)
hard_negative = rank < nd.maximum(self._min_hard_negatives, pos.sum(axis=1)
* self._negative_mining_ratio).expand_dims(-1)
# mask out if not positive or negative
cls_loss = nd.where((pos + hard_negative) > 0, cls_loss, nd.zeros_like(cls_loss))
cls_losses.append(nd.sum(cls_loss, axis=0, exclude=True) / max(1., num_pos_all))
bp = _reshape_like(nd, bp, bt)
box_loss = nd.abs(bp - bt)
box_loss = nd.where(box_loss > self._rho, box_loss - 0.5 * self._rho,
(0.5 / self._rho) * nd.square(box_loss))
# box loss only apply to positive samples
box_loss = box_loss * pos.expand_dims(axis=-1)
box_losses.append(nd.sum(box_loss, axis=0, exclude=True) / max(1., num_pos_all))
sum_losses.append(cls_losses[-1] + self._lambd * box_losses[-1])
return sum_losses, cls_losses, box_losses