Пример #1
0
def test_rotated_img():
    """
    The harris filter should yield the same results with an image and it's
    rotation.
    """
    im = img_as_float(cp.asarray(data.astronaut().mean(axis=2)))
    im_rotated = im.T

    # # Moravec
    # results = peak_local_max(corner_moravec(im),
    #                          min_distance=10, threshold_rel=0)
    # results_rotated = peak_local_max(corner_moravec(im_rotated),
    #                                  min_distance=10, threshold_rel=0)
    # assert (cp.sort(results[:, 0]) == cp.sort(results_rotated[:, 1])).all()
    # assert (cp.sort(results[:, 1]) == cp.sort(results_rotated[:, 0])).all()

    # Harris
    results = cp.nonzero(corner_harris(im))
    results_rotated = cp.nonzero(corner_harris(im_rotated))

    assert (cp.sort(results[0]) == cp.sort(results_rotated[1])).all()
    assert (cp.sort(results[1]) == cp.sort(results_rotated[0])).all()

    # Shi-Tomasi
    results = cp.nonzero(corner_shi_tomasi(im))
    results_rotated = cp.nonzero(corner_shi_tomasi(im_rotated))

    assert (cp.sort(results[0]) == cp.sort(results_rotated[1])).all()
    assert (cp.sort(results[1]) == cp.sort(results_rotated[0])).all()
Пример #2
0
 def test_indices_with_labels(self):
     image = cp.asarray(np.random.uniform(size=(40, 60)))
     i, j = cp.mgrid[0:40, 0:60]
     labels = 1 + (i >= 20) + (j >= 30) * 2
     i, j = cp.mgrid[-3:4, -3:4]
     footprint = i * i + j * j <= 9
     expected = cp.zeros(image.shape, float)
     for imin, imax in ((0, 20), (20, 40)):
         for jmin, jmax in ((0, 30), (30, 60)):
             expected[imin:imax,
                      jmin:jmax] = ndi.maximum_filter(image[imin:imax,
                                                            jmin:jmax],
                                                      footprint=footprint)
     expected = cp.column_stack(cp.nonzero(expected == image))
     expected = expected[cp.argsort(image[tuple(expected.T)])[::-1]]
     result = peak.peak_local_max(
         image,
         labels=labels,
         min_distance=1,
         threshold_rel=0,
         footprint=footprint,
         indices=True,
         exclude_border=False,
     )
     result = result[cp.argsort(image[tuple(result.T)])[::-1]]
     assert (result == expected).all()
Пример #3
0
def isolated_peaks_new(S1, params):
    """
    takes a matrix of timepoints by channels S1
    outputs threshold crossings that are relatively isolated from other peaks
    outputs row, column and magnitude of the threshold crossing
    """
    S1 = cp.asarray(S1)

    # finding the local minimum in a sliding window within plus/minus loc_range extent
    # across time and across channels
    smin = my_min(S1, params.loc_range, [0, 1])

    # the peaks are samples that achieve this local minimum, AND have negativities less
    # than a preset threshold
    peaks = (S1 < smin + 1e-3) & (S1 < params.spkTh)

    # only take local peaks that are isolated from other local peaks
    # if there is another local peak close by, this sum will be at least 2
    sum_peaks = my_sum(peaks, params.long_range, [0, 1])
    # set to 0 peaks that are not isolated, and multiply with the voltage values
    peaks = peaks * (sum_peaks < 1.2) * S1

    # exclude temporal buffers
    peaks[:params.nt0, :] = 0
    peaks[-params.nt0:, :] = 0

    # find the non-zero peaks, and take their amplitudes
    col, row = cp.nonzero(peaks.T)
    # invert the sign of the amplitudes
    mu = -peaks[row, col]

    return row, col, mu
Пример #4
0
def nonzero(x: Array, /) -> Tuple[Array, ...]:
    """
    Array API compatible wrapper for :py:func:`np.nonzero <numpy.nonzero>`.

    See its docstring for more information.
    """
    return tuple(Array._new(i) for i in np.nonzero(x._array))
Пример #5
0
def analize(x):
    print("the following elements are nonzero")
    idx_nonzero = cupy.nonzero(x)
    nonzero = x[idx_nonzero]
    nonzero_cpu = cupy.asnumpy(nonzero)
    bits = np.unpackbits(nonzero_cpu)
    print(idx_nonzero)
    print(bits)
Пример #6
0
 def cluster(self,maxClust):
     D = self.corrDist()
     Z = linkage(D[cupy.nonzero(~cupy.tri(self.n, k=0, dtype=bool))]) 
      # create a linkage matrix based on the distance matrix
     if maxClust < 1:
         maxClust = 1
     if maxClust > self.n:
         maxClust = self.n
     map = self.__breakClust__(to_tree(Z),maxClust)
     return map
