亲宝软件园·资讯

展开

minpy使用GPU加速Numpy科学计算方式

乌拉队长 人气:0

minpy使用GPU加速Numpy科学计算

minpy是一个基于MXNet的支持GPU的加速Numpy计算的库,用法和Numpy几乎一样,使用:

import scipy.io as sio
import matplotlib as plt
import minpy.numpy as np

使用时只需要在Numpy前面加上minpy,就可以像Numpy一样使用它进行矩阵运算。

甩一条MXNet官网链接

minpy安装起来也很简单:

先安装MXNet依赖

# 安装cuda10.1版本的MXNet
pip install mxnet-cu101
  
# 如果你的cuda版本为10.0,则执行下面的命令,其他版本同理
pip install mxnet-cu100

然后安装minpy:

pip install minpy

然后就可以正常使用了。

Install mxnet and install minpy

make sure the things below.

how to install mxnet?

pip install -i http://pypi.tuna.tsinghua.edu.cn/simple     --upgrade mxnet-cu90

use cu90 because my cuda version is 9.0

then install minpy

pip install minpy

then is a demo to test minpy gpu acceleration.

import time    
import numpy as np    
import numpy.random as random    
import minpy.numpy as mnp    
    
    
def main():    
    random.seed(0)    
    X = random.randn(10000, 16000)    
    A = np.array(X,dtype=np.float32)    
    Y = random.randn(16000, 5000)    
    B = np.array(Y,dtype=np.float32)    
    
    print("A.shape:%s" ,A.shape)    
    print("B.shape:%s" ,B.shape)    
    
    start = time.time()    
    C = mnp.dot(A,B)    
    d1 = time.time() - start    
    print('minpy numpy:', d1)    
    print(C)    
    
    start = time.time()    
    C = np.dot(A,B)    
    d2 = time.time() - start    
    print('numpy:', d2)    
    print(C)    
    print("%s" , d2/d1)    
    
    
if __name__ == '__main__':    
    main()    

output

A.shape:%s (10000, 16000)
B.shape:%s (16000, 5000)
minpy numpy: 0.3046295642852783
[[-129.23964     34.24473    205.77763   ...   64.57458   -134.04288
  -282.5226   ]
 [  56.055874   151.66455      4.534541  ...  -59.855354    77.807755
   102.97847  ]
 [  53.7853    -133.20685   -114.16803   ...  -78.15841    -22.429447
  -100.71634  ]
 ...
 [  18.944311  -179.30074   -114.42271   ...  -22.20309    -29.131681
    16.166618 ]
 [  -5.1453457  -11.761197   -28.63139   ... -236.34016    -67.44423
   -50.811813 ]
 [ 137.46251    -77.67743    -74.262535  ...  -25.249132    83.94517
   -14.008699 ]]
numpy: 3.323066234588623
[[-129.23964     34.24473    205.77763   ...   64.57458   -134.04288
  -282.5226   ]
 [  56.055874   151.66455      4.534541  ...  -59.855354    77.807755
   102.97847  ]
 [  53.7853    -133.20685   -114.16803   ...  -78.15841    -22.429447
  -100.71634  ]
 ...
 [  18.944311  -179.30074   -114.42271   ...  -22.20309    -29.131681
    16.166618 ]
 [  -5.1453457  -11.761197   -28.63139   ... -236.34016    -67.44423
   -50.811813 ]
 [ 137.46251    -77.67743    -74.262535  ...  -25.249132    83.94517
   -14.008699 ]]
%s 10.908548034020265

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论