コード例 #1
0
def _allclose_dense_sparse(x, y, rtol=1e-7, atol=1e-9):
    """Check allclose for sparse and dense data.

    Both x and y need to be either sparse or dense, they
    can't be mixed.

    Parameters
    ----------
    x : array-like or sparse matrix
        First array to compare.

    y : array-like or sparse matrix
        Second array to compare.

    rtol : float, optional
        relative tolerance; see numpy.allclose

    atol : float, optional
        absolute tolerance; see numpy.allclose. Note that the default here is
        more tolerant than the default for numpy.testing.assert_allclose, where
        atol=0.
    """
    if sp.issparse(x) and sp.issparse(y):
        x = x.tocsr()
        y = y.tocsr()
        x.sum_duplicates()
        y.sum_duplicates()
        return (cp.array_equal(x.indices, y.indices)
                and cp.array_equal(x.indptr, y.indptr)
                and cp.allclose(x.data, y.data, rtol=rtol, atol=atol))
    elif not sp.issparse(x) and not sp.issparse(y):
        return cp.allclose(x, y, rtol=rtol, atol=atol)
    raise ValueError("Can only compare two sparse matrices, not a sparse "
                     "matrix and an array")
コード例 #2
0
 def __eq__(self, other):
     other = xp.asarray(other)
     if self.__class__ != other.__class__:
         return False
     if xp.array_equal(self.xx, other.xx) and xp.array_equal(
             self.yy, other.yy):
         return True
     else:
         return False
コード例 #3
0
def check_valid_y(data: np.ndarray) -> bool:
    """
    Check if any key has been pressed in the datased. Some files may not have any key recorded due to windows
    permission errors on some computers, people not using WASD or other problems, we want to discard these files.
    Input:
     - data: ndarray [num_examples x 6]
    Output:
    - Bool: True if the file is valid, False is there no key recorded
    """
    seen_keys: Set[int] = set()
    for i in range(0, data.shape[0]):
        if np.array_equal(data[i][5], [0, 0, 0, 0]):
            seen_keys.add(0)
        elif np.array_equal(data[i][5], [1, 0, 0, 0]):
            seen_keys.add(1)
        elif np.array_equal(data[i][5], [0, 1, 0, 0]):
            seen_keys.add(2)
        elif np.array_equal(data[i][5], [0, 0, 1, 0]):
            seen_keys.add(3)
        elif np.array_equal(data[i][5], [0, 0, 0, 1]):
            seen_keys.add(4)
        elif np.array_equal(data[i][5], [1, 0, 1, 0]):
            seen_keys.add(5)
        elif np.array_equal(data[i][5], [1, 0, 0, 1]):
            seen_keys.add(6)
        elif np.array_equal(data[i][5], [0, 1, 1, 0]):
            seen_keys.add(7)
        elif np.array_equal(data[i][5], [0, 1, 0, 1]):
            seen_keys.add(8)

        if len(seen_keys) >= 3:
            return True

    else:
        return False
コード例 #4
0
    def check_seed(self, seed):
        bg1 = self.bg(seed)
        bg2 = self.bg(seed)
        bg3 = self.bg(None)

        xs1 = bg1.random_raw(10)
        xs2 = bg2.random_raw(10)
        xs3 = bg3.random_raw(10)

        # Random state must be reproducible
        assert cupy.array_equal(xs1, xs2)
        # Random state must be initialized randomly with seed=None
        assert not cupy.array_equal(xs1, xs3)
def test_memory_reference_for_gpu_cuda():
    if Test_Mem_Ref.is_cuda_available() == True:
        algo = AlgoGPU()

        algo.whoami()
        algo.initialize_array(10)

        input = algo.get_input_memory()
        output = algo.get_output_memory()

        print(input)
        print(type(input))

        for i in range(10):
            input[i] = i / 5

        algo.compute(input, output)

        print(output)
        print(type(output))

        assert (cp.array_equal(input, output))
        assert (isinstance(input, cp._core.core.ndarray))
        assert (isinstance(output, cp._core.core.ndarray))

    else:
        print("CUDA is not available")
