Exemplo n.º 1
0
    def __init__(self,
                 mean=None,
                 sd=None,
                 seed=0,
                 dtype=mstype.float32,
                 name="Normal"):
        """
        Constructor of normal distribution.
        """
        param = dict(locals())
        valid_dtype = mstype.float_type
        check_type(dtype, valid_dtype, "Normal")
        super(Normal, self).__init__(seed, dtype, name, param)
        if  mean is not None and sd is not None:
            self._mean_value = convert_to_batch(mean, self.broadcast_shape, dtype)
            self._sd_value = convert_to_batch(sd, self.broadcast_shape, dtype)
            check_greater_zero(self._sd_value, "Standard deviation")
        else:
            self._mean_value = mean
            self._sd_value = sd


        #ops needed for the class
        self.const = P.ScalarToArray()
        self.erf = P.Erf()
        self.exp = P.Exp()
        self.expm1 = self._expm1_by_step
        self.fill = P.Fill()
        self.log = P.Log()
        self.shape = P.Shape()
        self.sq = P.Square()
        self.sqrt = P.Sqrt()
        self.zeroslike = P.ZerosLike()
Exemplo n.º 2
0
def log_generic(input_x):
    """
    Log op on Ascend is calculated as log(abs(x)).
    Fix this with putting negative values as nan.
    And log op on Ascend doesn't supprot int types.
    Fix this with casting the type.
    """
    log = P.Log()
    less = P.Less()
    lessequal = P.LessEqual()
    fill = P.Fill()
    cast = P.Cast()
    dtype = P.DType()
    shape = P.Shape()
    select = P.Select()
    checktype = P.IsSubClass()

    if not checktype(dtype(input_x), mstype.float_):
        input_x = cast(input_x, mstype.float32)
    nan = fill(dtype(input_x), shape(input_x), np.nan)
    inf = fill(dtype(input_x), shape(input_x), np.inf)
    neg_x = less(input_x, 0.0)
    nonpos_x = lessequal(input_x, 0.0)
    log_x = log(input_x)
    result = select(nonpos_x, -inf, log_x)
    return select(neg_x, nan, result)
    def construct(self, data):
        weights = self.weights
        loss = self.network(data)
        sens = P.Fill()(P.DType()(loss), P.Shape()(loss), self.sens)
        grads = self.grad(self.network, weights)(data, sens)

        return F.depend(loss, self.optimizer(grads))
Exemplo n.º 4
0
    def __init__(self, sharpness=1.0, name='Softplus'):
        """
        Constructor of Softplus Bijector.
        """
        param = dict(locals())
        param['param_dict'] = {'sharpness': sharpness}
        super(Softplus, self).__init__(name=name, dtype=None, param=param)
        self._sharpness = self._add_parameter(sharpness, 'sharpness')

        self.exp = exp_generic
        self.log = log_generic
        self.expm1 = P.Expm1()
        self.abs = P.Abs()
        self.dtypeop = P.DType()
        self.cast = P.Cast()
        self.fill = P.Fill()
        self.greater = P.Greater()
        self.less = P.Less()
        self.log_sigmoid = LogSigmoid()
        self.logicalor = P.LogicalOr()
        self.select = P.Select()
        self.shape = P.Shape()
        self.sigmoid = P.Sigmoid()
        self.softplus = self._softplus
        self.inverse_softplus = self._inverse_softplus

        self.threshold = np.log(np.finfo(np.float32).eps) + 1
        self.tiny = np.exp(self.threshold)
Exemplo n.º 5
0
 def construct(self, batch_ids, batch_wts, label):
     weights = self.weights
     loss = self.network(batch_ids, batch_wts, label)
     sens = P.Fill()(P.DType()(loss), P.Shape()(loss), self.sens)  #
     grads = self.grad(self.network, weights)(batch_ids, batch_wts, label,
                                              sens)
     return F.depend(loss, self.optimizer(grads))
Exemplo n.º 6
0
    def __init__(self,
                 rate=None,
                 seed=0,
                 dtype=mstype.float32,
                 name="Exponential"):
        """
        Constructor of Exponential distribution.
        """
        param = dict(locals())
        valid_dtype = mstype.float_type
        check_type(dtype, valid_dtype, "Exponential")
        super(Exponential, self).__init__(seed, dtype, name, param)
        if rate is not None:
            self._rate = cast_to_tensor(rate, dtype)
            check_greater_zero(self._rate, "rate")
        else:
            self._rate = rate

        self.minval = np.finfo(np.float).tiny

        # ops needed for the class
        self.cast = P.Cast()
        self.const = P.ScalarToArray()
        self.dtypeop = P.DType()
        self.exp = P.Exp()
        self.fill = P.Fill()
        self.less = P.Less()
        self.log = P.Log()
        self.select = P.Select()
        self.shape = P.Shape()
        self.sqrt = P.Sqrt()
        self.sq = P.Square()
        self.uniform = C.uniform
