Beispiel #1
0
 def update_pair(self, i, k):
     cp.subtract(self.mass_r_arrayg[i, :],
                 self.mass_r_arrayg[i + 1:, :],
                 out=self.relative_r[:k])
     cp.multiply(self.relative_r[:k],
                 self.relative_r[:k],
                 out=self.distance_sqv[:k])
     # np.add.reduce(self.distance_sqv[:k], axis = 1, out = self.distance_sq[:k])
     cp.sum(self.distance_sqv[:k], axis=1, out=self.distance_sq[:k])
     cp.sqrt(self.distance_sq[:k], out=self.distance_inv[:k])
     cp.divide(1.0, self.distance_inv[:k], out=self.distance_inv[:k])
     cp.multiply(self.relative_r[:k],
                 self.distance_inv[:k].reshape(k, 1),
                 out=self.relative_r[:k])
     cp.divide(self._G, self.distance_sq[:k], out=self.a_factor[:k])
     cp.multiply(self.a_factor[:k],
                 self.mass_m_array[i + 1:],
                 out=self.a1[:k])
     cp.multiply(self.a_factor[:k], self.mass_m_array[i], out=self.a2[:k])
     cp.multiply(self.relative_r[:k],
                 self.a1[:k].reshape(k, 1),
                 out=self.a1r[:k])
     # np.add.reduce(self.a1r[:k], axis = 0, out = self.a1v)
     cp.sum(self.a1r[:k], axis=0, out=self.a1v)
     cp.subtract(self.mass_a_arrayg[i, :],
                 self.a1v,
                 out=self.mass_a_arrayg[i, :])
     cp.multiply(self.relative_r[:k],
                 self.a2[:k].reshape(k, 1),
                 out=self.a2r[:k])
     cp.add(self.mass_a_arrayg[i + 1:, :],
            self.a2r[:k],
            out=self.mass_a_arrayg[i + 1:, :])
Beispiel #2
0
def calculate_gradient(image_mat: cp.array, gradient_x: cp.array,
                       gradient_y: cp.array):
    cp.subtract(image_mat[:, :, :-2],
                image_mat[:, :, 2:],
                out=gradient_x[:, :, 1:-1])
    cp.subtract(image_mat[:, :-2, :],
                image_mat[:, 2:, :],
                out=gradient_y[:, 1:-1, :])
Beispiel #3
0
def kernel_rbf(x1, x2, params):
    if x2.ndim == 2:
        return xp.exp(-xp.linalg.norm(xp.subtract(x1[:, :, xp.newaxis],
                                                  x2[:, :, xp.newaxis].T),
                                      axis=1)**2 / params['sigma']**2)
    else:
        return xp.exp(-xp.linalg.norm(xp.subtract(x1, x2), axis=1)**2 /
                      params['sigma']**2)
Beispiel #4
0
 def __call__(self, params, g_params):
     new_params = tuple(
         cp.add(
             param,
             cp.subtract(cp.multiply(self.momentum, cp.subtract(param, v)),
                         cp.multiply(self.rate, g_param)))
         for param, g_param, v in zip(params, g_params, self.v))
     self.v = params
     return new_params