コード例 #6
0
 def test_prior_in_posteriors(self):
     for frame in self.data:
         frame["prior"] = 1
     like = HyperparameterLikelihood(posteriors=self.data,
                                     hyper_prior=self.model)
     self.assertTrue(
         xp.array_equal(like.sampling_prior, xp.ones_like(like.data["a"])))
コード例 #7
0
    def testFilterLikelihoods(self):
        """
    Tests _filterLikelihoods function for several cases:
      i. Likelihood goes straight to redzone, skipping over yellowzone, repeats
      ii. Case (i) with different values, and cupy array instead of float list
      iii. A scenario where changing the redzone from four to five 9s should
           filter differently
    """
        redThreshold = 0.9999
        yellowThreshold = 0.999

        # Case (i): values at indices 1 and 7 should be filtered to yellowzone
        l = [1.0, 1.0, 0.9, 0.8, 0.5, 0.4, 1.0, 1.0, 0.6, 0.0]
        l = [1 - x for x in l]
        l2 = copy.copy(l)
        l2[1] = 1 - yellowThreshold
        l2[7] = 1 - yellowThreshold
        l3 = an._filterLikelihoods(l, redThreshold=redThreshold)

        for i in range(len(l2)):
            self.assertAlmostEqual(l2[i], l3[i], msg="Failure in case (i)")

        # Case (ii): values at indices 1-10 should be filtered to yellowzone
        l = cupy.array([
            0.999978229, 0.999978229, 0.999999897, 1, 1, 1, 1, 0.999999994,
            0.999999966, 0.999999966, 0.999994331, 0.999516576, 0.99744487
        ])
        l = 1.0 - l
        l2 = copy.copy(l)
        l2[1:11] = 1 - yellowThreshold
        l3 = an._filterLikelihoods(l, redThreshold=redThreshold)

        for i in range(len(l2)):
            self.assertAlmostEqual(l2[i], l3[i], msg="Failure in case (ii)")

        # Case (iii): redThreshold difference should be at index 2
        l = cupy.array([
            0.999968329, 0.999999897, 1, 1, 1, 1, 0.999999994, 0.999999966,
            0.999999966, 0.999994331, 0.999516576, 0.99744487
        ])
        l = 1.0 - l
        l2a = copy.copy(l)
        l2b = copy.copy(l)
        l2a[1:10] = 1 - yellowThreshold
        l2b[2:10] = 1 - yellowThreshold
        l3a = an._filterLikelihoods(l, redThreshold=redThreshold)
        l3b = an._filterLikelihoods(l, redThreshold=0.99999)

        for i in range(len(l2a)):
            self.assertAlmostEqual(l2a[i],
                                   l3a[i],
                                   msg="Failure in case (iii), list a")

        for i in range(len(l2b)):
            self.assertAlmostEqual(l2b[i],
                                   l3b[i],
                                   msg="Failure in case (iii), list b")

        self.assertFalse(cupy.array_equal(l3a, l3b),
                         msg="Failure in case (iii), list 3")
コード例 #8
0
def crossover_chromosomes(x_probability, x_method, selection_method,
                          population, optim, encoding):
    parent_pairs = select_chromosomes(selection_method, population, optim)
    population_hat = None
    if x_method == 'single point':
        population_hat = single_point_crossover(parent_pairs, x_probability)
    elif x_method == 'multiple point':
        population_hat = multiple_point_crossover(parent_pairs, x_probability)
    elif x_method == 'uniform point':
        population_hat = uniform_crossover(parent_pairs, x_probability)
    # elif x_method == 'OX':
    #     population_hat = order_crossover(parent_pairs, x_probability, encoding)
    # elif x_method == 'PMX':
    #     population_hat = partially_mapped_crossover(parent_pairs, x_probability, encoding)
    else:
        print(
            '\nError [4]: Invalid crossover method [at crossover chromosomes]\nExiting...'
        )
        exit()
    # check crossover effect over current generation
    if cupy.array_equal(population, population_hat):
        print(
            '\nWarning [1]: No crossover change in population [at crossover chromosomes]'
        )
        return population
    else:
        return population_hat