Exemplo n.º 7
0
    def __init__(self,
                 probs=None,
                 seed=0,
                 dtype=mstype.int32,
                 name="Geometric"):
        """
        Constructor of Geometric distribution.
        """
        param = dict(locals())
        valid_dtype = mstype.int_type + mstype.uint_type
        check_type(dtype, valid_dtype, "Geometric")
        super(Geometric, self).__init__(seed, dtype, name, param)
        if probs is not None:
            self._probs = cast_to_tensor(probs, hint_dtype=mstype.float32)
            check_prob(self._probs)
        else:
            self._probs = probs

        self.minval = np.finfo(np.float).tiny

        # ops needed for the class
        self.cast = P.Cast()
        self.const = P.ScalarToArray()
        self.dtypeop = P.DType()
        self.fill = P.Fill()
        self.floor = P.Floor()
        self.issubclass = P.IsSubClass()
        self.less = P.Less()
        self.log = P.Log()
        self.pow = P.Pow()
        self.select = P.Select()
        self.shape = P.Shape()
        self.sq = P.Square()
        self.sqrt = P.Sqrt()
        self.uniform = C.uniform
Exemplo n.º 8
0
    def __init__(self,
                 bijector,
                 distribution,
                 seed=None,
                 name="transformed_distribution"):
        """
        Constructor of transformed_distribution class.
        """
        param = dict(locals())
        validator.check_value_type('bijector', bijector,
                                   [nn.probability.bijector.Bijector], type(self).__name__)
        validator.check_value_type('distribution', distribution,
                                   [Distribution], type(self).__name__)
        super(TransformedDistribution, self).__init__(seed, distribution.dtype, name, param)

        self._bijector = bijector
        self._distribution = distribution
        self._is_linear_transformation = bijector.is_constant_jacobian
        self.default_parameters = distribution.default_parameters
        self.parameter_names = distribution.parameter_names

        self.exp = exp_generic
        self.log = log_generic
        self.isnan = P.IsNan()
        self.equal_base = P.Equal()
        self.select_base = P.Select()
        self.fill = P.Fill()

        # check if batch shape of the distribution and event shape is broadcastable
        if hasattr(self.bijector, 'event_shape'):
            event_shape_tensor = self.fill(self.dtype, self.bijector.event_shape, 0.0)
            broadcast_shape_tensor = self.fill(self.dtype, self.broadcast_shape, 0.0)
            self._batch_event = (event_shape_tensor + broadcast_shape_tensor).shape
        else:
            self._batch_event = self.broadcast_shape
Exemplo n.º 9
0
    def __init__(self,
                 probs=None,
                 seed=None,
                 dtype=mstype.int32,
                 name="Bernoulli"):
        """
        Constructor of Bernoulli.
        """
        param = dict(locals())
        param['param_dict'] = {'probs': probs}
        valid_dtype = mstype.int_type + mstype.uint_type + mstype.float_type
        Validator.check_type(type(self).__name__, dtype, valid_dtype)
        super(Bernoulli, self).__init__(seed, dtype, name, param)

        self._probs = self._add_parameter(probs, 'probs')
        if self._probs is not None:
            check_prob(self.probs)

        # ops needed for the class
        self.exp = exp_generic
        self.log = log_generic
        self.squeeze = P.Squeeze(0)
        self.cast = P.Cast()
        self.const = P.ScalarToArray()
        self.floor = P.Floor()
        self.fill = P.Fill()
        self.less = P.Less()
        self.shape = P.Shape()
        self.select = P.Select()
        self.uniform = C.uniform
Exemplo n.º 10
0
    def __init__(self):
        super(IGamma, self).__init__()
        # const numbers
        # If more data types are supported, this float max value need to be selected.
        self.log_maxfloat32 = Tensor(np.log(np.finfo(np.float32).max),
                                     mstype.float32)

        # operations
        self.logicaland = P.LogicalAnd()
        self.logicalor = P.LogicalOr()
        self.logicalnot = P.LogicalNot()
        self.equal = P.Equal()
        self.greater = P.Greater()
        self.less = P.Less()
        self.neg = P.Neg()
        self.log = P.Log()
        self.exp = P.Exp()
        self.select = P.Select()
        self.zeroslike = P.ZerosLike()
        self.fill = P.Fill()
        self.shape = P.Shape()
        self.dtype = P.DType()
        self.lgamma = LGamma()
        self.const = P.ScalarToArray()
        self.cast = P.Cast()
