def test_eye(): """ test_eye """ x = np.arange(3) expect = np.ones_like(x) expect = np.diag(expect) eye = P.Eye() eye_output = eye(3, 3, ms.float32) assert np.all(eye_output.asnumpy() == expect)
def __init__(self, temp=0.1): super(LossNet, self).__init__() self.concat = P.Concat() self.exp = P.Exp() self.t = P.Transpose() self.diag_part = P.DiagPart() self.matmul = P.MatMul() self.sum = P.ReduceSum() self.sum_keep_dim = P.ReduceSum(keep_dims=True) self.log = P.Log() self.mean = P.ReduceMean() self.shape = P.Shape() self.eye = P.Eye() self.temp = temp
def __init__(self, rgb_range, rgb_mean=(0.4488, 0.4371, 0.4040), rgb_std=(1.0, 1.0, 1.0), sign=-1): super(MeanShift, self).__init__(3, 3, kernel_size=1) self.reshape = P.Reshape() self.eye = P.Eye() std = Tensor(rgb_std, mstype.float32) self.weight.set_data( self.reshape(self.eye(3, 3, mstype.float32), (3, 3, 1, 1)) / self.reshape(std, (3, 1, 1, 1))) self.weight.requires_grad = False self.bias = Parameter( sign * rgb_range * Tensor(rgb_mean, mstype.float32) / std, name='bias', requires_grad=False) self.has_bias = True
def eye(N, M=None, k=0, dtype=DEFAULT_FLOAT_DTYPE): """ Return a 2-D array with ones on the diagnoal and zeros elsewhere. Args: N (int): Number of rows in the output, must be larger than 0. M (int, optional): Number of columns in the output. If None, defaults to N, if defined, must be larger than 0. Deault is None. k (int, optional): Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. Default is 0. dtype (Union[mindspore.dtype, str], optional): Designated array dtype, can be in format of np.float32, or `float32`. Default is mindspore.float32. Returns: result (Tensor): A tensor array of shape (N,M). An array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> import mindspore.numpy as np >>> print(np.eye(2, 2)) [[1. 0.] [0. 1.]] """ dtype = _check_dtype(dtype) make_eye = P.Eye() if M is None: M = N M = int(M) N = int(N) k = int(k) out = None if k != 0 or N == 0 or M == 0: # Fall back to original numpy creation method out = onp.eye(N, M, k) else: out = make_eye(N, M, dtype) return asarray(out, dtype=dtype)
def __init__(self): super(ComputeDescriptor, self).__init__() self.reshape = P.Reshape() self.transpose = P.Transpose() self.cast = P.Cast() self.rsum = P.ReduceSum() self.broadcastto = P.BroadcastTo((1, 192 * 138)) self.broadcastto1 = P.BroadcastTo((1, 192, 138, 3)) self.broadcastto2 = P.BroadcastTo((1, 192, 138, 3, 3)) self.broadcastto3 = P.BroadcastTo((1, 192, 138, 4)) self.broadcastto4 = P.BroadcastTo((1, 192, 138, 4, 3)) self.expdims = P.ExpandDims() self.concat = P.Concat(axis=3) self.gather = P.GatherV2() self.mul = P.Mul() self.slice = P.Slice() self.square = P.Square() self.inv = P.Inv() self.sqrt = P.Sqrt() self.ones = P.OnesLike() self.eye = P.Eye()
def __init__(self, temperature=0.07, contrast_mode='all', base_temperature=0.07): super(SupConLoss, self).__init__() self.temperature = temperature self.contrast_mode = contrast_mode self.base_temperature = base_temperature self.normalize = P.L2Normalize(axis=2) self.eye = P.Eye() self.unbind = P.Unstack(axis=1) self.cat = P.Concat(axis=0) self.matmul = P.MatMul() self.div = P.Div() self.transpose = P.Transpose() self.maxes = P.ArgMaxWithValue(axis=1, keep_dims=True) self.tile = P.Tile() self.scatter = P.ScatterNd() self.oneslike = P.OnesLike() self.exp = P.Exp() self.sum = P.ReduceSum(keep_dims=True) self.log = P.Log() self.reshape = P.Reshape() self.mean = P.ReduceMean()
def _get_matrix_diag_part_assist(x_shape, x_dtype): base_eye = P.Eye()(x_shape[-2], x_shape[-1], x_dtype).flatten() tile = P.Tile()(base_eye, x_shape[:-2]) assist = P.Reshape()(tile, x_shape) return assist