解决AttributeError: module 'cupy' has no attribute 'ones'
今天在运行代码的时候发现,如图报错。

cupy.py
import numpy as np
import cupy as cp
import time
### Numpy and CPU
s = time.time()
x_cpu = np.ones((10000,10000))
e = time.time()
print(e - s)### CuPy and GPU
s = time.time()
x_gpu = cp.ones((10000,10000))
e = time.time()
print(e - s)
搜了一下,cupy是有“ones”这个属性的
仔细查了很久发现问题出在我的python代码文件名上
如上图,我的代码名为“cupy.py"和这个cupy库重名了
导致代码调用的时候指向不明确,给我报错。
于是,我把文件名改成了”cupy_test“,问题解决。
cupy_test.py
import numpy as np
import cupy as cp
import time
### Numpy and CPU
s = time.time()
x_cpu = np.ones((10000,10000))
e = time.time()
print(e - s)### CuPy and GPU
s = time.time()
x_gpu = cp.ones((10000,10000))
e = time.time()
print(e - s)