Exemplo n.º 11
0
    def __init__(self):
        super(DiGamma, self).__init__()
        # const numbers
        self.k_lanczos_gamma = 7
        self.k_base_lanczos_coeff = 0.99999999999980993227684700473478
        self.k_lanczos_coefficients = [
            676.520368121885098567009190444019,
            -1259.13921672240287047156078755283,
            771.3234287776530788486528258894, -176.61502916214059906584551354,
            12.507343278686904814458936853, -0.13857109526572011689554707,
            9.984369578019570859563e-6, 1.50563273514931155834e-7
        ]
        self.nan = np.nan
        self.pi = np.pi
        self.lanczos_gamma_plus_one_half = self.k_lanczos_gamma + 0.5
        self.log_lanczos_gamma_plus_one_half = np.log(
            self.lanczos_gamma_plus_one_half)

        # operations
        self.log1p = P.Log1p()
        self.abs = P.Abs()
        self.shape = P.Shape()
        self.dtype = P.DType()
        self.fill = P.Fill()
        self.floor = P.Floor()
        self.equal = P.Equal()
        self.less = P.Less()
        self.select = P.Select()
        self.sin = P.Sin()
        self.cos = P.Cos()
        self.logicaland = P.LogicalAnd()
Exemplo n.º 12
0
 def __init__(self, strategy1, strategy2, strategy3):
     super().__init__()
     self.matmul1 = P.MatMul().set_strategy(strategy1)
     self.matmul2 = P.MatMul().set_strategy(strategy2)
     self.matmul3 = P.MatMul().set_strategy(strategy3)
     self.diag = P.Diag()
     self.fill = P.Fill()
Exemplo n.º 13
0
    def __init__(self,
                 seed,
                 dtype,
                 name,
                 param):
        """
        Constructor of distribution class.
        """
        super(Distribution, self).__init__()
        if seed is None:
            seed = 0
        validator.check_value_type('name', name, [str], type(self).__name__)
        validator.check_non_negative_int(seed, 'seed', name)

        self._name = name
        self._seed = seed
        self._dtype = cast_type_for_device(dtype)
        self._parameters = {}

        # parsing parameters
        for k in param.keys():
            if not(k == 'self' or k.startswith('_')):
                self._parameters[k] = param[k]

        # some attributes
        if 'distribution' in self.parameters.keys():
            self.parameter_type = self.parameters['distribution'].parameter_type
        else:
            self.parameter_type = set_param_type(self.parameters['param_dict'], dtype)
        self._broadcast_shape = self._calc_broadcast_shape()
        self._is_scalar_batch = self._check_is_scalar_batch()

        # set the function to call according to the derived class's attributes
        self._set_prob()
        self._set_log_prob()
        self._set_sd()
        self._set_var()
        self._set_cdf()
        self._set_survival()
        self._set_log_cdf()
        self._set_log_survival()
        self._set_cross_entropy()

        self.context_mode = context.get_context('mode')
        self.device_target = context.get_context('device_target')
        self.checktuple = CheckTuple()
        self.checktensor = CheckTensor()
        self.broadcast = broadcast_to

        # ops needed for the base class
        self.cast_base = P.Cast()
        self.dtype_base = P.DType()
        self.exp_base = exp_generic
        self.fill_base = P.Fill()
        self.log_base = log_generic
        self.sametypeshape_base = P.SameTypeShape()
        self.sq_base = P.Square()
        self.sqrt_base = P.Sqrt()
        self.shape_base = P.Shape()