Пример #7
0
def _preprocess(labels):

    label_values, inv_idx = cp.unique(labels, return_inverse=True)
    if not (label_values == 0).any():
        warn('Random walker only segments unlabeled areas, where '
             'labels == 0. No zero valued areas in labels were '
             'found. Returning provided labels.',
             stacklevel=2)

        return labels, None, None, None, None

    # If some labeled pixels are isolated inside pruned zones, prune them
    # as well and keep the labels for the final output

    null_mask = labels == 0
    pos_mask = labels > 0
    mask = labels >= 0

    fill = ndi.binary_propagation(null_mask, mask=mask)
    isolated = cp.logical_and(pos_mask, cp.logical_not(fill))

    pos_mask[isolated] = False

    # If the array has pruned zones, be sure that no isolated pixels
    # exist between pruned zones (they could not be determined)
    if label_values[0] < 0 or cp.any(isolated):  # synchronize!
        isolated = cp.logical_and(
            cp.logical_not(ndi.binary_propagation(pos_mask, mask=mask)),
            null_mask)

        labels[isolated] = -1
        if cp.all(isolated[null_mask]):
            warn('All unlabeled pixels are isolated, they could not be '
                 'determined by the random walker algorithm.',
                 stacklevel=2)
            return labels, None, None, None, None

        mask[isolated] = False
        mask = cp.atleast_3d(mask)

    else:
        mask = None

    # Reorder label values to have consecutive integers (no gaps)
    zero_idx = cp.searchsorted(label_values, cp.array(0))
    labels = cp.atleast_3d(inv_idx.reshape(labels.shape) - zero_idx)

    nlabels = label_values[zero_idx + 1:].shape[0]

    inds_isolated_seeds = cp.nonzero(isolated)
    isolated_values = labels[inds_isolated_seeds]

    return labels, nlabels, mask, inds_isolated_seeds, isolated_values
Пример #8
0
def svm_loss_vectorized(W, X, y, reg):
    """
    Structured SVM loss function, vectorized implementation.

    Inputs and outputs are the same as svm_loss_naive.
    """
    #covert to cupy
    X = cp.array(X)
    W = cp.array(W)
    y = cp.array(y)

    loss = 0.0
    dW = cp.zeros(W.shape) # initialize the gradient as zero

    #############################################################################
    # TODO:                                                                     #
    # Implement a vectorized version of the structured SVM loss, storing the    #
    # result in loss.                                                           #
    #############################################################################
    # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
    num_train = X.shape[0]
    num_classes = W.shape[1]
    scores = cp.dot(X,W)
    correct_class_scores = scores[cp.arange(num_train),y][:,cp.newaxis]
    margin = cp.maximum(0, scores - correct_class_scores + 1)
    margin[cp.arange(num_train),y] = 0 #ignore the margin for correct class of each picture
    loss = cp.sum(margin)
    loss /= num_train
    loss += reg * cp.sum(W*W)

    # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

    #############################################################################
    # TODO:                                                                     #
    # Implement a vectorized version of the gradient for the structured SVM     #
    # loss, storing the result in dW.                                           #
    #                                                                           #
    # Hint: Instead of computing the gradient from scratch, it may be easier    #
    # to reuse some of the intermediate values that you used to compute the     #
    # loss.                                                                     #
    #############################################################################
    # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
    index_valid_margin = cp.nonzero(margin)
    margin[index_valid_margin] = 1
    valid_margin_sum = cp.sum(margin,axis=1)
    margin[cp.arange(num_train),y] -= valid_margin_sum
    dW = cp.transpose(X).dot(margin)
    dW /= num_train
    dW += reg * 2 * W

    # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

    return cp.asnumpy(loss), cp.asnumpy(dW)
Пример #9
0
def filter_by_distance_less(s_p, i_p):
    t0 = time.time()
    dists = get_absolute_values(i_p)
    t1 = time.time()
    mask = map_dists_less(dists)
    indices = np.nonzero(mask)
    s_p = s_p[indices]
    i_p = i_p[indices]
    t2 = time.time()
    # print()
    # print("%.4f\t%.4f\t%d" % (t1-t0, t2-t1, np.sum(dists < 25)))
    return s_p, i_p
Пример #10
0
def argrelextrema(data, comparator, axis=0, order=1, mode="clip"):
    """
    Calculate the relative extrema of `data`.

    Parameters
    ----------
    data : ndarray
        Array in which to find the relative extrema.
    comparator : callable
        Function to use to compare two data points.
        Should take two arrays as arguments.
    axis : int, optional
        Axis over which to select from `data`.  Default is 0.
    order : int, optional
        How many points on each side to use for the comparison
        to consider ``comparator(n, n+x)`` to be True.

    Returns
    -------
    extrema : tuple of ndarrays
        Indices of the maxima in arrays of integers.  ``extrema[k]`` is
        the array of indices of axis `k` of `data`.  Note that the
        return value is a tuple even when `data` is one-dimensional.

    See Also
    --------
    argrelmin, argrelmax

    Examples
    --------
    >>> from cusignal import argrelextrema
    >>> import cupy as cp
    >>> x = cp.array([2, 1, 2, 3, 2, 0, 1, 0])
    >>> argrelextrema(x, cp.greater)
    (array([0, 3, 6]),)
    >>> y = cp.array([[1, 2, 1, 2],
    ...               [2, 2, 0, 0],
    ...               [5, 3, 4, 4]])
    ...
    >>> argrelextrema(y, cp.less, axis=1)
    (array([0, 0, 2]), array([0, 2, 1]))

    """
    data = cp.asarray(data)
    results = _boolrelextrema(data, comparator, axis, order, mode)

    if mode == "raise":
        raise NotImplementedError(
            "CuPy `take` doesn't support `mode='raise'`.")

    return cp.nonzero(results)
