本文整理匯總了Python中mxnet.gluon.loss.Loss方法的典型用法代碼示例。如果您正苦於以下問題:Python loss.Loss方法的具體用法?Python loss.Loss怎麽用?Python loss.Loss使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mxnet.gluon.loss
的用法示例。
在下文中一共展示了loss.Loss方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: hybrid_forward
# 需要導入模塊: from mxnet.gluon import loss [as 別名]
# 或者: from mxnet.gluon.loss import Loss [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)
示例2: hybrid_forward
# 需要導入模塊: from mxnet.gluon import loss [as 別名]
# 或者: from mxnet.gluon.loss import Loss [as 別名]
def hybrid_forward(self, F, fts, ys, ftt, yt):
"""
Semantic Alignment Loss
:param F: Function
:param yt: label for the target domain [N]
:param ftt: features for the target domain [N, K]
:param ys: label for the source domain [M]
:param fts: features for the source domain [M, K]
:return:
"""
if self._fn:
# Normalize ft
fts = F.L2Normalization(fts, mode='instance')
ftt = F.L2Normalization(ftt, mode='instance')
fts_rpt = F.broadcast_to(fts.expand_dims(axis=0), shape=(self._bs_tgt, self._bs_src, self._embed_size))
ftt_rpt = F.broadcast_to(ftt.expand_dims(axis=1), shape=(self._bs_tgt, self._bs_src, self._embed_size))
dists = F.sum(F.square(ftt_rpt - fts_rpt), axis=2)
yt_rpt = F.broadcast_to(yt.expand_dims(axis=1), shape=(self._bs_tgt, self._bs_src)).astype('int32')
ys_rpt = F.broadcast_to(ys.expand_dims(axis=0), shape=(self._bs_tgt, self._bs_src)).astype('int32')
y_same = F.equal(yt_rpt, ys_rpt).astype('float32')
y_diff = F.not_equal(yt_rpt, ys_rpt).astype('float32')
intra_cls_dists = dists * y_same
inter_cls_dists = dists * y_diff
max_dists = F.max(dists, axis=1, keepdims=True)
max_dists = F.broadcast_to(max_dists, shape=(self._bs_tgt, self._bs_src))
revised_inter_cls_dists = F.where(y_same, max_dists, inter_cls_dists)
max_intra_cls_dist = F.max(intra_cls_dists, axis=1)
min_inter_cls_dist = F.min(revised_inter_cls_dists, axis=1)
loss = F.relu(max_intra_cls_dist - min_inter_cls_dist + self._margin)
return loss
示例3: __init__
# 需要導入模塊: from mxnet.gluon import loss [as 別名]
# 或者: from mxnet.gluon.loss import Loss [as 別名]
def __init__(self, loss_type: Loss=L2Loss, weight: float=1, batch_axis: int=0) -> None:
"""
Loss for Q-Value Head.
:param loss_type: loss function with default of mean squared error (i.e. L2Loss).
:param weight: scalar used to adjust relative weight of loss (if using this loss with others).
:param batch_axis: axis used for mini-batch (default is 0) and excluded from loss aggregation.
"""
super(QHeadLoss, self).__init__(weight=weight, batch_axis=batch_axis)
with self.name_scope():
self.loss_fn = loss_type(weight=weight, batch_axis=batch_axis)
示例4: loss
# 需要導入模塊: from mxnet.gluon import loss [as 別名]
# 或者: from mxnet.gluon.loss import Loss [as 別名]
def loss(self) -> Loss:
"""
Specifies loss block to be used for specific value head implementation.
:return: loss block (can be called as function) for outputs returned by the head network.
"""
return QHeadLoss(loss_type=self.loss_type, weight=self.loss_weight)
示例5: __init__
# 需要導入模塊: from mxnet.gluon import loss [as 別名]
# 或者: from mxnet.gluon.loss import Loss [as 別名]
def __init__(self, loss_type: Loss=L2Loss, weight: float=1, batch_axis: int=0) -> None:
"""
Loss for Value Head.
:param loss_type: loss function with default of mean squared error (i.e. L2Loss).
:param weight: scalar used to adjust relative weight of loss (if using this loss with others).
:param batch_axis: axis used for mini-batch (default is 0) and excluded from loss aggregation.
"""
super(VHeadLoss, self).__init__(weight=weight, batch_axis=batch_axis)
with self.name_scope():
self.loss_fn = loss_type(weight=weight, batch_axis=batch_axis)
示例6: loss
# 需要導入模塊: from mxnet.gluon import loss [as 別名]
# 或者: from mxnet.gluon.loss import Loss [as 別名]
def loss(self) -> Loss:
"""
Specifies loss block to be used for specific value head implementation.
:return: loss block (can be called as function) for outputs returned by the head network.
"""
return VHeadLoss(loss_type=self.loss_type, weight=self.loss_weight)
示例7: hybrid_forward
# 需要導入模塊: from mxnet.gluon import loss [as 別名]
# 或者: from mxnet.gluon.loss import Loss [as 別名]
def hybrid_forward(self, F, objness, box_centers, box_scales, cls_preds,
objness_t, center_t, scale_t, weight_t, class_t, class_mask):
"""Compute YOLOv3 losses.
Parameters
----------
objness : mxnet.nd.NDArray
Predicted objectness (B, N), range (0, 1).
box_centers : mxnet.nd.NDArray
Predicted box centers (x, y) (B, N, 2), range (0, 1).
box_scales : mxnet.nd.NDArray
Predicted box scales (width, height) (B, N, 2).
cls_preds : mxnet.nd.NDArray
Predicted class predictions (B, N, num_class), range (0, 1).
objness_t : mxnet.nd.NDArray
Objectness target, (B, N), 0 for negative 1 for positive, -1 for ignore.
center_t : mxnet.nd.NDArray
Center (x, y) targets (B, N, 2).
scale_t : mxnet.nd.NDArray
Scale (width, height) targets (B, N, 2).
weight_t : mxnet.nd.NDArray
Loss Multipliers for center and scale targets (B, N, 2).
class_t : mxnet.nd.NDArray
Class targets (B, N, num_class).
It's relaxed one-hot vector, i.e., (1, 0, 1, 0, 0).
It can contain more than one positive class.
class_mask : mxnet.nd.NDArray
0 or 1 mask array to mask out ignored samples (B, N, num_class).
Returns
-------
tuple of NDArrays
obj_loss: sum of objectness logistic loss
center_loss: sum of box center logistic regression loss
scale_loss: sum of box scale l1 loss
cls_loss: sum of per class logistic loss
"""
# compute some normalization count, except batch-size
denorm = F.cast(
F.shape_array(objness_t).slice_axis(axis=0, begin=1, end=None).prod(), 'float32')
weight_t = F.broadcast_mul(weight_t, objness_t)
hard_objness_t = F.where(objness_t > 0, F.ones_like(objness_t), objness_t)
new_objness_mask = F.where(objness_t > 0, objness_t, objness_t >= 0)
obj_loss = F.broadcast_mul(
self._sigmoid_ce(objness, hard_objness_t, new_objness_mask), denorm)
center_loss = F.broadcast_mul(self._sigmoid_ce(box_centers, center_t, weight_t), denorm * 2)
scale_loss = F.broadcast_mul(self._l1_loss(box_scales, scale_t, weight_t), denorm * 2)
denorm_class = F.cast(
F.shape_array(class_t).slice_axis(axis=0, begin=1, end=None).prod(), 'float32')
class_mask = F.broadcast_mul(class_mask, objness_t)
cls_loss = F.broadcast_mul(self._sigmoid_ce(cls_preds, class_t, class_mask), denorm_class)
return obj_loss, center_loss, scale_loss, cls_loss