Exemplo n.º 14
0
    def __init__(self, config, batch_size, in_channels, feat_channels,
                 num_anchors, cls_out_channels):
        super(RPN, self).__init__()
        cfg_rpn = config
        self.dtype = np.float32
        self.ms_type = mstype.float32
        self.num_bboxes = cfg_rpn.num_bboxes
        self.slice_index = ()
        self.feature_anchor_shape = ()
        self.slice_index += (0, )
        index = 0
        for shape in cfg_rpn.feature_shapes:
            self.slice_index += (self.slice_index[index] +
                                 shape[0] * shape[1] * num_anchors, )
            self.feature_anchor_shape += (shape[0] * shape[1] * num_anchors *
                                          batch_size, )
            index += 1

        self.num_anchors = num_anchors
        self.batch_size = batch_size
        self.test_batch_size = cfg_rpn.test_batch_size
        self.num_layers = 5
        self.real_ratio = Tensor(np.ones((1, 1)).astype(self.dtype))

        self.rpn_convs_list = nn.layer.CellList(
            self._make_rpn_layer(self.num_layers, in_channels, feat_channels,
                                 num_anchors, cls_out_channels))

        self.transpose = P.Transpose()
        self.reshape = P.Reshape()
        self.concat = P.Concat(axis=0)
        self.fill = P.Fill()
        self.placeh1 = Tensor(np.ones((1, )).astype(self.dtype))

        self.trans_shape = (0, 2, 3, 1)

        self.reshape_shape_reg = (-1, 4)
        self.reshape_shape_cls = (-1, )
        self.rpn_loss_reg_weight = Tensor(
            np.array(cfg_rpn.rpn_loss_reg_weight).astype(self.dtype))
        self.rpn_loss_cls_weight = Tensor(
            np.array(cfg_rpn.rpn_loss_cls_weight).astype(self.dtype))
        self.num_expected_total = Tensor(
            np.array(cfg_rpn.num_expected_neg * self.batch_size).astype(
                self.dtype))
        self.num_bboxes = cfg_rpn.num_bboxes
        self.get_targets = BboxAssignSample(cfg_rpn, self.batch_size,
                                            self.num_bboxes, False)
        self.CheckValid = P.CheckValid()
        self.sum_loss = P.ReduceSum()
        self.loss_cls = P.SigmoidCrossEntropyWithLogits()
        self.loss_bbox = P.SmoothL1Loss(beta=1.0 / 9.0)
        self.squeeze = P.Squeeze()
        self.cast = P.Cast()
        self.tile = P.Tile()
        self.zeros_like = P.ZerosLike()
        self.loss = Tensor(np.zeros((1, )).astype(self.dtype))
        self.clsloss = Tensor(np.zeros((1, )).astype(self.dtype))
        self.regloss = Tensor(np.zeros((1, )).astype(self.dtype))
Exemplo n.º 15
0
 def construct(self, x):
     shape = self.shape(x)
     dtype = self.dtype(x)
     ones_tensor = P.Fill()(dtype, (shape[0], 1, 1, 1), 1)
     _, mask = self.dropout(ones_tensor)
     x = x * mask
     x = x / self.keep_prob_tensor
     return x
Exemplo n.º 16
0
 def construct(self, input_x):
     """fusedlayernorm"""
     if self.use_batch_norm and self.training:
         ones = P.Fill()(mstype.float32,
                         F.shape(input_x)[:self.begin_norm_axis], 1.0)
         zeros = P.Fill()(mstype.float32,
                          F.shape(input_x)[:self.begin_norm_axis], 0.0)
         shape_x = F.shape(input_x)
         norm_shape = get_shape_for_norm(shape_x, self.begin_norm_axis)
         input_x = F.reshape(input_x, norm_shape)
         output, _, _, _, _, _ = self.batch_norm(input_x, ones, zeros, None,
                                                 None)
         output = F.reshape(output, shape_x)
         y = output * self.gamma + self.beta
     else:
         y, _, _ = self.layer_norm(input_x, self.gamma, self.beta)
     return y
Exemplo n.º 17
0
 def construct(self, *args):
     weights = self.weights
     loss = self.network(*args)
     sens = P.Fill()(P.DType()(loss), P.Shape()(loss), self.sens)
     grads = self.grad(self.network, weights)(*args, sens)
     if self.reducer_flag:
         grads = self.grad_reducer(grads)
     return F.depend(loss, self.optimizer(grads))