Пример #11
0
def argrelextrema(data, comparator, axis=0, order=1):
    """
    Calculate the relative extrema of `data`.

    Parameters
    ----------
    data : ndarray
        Array in which to find the relative extrema.
    comparator : callable
        Function to use to compare two data points.
        Should take two arrays as arguments.
    axis : int, optional
        Axis over which to select from `data`.  Default is 0.
    order : int, optional
        How many points on each side to use for the comparison
        to consider ``comparator(n, n+x)`` to be True.

    Returns
    -------
    extrema : tuple of ndarrays
        Indices of the maxima in arrays of integers.  ``extrema[k]`` is
        the array of indices of axis `k` of `data`.  Note that the
        return value is a tuple even when `data` is one-dimensional.

    See Also
    --------
    argrelmin, argrelmax

    Notes
    -----

    .. versionadded:: 0.11.0

    Examples
    --------
    >>> from scipy.signal import argrelextrema
    >>> x = np.array([2, 1, 2, 3, 2, 0, 1, 0])
    >>> argrelextrema(x, np.greater)
    (array([3, 6]),)
    >>> y = np.array([[1, 2, 1, 2],
    ...               [2, 2, 0, 0],
    ...               [5, 3, 4, 4]])
    ...
    >>> argrelextrema(y, np.less, axis=1)
    (array([0, 2]), array([2, 1]))

    """
    data = cp.asarray(data)
    results = _boolrelextrema(data, comparator,
                              axis, order)
    return cp.nonzero(results)
Пример #12
0
def _get_high_intensity_peaks(image, mask, num_peaks):
    """
    Return the highest intensity peak coordinates.
    """
    # get coordinates of peaks
    coord = cp.nonzero(mask)
    intensities = image[coord]
    # Highest peak first
    idx_maxsort = cp.argsort(-intensities)
    coord = cp.column_stack(coord)[idx_maxsort]
    # select num_peaks peaks
    if len(coord) > num_peaks:
        coord = coord[:num_peaks]
    return coord
Пример #13
0
def _get_high_intensity_peaks(image, mask, num_peaks, min_distance, p_norm):
    """
    Return the highest intensity peak coordinates.
    """
    # get coordinates of peaks
    coord = cp.nonzero(mask)
    intensities = image[coord]
    # Highest peak first
    idx_maxsort = cp.argsort(-intensities)
    coord = cp.column_stack(coord)[idx_maxsort]

    coord = ensure_spacing(coord, spacing=min_distance, p_norm=p_norm)
    if len(coord) > num_peaks:
        coord = coord[:num_peaks]
    return coord
Пример #14
0
    def diagonal(self, k=0):
        """Returns the k-th diagonal of the matrix.

        Args:
            k (int, optional): Which diagonal to get, corresponding to elements
            a[i, i+k]. Default: 0 (the main diagonal).

        Returns:
            cupy.ndarray : The k-th diagonal.
        """
        rows, cols = self.shape
        if k <= -rows or k >= cols:
            return cupy.empty(0, dtype=self.data.dtype)
        idx, = cupy.nonzero(self.offsets == k)
        first_col, last_col = max(0, k), min(rows + k, cols)
        if idx.size == 0:
            return cupy.zeros(last_col - first_col, dtype=self.data.dtype)
        return self.data[idx[0], first_col:last_col]
Пример #15
0
def auc(vector_predict, vector_true, gpu=False):
    if gpu:
        vector_predict = cp.array(vector_predict)
        vector_true = cp.array(vector_true)
        pos_indexes = cp.where(vector_true == 1)[0]
        sort_indexes = cp.argsort(vector_predict)
        rank = cp.nonzero(cp.in1d(sort_indexes, pos_indexes))[0]
        return (cp.sum(rank) - len(pos_indexes) *
                (len(pos_indexes) + 1) / 2) / (
                    len(pos_indexes) *
                    (len(vector_predict) - len(pos_indexes)))
    else:
        pos_indexes = np.where(vector_true == 1)[0]
        sort_indexes = np.argsort(vector_predict)
        rank = np.nonzero(np.in1d(sort_indexes, pos_indexes))[0]
        return (np.sum(rank) - len(pos_indexes) *
                (len(pos_indexes) + 1) / 2) / (
                    len(pos_indexes) *
                    (len(vector_predict) - len(pos_indexes)))
Пример #16
0
def ent(wave, n, L, la):
    lb = L - la
    # convert the wavefunction into a matrix for SVD
    temp = cp.reshape(wave, (2**la, 2**lb))
    # SVD for entanglement entropy, only singular values calculated
    sp = cp.linalg.svd(temp, compute_uv=False)
    tol = 1e-10
    # chop small singular values to zero to avoid numerical instability
    sp[abs(sp) < tol] = 0.0
    # choose only non-zero values to avoid feeding to log function
    sp = sp[cp.nonzero(sp)]
    el = sp**2
    von = -cp.dot(el, np.log2(el))
    ren = (1 / (1 - n)) * cp.log2(np.sum(el**(n)))
    # chop small values to zero
    if (abs(von) < tol):
        von = 0
    if (abs(ren) < tol):
        ren = 0
    # EE in log2 base
    return von, ren
Пример #17
0
def filter_by_distance_greater(s_p, i_p):
    # print(i_p.device)
    # mask2 = np.ones_like(mask).astype(np.bool)
    # print(mask, mask.dtype)
    # print(mask2, mask2.dtype)
    # print(np.equal(mask, mask2))
    # t0 = time.time()
    dists = get_absolute_values(i_p)
    # t1 = time.time()
    # s_p = s_p[dists > 4]
    # dists = dists > 4
    dists = map_dists_greater(dists)
    indices = np.nonzero(dists)
    # mask = np.nonzero(dists > 4)
    # t2 = time.time()
    s_p = s_p[indices]
    # t3 = time.time()
    # print()
    # print("get_absolute_values: %.4f\ncreate mask: %.4f\nfilter array: %.4f\nnumber of elements: %d" % (t1-t0, t2-t1, t3-t2, np.sum(dists > 4)))
    # return s_p
    return s_p
