lebedov / scikit-cuda

Python interface to GPU-powered libraries
http://scikit-cuda.readthedocs.org/
Other
986 stars 179 forks source link

Error replicating the PCA example in Docs #305

Open Feulo opened 4 years ago

Feulo commented 4 years ago

Problem

I'm trying to replicate the PCA example in docs with the following code:

import pycuda.autoinit
import pycuda.gpuarray as gpuarray
import numpy as np
import skcuda.linalg as linalg
from skcuda.linalg import PCA as cuPCA
pca = cuPCA(n_components=4) # map the data to 4 dimensions
X = np.random.rand(1000,100) # 1000 samples of 100-dimensional data vectors
X_gpu = gpuarray.GPUArray((1000,100), np.float64) # note that order="F" or a transpose is necessary. fit_transform requires row-major matrices, and column-major is the default
X_gpu.set(X) # copy data to gpu
T_gpu = pca.fit_transform(X_gpu) # calculate the principal components
linalg.dot(T_gpu[:,0], T_gpu[:,1]) # show that the resulting eigenvectors are orthogonal

But I get the error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-aa2d786e8368> in <module>
      8 X_gpu = gpuarray.GPUArray((1000,100), np.float64, order="F") # note that order="F" or a transpose is necessary. fit_transform requires row-major matrices, and column-major is the default
      9 X_gpu.set(X) # copy data to gpu
---> 10 T_gpu = pca.fit_transform(X_gpu) # calculate the principal components
     11 linalg.dot(T_gpu[:,0], T_gpu[:,1]) # show that the resulting eigenvectors are orthogonal

/opt/conda/lib/python3.6/site-packages/scikit_cuda-0.5.4-py3.6.egg/skcuda/linalg.py in fit_transform(self, X_gpu, transposed_input)
    166             raise ValueError("Array must be 2D for PCA")
    167         if X_gpu.flags.f_contiguous:
--> 168             raise ValueError("Array must be c_contiguous. Please do NOT instantiate with "
    169                              "'order=\"F\"'")
    170         if transposed_input:

ValueError: Array must be c_contiguous. Please do NOT instantiate with 'order="F"'

And if I remove de order='F', the two vector are not orthogonal, the inner procdict result is always not zero.

Environment

HarleyAppleChoi commented 3 years ago

I get the same problem with google colab with the following spec:

!nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Wed_Jul_22_19:09:09_PDT_2020
Cuda compilation tools, release 11.0, V11.0.221
Build cuda_11.0_bu.TC445_37.28845127_0