Exemplo n.º 18
0
def _update_run_op_graph_kernel(beta1, beta2, eps, global_step, lr,
                                weight_decay, param, m, v, gradient,
                                decay_flag):
    """
    Update parameters.

    Args:
        beta1 (Tensor): The exponential decay rate for the 1st moment estimations. Should be in range (0.0, 1.0).
        beta2 (Tensor): The exponential decay rate for the 2nd moment estimations. Should be in range (0.0, 1.0).
        eps (Tensor): Term added to the denominator to improve numerical stability. Should be greater than 0.
        lr (Tensor): Learning rate.
        weight_decay (Number): Weight decay. Should be equal to or greater than 0.
        global_step (Tensor): Global step.
        param (Tensor): Parameters.
        m (Tensor): m value of parameters.
        v (Tensor): v value of parameters.
        gradient (Tensor): Gradient of parameters.
        decay_flag (bool): Specifies whether param update with weight decay.

    Returns:
        Tensor, the new value of v after updating.
    """
    op_mul = P.Mul()
    op_square = P.Square()
    op_cast = P.Cast()
    op_shape = P.Shape()
    op_pow = P.Pow()
    op_norm = layer.Norm()
    op_fill = P.Fill()
    op_dtype = P.DType()

    param_fp32 = op_cast(param, mstype.float32)
    gradient_fp32 = op_cast(gradient, mstype.float32)

    i6_ex = op_cast(global_step + num_one, mstype.float32)
    i9 = op_cast(num_one, mstype.float32) - beta1
    x1 = op_cast(num_one, mstype.float32) - beta2
    i6 = op_cast(num_one, mstype.float32) - op_pow(beta1, i6_ex)
    i3 = op_cast(num_one, mstype.float32) - op_pow(beta2, i6_ex)
    i1 = op_square(gradient_fp32)
    add3, update = G.LambNextMV()(i1, v, i3, gradient, m, i6, param, beta1, i9,
                                  beta2, x1, weight_decay, eps)

    if decay_flag:
        update = update + op_mul(weight_decay, param_fp32)

    w_norm = op_norm(param_fp32)
    g_norm = op_norm(gradient_fp32)
    g_norm_hat = op_norm(add3)

    zeros = F.zeros_like(w_norm)
    ones = op_fill(op_dtype(w_norm), op_shape(w_norm), 1.0)
    tens = op_fill(op_dtype(w_norm), op_shape(w_norm), 10.0)

    next_param = G.LambUpdateWithLR()(g_norm, w_norm, g_norm_hat, lr, update,
                                      param, zeros, ones, tens)
    next_v = F.control_depend(add3, next_param)
    return next_v
Exemplo n.º 19
0
    def construct(self, prediction, pred_xy, pred_wh, y_true, gt_box, input_shape):
        """
        prediction : origin output from yolo
        pred_xy: (sigmoid(xy)+grid)/grid_size
        pred_wh: (exp(wh)*anchors)/input_shape
        y_true : after normalize
        gt_box: [batch, maxboxes, xyhw] after normalize
        """
        object_mask = y_true[:, :, :, :, 4:5]
        class_probs = y_true[:, :, :, :, 5:]
        true_boxes = y_true[:, :, :, :, :4]

        grid_shape = P.Shape()(prediction)[1:3]
        grid_shape = P.Cast()(F.tuple_to_array(grid_shape[::-1]), ms.float32)

        pred_boxes = self.concat((pred_xy, pred_wh))
        true_wh = y_true[:, :, :, :, 2:4]
        true_wh = P.Select()(P.Equal()(true_wh, 0.0),
                             P.Fill()(P.DType()(true_wh),
                                      P.Shape()(true_wh), 1.0),
                             true_wh)
        true_wh = P.Log()(true_wh / self.anchors * input_shape)
        # 2-w*h for large picture, use small scale, since small obj need more precise
        box_loss_scale = 2 - y_true[:, :, :, :, 2:3] * y_true[:, :, :, :, 3:4]

        gt_shape = P.Shape()(gt_box)
        gt_box = P.Reshape()(gt_box, (gt_shape[0], 1, 1, 1, gt_shape[1], gt_shape[2]))

        # add one more dimension for broadcast
        iou = self.iou(P.ExpandDims()(pred_boxes, -2), gt_box)
        # gt_box is x,y,h,w after normalize
        # [batch, grid[0], grid[1], num_anchor, num_gt]
        best_iou = self.reduce_max(iou, -1)
        # [batch, grid[0], grid[1], num_anchor]

        # ignore_mask IOU too small
        ignore_mask = best_iou < self.ignore_threshold
        ignore_mask = P.Cast()(ignore_mask, ms.float32)
        ignore_mask = P.ExpandDims()(ignore_mask, -1)
        # ignore_mask backpro will cause a lot maximunGrad and minimumGrad time consume.
        # so we turn off its gradient
        ignore_mask = F.stop_gradient(ignore_mask)

        confidence_loss = self.confidence_loss(object_mask, prediction[:, :, :, :, 4:5], ignore_mask)
        class_loss = self.class_loss(object_mask, prediction[:, :, :, :, 5:], class_probs)

        object_mask_me = P.Reshape()(object_mask, (-1, 1))  # [8, 72, 72, 3, 1]
        box_loss_scale_me = P.Reshape()(box_loss_scale, (-1, 1))
        pred_boxes_me = xywh2x1y1x2y2(pred_boxes)
        pred_boxes_me = P.Reshape()(pred_boxes_me, (-1, 4))
        true_boxes_me = xywh2x1y1x2y2(true_boxes)
        true_boxes_me = P.Reshape()(true_boxes_me, (-1, 4))
        ciou = self.giou(pred_boxes_me, true_boxes_me)
        ciou_loss = object_mask_me * box_loss_scale_me * (1 - ciou)
        ciou_loss_me = self.reduce_sum(ciou_loss, ())
        loss = ciou_loss_me * 10 + confidence_loss + class_loss
        batch_size = P.Shape()(prediction)[0]
        return loss / batch_size