Пример #18
0
    def __call__(self, x):
        """
        Args:
            x (cp.ndarray): input data
        """
        nz, ny, nx = x.shape
        grid_sz = (
            int(ceil(nx / self._tile_width)),
            int(ceil(ny / self._tile_width)),
            int(ceil(nz / self._tile_width)),
        )

        buf = cp.copy(x)
        if not isinstance(x.dtype, cp.uint16):
            logger.info(".. rescaling to uint16")
            buf = buf / buf.max() * 65535
            buf = buf.astype(cp.uint16)

        threshold = np.uint16(
            self._threshold if self._threshold else FindPeak3D.find_threshold(buf)
        )
        logger.debug(f".. threshold: {threshold}")
        self._kernels["find_peak_3d_kernel"](
            grid_sz,
            (self._tile_width,) * 3,
            (buf, buf, threshold, nx, ny, nz)
        )

        coords = cp.nonzero(buf)
        coords = [cp.asnumpy(_coords) for _coords in coords]
        logger.debug(f'found {len(coords[0])} candidates')
        peak_list = []
        for coord in zip(*coords):
            print(coord)
            peak_list.append(coord)
        peak_list.sort(key=lambda x: x[0], reverse=True)

        return peak_list
Пример #19
0
    def cupy_unique_axis0(array, return_counts=False):
        """
        Support method as cupy currently doesn't support .unique + axis
        :param array:
        :param return_counts:
        :return:
        """
        if len(array.shape) != 2:
            raise ValueError("Input array must be 2D.")

        sortarr = array[np.lexsort(array.T[::-1])]
        mask = np.empty(array.shape[0], dtype=np.bool_)
        mask[0] = True
        mask[1:] = np.any(sortarr[1:] != sortarr[:-1], axis=1)

        if return_counts:
            nonzero = np.nonzero(mask)[0]  # may synchronize
            idx = np.empty((nonzero.size + 1, ), nonzero.dtype)
            idx[:-1] = nonzero
            idx[-1] = mask.size
            return sortarr[mask], idx[1:] - idx[:-1]
        else:
            return sortarr[mask]
Пример #20
0
def unique(ar,
           return_index=False,
           return_inverse=False,
           return_counts=False,
           axis=None):
    """ This implementation is copied from cupy 6.0.0.a
    Find the unique elements of an array.
    Returns the sorted unique elements of an array. There are three optional
    outputs in addition to the unique elements:
    * the indices of the input array that give the unique values
    * the indices of the unique array that reconstruct the input array
    * the number of times each unique value comes up in the input array
    Args:
        ar(array_like): Input array. This will be flattened if it is not
            already 1-D.
        return_index(bool, optional): If True, also return the indices of `ar`
            (along the specified axis, if provided, or in the flattened array)
            that result in the unique array.
        return_inverse(bool, optional): If True, also return the indices of the
            unique array (for the specified axis, if provided) that can be used
            to reconstruct `ar`.
        return_counts(bool, optional): If True, also return the number of times
            each unique item appears in `ar`.
        axis(int or None, optional): Not supported yet.
    Returns:
        cupy.ndarray or tuple:
            If there are no optional outputs, it returns the
            :class:`cupy.ndarray` of the sorted unique values. Otherwise, it
            returns the tuple which contains the sorted unique values and
            followings.
            * The indices of the first occurrences of the unique values in the
              original array. Only provided if `return_index` is True.
            * The indices to reconstruct the original array from the
              unique array. Only provided if `return_inverse` is True.
            * The number of times each of the unique values comes up in the
              original array. Only provided if `return_counts` is True.
    .. seealso:: :func:`numpy.unique`
    """
    if axis is not None:
        raise NotImplementedError('axis option is not supported yet.')

    ar = cp.asarray(ar).flatten()

    if return_index or return_inverse:
        perm = ar.argsort()
        aux = ar[perm]
    else:
        ar.sort()
        aux = ar
    mask = cp.empty(aux.shape, dtype=cp.bool_)
    mask[0] = True
    mask[1:] = aux[1:] != aux[:-1]

    ret = aux[mask]
    if not return_index and not return_inverse and not return_counts:
        return ret

    ret = ret,
    if return_index:
        ret += perm[mask],
    if return_inverse:
        imask = cp.cumsum(mask) - 1
        inv_idx = cp.empty(mask.shape, dtype=cp.intp)
        inv_idx[perm] = imask
        ret += inv_idx,
    if return_counts:
        nonzero = cp.nonzero(mask)[0]
        idx = cp.empty((nonzero.size + 1, ), nonzero.dtype)
        idx[:-1] = nonzero
        idx[-1] = mask.size
        ret += idx[1:] - idx[:-1],
    return ret
Пример #21
0
 def test_argwhere(self):
     with testing.assert_warns(DeprecationWarning):
         return cupy.nonzero(self.array)
Пример #22
0
 def coords(self):
     indices = cp.nonzero(self.image)
     return cp.vstack(
         [indices[i] + self.slice[i].start for i in range(self._ndim)]).T