Beispiel #5
0
def black_tophat(
    input,
    size=None,
    footprint=None,
    structure=None,
    output=None,
    mode='reflect',
    cval=0.0,
    origin=0,
):
    """
    Multidimensional black tophat filter.

    Args:
        input (cupy.ndarray): The input array.
        size (tuple of ints): Shape of a flat and full structuring element used
            for the black tophat. Optional if ``footprint`` or ``structure`` is
            provided.
        footprint (array of ints): Positions of non-infinite elements of a flat
            structuring element used for the black tophat. Non-zero values
            give the set of neighbors of the center over which opening is
            chosen.
        structure (array of ints): Structuring element used for the black
            tophat. ``structure`` may be a non-flat structuring element.
        output (cupy.ndarray, dtype or None): The array in which to place the
            output.
        mode (str): The array borders are handled according to the given mode
            (``'reflect'``, ``'constant'``, ``'nearest'``, ``'mirror'``,
            ``'wrap'``). Default is ``'reflect'``.
        cval (scalar): Value to fill past edges of input if mode is
            ``constant``. Default is ``0.0``.
        origin (scalar or tuple of scalar): The origin parameter controls the
            placement of the filter, relative to the center of the current
            element of the input. Default of 0 is equivalent to
            ``(0,)*input.ndim``.

    Returns:
        cupy.ndarry : Result of the filter of ``input`` with ``structure``.

    .. seealso:: :func:`scipy.ndimage.black_tophat`
    """
    if (size is not None) and (footprint is not None):
        warnings.warn(
            'ignoring size because footprint is set', UserWarning, stacklevel=2
        )
    tmp = grey_dilation(
        input, size, footprint, structure, None, mode, cval, origin
    )
    tmp = grey_erosion(
        tmp, size, footprint, structure, output, mode, cval, origin
    )
    if input.dtype == numpy.bool_ and tmp.dtype == numpy.bool_:
        cupy.bitwise_xor(tmp, input, out=tmp)
    else:
        cupy.subtract(tmp, input, out=tmp)
    return tmp
Beispiel #6
0
def _calc_offset(offset_map, region, parent_y, parent_x):
    y0, y1, x0, x1 = region
    x_area, y_area = offset_map[:, y0:y1, x0:x1]
    x_vec = np.power(np.subtract(np.arange(x0, x1), parent_x), 2)
    y_vec = np.power(np.subtract(np.arange(y0, y1), parent_y), 2)
    xv, yv = np.meshgrid(x_vec, y_vec)
    dist = np.sqrt(xv + yv)  # sqrt(y^2 + x^2)
    xv = np.divide(xv, dist)  # normalize x
    yv = np.divide(yv, dist)  # normalize y
    offset_map[0, y0:y1, x0:x1] = np.maximum(x_area, xv)
    offset_map[1, y0:y1, x0:x1] = np.maximum(y_area, yv)
def run_cupy(lat2, lon2):
    import cupy as cp

    # Allocate temporary arrays
    size = len(lat2)
    a = cp.empty(size, dtype='float64')
    dlat = cp.empty(size, dtype='float64')
    dlon = cp.empty(size, dtype='float64')

    # Transfer inputs to the GPU
    lat2 = cp.array(lat2)
    lon2 = cp.array(lon2)

    # Begin computation
    lat1 = 0.70984286
    lon1 = 1.23892197
    MILES_CONST = 3959.0

    cp.subtract(lat2, lat1, out=dlat)
    cp.subtract(lon2, lon1, out=dlon)

    # dlat = sin(dlat / 2.0) ** 2.0
    cp.divide(dlat, 2.0, out=dlat)
    cp.sin(dlat, out=dlat)
    cp.multiply(dlat, dlat, out=dlat)

    # a = cos(lat1) * cos(lat2)
    lat1_cos = math.cos(lat1)
    cp.cos(lat2, out=a)
    cp.multiply(a, lat1_cos, out=a)

    # a = a + sin(dlon / 2.0) ** 2.0
    cp.divide(dlon, 2.0, out=dlon)
    cp.sin(dlon, out=dlon)
    cp.multiply(dlon, dlon, out=dlon)
    cp.multiply(a, dlon, out=a)
    cp.add(dlat, a, out=a)

    c = a
    cp.sqrt(a, out=a)
    cp.arcsin(a, out=a)
    cp.multiply(a, 2.0, out=c)

    mi = c
    cp.multiply(c, MILES_CONST, out=mi)

    # Transfer outputs back to CPU
    a = cp.asnumpy(a)

    return a