Exemplo n.º 20
0
def erfc_f64_generic(x):
    """
    Calculate erfc for dtype of f64
    """
    k_maxlog = 7.09782712893383996843e2
    k_erfc_pcoefficient = [
        2.46196981473530512524e-10, 5.64189564831068821977e-1,
        7.46321056442269912687e0, 4.86371970985681366614e1,
        1.96520832956077098242e2, 5.26445194995477358631e2,
        9.34528527171957607540e2, 1.02755188689515710272e3,
        5.57535335369399327526e2
    ]
    k_erfc_qcoefficient = [
        1.00000000000000000000e0, 1.32281951154744992508e1,
        8.67072140885989742329e1, 3.54937778887819891062e2,
        9.75708501743205489753e2, 1.82390916687909736289e3,
        2.24633760818710981792e3, 1.65666309194161350182e3,
        5.57535340817727675546e2
    ]
    k_erfc_rcoefficient = [
        5.64189583547755073984e-1, 1.27536670759978104416e0,
        5.01905042251180477414e0, 6.16021097993053585195e0,
        7.40974269950448939160e0, 2.97886665372100240670e0
    ]
    k_erfc_scoefficient = [
        1.00000000000000000000e0, 2.26052863220117276590e0,
        9.39603524938001434673e0, 1.20489539808096656605e1,
        1.70814450747565897222e1, 9.60896809063285878198e0,
        3.36907645100081516050e02
    ]
    abs_cal = P.Abs()
    select = P.Select()
    less = P.Less()
    fill = P.Fill()
    dtype = P.DType()
    shape = P.Shape()

    abs_x = abs_cal(x)
    z = -x * x
    exp_z = exp_generic(z)

    temp1 = exp_z * _evaluate_polynomial(
        abs_x, k_erfc_pcoefficient) / _evaluate_polynomial(
            abs_x, k_erfc_qcoefficient)
    temp2 = exp_z * _evaluate_polynomial(
        abs_x, k_erfc_rcoefficient) / _evaluate_polynomial(
            abs_x, k_erfc_scoefficient)
    y = select(less(abs_x, 8.0), temp1, temp2)
    zeros = fill(dtype(x), shape(x), 0)
    y_clamp = select(less(z, k_maxlog), zeros, y)

    poly2 = _evaluate_polynomial(y, k_erfc_rcoefficient)
    p = select(less(abs_x, 2.0), poly1, poly2)
    y = z * q * p
    zeros = fill(dtype(x), shape(x), 0)
    y_clamp = select(less(z, -k_maxlog), zeros, y)
    return select(less(x, 0), 2.0 - y_clamp, y_clamp)
Exemplo n.º 21
0
 def construct(self, batch_users, batch_items, labels, valid_pt_mask):
     weights = self.weights
     loss = self.network(batch_users, batch_items, labels, valid_pt_mask)
     sens = P.Fill()(P.DType()(loss), P.Shape()(loss), self.sens)  #
     grads = self.grad(self.network, weights)(batch_users, batch_items, labels, valid_pt_mask, sens)
     if self.reducer_flag:
         # apply grad reducer on grads
         grads = self.grad_reducer(grads)
     return F.depend(loss, self.optimizer(grads))