def test_return_as_cupy_copy():
    a = cp.array([1.1, 3.14, 42.69])
    b = cupy_ref.Cupy_Ref(ptr = a.data.ptr, shape = a.shape, dtype = a.dtype, typestr = a.dtype.str)

    c = cp.array(b, dtype=b.dtype, copy=True) # we use copy to create a new cupy object with the same value of "a"
    
    assert(cp.array_equal(a,c))
    assert(a.data.ptr != c.data.ptr) # make sure it is copied
def test_return_as_cupy_using_cupy_ref_class():
    a = cp.array([1.1, 3.14, 42.69])
    b = cupy_ref.Cupy_Ref(ptr = a.data.ptr, shape = a.shape, dtype = a.dtype, typestr = a.dtype.str)

    c = b.get_cupy_array()
     
    assert(cp.array_equal(a,c))
    assert(a.data.ptr == c.data.ptr) # make sure it is not copied
def test_create_real_cupy_from_c():
    a = cp.array([3.14, 4.25, 5.36], dtype=cp.float64)
    b = Test_Custom_Cupy.test_create_real_cupy_from_c()

    assert (cp.array_equal(a, b))
    assert (
        cp.asnumpy(b).sum() == b.sum()
    )  #see if the normal cupy from c++ can be converted to numpy like a normal cupy
def test_send_cupy_complex_to_c_and_send_it_back():
    a = cp.array([[3.14, 4.25, 5.36], [4, 5, 6], [1.23, 4.56, 7.89]],
                 dtype=cp.complex128)
    b = Test_Cupy.test_send_cupy_complex_to_c_and_send_it_back(
        a.data.ptr, a.size, a.shape[0],
        a.shape[1])  #is there any way to receive shape a single variable?

    assert (cp.array_equal(a, b))
def test_return_as_cupy_not_copy():
    a = cp.array([1.1, 3.14, 42.69])
    b = cupy_ref.Cupy_Ref(ptr = a.data.ptr, shape = a.shape, dtype = a.dtype, typestr = a.dtype.str)

    c = cp.array(b, dtype=b.dtype, copy=False) # we dont use copy to get the original "a"
     
    assert(cp.array_equal(a,c))
    assert(a.data.ptr == c.data.ptr) # make sure it is not copied