Beispiel #8
0
    def gradient(self, x0, y_true):
        def func(a, t, params, A, function, bT, x, division):
            index = int(t * (division - 1))
            return cp.multiply(
                -1.,
                cp.add(
                    cp.dot(a, params[1][index]),
                    cp.dot(
                        cp.multiply(
                            bT,
                            cp.multiply(params[0][index],
                                        function(cp.dot(x[index], A.T)))), A)))

        n_data = len(x0)
        y_pred = self(x0)
        aT = cp.zeros_like(x0, dtype=cp.float32)
        bT = cp.divide(cp.subtract(y_pred, y_true), n_data)
        a = euler(func,
                  aT,
                  self.t[::-1],
                  args=(self.params, self.A, self.d_function, bT, self.x,
                        self.division))
        g_alpha = cp.sum(
            cp.multiply(bT, self.function(cp.dot(self.x, self.A.T))), 1)
        g_beta = cp.einsum("ilj,ilk->ijk", a[::-1], self.x)
        g_gamma = cp.sum(a[::-1], 1)
        return (g_alpha, g_beta, g_gamma)
Beispiel #9
0
	def forward(self, bottom, top):
		self.label = cp.asarray(copy.deepcopy(bottom[1].data),cp.uint8)
		prob = cp.asarray(copy.deepcopy(bottom[0].data),cp.float64)
		prob = cp.subtract(prob,cp.max(prob,axis=1)[:,cp.newaxis,...])
		prob = cp.exp(prob)
		self.softmax = cp.divide(prob,cp.sum(prob,axis=1)[:,cp.newaxis,...])

		## mask
		self.weight_mask = cp.ones_like(self.label, cp.float64)
		for weight_id in self.weight_dic:
			self.weight_mask[self.label == weight_id] = self.weight_dic[weight_id]

		if self.has_ignore_label:
			self.weight_mask[self.label == self.ignore_label] = 0
		# self.weight_mask[self.label == 0] = 0.3
		# self.weight_mask[self.label == 1] = 0.25
		# self.weight_mask[self.label == 2] = 5
		# self.weight_mask[self.label == 4] = 2
		self.label[self.label == 3] = 2


		compute_count = self.weight_mask[self.weight_mask != 0].size

		## nomalize mask
		self.weight_mask = cp.divide(self.weight_mask, cp.divide(cp.sum(self.weight_mask), compute_count))


		## compute loss
		prob_compute_matrix = copy.deepcopy(self.softmax[self.index_0,self.label,self.index_2,self.index_3])
		prob_compute_matrix[prob_compute_matrix < (1e-10)] = 1e-10

		loss = - cp.divide(cp.sum(cp.multiply(cp.log(prob_compute_matrix),self.weight_mask)),compute_count)

		loss = cp.asnumpy(loss)
		top[0].data[...] = loss