Exemplo n.º 22
0
    def __init__(self, config, batch_size, in_channels, feat_channels,
                 num_anchors, cls_out_channels):
        super(RPN, self).__init__()
        cfg_rpn = config
        self.cfg = config
        self.num_bboxes = cfg_rpn.num_bboxes
        self.feature_anchor_shape = cfg_rpn.feature_shapes
        self.feature_anchor_shape = self.feature_anchor_shape[0] * \
            self.feature_anchor_shape[1] * num_anchors * batch_size
        self.num_anchors = num_anchors
        self.batch_size = batch_size
        self.test_batch_size = cfg_rpn.test_batch_size
        self.num_layers = 1
        self.real_ratio = Tensor(np.ones((1, 1)).astype(np.float16))
        self.use_sigmoid_cls = config.use_sigmoid_cls
        if config.use_sigmoid_cls:
            self.reshape_shape_cls = (-1, )
            self.loss_cls = P.SigmoidCrossEntropyWithLogits()
            cls_out_channels = 1
        else:
            self.reshape_shape_cls = (-1, cls_out_channels)
            self.loss_cls = nn.SoftmaxCrossEntropyWithLogits(sparse=True,
                                                             reduction="none")
        self.rpn_convs_list = self._make_rpn_layer(self.num_layers, in_channels, feat_channels,\
            num_anchors, cls_out_channels)

        self.transpose = P.Transpose()
        self.reshape = P.Reshape()
        self.concat = P.Concat(axis=0)
        self.fill = P.Fill()
        self.placeh1 = Tensor(np.ones((1, )).astype(np.float16))

        self.trans_shape = (0, 2, 3, 1)

        self.reshape_shape_reg = (-1, 4)
        self.softmax = nn.Softmax()
        self.rpn_loss_reg_weight = Tensor(
            np.array(cfg_rpn.rpn_loss_reg_weight).astype(np.float16))
        self.rpn_loss_cls_weight = Tensor(
            np.array(cfg_rpn.rpn_loss_cls_weight).astype(np.float16))
        self.num_expected_total = Tensor(
            np.array(cfg_rpn.num_expected_neg * self.batch_size).astype(
                np.float16))
        self.num_bboxes = cfg_rpn.num_bboxes
        self.get_targets = BboxAssignSample(cfg_rpn, self.batch_size,
                                            self.num_bboxes, False)
        self.CheckValid = P.CheckValid()
        self.sum_loss = P.ReduceSum()
        self.loss_bbox = P.SmoothL1Loss(beta=1.0 / 9.0)
        self.squeeze = P.Squeeze()
        self.cast = P.Cast()
        self.tile = P.Tile()
        self.zeros_like = P.ZerosLike()
        self.loss = Tensor(np.zeros((1, )).astype(np.float16))
        self.clsloss = Tensor(np.zeros((1, )).astype(np.float16))
        self.regloss = Tensor(np.zeros((1, )).astype(np.float16))
        self.print = P.Print()
Exemplo n.º 23
0
 def construct(self, batch_ids, batch_wts, label):
     '''
     Construct wide and deep model
     '''
     weights_w = self.weights_w
     weights_d = self.weights_d
     loss_w, loss_d = self.network(batch_ids, batch_wts, label)
     sens_w = P.Fill()(P.DType()(loss_w), P.Shape()(loss_w), self.sens)
     sens_d = P.Fill()(P.DType()(loss_d), P.Shape()(loss_d), self.sens)
     grads_w = self.grad_w(self.loss_net_w, weights_w)(batch_ids, batch_wts,
                                                       label, sens_w)
     grads_d = self.grad_d(self.loss_net_d, weights_d)(batch_ids, batch_wts,
                                                       label, sens_d)
     if self.reducer_flag:
         grads_w = self.grad_reducer_w(grads_w)
         grads_d = self.grad_reducer_d(grads_d)
     return F.depend(loss_w, self.optimizer_w(grads_w)), F.depend(loss_d,
                                                                  self.optimizer_d(grads_d))
Exemplo n.º 24
0
    def __init__(self,
                 probs=None,
                 seed=None,
                 dtype=mstype.int32,
                 name="Categorical"):
        param = dict(locals())
        param['param_dict'] = {'probs': probs}
        valid_dtype = mstype.uint_type + mstype.int_type + mstype.float_type
        Validator.check_type_name("dtype", dtype, valid_dtype,
                                  type(self).__name__)
        super(Categorical, self).__init__(seed, dtype, name, param)

        self._probs = self._add_parameter(probs, 'probs')
        if self.probs is not None:
            check_rank(self.probs)
            check_prob(self.probs)
            check_sum_equal_one(probs)

            # update is_scalar_batch and broadcast_shape
            # drop one dimension
            if self.probs.shape[:-1] == ():
                self._is_scalar_batch = True
            self._broadcast_shape = self._broadcast_shape[:-1]

        self.argmax = P.ArgMaxWithValue(axis=-1)
        self.broadcast = broadcast_to
        self.cast = P.Cast()
        self.clip_by_value = C.clip_by_value
        self.concat = P.Concat(-1)
        self.cumsum = P.CumSum()
        self.dtypeop = P.DType()
        self.exp = exp_generic
        self.expand_dim = P.ExpandDims()
        self.fill = P.Fill()
        self.gather = P.GatherNd()
        self.greater = P.Greater()
        self.issubclass = P.IsSubClass()
        self.less = P.Less()
        self.log = log_generic
        self.log_softmax = P.LogSoftmax()
        self.logicor = P.LogicalOr()
        self.logicand = P.LogicalAnd()
        self.multinomial = P.Multinomial(seed=self.seed)
        self.reshape = P.Reshape()
        self.reduce_sum = P.ReduceSum(keep_dims=True)
        self.select = P.Select()
        self.shape = P.Shape()
        self.softmax = P.Softmax()
        self.squeeze = P.Squeeze()
        self.squeeze_first_axis = P.Squeeze(0)
        self.squeeze_last_axis = P.Squeeze(-1)
        self.square = P.Square()
        self.transpose = P.Transpose()
        self.is_nan = P.IsNan()

        self.index_type = mstype.int32
        self.nan = np.nan