コード例 #14
0
  def testFilterLikelihoods(self):
    """
    Tests _filterLikelihoods function for several cases:
      i. Likelihood goes straight to redzone, skipping over yellowzone, repeats
      ii. Case (i) with different values, and cupy array instead of float list
      iii. A scenario where changing the redzone from four to five 9s should
           filter differently
    """
    redThreshold = 0.9999
    yellowThreshold = 0.999

    # Case (i): values at indices 1 and 7 should be filtered to yellowzone
    l = [1.0, 1.0, 0.9, 0.8, 0.5, 0.4, 1.0, 1.0, 0.6, 0.0]
    l = [1 - x for x in l]
    l2 = copy.copy(l)
    l2[1] = 1 - yellowThreshold
    l2[7] = 1 - yellowThreshold
    l3 = an._filterLikelihoods(l, redThreshold=redThreshold)

    for i in range(len(l2)):
      self.assertAlmostEqual(l2[i], l3[i], msg="Failure in case (i)")


    # Case (ii): values at indices 1-10 should be filtered to yellowzone
    l = cupy.array([0.999978229, 0.999978229, 0.999999897, 1, 1, 1, 1,
                     0.999999994, 0.999999966, 0.999999966, 0.999994331,
                     0.999516576, 0.99744487])
    l = 1.0 - l
    l2 = copy.copy(l)
    l2[1:11] = 1 - yellowThreshold
    l3 = an._filterLikelihoods(l, redThreshold=redThreshold)

    for i in range(len(l2)):
      self.assertAlmostEqual(l2[i], l3[i], msg="Failure in case (ii)")


    # Case (iii): redThreshold difference should be at index 2
    l = cupy.array([0.999968329, 0.999999897, 1, 1, 1,
                     1, 0.999999994, 0.999999966, 0.999999966,
                     0.999994331, 0.999516576, 0.99744487])
    l = 1.0 - l
    l2a = copy.copy(l)
    l2b = copy.copy(l)
    l2a[1:10] = 1 - yellowThreshold
    l2b[2:10] = 1 - yellowThreshold
    l3a = an._filterLikelihoods(l, redThreshold=redThreshold)
    l3b = an._filterLikelihoods(l, redThreshold=0.99999)

    for i in range(len(l2a)):
      self.assertAlmostEqual(l2a[i], l3a[i],
                             msg="Failure in case (iii), list a")

    for i in range(len(l2b)):
      self.assertAlmostEqual(l2b[i], l3b[i],
                             msg="Failure in case (iii), list b")

    self.assertFalse(cupy.array_equal(l3a, l3b),
                     msg="Failure in case (iii), list 3")
def test_memory_holder():
    a = Test_Interface.GPU_memory_holder([3])
    b = a.get_memory_reference()
    c = cp.array(b, dtype=b.dtype, copy=False)

    a_2 = cp.array([0.0, 0.0, 0.0])

    assert(c.shape == a_2.shape)
    assert(cp.array_equal(c, a_2))
def test_memory():
    assert (cp.get_default_memory_pool().used_bytes() == 0)
    a = Test_Custom_Cupy.test_create_real_cupy_from_c()

    b = a * 2
    assert (cp.array_equal(b.sum(), a.sum() * 2))

    a = None
    b = None

    assert (cp.get_default_memory_pool().used_bytes() == 0)
コード例 #17
0
  def testBucketIndexSupport(self):
    """Check bucket index support"""
    bucketIndices = self._e.getBucketIndices(self._d)
    topDown = self._e.getBucketInfo(bucketIndices)
    topDownValues = cupy.array([elem.value for elem in topDown])
    errs = topDownValues - cupy.array([320.25, 3.5, .167, 14.8])
    self.assertAlmostEqual(errs.max(), 0, 4)

    encodings = []
    for x in topDown:
      encodings.extend(x.encoding)
    self.assertTrue(cupy.array_equal(encodings, self._expected))
def test_add_cupy_and_numpy():
    a = Test_Interface.GPU_memory_holder([3])
    b = a.get_memory_reference()
    c = cp.array(b, dtype=b.dtype, copy=False)

    data = np.array([1.0, 1.0, 1.0])

    c_inc = cp.array([1.0, 1.0, 1.0])

    for i in range(3):
        c[i] = data[i]

    assert cp.array_equal(c, c_inc)
コード例 #19
0
def mutate_chromosomes(probability, m_method, population, optim):
    population_hat = None
    if m_method[0] == 'random':
        population_hat = random_mutation(population, probability, m_method)
    elif m_method[0] == 'elitism':
        population_hat = elitism(population, probability, m_method, optim)
    else:
        print(
            '\nError [9]: Invalid mutation method [at mutate chromosomes]\nExiting...'
        )
        exit()
    # check mutation effect over current generation
    if cupy.array_equal(population, population_hat):
        # print('\nWarning [3]: No mutation change in population [at mutate chromosomes]')
        return population
    else:
        return population_hat