Beispiel #10
0
def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
    """Returns the variance along an axis.

    Args:
        a (cupy.ndarray): Array to compute variance.
        axis (int): Along which axis to compute variance. The flattened array
            is used by default.
        dtype: Data type specifier.
        out (cupy.ndarray): Output array.
        keepdims (bool): If True, the axis is remained as an axis of size one.

    Returns:
        cupy.ndarray: The variance of the input array along the axis.

    .. seealso:: :func:`numpy.var`

    """
    if axis is None:
        axis = tuple(range(a.ndim))
    if not isinstance(axis, tuple):
        axis = (axis,)

    if dtype is None and issubclass(a.dtype.type,
                                    (numpy.integer, numpy.bool_)):
        dtype = numpy.dtype(numpy.float64)

    arrmean = mean(a, axis=axis, dtype=dtype, keepdims=True)

    x = cupy.subtract(a, arrmean, dtype=dtype)
    cupy.square(x, x)
    ret = cupy.sum(x, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
    rcount = max(_count_reduce_items(a, axis) - ddof, 0)
    return cupy.multiply(ret, ret.dtype.type(1.0 / rcount), out=ret)
Beispiel #11
0
def subtract(tensor1, tensor2, dtype=None):
    """Substract two tensors

    Args:
        tensor1 (ndarray): Left tensor.
        tensor2 (ndarray): right tensor.
    """
    return cp.subtract(tensor1, tensor2)
def _normalize_on_device_masks(input, stream, out):
    allocate_place = np.prod(input.shape)
    with stream:
        g_img = cp.asarray(input)
        g_img = g_img[..., ::-1]
        g_img = cp.multiply(g_img, 1 / 127.5, dtype=cp.float32)
        out.device[:allocate_place] = cp.subtract(g_img, 1.).flatten()
    return g_img.shape
Beispiel #13
0
def mse(predictions, targets, cuda=False):
    if cuda:
        res = cp.square(cp.subtract(predictions, targets)).mean()
        cp.cuda.Stream.null.synchronize()
        return res
    else:
        return np.square(np.subtract(predictions,
                                     targets)).sum() / len(predictions)
Beispiel #14
0
def morphological_laplace(
    input,
    size=None,
    footprint=None,
    structure=None,
    output=None,
    mode='reflect',
    cval=0.0,
    origin=0,
):
    """
    Multidimensional morphological laplace.

    Args:
        input (cupy.ndarray): The input array.
        size (tuple of ints): Shape of a flat and full structuring element used
            for the morphological laplace. Optional if ``footprint`` or
            ``structure`` is provided.
        footprint (array of ints): Positions of non-infinite elements of a flat
            structuring element used for morphological laplace. Non-zero
            values give the set of neighbors of the center over which opening
            is chosen.
        structure (array of ints): Structuring element used for the
            morphological laplace. ``structure`` may be a non-flat
            structuring element.
        output (cupy.ndarray, dtype or None): The array in which to place the
            output.
        mode (str): The array borders are handled according to the given mode
            (``'reflect'``, ``'constant'``, ``'nearest'``, ``'mirror'``,
            ``'wrap'``). Default is ``'reflect'``.
        cval (scalar): Value to fill past edges of input if mode is
            ``constant``. Default is ``0.0``.
        origin (scalar or tuple of scalar): The origin parameter controls the
            placement of the filter, relative to the center of the current
            element of the input. Default of 0 is equivalent to
            ``(0,)*input.ndim``.

    Returns:
        cupy.ndarray: The morphological laplace of the input.

    .. seealso:: :func:`scipy.ndimage.morphological_laplace`
    """
    tmp1 = grey_dilation(
        input, size, footprint, structure, None, mode, cval, origin
    )
    if isinstance(output, cupy.ndarray):
        grey_erosion(
            input, size, footprint, structure, output, mode, cval, origin
        )
        cupy.add(tmp1, output, output)
        cupy.subtract(output, input, output)
        return cupy.subtract(output, input, output)
    else:
        tmp2 = grey_erosion(
            input, size, footprint, structure, None, mode, cval, origin
        )
        cupy.add(tmp1, tmp2, tmp2)
        cupy.subtract(tmp2, input, tmp2)
        cupy.subtract(tmp2, input, tmp2)
        return tmp2
def _normalize_on_device(input, stream, out, mean=0., std=1.):
    allocate_place = np.prod(input.shape)
    with stream:
        g_img = cp.asarray(input)
        g_img = g_img[..., ::-1]
        g_img = cp.transpose(g_img, (0, 3, 1, 2))
        g_img = cp.subtract(g_img, mean, dtype=cp.float32)
        out.device[:allocate_place] = cp.multiply(g_img, 1 / std).flatten()
    return g_img.shape
Beispiel #16
0
def _calc_gaussian(heatmap,
                   region,
                   center_x,
                   center_y,
                   theta=2.,
                   threshold=4.605):
    # [theta, radius]: [1.0, 3.5px]; [2.0, 6.5px], and [0.5, 2.0px]
    y0, y1, x0, x1 = region
    # fast way
    heat_area = heatmap[y0:y1, x0:x1]
    factor = 1 / 2.0 / theta / theta
    x_vec = np.power(np.subtract(np.arange(x0, x1), center_x), 2)
    y_vec = np.power(np.subtract(np.arange(y0, y1), center_y), 2)
    xv, yv = np.meshgrid(x_vec, y_vec)
    _sum = factor * (xv + yv)
    _exp = np.exp(-_sum)
    _exp[_sum > threshold] = 0
    heatmap[y0:y1, x0:x1] = np.maximum(heat_area, _exp)
 def __iteration(self) -> cp.ndarray:
     """
     One iteration of Landweber algorithm.
     :return: Numpy ndarray with the next approximation of solution from algorithm.
     # """
     self.current = cp.add(self.previous, cp.multiply(self.relaxation, cp.subtract(self.q_estimator,
                                                                                   cp.matmul(self.KHK,
                                                                                             self.previous))))
     return self.current
 def L2norm(self, x: cp.ndarray, y: cp.ndarray) -> cp.ndarray:
     """
     Calculate the approximation of L2 norm of difference of two approximation of function.
     :param x: Approximation of function on given grid.
     :type x: np.ndarray
     :param y: Approximation of function on given grid.
     :type y: np.ndarray
     :return: Float representing the L2 norm of difference between given functions.
     """
     return cp.sqrt(cp.sum(cp.multiply(cp.square(cp.subtract(x, y)), self.__weights)))
Beispiel #19
0
    def rand_jitter(self, arr):
        """
        Introduces random displacements to spread the points
        """
        stdev = .023 * cupy.subtract(cupy.max(arr), cupy.min(arr))
        for i in range(arr.shape[1]):
            rnd = cupy.multiply(cupy.random.randn(len(arr)), stdev)
            arr[:, i] = cupy.add(arr[:, i], rnd)

        return arr
Beispiel #20
0
 def update_pair(self, i, k):
     (self.a1r[:k, 0], self.a1r[:k, 1], self.a1r[:k, 2], self.a2r[:k, 0],
      self.a2r[:k, 1], self.a2r[:k, 2]) = self.update_pair_kernel(
          self.mass_r_arrayg[i, 0],
          self.mass_r_arrayg[i, 1],
          self.mass_r_arrayg[i, 2],
          self.mass_m_array[i],
          self.mass_r_arrayg[i + 1:, 0],
          self.mass_r_arrayg[i + 1:, 1],
          self.mass_r_arrayg[i + 1:, 2],
          self.mass_m_array[i + 1:],
      )
     cp.sum(self.a1r[:k], axis=0, out=self.a1v)
     cp.subtract(self.mass_a_arrayg[i, :],
                 self.a1v,
                 out=self.mass_a_arrayg[i, :])
     cp.add(self.mass_a_arrayg[i + 1:, :],
            self.a2r[:k],
            out=self.mass_a_arrayg[i + 1:, :])
Beispiel #21
0
	def forward(self, bottom, top):
		self.label = cp.asarray(copy.deepcopy(bottom[1].data),cp.uint8)
		prob = cp.asarray(copy.deepcopy(bottom[0].data),cp.float64)
		prob = cp.subtract(prob,cp.max(prob,axis=1)[:,cp.newaxis,...])
		prob = cp.exp(prob)
		self.softmax = cp.divide(prob,cp.sum(prob,axis=1)[:,cp.newaxis,...])

		## mask
		self.weight_mask = cp.ones_like(self.label, cp.float64)
		for weight_id in self.weight_dic:
			self.weight_mask[self.label == weight_id] = self.weight_dic[weight_id]

		if self.has_ignore_label:
			self.weight_mask[self.label == self.ignore_label] = 0
		# num_total = 15422668800
		# empty_num = 3679002314
		# road_num = 10565335603
		# ped_num = 99066996
		# car_num = 995347874
		#self.label[self.label == 3] = 2
		# w_empty = float((num_total-empty_num)/num_total)
		# w_road = float((num_total-road_num)/num_total)
		# w_ped = float((num_total-ped_num)/num_total)
		# w_car = float((num_total-car_num)/num_total)
		# print(w_empty)
		# print(w_road)
		# print(w_ped)
		# print(w_car)
		# empty:0.3
		# road:0.25

		self.weight_mask[self.label == 0] = 0.3
		self.weight_mask[self.label == 1] = 0.25
		self.weight_mask[self.label == 3] = 0.5

		# self.weight_mask[self.label == 2] = w_ped
		# self.weight_mask[self.label == 4] = w_car



		compute_count = self.weight_mask[self.weight_mask != 0].size


		## nomalize mask
		self.weight_mask = cp.divide(self.weight_mask, cp.divide(cp.sum(self.weight_mask), compute_count))


		## compute loss
		prob_compute_matrix = copy.deepcopy(self.softmax[self.index_0,self.label,self.index_2,self.index_3])
		prob_compute_matrix[prob_compute_matrix < (1e-10)] = 1e-10
		loss = - cp.divide(cp.sum(cp.multiply(cp.log(prob_compute_matrix),self.weight_mask)),compute_count)

		loss = cp.asnumpy(loss)
		top[0].data[...] = loss
Beispiel #22
0
 def Minkowski(self,usu1,usu2,r):
     list_items1 = self.rating_avg.loc[self.rating_avg.loc[:,'userId'] == usu1 ]
     list_items2 = self.rating_avg.loc[self.rating_avg.loc[:,'userId'] == usu2 ]
     mezclar = pd.merge(list_items1, list_items2, how='inner', on='itemId')
     rating_x = mezclar[['adg_rating_x']].to_numpy().transpose()[0]
     rating_y = mezclar[['adg_rating_y']].to_numpy().transpose()[0]
     car=mezclar.shape[0]
     if(car==0 or r == 0):
         return 0
     Mink = np.sum(pow(np.absolute(np.subtract(rating_x,rating_y)),r))
     return pow(Mink,1/r)
Beispiel #23
0
 def Manhattan(self,usu1,usu2):
     list_items1 = self.rating_avg.loc[self.rating_avg.loc[:,'userId'] == usu1 ]
     list_items2 = self.rating_avg.loc[self.rating_avg.loc[:,'userId'] == usu2 ]
     mezclar = pd.merge(list_items1, list_items2, how='inner', on='itemId')
     rating_x = mezclar[['adg_rating_x']].to_numpy().transpose()[0]
     rating_y = mezclar[['adg_rating_y']].to_numpy().transpose()[0]
     car=mezclar.shape[0]
     if(car==0):
         return 0
     Man = np.sum(np.absolute(np.subtract(rating_x,rating_y)))
     return Man
def cupy_background_correction(data,
                               dark,
                               flat,
                               clip_min=MINIMUM_PIXEL_VALUE,
                               clip_max=MAXIMUM_PIXEL_VALUE):
    """
    Carry out something akin to background correction in Mantid Imaging using cupy.
    :param data: The fake data array.
    :param dark: The fake dark array.
    :param flat: The fake flat array.
    :param clip_min: Minimum clipping value.
    :param clip_max: Maximum clipping value.
    """
    flat = cp.subtract(flat, dark)
    flat[flat == 0] = MINIMUM_PIXEL_VALUE
    data = cp.subtract(data, dark)
    data = cp.true_divide(data, flat)
    data = cp.clip(
        data, clip_min,
        clip_max)  # For some reason using the 'out' parameter doesn't work
def error_minimization(W,
                       b,
                       zeta,
                       a,
                       prev_layer,
                       activation_func,
                       den_activation,
                       y,
                       w=None,
                       d=None,
                       y_pred=None):
    dW = {}
    dB = {}
    delta = {}
    try:
        batch_size = y.shape[1]
    except IndexError:
        batch_size = 1
        y = cp.reshape(y, (y.shape[0], batch_size))

    is_last_layer = (type(w) == type(d)) and (type(d) == type(None))

    if is_last_layer:

        delta['s'] = cp.subtract(a['s'], y)
        dB['s'] = (1 / batch_size) * cp.sum(delta['s'], axis=1)
        dB['s'] = cp.reshape(dB['s'], (dB['s'].shape[0], 1, 1))

        delta['s'] = cp.reshape(delta['s'],
                                (delta['s'].shape[0], 1, delta['s'].shape[1]))

        dW['s'] = (1 / batch_size) * cp.einsum('nik,kjn->nij', delta['s'],
                                               a['d'].T)

    else:
        w = cp.array(w)

        deltaW = cp.einsum('nik,kij->nj', w.T, d)
        deltaW = cp.reshape(deltaW, (deltaW.shape[0], 1, deltaW.shape[1]))
        a_der = activation(str(activation_func) + '_der', zeta['s'])

        delta['s'] = cp.multiply(deltaW, a_der)
        dB['s'] = (1 / batch_size) * cp.sum(delta['s'].squeeze(), axis=1)
        dB['s'] = cp.reshape(dB['s'], (dB['s'].shape[0], 1, 1))
        dW['s'] = (1 / batch_size) * cp.einsum('nik,kjn->nij', delta['s'],
                                               a['d'].T)

    deltaW = cp.einsum('nik,kij->knj', W['s'].T, delta['s'])
    a_der = activation(den_activation + '_der', zeta['d'])
    delta['d'] = cp.multiply(deltaW, a_der)
    dB['d'] = (1 / batch_size) * cp.sum(delta['d'], axis=2)
    dB['d'] = cp.reshape(dB['d'], (dB['d'].shape[0], dB['d'].shape[1], 1))
    dW['d'] = (1 / batch_size) * cp.dot(delta['d'], prev_layer.T)
    return [dW, dB, delta]
Beispiel #26
0
def triu(m, k=0):
    """Make a copy of a matrix with elements below the ``k``-th diagonal
    zeroed.

    Args:
        m (cupy.ndarray): Matrix whose elements to return
        k (int, optional): Diagonal above which to zero elements.
            ``k == 0`` is the main diagonal, ``k < 0`` subdiagonal and
            ``k > 0`` superdiagonal.

    Returns:
        (cupy.ndarray): Return matrix with zeroed elements below the kth
        diagonal and has same shape and type as ``m``.

    .. seealso:: :func:`scipy.linalg.triu`
    """
    # this is ~25% faster than cupy.tril for a 500x500 float64 matrix
    t = tri(m.shape[0], m.shape[1], k - 1, m.dtype.char)
    cupy.subtract(1, t, out=t)
    t *= m
    return t
Beispiel #27
0
def run_gpu(X, eps, min_samples):
    # Transfer inputs to GPU
    X = cp.array(X)

    # Begin computation
    t0 = time.time()
    mean = cp.mean(X, axis=0)
    std = cp.std(X, axis=0)
    cp.subtract(X, mean, out=X)
    cp.divide(X, std, out=X)
    print('Preprocessing:', time.time() - t0)

    # Run DBSCAN
    db = cuml.DBSCAN(eps=eps, min_samples=min_samples)
    db = db.fit(X)
    labels = db.labels_

    # Transfer outputs to CPU
    # labels = labels.to_pandas().to_numpy()
    labels = cp.asnumpy(labels)
    return labels
Beispiel #28
0
def subtract(x1: Array, x2: Array, /) -> Array:
    """
    Array API compatible wrapper for :py:func:`np.subtract <numpy.subtract>`.

    See its docstring for more information.
    """
    if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes:
        raise TypeError("Only numeric dtypes are allowed in subtract")
    # Call result type here just to raise on disallowed type combinations
    _result_type(x1.dtype, x2.dtype)
    x1, x2 = Array._normalize_two_args(x1, x2)
    return Array._new(np.subtract(x1._array, x2._array))
Beispiel #29
0
 def __call__(self, params, g_params):
     new_params, new_v = zip(
         *[(cp.subtract(
             param,
             cp.multiply(
                 cp.divide(
                     self.rate,
                     cp.sqrt(
                         cp.add(
                             cp.add(
                                 cp.multiply(self.decay, v),
                                 cp.multiply(cp.subtract(1., self.decay),
                                             cp.square(g_param))),
                             self.eps).astype(cp.float32))), g_param)),
            cp.add(
                cp.multiply(self.decay, v),
                cp.multiply(cp.subtract(1., self.decay), cp.square(
                    g_param))))
           for param, g_param, v in zip(params, g_params, self.v)])
     self.v = new_v
     return new_params
Beispiel #30
0
 def compute_distance_cupy(self):
     """
     Method helps compute the MSE between two N-d vectors and is used to make the
     Helps facilitate fast computation.                 
     """
     check = cp.array(self.encoder_injected.predict(self.test))
     anchor = cp.array(self.encoder_injected.predict(self.anchor))
     for j in range(0, self.test.shape[0] - 1):
         # index = int(random()*10)
         index = 0
         self.values[j] = (cp.square(
             cp.subtract(anchor[index:index + 1, :],
                         check[j:j + 1, :]))).mean()
     return self.values