Пример #23
0
def get_good_channels(raw_data=None, probe=None, params=None):
    """
    of the channels indicated by the user as good (chanMap)
    further subset those that have a mean firing rate above a certain value
    (default is ops.minfr_goodchannels = 0.1Hz)
    needs the same filtering parameters in ops as usual
    also needs to know where to start processing batches (twind)
    and how many channels there are in total (NchanTOT)
    """
    fs = params.fs
    fshigh = params.fshigh
    fslow = params.fslow
    Nbatch = get_Nbatch(raw_data, params)
    NT = params.NT
    spkTh = params.spkTh
    nt0 = params.nt0
    minfr_goodchannels = params.minfr_goodchannels
    car_first = params.car_first
    car_type = params.car_type

    chanMap = probe.chanMap
    NchanTOT = len(chanMap)

    ich = []
    k = 0
    ttime = 0

    # skip every 100 batches
    # TODO: move_to_config - every N batches
    for ibatch in tqdm(range(0, Nbatch, int(ceil(Nbatch / 100))),
                       desc="Finding good channels"):
        i = NT * ibatch
        if (i + NT) > raw_data.shape[0]:
            break
        buff = raw_data[i:i + NT]
        # buff = _make_fortran(buff)
        # NOTE: using C order now
        assert buff.shape[0] > buff.shape[1]
        assert buff.flags.c_contiguous
        if buff.size == 0:
            break

        # Put on GPU.
        buff = cp.asarray(buff, dtype=np.float32)
        assert buff.flags.c_contiguous
        datr = gpufilter(buff,
                         chanMap=chanMap,
                         fs=fs,
                         fshigh=fshigh,
                         fslow=fslow,
                         car_first=car_first,
                         car_type=car_type)
        assert datr.shape[0] > datr.shape[1]

        # very basic threshold crossings calculation
        s = cp.std(datr, axis=0)
        datr = datr / s  # standardize each channel ( but don't whiten)
        # TODO: move_to_config (30 sample range)
        mdat = my_min(
            datr, 30,
            0)  # get local minima as min value in +/- 30-sample range

        # take local minima that cross the negative threshold
        xi, xj = cp.nonzero((datr < mdat + 1e-3) & (datr < spkTh))

        # filtering may create transients at beginning or end. Remove those.
        xj = xj[(xi >= nt0) & (xi <= NT - nt0)]

        # collect the channel identities for the detected spikes
        ich.append(xj)
        k += xj.size

        # keep track of total time where we took spikes from
        ttime += datr.shape[0] / fs

    ich = cp.concatenate(ich)

    # count how many spikes each channel got
    nc, _ = cp.histogram(ich, cp.arange(NchanTOT + 1))

    # divide by total time to get firing rate
    nc = nc / ttime

    # keep only those channels above the preset mean firing rate
    igood = cp.asnumpy(nc >= minfr_goodchannels)

    if len(igood) == 0:
        raise RuntimeError(
            "No good channels found! Verify your raw data and parameters.")
    if np.sum(igood) == 0:
        raise RuntimeError(
            "No good channels found! Verify your raw data and parameters.")

    logger.info('Found %d threshold crossings in %2.2f seconds of data.' %
                (k, ttime))
    logger.info('Found %d/%d bad channels.' % (np.sum(~igood), len(igood)))

    return igood