Exemplo n.º 25
0
    def construct(self, images):
        check = _check_input_4d(F.shape(images), "images", self.cls_name)
        images = F.depend(images, check)
        batch_size, depth, height, width = P.Shape()(images)
        if height == 1:
            dy = P.Fill()(P.DType()(images), (batch_size, depth, 1, width), 0)
        else:
            dy = images[:, :, 1:, :] - images[:, :, :height - 1, :]
            dy_last = P.Fill()(P.DType()(images), (batch_size, depth, 1, width), 0)
            dy = P.Concat(2)((dy, dy_last))

        if width == 1:
            dx = P.Fill()(P.DType()(images), (batch_size, depth, height, 1), 0)
        else:
            dx = images[:, :, :, 1:] - images[:, :, :, :width - 1]
            dx_last = P.Fill()(P.DType()(images), (batch_size, depth, height, 1), 0)
            dx = P.Concat(3)((dx, dx_last))
        return dy, dx
Exemplo n.º 26
0
 def construct(self, *inputs):
     weights = self.weights
     loss = self.network(*inputs)
     sens = P.Fill()(P.DType()(loss), P.Shape()(loss), self.sens)
     grads = self.grad(self.network, weights)(*inputs, sens)
     grads = self.hyper_map(F.partial(clip_grad, GRADIENT_CLIP_TYPE, GRADIENT_CLIP_VALUE), grads)
     if self.reducer_flag:
         # apply grad reducer on grads
         grads = self.grad_reducer(grads)
     return F.depend(loss, self.optimizer(grads))
Exemplo n.º 27
0
    def construct(self, x):
        """
        Construct method.
        """
        output_hm, output_wh, output_off, output_kps = self.centerface_network(x)
        output_hm_nms, _ = self.maxpool2d(output_hm)
        abs_error = P.Abs()(output_hm - output_hm_nms)
        abs_out = P.Abs()(output_hm)
        error = abs_error / (abs_out + 1e-12)

        # cannot use P.Equal()(output_hm, output_hm_nms), since maxpooling output has 0.1% error
        keep = P.Select()(P.LessEqual()(error, 1e-3), \
           P.Fill()(ms.float32, P.Shape()(error), 1.0), \
           P.Fill()(ms.float32, P.Shape()(error), 0.0))
        output_hm = output_hm * keep

        # get topK and index
        scores = self.reshape(output_hm, (self.test_batch, -1))
        topk_scores, topk_inds = self.topk(scores, self.k)
        return topk_scores, output_wh, output_off, output_kps, topk_inds
Exemplo n.º 28
0
 def construct(self, x, gt_score, gt_geo, ignored_map):
     weights = self.weights
     loss = self.network(x, gt_score, gt_geo, ignored_map)
     sens = P.Fill()(P.DType()(loss), P.Shape()(loss), self.sens)
     grads = self.grad(self.network, weights)(x, gt_score, gt_geo,
                                              ignored_map, sens)
     self.print(grads[0])
     if self.reducer_flag:
         # apply grad reducer on grads
         grads = self.grad_reducer(grads)
     return F.depend(loss, self.optimizer(grads))
Exemplo n.º 29
0
 def construct(self, *args):
     weights = self.weights
     loss = self.network(*args)
     sens = P.Fill()(P.DType()(loss), P.Shape()(loss), self.sens)
     grads = self.grad(self.network, weights)(*args, sens)
     if self.reducer_flag:
         # apply grad reducer on grads
         grads = self.grad_reducer(grads)
     if self.use_global_norm:
         grads = self.hyper_map(F.partial(grad_scale, F.scalar_to_array(self.sens)), grads)
         grads = C.clip_by_global_norm(grads)
     return F.depend(loss, self.optimizer(grads))
Exemplo n.º 30
0
 def __init__(self):
     super(ClipByNorm, self).__init__()
     self.reduce_sum = P.ReduceSum(keep_dims=True)
     self.select_ = P.Select()
     self.greater_ = P.Greater()
     self.cast = P.Cast()
     self.sqrt = P.Sqrt()
     self.max_op = P.Maximum()
     self.shape = P.Shape()
     self.reshape = P.Reshape()
     self.fill = P.Fill()
     self.expand_dims = P.ExpandDims()
     self.dtype = P.DType()