コード例 #20
0
ファイル: _routines_poly.py プロジェクト: the-lay/cupy
def roots(p):
    """Computes the roots of a polynomial with given coefficients.

    Args:
        p (cupy.ndarray or cupy.poly1d): polynomial coefficients.

    Returns:
        cupy.ndarray: polynomial roots.

    .. warning::

        This function doesn't support currently polynomial coefficients
        whose companion matrices are general 2d square arrays. Only those
        with complex Hermitian or real symmetric 2d arrays are allowed.

        The current `cupy.roots` doesn't guarantee the order of results.

    .. seealso:: :func:`numpy.roots`

    """
    if isinstance(p, cupy.poly1d):
        p = p.coeffs
    if p.dtype.kind == 'b':
        raise NotImplementedError('boolean inputs are not supported')
    if p.ndim == 0:
        raise TypeError('0-dimensional input is not allowed')
    if p.size < 2:
        return cupy.array([])
    [p] = cupy.polynomial.polyutils.as_series([p[::-1]])
    if p.size < 2:
        return cupy.array([])
    if p.size == 2:
        out = (-p[0] / p[1])[None]
        if p[0] == 0:
            out = out.real.astype(numpy.float64)
        return out
    cmatrix = cupy.polynomial.polynomial.polycompanion(p)
    # TODO(Dahlia-Chehata): Support after cupy.linalg.eigvals is supported
    if cupy.array_equal(cmatrix, cmatrix.conj().T):
        out = cupy.linalg.eigvalsh(cmatrix)
    else:
        raise NotImplementedError('Only complex Hermitian and real '
                                  'symmetric 2d arrays are supported '
                                  'currently')
    return out.astype(p.dtype)
def test_memory_sychronization():
    """
    Check if automatic copy to host and copy to device are correct done,
    if c++ cuda kernel is called via python binding
    """
    size = 30

    mem_holder = Test_Interface.GPU_memory_holder([size])
    mem_holder_ref = mem_holder.get_memory_reference()
    mem_holder_array = mem_holder_ref.get_cupy_array()

    data = np.arange(1, size + 1, 1, np.float64)
    expected_result = cp.array(data + 1)

    for i in range(size):
        mem_holder_array[i] = data[i]

    Test_Interface.incOne(mem_holder_ref, size)

    assert cp.array_equal(expected_result, mem_holder_array)
コード例 #22
0
ファイル: _routines_poly.py プロジェクト: takagi/cupy
def poly(seq_of_zeros):
    """Computes the coefficients of a polynomial with the given roots sequence.

    Args:
        seq_of_zeros (cupy.ndarray): a sequence of polynomial roots.

    Returns:
        cupy.ndarray: polynomial coefficients from highest to lowest degree.

    .. warning::

        This function doesn't support general 2d square arrays currently.
        Only complex Hermitian and real symmetric 2d arrays are allowed.

    .. seealso:: :func:`numpy.poly`

    """
    x = seq_of_zeros
    if x.ndim == 2 and x.shape[0] == x.shape[1] and x.shape[0] != 0:
        if cupy.array_equal(x, x.conj().T):
            x = cupy.linalg.eigvalsh(x)
        else:
            raise NotImplementedError('Only complex Hermitian and real '
                                      'symmetric 2d arrays are supported '
                                      'currently')
    elif x.ndim == 1:
        x = x.astype(cupy.mintypecode(x.dtype.char), copy=False)
    else:
        raise ValueError('Input must be 1d or non-empty square 2d array.')

    if x.size == 0:
        return 1.0

    size = 2 ** (x.size - 1).bit_length()
    a = cupy.zeros((size, 2), x.dtype)
    a[:, 0].fill(1)
    cupy.negative(x, out=a[:x.size, 1])
    while size > 1:
        size = size // 2
        a = cupy._math.misc._fft_convolve(a[:size], a[size:], 'full')
    return a[0, :x.size + 1]
def test_generating_cupy_of_complex_double_from_c():
    a = cp.array([3.14, 4.25, 5.36], dtype=cp.complex128)
    b = Test_Cupy.test_generating_cupy_of_complex_double_from_c()

    assert (cp.array_equal(a, b))