[{"data":1,"prerenderedAt":397},["ShallowReactive",2],{"content-query-8UonwS2hce":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":391,"_id":392,"_source":393,"_file":394,"_stem":395,"_extension":396},"/technology-blogs/zh/1047","zh",false,"","为SciPy插上异构的翅膀，MindSpore SciPy教程详解","SciPy 是基于NumPy实现的科学计算库","2022-02-21","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/03/02/ccfa5bd411674f61a002d7be808a38b2.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":381},"root",[17,25,29,51,57,62,67,72,77,83,88,97,102,107,117,122,129,134,142,150,155,163,178,184,189,194,202,207,215,220,228,233,241,246,254,259,267,272,283,291,297,302,310,323,329,334,339,344,349,354,359,370],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"为scipy插上异构的翅膀mindspore-scipy教程详解",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"h3",{"id":7},[],{"type":18,"tag":30,"props":31,"children":32},"p",{},[33,44,46],{"type":18,"tag":34,"props":35,"children":36},"strong",{},[37,39],{"type":24,"value":38},"作者：",{"type":18,"tag":34,"props":40,"children":41},{},[42],{"type":24,"value":43},"Mrzoloft",{"type":24,"value":45},"｜",{"type":18,"tag":34,"props":47,"children":48},{},[49],{"type":24,"value":50},"来源：华为云 MindSpore论坛",{"type":18,"tag":26,"props":52,"children":54},{"id":53},"_1-mindspore框架的scipy模块",[55],{"type":24,"value":56},"1. MindSpore框架的SciPy模块",{"type":18,"tag":30,"props":58,"children":59},{},[60],{"type":24,"value":61},"SciPy 是基于NumPy实现的科学计算库，主要用于数学、物理学、生物学等科学以及工程学领域。诸如高阶迭代，线性代数求解等都会需要用到SicPy。SciPy大体上有数值最优化、线性代数、积分、插值、信号处理、常微分方程求解等计算求解模块。",{"type":18,"tag":30,"props":63,"children":64},{},[65],{"type":24,"value":66},"原生的SciPy科学计算库仅可CPU平台下使能，对于GPU、Ascend等计算平台无法使用且没有高效的实现。MindSpore是以Python作为表达语言的AI框架，能够合理利用GPU和Ascend的计算能力进行高效计算，并且MindSopre提供了SciPy模块。mindsopre.scipy是MindSpore基于mindspore.numpy和自研算子实现的科学计算库，不仅兼容原生的SciPy能在CPU平台高效运行，而且支持在GPU、Ascend等平台加速计算。",{"type":18,"tag":30,"props":68,"children":69},{},[70],{"type":24,"value":71},"mindspore.scipy在CPU后端不仅能借助Minspore底层高效的CPU算子而且针对GPU平台也自实现了一系列的GPU底层高效算子。在GPU场景下能够提升硬件算力利用率，大幅提升科学计算的速度。随着后续的更新迭代，Ascend平台上也会使能高性能的mindspore.scipy模块。",{"type":18,"tag":30,"props":73,"children":74},{},[75],{"type":24,"value":76},"除支持在不同硬件平台加速计算外，mindspore.scipy的接口定义与原生的SciPy保持高度一致，而当前业界已知的TensorFlow和PyTorch等框架中对于科学计算的接口支持与原生SciPy的接口对比存在差异。对于当前MindSpore已经支持的接口，原生的SciPy实现可以直接替换成mindspore.scipy的实现（后面的例子可以看到）。接口定义的高度一致最大程度地降低用户的迁移成本，提高minspore.scipy的易用性。",{"type":18,"tag":26,"props":78,"children":80},{"id":79},"_2安装mindsporescipy",[81],{"type":24,"value":82},"2.安装mindspore.scipy",{"type":18,"tag":30,"props":84,"children":85},{},[86],{"type":24,"value":87},"MindSpore的SciPy模块是作为科学计算的组件嵌入在MindSpore中的。当用户成功安装MindSpore框架后，minspore.scipy也会随之安装。MindSpore的安装命令由下图所示。",{"type":18,"tag":30,"props":89,"children":90},{},[91],{"type":18,"tag":92,"props":93,"children":96},"img",{"alt":94,"src":95},"图1.png","https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20222/16/1644995260845721338.png",[],{"type":18,"tag":30,"props":98,"children":99},{},[100],{"type":24,"value":101},"推荐使用采用pip的方式完成MindSpore的安装。值得注意的是**，**mindspore.scipy目前仅支持1.6.0及以上的版本，并且支持的平台仅包含CPU(Linux-x86_64)以及GPU(Linux-x86_64)，Windows以及Ascend平台目前尚未支持。mindspore.scipy还在不断的发展，在后续的更新迭代中会逐渐更丰富、更高效、更便捷的用户接口以及更多计算平台。",{"type":18,"tag":30,"props":103,"children":104},{},[105],{"type":24,"value":106},"Linux系统下CPU的pip安装命令为:",{"type":18,"tag":108,"props":109,"children":111},"pre",{"code":110},"pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.6.0/MindSpore/cpu/x86_64/mindspore-1.6.0-cp37-cp37m-linux_x86_64.whl  --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple\n",[112],{"type":18,"tag":113,"props":114,"children":115},"code",{"__ignoreMap":7},[116],{"type":24,"value":110},{"type":18,"tag":30,"props":118,"children":119},{},[120],{"type":24,"value":121},"Linux 系统下GPU的pip安装命令为:",{"type":18,"tag":108,"props":123,"children":124},{"code":110},[125],{"type":18,"tag":113,"props":126,"children":127},{"__ignoreMap":7},[128],{"type":24,"value":110},{"type":18,"tag":30,"props":130,"children":131},{},[132],{"type":24,"value":133},"安装成功后显示的界面如下：",{"type":18,"tag":30,"props":135,"children":136},{},[137],{"type":18,"tag":92,"props":138,"children":141},{"alt":139,"src":140},"图2.png","https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20222/16/1644995376855662187.png",[],{"type":18,"tag":108,"props":143,"children":145},{"code":144},"import mindspore  \nimport mindspore.scipy  \nprint(mindspore.__version__)\n",[146],{"type":18,"tag":113,"props":147,"children":148},{"__ignoreMap":7},[149],{"type":24,"value":144},{"type":18,"tag":30,"props":151,"children":152},{},[153],{"type":24,"value":154},"如果输出1.6.0并且没有错误提示的话，说明mindspore.scipy正确安装了。",{"type":18,"tag":30,"props":156,"children":157},{},[158],{"type":18,"tag":92,"props":159,"children":162},{"alt":160,"src":161},"图3.png","https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20222/16/1644995483444799578.png",[],{"type":18,"tag":30,"props":164,"children":165},{},[166,168,176],{"type":24,"value":167},"除了上述的pip命令安装，还可以用别的方式（例如源码）进行安装，详细的执行步骤可以参考",{"type":18,"tag":169,"props":170,"children":174},"a",{"href":171,"rel":172},"https://www.mindspore.cn/install",[173],"nofollow",[175],{"type":24,"value":171},{"type":24,"value":177},"。",{"type":18,"tag":26,"props":179,"children":181},{"id":180},"_3如何使用mindsporescipy",[182],{"type":24,"value":183},"3.如何使用mindspore.scipy",{"type":18,"tag":30,"props":185,"children":186},{},[187],{"type":24,"value":188},"从1,2小节可知，mindspore.scipy的接口是与原生SciPy高度对齐的，以下是一个简单的例子来展示如何极小程度修改原生SciPy的脚本，使能GPU计算。",{"type":18,"tag":30,"props":190,"children":191},{},[192],{"type":24,"value":193},"以下代码是使用原生NumPy和SciPy计算一个正定对称的实数矩阵进行cholesky分解和求解线性方程的过程：",{"type":18,"tag":108,"props":195,"children":197},{"code":196},"import numpy as np  \nfrom scipy.linalg import cho_factor as cho_factor  \nfrom scipy.linalg import cho_solve as cho_solve  \na = np.array([[9., 3., 1., 5.], [3., 7., 5., 1.], [1., 5., 9., 2.], [5., 1., 2., 6.]])  \nb = np.array([1., 1., 1., 1.])  \nc, low = cho_factor(A)  # 计算 A = L L*  \nx = cho_solve((c, low), b)  # 计算 A x = b  \nprint(np.dot(A, x) - b)  # 输出 A x - b 是否为0\n",[198],{"type":18,"tag":113,"props":199,"children":200},{"__ignoreMap":7},[201],{"type":24,"value":196},{"type":18,"tag":30,"props":203,"children":204},{},[205],{"type":24,"value":206},"运行上述脚本输出为：",{"type":18,"tag":108,"props":208,"children":210},{"code":209},"[0.00000000e+00 2.22044605e-16 0.00000000e+00 0.00000000e+00] \n",[211],{"type":18,"tag":113,"props":212,"children":213},{"__ignoreMap":7},[214],{"type":24,"value":209},{"type":18,"tag":30,"props":216,"children":217},{},[218],{"type":24,"value":219},"上述脚本中的分解和求解方程只能通过CPU进行计算，因为原生NumPy和SciPy只能在CPU上运行。安装了GPU下的MindSpore之后，将import的包改为mindspore.scipy，便可在GPU下对矩阵进行运算。修改后的脚本为：",{"type":18,"tag":108,"props":221,"children":223},{"code":222},"import mindspore.numpy as np  # 修改为mindspore.numpy  \nfrom mindspore.scipy.linalg import cho_factor as cho_factor  # 修改为mindspore.scipy  \nfrom mindspore.scipy.linalg import cho_solve as cho_solve  # 修改为mindspore.scipy  \na = np.array([[9., 3., 1., 5.], [3., 7., 5., 1.], [1., 5., 9., 2.], [5., 1., 2., 6.]])  \nb = np.array([1., 1., 1., 1.])  \nc, low = cho_factor(A)  \nx = cho_solve((c, low), b)  \nprint(np.dot(A, x) - b)\n",[224],{"type":18,"tag":113,"props":225,"children":226},{"__ignoreMap":7},[227],{"type":24,"value":222},{"type":18,"tag":30,"props":229,"children":230},{},[231],{"type":24,"value":232},"运行上述脚本，输出结果为：",{"type":18,"tag":108,"props":234,"children":236},{"code":235},"[-5.9604645e-08  0.0000000e+00  0.0000000e+00 -5.9604645e-08] \n",[237],{"type":18,"tag":113,"props":238,"children":239},{"__ignoreMap":7},[240],{"type":24,"value":235},{"type":18,"tag":30,"props":242,"children":243},{},[244],{"type":24,"value":245},"值得注意的是，mindspore.numpy.array默认构造的浮点类型是float32，如果需要更高的精度，用户可以将对应输入数据类型指定为float64，即",{"type":18,"tag":108,"props":247,"children":249},{"code":248},"a = np.array([[9., 3., 1., 5.], [3., 7., 5., 1.], [1., 5., 9., 2.], [5., 1., 2., 6.]], dtype=np.float64)\n",[250],{"type":18,"tag":113,"props":251,"children":252},{"__ignoreMap":7},[253],{"type":24,"value":248},{"type":18,"tag":30,"props":255,"children":256},{},[257],{"type":24,"value":258},"若用户使用mindspore.scipy时存在编译时间过长的问题，可以尝试使用动态图模式运行MindSpore。mindspore.scipy的接口支持动静态图两种模式运行。默认情况下，MindSpore的运行是图模式（GRAPH_MODE），用户进需要输入代码，即可切换为动态图模式（PYNATIVE_MODE）：",{"type":18,"tag":108,"props":260,"children":262},{"code":261},"from mindspore import context  \ncontext.set_context(mode=context.PYNATIVE_MODE)\n",[263],{"type":18,"tag":113,"props":264,"children":265},{"__ignoreMap":7},[266],{"type":24,"value":261},{"type":18,"tag":30,"props":268,"children":269},{},[270],{"type":24,"value":271},"关于cholesky分解，mindspore.scipy.linalg中有mindspore.scipy.linalg.cho_factor、mindspore.scipy.linalg.cholesky两个对外接口，对应的api描述可在",{"type":18,"tag":30,"props":273,"children":274},{},[275,281],{"type":18,"tag":169,"props":276,"children":279},{"href":277,"rel":278},"https://www.mindspore.cn/docs/zh-CN/master/api_python/scipy/mindspore.scipy.linalg.cho_factor.html#mindspore.scipy.linalg.cho_factor",[173],[280],{"type":24,"value":277},{"type":24,"value":282}," 中查看。",{"type":18,"tag":30,"props":284,"children":285},{},[286],{"type":18,"tag":92,"props":287,"children":290},{"alt":288,"src":289},"图4.png","https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20222/16/1644995804084674089.png",[],{"type":18,"tag":26,"props":292,"children":294},{"id":293},"_4更多的mindsporescipy接口",[295],{"type":24,"value":296},"4.更多的mindspore.scipy接口",{"type":18,"tag":30,"props":298,"children":299},{},[300],{"type":24,"value":301},"更多具体的接口开放信息，可以查看对应的官方文档链接。",{"type":18,"tag":30,"props":303,"children":304},{},[305],{"type":18,"tag":92,"props":306,"children":309},{"alt":307,"src":308},"图5.png","https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20222/16/1644995880610786067.png",[],{"type":18,"tag":30,"props":311,"children":312},{},[313,315,321],{"type":24,"value":314},"目前mindspore.scipy模块初步完成了scipy.linalg(线性代数)、scipy.sparse.linalg(稀疏线性代数)、scipy.optimize(数值优化)这几个模块中的部分基础接口的开发。接口和文档都在持续完善中，最新、最全面的接口信息均可在",{"type":18,"tag":169,"props":316,"children":319},{"href":317,"rel":318},"https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore.scipy.html",[173],[320],{"type":24,"value":317},{"type":24,"value":322},"中查看。其中包含了mindspore.scipy模块支持的所有接口，以及每个接口对应的输入输出，简单用例代码等。",{"type":18,"tag":26,"props":324,"children":326},{"id":325},"qa",[327],{"type":24,"value":328},"QA",{"type":18,"tag":30,"props":330,"children":331},{},[332],{"type":24,"value":333},"Q1：我安装了MindSpore 1.5的cpu 版本，为什么无法使用SciPy？",{"type":18,"tag":30,"props":335,"children":336},{},[337],{"type":24,"value":338},"A1：mindspore.scipy 是r1.6以后开源到MindSpore的，用户可以安装更新的版本(r1.6)或者master上的MindSpore源码进行安装体验。",{"type":18,"tag":30,"props":340,"children":341},{},[342],{"type":24,"value":343},"Q2: 我安装了MindSpore 1.6的cpu windows版本，为什么无法使用SciPy？",{"type":18,"tag":30,"props":345,"children":346},{},[347],{"type":24,"value":348},"A2：mindspore.scipy 当前还未支持windows操作系统，后续会加紧迭代加入，可以使用linux-x86-64下的cpu或者gpu版本安装体验。",{"type":18,"tag":30,"props":350,"children":351},{},[352],{"type":24,"value":353},"Q3：我想用mindspore.scipy模块下的xxx API，但是在官网的并未查询到相应的支持，怎么办？",{"type":18,"tag":30,"props":355,"children":356},{},[357],{"type":24,"value":358},"A3：如果在官网的最新介绍中没有查询到xxx API,可以再MindSpore的社区提issue给社区维护人员，我们会根据计划尽快开发xxx API。",{"type":18,"tag":30,"props":360,"children":361},{},[362,364],{"type":24,"value":363},"MindSpore码云：",{"type":18,"tag":169,"props":365,"children":368},{"href":366,"rel":367},"https://gitee.com/mindspore/mindspore",[173],[369],{"type":24,"value":366},{"type":18,"tag":30,"props":371,"children":372},{},[373,375],{"type":24,"value":374},"MindSpore官网：",{"type":18,"tag":169,"props":376,"children":379},{"href":377,"rel":378},"https://www.mindspore.cn",[173],[380],{"type":24,"value":377},{"title":7,"searchDepth":382,"depth":382,"links":383},4,[384,386,387,388,389,390],{"id":7,"depth":385,"text":7},3,{"id":53,"depth":385,"text":56},{"id":79,"depth":385,"text":82},{"id":180,"depth":385,"text":183},{"id":293,"depth":385,"text":296},{"id":325,"depth":385,"text":328},"markdown","content:technology-blogs:zh:1047.md","content","technology-blogs/zh/1047.md","technology-blogs/zh/1047","md",1776506111600]