Пример #24
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--batchsize', '-b', type=int, default=100,
                        help='Number of examples in each mini-batch')
    parser.add_argument('--bproplen', '-l', type=int, default=200,
                        help='Number of words in each mini-batch '
                             '(= length of truncated BPTT)')
    parser.add_argument('--epoch', '-e', type=int, default=40,
                        help='Number of sweeps over the dataset to train')
    parser.add_argument('--gpu', '-g', type=int, default=0,
                        help='GPU ID (negative value indicates CPU)')

    parser.add_argument('--out', '-o', default='result',
                        help='Directory to output the result')

    parser.add_argument('--file', default="enwik8",
                        help='path to text file for training')
    parser.add_argument('--unit', '-u', type=int, default=2800,
                        help='Number of LSTM units')
    parser.add_argument('--embd', type=int, default=400,
                        help='Number of embedding units')
    parser.add_argument('--hdrop', type=float, default=0.2,
                        help='hidden state dropout (variational)')
    parser.add_argument('--edrop', type=float, default=0.5,
                        help='embedding dropout')

    args = parser.parse_args()

    nembd = args.embd
    #number of training iterations per model save, log write, and validation set evaluation
    interval =100

    pdrop = args.hdrop

    pdrope = args.edrop

    #initial learning rate
    alpha0 = .001
    #inverse of linear decay rate towards 0
    dec_it = 12*9000
    #minimum learning rate
    alpha_min = .00007

    #first ntrain words of dataset will be used for training
    ntrain = 90000000


    seqlen = args.bproplen
    nbatch = args.batchsize

    filename= args.file

    text,mapping = get_char(filename)
    sequence = np.array(text).astype(np.int32)

    itrain =sequence[0:ntrain]
    ttrain = sequence[1:ntrain+1]
    fullseql=int(ntrain/nbatch)

    itrain = itrain.reshape(nbatch,fullseql)
    ttrain = ttrain.reshape(nbatch,fullseql)

    #doesn't use full validations set
    nval = 500000
    ival = sequence[ntrain:ntrain+nval]
    tval = sequence[ntrain+1:ntrain+nval+1]

    ival = ival.reshape(ival.shape[0]//1000,1000)
    tval = tval.reshape(tval.shape[0]//1000,1000)
    #test = sequence[ntrain+nval:ntrain+nval+ntest]


    nvocab = max(sequence) + 1  # train is just an array of integers
    print('#vocab =', nvocab)

    # Prepare an RNNLM model
    rnn = RNNForLM(nvocab, args.unit,args.embd)
    model = L.Classifier(rnn)
    model.compute_accuracy = False  # we only want the perplexity
    if args.gpu >= 0:
        chainer.cuda.get_device(args.gpu).use()  # make the GPU current
        model.to_gpu()

    # Set up an optimizer
    optimizer = Adam(alpha=alpha0)
    optimizer.setup(model)
    resultdir = args.out

    print('starting')
    nepoch = args.epoch

    start = 0
    loss_sum = 0;

    if not os.path.isdir(resultdir):
        os.mkdir(resultdir)

    vloss = test(rnn,ival,tval)
    vloss= (1.4427*vloss)
    f = open(os.path.join(resultdir,'log'), 'w')
    outstring = "Initial Validation loss (bits/word): " + str(vloss) + '\n'
    f.write(outstring)
    f.close()

    i=0
    epoch_num = 0
    it_num = 0

    while True:
        # Get the result of the forward pass.
        fin = start+seqlen

        if fin>(itrain.shape[1]):
            start = 0
            fin = start+seqlen
            epoch_num = epoch_num+1
            if epoch_num== nepoch:
                break

        inputs = itrain[:,start:fin]
        targets = ttrain[:,start:fin]
        start = fin

        inputs = Variable(inputs)
        targets = Variable(targets)

        targets.to_gpu()
        inputs.to_gpu()
        it_num+=1
        loss = 0
        rnn.applyWN()

        #make hidden dropout mask
        mask = cp.zeros((inputs.shape[0],args.unit),dtype = cp.float32)
        ind = cp.nonzero(cp.random.rand(inputs.shape[0],args.unit)>pdrop)
        mask[ind] = 1/(1-pdrop)

        #make embedding dropout mask
        mask2 = cp.zeros((inputs.shape[0],nembd),dtype = cp.float32)
        ind = cp.nonzero(cp.random.rand(inputs.shape[0],nembd)>pdrope)
        mask2[ind] = 1/(1-pdrope)

        for j in range(seqlen):

            output = rnn(inputs[:,j],mask,mask2)
            loss = loss+ F.softmax_cross_entropy(output,targets[:,j])

        loss = loss/(seqlen)

        # Zero all gradients before updating them.
        rnn.zerograds()
        loss_sum += loss.data

        # Calculate and update all gradients.
        loss.backward()
        s = 0;

        # Use the optmizer to move all parameters of the network
        # to values which will reduce the loss.
        optimizer.update()
        #decays learning rate linearly
        optimizer.alpha = alpha0*(dec_it-it_num)/float(dec_it)
        #prevents learning rate from going below minumum
        if optimizer.alpha<alpha_min:
            optimizer.alpha = alpha_min

        loss.unchain_backward()

        if ((i+1)%interval) ==0:
            rnn.reset_state()
            vloss = test(rnn,ival,tval)

            #converts to binary entropy
            vloss= (1.4427*vloss)
            loss_sum = (1.4427*loss_sum/interval)

            serializers.save_npz(os.path.join(resultdir,'model'),rnn)

            outstring = "Training iteration: " + str(i+1) + " Training loss (bits/char): " + str(loss_sum) + " Validation loss (bits/word): " + str(vloss) + '\n'
            f = open(os.path.join(resultdir,'log'), 'a')
            f.write(outstring)
            f.close()
            print("Training iteration: " + str(i+1))
            print('training loss: ' + str(loss_sum))
            print('validation loss: ' + str(vloss))
            loss_sum=0

        i+=1
    def admittanceMatrixE(self):
        shapeParams = self.shapeFunctionParameters()
        whereIsZero = (cp.absolute(shapeParams) - 1e-12 < 0)
        indexZero = cp.where(whereIsZero)
        isConst = indexZero[2] == 2 # 1 for const x, 0 for const y
        indicesConstX = cp.where(isConst)[0]
        indicesConstY = cp.where(~isConst)[0]
        sortedElNodeIndices = cp.sort(self.nodeisElectrode[self.isValid], axis=1)
        admittanceMatrixE = cp.zeros((self.n_pts, self.ne))
        shapeMatrix = cp.zeros((shapeParams.shape[0], shapeParams.shape[1], 2))
        integratingMatrix = cp.zeros((shapeParams.shape[0], 2))
        shapeMatrix[indicesConstY, :, 0] = shapeParams[indicesConstY, :, 0] + shapeParams[indicesConstY, :, 2] * self.pts[sortedElNodeIndices, :][indicesConstY, 1, 1][:, None]
        shapeMatrix[indicesConstY, :, 1] = shapeParams[indicesConstY, :, 1]
        shapeMatrix[indicesConstX, :, 0] = shapeParams[indicesConstX, :, 0] + shapeParams[indicesConstX, :, 1] * self.pts[sortedElNodeIndices, :][indicesConstX, 1, 0][:, None]
        shapeMatrix[indicesConstX, :, 1] = shapeParams[indicesConstX, :, 2]
        integratingMatrix[indicesConstY, 0] = self.pts[sortedElNodeIndices, :][indicesConstY, 1, 0] - self.pts[sortedElNodeIndices, :][indicesConstY, 0, 0]
        integratingMatrix[indicesConstY, 1] = 0.5 * (cp.power(self.pts[sortedElNodeIndices, :][indicesConstY, 1, 0], 2) - cp.power(self.pts[sortedElNodeIndices, :][indicesConstY, 0, 0], 2))
        integratingMatrix[indicesConstX, 0] = self.pts[sortedElNodeIndices, :][indicesConstX, 1, 1] - self.pts[sortedElNodeIndices, :][indicesConstX, 0, 1]
        integratingMatrix[indicesConstX, 1] = 0.5 * (cp.power(self.pts[sortedElNodeIndices, :][indicesConstX, 1, 1], 2) - cp.power(self.pts[sortedElNodeIndices, :][indicesConstX, 0, 1], 2))
        #print(integratingMatrix.shape)
        integrals = cp.einsum('ijk, ik -> ij', shapeMatrix, integratingMatrix)
        integrals[:] = cp.absolute(integrals)
        #integr = cp.sum(cp.multiply(shapeMatrix, integratingMatrix[:, None]), axis=2)

        #print(cp.sum(cp.round_(integrals, 16) == cp.round_(integr, 16)))

        indexElectrode = sortedElNodeIndices[:, 0] // self.n_per_el
        #print(indexElectrode)
        integrals = - integrals / self.z[indexElectrode][:, None, None]
        integrals = integrals.ravel()
        indexElectrode = cp.tile(indexElectrode, (self.n_per_el, 1)).T.ravel()
        #print(self.tri[twoFromElectrode][isValid])
        indexNode = self.tri[self.twoFromElectrode][self.isValid].ravel()
        
        #admittanceMatrixE [self.tri[twoFromElectrode][isValid].ravel(), indexElectrode] += integrals.ravel()
        indSort = cp.argsort(indexNode)
        indexNode = indexNode[indSort]
        indexElectrode = indexElectrode[indSort]
        integrals = integrals[indSort]

        unique, counts = cp.unique(indexNode, return_counts=True)
        #print("number of unique entries", unique.shape)
        #print("counts \n", counts)
        index_pointer = cp.zeros(self.n_pts + 1)
        sum_count = cp.cumsum(counts)
        #print(sum_count)
        index_pointer[unique[:]+1] = sum_count[:]
        #print(index_pointer)
        nonzeroes = cp.nonzero(index_pointer)[0]
        #print(nonzeroes)
        mask = cp.zeros(index_pointer.shape[0], dtype='b1')
        mask[nonzeroes] = True
        mask[0] = True
        zeroes = cp.where(~mask)[0]
        #time_loop = time()
        while (index_pointer[1:]==0).any():
            index_pointer[zeroes] = index_pointer[zeroes - 1]
        '''for i in range(index_pointer.shape[0]):
            if i == 0:
                continue
            elif index_pointer[i] == 0:
                index_pointer[i] = index_pointer[i-1]'''
        #print('time for loop ',time()-time_loop)
        index_pointer2 = cp.arange(self.n_pts + 1)
        #print('indexEl', indexElectrode)
        #print(index_pointer.shape)
        admittanceMatrixE = sp.csr_matrix((integrals, indexElectrode, index_pointer), shape=(self.n_pts, self.ne), dtype=integrals.dtype)
        adm = admittanceMatrixE.toarray();
        #print(integrals)
        #print(indexNode)
        #print(indexElectrode)
        #a = (sortedElNodeIndices[0,0])
        #print(adm[4])
        # print(adm[:,1])
        #print('sum zeroes ',cp.sum(adm>0))
        return adm; 
Пример #26
0
 def time_nonzero(self):
     np.nonzero(self.b)
Пример #27
0
def _correlate_or_convolve(
    input,
    weights,
    output,
    mode,
    cval,
    origin,
    convolution,
    dtype_mode,
    use_weights_mask=False,
):
    # if use_weights_mask:
    #     raise NotImplementedError("TODO")
    origins, int_type = _check_nd_args(input, weights, mode, origin)
    if weights.size == 0:
        return cupy.zeros_like(input)
    if convolution:
        weights = weights[tuple([slice(None, None, -1)] * weights.ndim)]
        origins = list(origins)
        for i, wsize in enumerate(weights.shape):
            origins[i] = -origins[i]
            if wsize % 2 == 0:
                origins[i] -= 1
        origins = tuple(origins)
    elif weights.dtype.kind == "c":
        # numpy.correlate conjugates weights rather than input.
        weights = weights.conj()

    if dtype_mode == "numpy":
        # This "numpy" mode is used by cupyimg.scipy.signal.signaltools
        # numpy.convolve and correlate do not always cast to floats
        dtype = cupy.promote_types(input.dtype, weights.dtype)
        output_dtype = dtype
        if dtype.char == "e":
            # promote internal float type to float32 for accuracy
            dtype = "f"
        if output is not None:
            raise ValueError(
                "dtype_mode == 'numpy' does not support the output "
                "argument")
        weight_dtype = dtype
        if weights.dtype != dtype:
            weights = weights.astype(dtype)
        if input.dtype != dtype:
            input = input.astype(dtype)
        output = cupy.zeros(input.shape, output_dtype)
        weight_dtype = dtype
    else:
        if weights.dtype.kind == "c" or input.dtype.kind == "c":
            if dtype_mode == "ndimage":
                weight_dtype = cupy.complex128
            elif dtype_mode == "float":
                weight_dtype = cupy.promote_types(input.real.dtype,
                                                  cupy.complex64)
        else:
            if dtype_mode == "ndimage":
                weight_dtype = cupy.float64
            elif dtype_mode == "float":
                weight_dtype = cupy.promote_types(input.real.dtype,
                                                  cupy.float32)
        weight_dtype = cupy.dtype(weight_dtype)
        output = _util._get_output(output, input, None, weight_dtype)
    unsigned_output = output.dtype.kind in ["u", "b"]

    if use_weights_mask:
        input = cupy.ascontiguousarray(input)

        # The kernel needs only the non-zero kernel values and their coordinates.
        # This allows us to use a single for loop to compute the ndim convolution.
        # The loop will be over only the the non-zero entries of the filter.
        weights = cupy.ascontiguousarray(weights, weight_dtype)
        wlocs = cupy.nonzero(weights)
        wvals = weights[wlocs]  # (nnz,) array of non-zero values
        wlocs = cupy.stack(
            wlocs)  # (ndim, nnz) array of indices for these values

        return _get_correlate_kernel_masked(
            mode,
            cval,
            input.shape,
            weights.shape,
            wvals.size,
            tuple(origins),
            unsigned_output,
        )(input, wlocs, wvals, output)
    else:
        if mode == "constant":
            # TODO: negative strides gives incorrect result for constant mode
            #       so make sure input is C contiguous.
            input = cupy.ascontiguousarray(input)
        kernel = _get_correlate_kernel(mode, weights.shape, int_type, origins,
                                       cval, unsigned_output)
        return _call_kernel(kernel, input, weights, output, weight_dtype)
def get_sentence_boundaries(metadata_df, token_ar, stride, fs_index_ls):
    """
        Given token array and meta-data we create sentence boundaries 
        We consider a sentence boundary as one which is at eol-chars (`##.`,`.`) or start/end of a review
    """
    seq_len = token_ar.shape[1]

    fullstop_flag = None
    for fs_token_idx in fs_index_ls:
        if fullstop_flag is None:
            fullstop_flag = token_ar == fs_token_idx
        else:
            fullstop_flag = (fullstop_flag) | (token_ar == fs_token_idx)

    fullstop_row, fullstop_col = cp.nonzero(fullstop_flag)

    min_row_df = (
        metadata_df.groupby("input_text_index").seq_row.min().reset_index(
            drop=False))
    min_row_df.rename(columns={"seq_row": "min_row"}, inplace=True)
    max_row_df = (
        metadata_df.groupby("input_text_index").seq_row.max().reset_index(
            drop=False))
    max_row_df.rename(columns={"seq_row": "max_row"}, inplace=True)

    metadata_df = metadata_df.merge(min_row_df).merge(max_row_df)

    ### Can filter to only sequences that have the org
    ## if below becomes a bottleneck

    fullstop_df = cudf.DataFrame()
    fullstop_df["seq_row"] = cudf.Series(fullstop_row)
    fullstop_df["fs_seq_col"] = cudf.Series(fullstop_col)
    fullstop_df = fullstop_df.merge(metadata_df)

    fullstop_df.rename(columns={"seq_row": "fs_seq_row"}, inplace=True)

    first_row_df = cudf.DataFrame()
    first_row_df["input_text_index"] = min_row_df["input_text_index"]
    first_row_df["fs_seq_row"] = min_row_df["min_row"]
    first_row_df["fs_seq_col"] = 1
    first_row_df["min_row"] = min_row_df["min_row"]
    first_row_df = first_row_df.merge(
        max_row_df[["input_text_index", "max_row"]])

    last_row_df = cudf.DataFrame()
    last_row_df["input_text_index"] = max_row_df["input_text_index"]
    last_row_df["fs_seq_row"] = max_row_df["max_row"]
    last_row_df["fs_seq_col"] = seq_len - 1
    last_row_df["max_row"] = max_row_df["max_row"]
    last_row_df = last_row_df.merge(min_row_df[["input_text_index",
                                                "min_row"]])

    fullstop_df = cudf.concat([fullstop_df, first_row_df, last_row_df])

    ## -2-> for padding
    valid_region = (seq_len - 2) - stride + 1

    ### only keep sentences in the valid_region
    valid_flag = fullstop_df["fs_seq_col"] < valid_region
    valid_flag = valid_flag | (fullstop_df["fs_seq_row"]
                               == fullstop_df["max_row"])
    fullstop_df = fullstop_df[valid_flag]

    fullstop_df["flat_loc_fs"] = (fullstop_df["fs_seq_row"] * seq_len +
                                  fullstop_df["fs_seq_col"])

    return fullstop_df[[
        "input_text_index", "fs_seq_row", "fs_seq_col", "flat_loc_fs"
    ]]
Пример #29
0
def check_batch(batch):
    idx_nonzero = cupy.nonzero(batch)[0]
    # nonzero = batch[idx_nonzero]
    # nonzero_cpu = cupy.asnumpy(nonzero)
    # bits = np.unpackbits(nonzero_cpu)
    return idx_nonzero
Пример #30
0
import time
import sys
import os
import scipy.misc
# import numpy as np
import cupy as np
# from numba import vectorize, float64
# import numba

abs2 = np.ElementwiseKernel(
    'complex64 x', 'float32 y',
    'y = isfinite(x) ? x.real()*x.real() + x.imag()*x.imag() : 100', 'abs2',
    False)
abs2.__call__(np.array([1 + 1j]).astype(np.complex64))
np.nonzero(np.array([1]).astype(np.float32))
map_dists_greater = np.ElementwiseKernel('float32 x', 'bool y', 'y = x > 4',
                                         'map_dists_greater', False)
map_dists_greater.__call__(np.array([1]).astype(np.float32))

map_dists_less = np.ElementwiseKernel('float32 x', 'bool y', 'y = x < 25',
                                      'map_dists_less', False)
map_dists_less.__call__(np.array([1]).astype(np.float32))


def get_random_points(x_min, x_max, y_min, y_max, n_points):
    s_p = np.random.random((n_points, 2), dtype=np.float32)
    s_p[:, 0] *= y_max - y_min + 0
    s_p[:, 1] *= x_max - x_min + 2
    s_p[:, 0] += y_min - 0
    s_p[:, 1] += x_min - 1
    # s_p[:,0] *= 3.0