diff --git a/docs/programming_guide/source_zh_cn/numpy.ipynb b/docs/programming_guide/source_zh_cn/numpy.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..0f7cfde09a48da3df3feffa52d930e8ae80bc21b --- /dev/null +++ b/docs/programming_guide/source_zh_cn/numpy.ipynb @@ -0,0 +1,890 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "eea28f19", + "metadata": {}, + "source": [ + "# MindSpore NumPy函数使用介绍" + ] + }, + { + "cell_type": "markdown", + "id": "824ab21f", + "metadata": {}, + "source": [ + "## 概述\n", + "MindSpore NumPy工具包提供了一系列类NumPy接口。用户可以使用类NumPy语法在MindSpore上进行模型的搭建。\n", + "\n", + "本教程的整体流程如下:\n", + "1. 介绍算子MindSpore Numpy,包含张量生成、张量操作、逻辑运算和数学运算。\n", + "2. MindSpore Numpy与MindSpore特性结合示例。" + ] + }, + { + "cell_type": "markdown", + "id": "3b358e12", + "metadata": {}, + "source": [ + "> 本样例适用于CPU、GPU和Ascend环境。" + ] + }, + { + "cell_type": "markdown", + "id": "a289ea64", + "metadata": {}, + "source": [ + "## 算子介绍\n", + "\n", + "MindSpore Numpy具有四大功能模块:张量生成、张量操作、逻辑运算和其他常用数学运算。算子的具体相关信息可以参考[NumPy接口列表](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.numpy.html)。" + ] + }, + { + "cell_type": "markdown", + "id": "4478f755", + "metadata": {}, + "source": [ + "### 张量生成\n", + "\n", + "生成类算子用来生成和构建具有指定数值、类型和形状的数组(Tensor)。\n", + "\n", + "构建数组代码示例:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "95de5443", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "input_x = [1. 2. 3.]\n", + "type of input_x = Tensor[Float32]\n" + ] + } + ], + "source": [ + "import mindspore.numpy as np\n", + "import mindspore.ops as ops\n", + "input_x = np.array([1, 2, 3], np.float32)\n", + "print(\"input_x =\", input_x)\n", + "print(\"type of input_x =\", ops.typeof(input_x))" + ] + }, + { + "cell_type": "markdown", + "id": "9114d9ef", + "metadata": {}, + "source": [ + "除了使用上述方法来创建外,也可以通过以下几种方式创建。\n", + "\n", + "#### 生成具有相同元素的数组\n", + "\n", + "生成具有相同元素的数组代码示例:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6ecc1902", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[6. 6. 6.]\n", + " [6. 6. 6.]]\n" + ] + } + ], + "source": [ + "input_x = np.full((2, 3), 6, np.float32)\n", + "print(input_x)" + ] + }, + { + "cell_type": "markdown", + "id": "bc231027", + "metadata": {}, + "source": [ + "生成指定形状的全1数组,示例:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "f1c53a7c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1. 1. 1.]\n", + " [1. 1. 1.]]\n" + ] + } + ], + "source": [ + "input_x = np.ones((2, 3), np.float32)\n", + "print(input_x)" + ] + }, + { + "cell_type": "markdown", + "id": "9fb6caba", + "metadata": {}, + "source": [ + "#### 生成具有某个范围内的数值的数组\n", + "生成指定范围内的等差数组代码示例:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "89c6dbba", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 1 2 3 4]\n" + ] + } + ], + "source": [ + "input_x = np.arange(0, 5, 1)\n", + "print(input_x)" + ] + }, + { + "cell_type": "markdown", + "id": "3e1c3186", + "metadata": {}, + "source": [ + "#### 生成特殊类型的数组\n", + "\n", + "生成给定对角线处下方元素为1,上方元素为0的矩阵,示例:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "0166d07b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1. 1. 0.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]]\n" + ] + } + ], + "source": [ + "input_x = np.tri(3, 3, 1)\n", + "print(input_x)" + ] + }, + { + "cell_type": "markdown", + "id": "2802f734", + "metadata": {}, + "source": [ + "生成对角线为1,其他元素为0的二维矩阵,示例:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "36989e82", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1. 0.]\n", + " [0. 1.]]\n" + ] + } + ], + "source": [ + "input_x = np.eye(2, 2)\n", + "print(input_x)" + ] + }, + { + "cell_type": "markdown", + "id": "df5d4e2c", + "metadata": {}, + "source": [ + "### 张量操作\n", + "\n", + "变换类算子主要进行数组的维度变换,分割和拼接等。\n", + "\n", + "#### 数组维度变换\n", + "\n", + "矩阵转置,代码示例:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "e173f2dd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 2 4 6 8]\n", + " [1 3 5 7 9]]\n" + ] + } + ], + "source": [ + "input_x = np.arange(10).reshape(5, 2)\n", + "output = np.transpose(input_x)\n", + "print(output)" + ] + }, + { + "cell_type": "markdown", + "id": "6fcf6de5", + "metadata": {}, + "source": [ + "交换指定轴,代码示例:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "1be654ec", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 1, 3)\n" + ] + } + ], + "source": [ + "input_x = np.ones((1, 2, 3))\n", + "output = np.swapaxes(input_x, 0, 1)\n", + "print(output.shape)" + ] + }, + { + "cell_type": "markdown", + "id": "a359c76e", + "metadata": {}, + "source": [ + "#### 数组分割\n", + "\n", + "将输入数组平均切分为多个数组,代码示例:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "4056b915", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(Tensor(shape=[3], dtype=Int32, value= [0, 1, 2]), Tensor(shape=[3], dtype=Int32, value= [3, 4, 5]), Tensor(shape=[3], dtype=Int32, value= [6, 7, 8]))\n" + ] + } + ], + "source": [ + "input_x = np.arange(9)\n", + "output = np.split(input_x, 3)\n", + "print(output)" + ] + }, + { + "cell_type": "markdown", + "id": "4da24806", + "metadata": {}, + "source": [ + "#### 数组拼接\n", + "\n", + "将两个数组按照指定轴进行拼接,代码示例:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "803c2c7b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 0 1 2 3 4 10 11 12 13 14]\n" + ] + } + ], + "source": [ + "input_x = np.arange(0, 5)\n", + "input_y = np.arange(10, 15)\n", + "output = np.concatenate((input_x, input_y), axis=0)\n", + "print(output)" + ] + }, + { + "cell_type": "markdown", + "id": "523fee35", + "metadata": {}, + "source": [ + "### 逻辑运算\n", + "\n", + "逻辑计算类算子主要进行逻辑运算。\n", + "\n", + "相等(equal)和小于(less)计算代码示例如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "c5054370", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "output of equal: [ True False False False False]\n", + "output of less: [False True True True True]\n" + ] + } + ], + "source": [ + "input_x = np.arange(0, 5)\n", + "input_y = np.arange(0, 10, 2)\n", + "output = np.equal(input_x, input_y)\n", + "print(\"output of equal:\", output)\n", + "output = np.less(input_x, input_y)\n", + "print(\"output of less:\", output)" + ] + }, + { + "cell_type": "markdown", + "id": "730a27fb", + "metadata": {}, + "source": [ + "### 数学运算\n", + "\n", + "数学计算类算子主要进行各类数学计算:\n", + "加减乘除乘方,以及指数、对数等常见函数等。\n", + "\n", + "数学计算支持类似NumPy的广播特性。\n", + "\n", + "#### 加法\n", + "\n", + "以下代码实现了`input_x`和`input_y`两数组相加的操作:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "9433aeab", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[4 6]\n", + " [4 6]\n", + " [4 6]]\n" + ] + } + ], + "source": [ + "input_x = np.full((3, 2), [1, 2])\n", + "input_y = np.full((3, 2), [3, 4])\n", + "output = np.add(input_x, input_y)\n", + "print(output)" + ] + }, + { + "cell_type": "markdown", + "id": "048a1879", + "metadata": {}, + "source": [ + "#### 矩阵乘法\n", + "\n", + "以下代码实现了`input_x`和`input_y`两矩阵相乘的操作:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "3c106380", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[20. 23. 26. 29.]\n", + " [56. 68. 80. 92.]]\n" + ] + } + ], + "source": [ + "input_x = np.arange(2*3).reshape(2, 3).astype('float32')\n", + "input_y = np.arange(3*4).reshape(3, 4).astype('float32')\n", + "output = np.matmul(input_x, input_y)\n", + "print(output)" + ] + }, + { + "cell_type": "markdown", + "id": "64d1b8f5", + "metadata": {}, + "source": [ + "#### 求平均值\n", + "\n", + "以下代码实现了求`input_x`所有元素的平均值的操作:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "194bccf7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.5\n" + ] + } + ], + "source": [ + "input_x = np.arange(6).astype('float32')\n", + "output = np.mean(input_x)\n", + "print(output)" + ] + }, + { + "cell_type": "markdown", + "id": "761f1e31", + "metadata": {}, + "source": [ + "#### 指数\n", + "\n", + "以下代码实现了自然常数`e`的`input_x`次方的操作:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "76492de6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 1. 2.7182817 7.389056 20.085537 54.59815 ]\n" + ] + } + ], + "source": [ + "input_x = np.arange(5).astype('float32')\n", + "output = np.exp(input_x)\n", + "print(output)" + ] + }, + { + "cell_type": "markdown", + "id": "c9933806", + "metadata": {}, + "source": [ + "## MindSpore Numpy与MindSpore特性结合\n", + "\n", + "`mindspore.numpy`能够充分利用MindSpore的强大功能,实现算子的自动微分,并使用图模式加速运算,帮助用户快速构建高效的模型。同时,MindSpore还支持多种后端设备,包括`Ascend`、`GPU`和`CPU`等,用户可以根据自己的需求灵活设置。以下提供了几种常用方法:\n", + "\n", + "- `ms_function`: 将代码包裹进图模式,用于提高代码运行效率。\n", + "- `GradOperation`: 用于自动求导。\n", + "- `mindspore.context`: 用于设置运行模式和后端设备等。\n", + "- `mindspore.nn.Cell`: 用于建立深度学习模型。\n", + "\n", + "### ms_function使用示例\n", + "\n", + "首先,以神经网络里经常使用到的矩阵乘与矩阵加算子为例:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "9af65a61", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x1: [[0 1 2 3]\n", + " [4 5 6 7]]\n", + "x2: [[0 1 2 3]\n", + " [4 5 6 7]]\n" + ] + } + ], + "source": [ + "import mindspore.numpy as np1\n", + "import numpy as np2\n", + "\n", + "x1 = np1.arange(8).reshape(2, 4)\n", + "x2 = np2.arange(8).reshape(2, 4)\n", + "print(\"x1:\", x1)\n", + "print(\"x2:\", x2)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "e5e014b6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 768. 768. 768. 768.]\n", + " [2816. 2816. 2816. 2816.]]\n" + ] + } + ], + "source": [ + "x = np.arange(8).reshape(2, 4).astype('float32')\n", + "w1 = np.ones((4, 8))\n", + "b1 = np.zeros((8,))\n", + "w2 = np.ones((8, 16))\n", + "b2 = np.zeros((16,))\n", + "w3 = np.ones((16, 4))\n", + "b3 = np.zeros((4,))\n", + "\n", + "def forward(x, w1, b1, w2, b2, w3, b3):\n", + " x = np.dot(x, w1) + b1\n", + " x = np.dot(x, w2) + b2\n", + " x = np.dot(x, w3) + b3\n", + " return x\n", + "\n", + "print(forward(x, w1, b1, w2, b2, w3, b3))" + ] + }, + { + "cell_type": "markdown", + "id": "4e6d7ea5", + "metadata": {}, + "source": [ + "对上述示例,我们可以借助`ms_function`将所有算子编译到一张静态图里以加快运行效率,示例如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "a7470cef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 768. 768. 768. 768.]\n", + " [2816. 2816. 2816. 2816.]]\n" + ] + } + ], + "source": [ + "from mindspore import ms_function\n", + "\n", + "forward_compiled = ms_function(forward)\n", + "print(forward(x, w1, b1, w2, b2, w3, b3))" + ] + }, + { + "cell_type": "markdown", + "id": "03004f9d", + "metadata": {}, + "source": [ + "> 目前静态图不支持在命令行模式中运行,并且有部分语法限制。`ms_function`的更多信息可参考[API: ms_function](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.html#mindspore.ms_function)。" + ] + }, + { + "cell_type": "markdown", + "id": "c60d2dcb", + "metadata": {}, + "source": [ + "### GradOperation使用示例\n", + "\n", + "`GradOperation` 可以实现自动求导。以下示例可以实现对上述没有用`ms_function`修饰的`forward`函数定义的计算求导。" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "980f426b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(Tensor(shape=[2, 4], dtype=Float32, value=\n", + " [[ 5.12000000e+02, 5.12000000e+02, 5.12000000e+02, 5.12000000e+02],\n", + " [ 5.12000000e+02, 5.12000000e+02, 5.12000000e+02, 5.12000000e+02]]),\n", + " Tensor(shape=[4, 8], dtype=Float32, value=\n", + " [[ 2.56000000e+02, 2.56000000e+02, 2.56000000e+02 ... 2.56000000e+02, 2.56000000e+02, 2.56000000e+02],\n", + " [ 3.84000000e+02, 3.84000000e+02, 3.84000000e+02 ... 3.84000000e+02, 3.84000000e+02, 3.84000000e+02],\n", + " [ 5.12000000e+02, 5.12000000e+02, 5.12000000e+02 ... 5.12000000e+02, 5.12000000e+02, 5.12000000e+02]\n", + " [ 6.40000000e+02, 6.40000000e+02, 6.40000000e+02 ... 6.40000000e+02, 6.40000000e+02, 6.40000000e+02]]),\n", + " Tensor(shape=[8], dtype=Float32, value= [ 1.28000000e+02, 1.28000000e+02, 1.28000000e+02, 1.28000000e+02, 1.28000000e+02, 1.28000000e+02, 1.28000000e+02, 1.28000000e+02]),\n", + " Tensor(shape=[8, 16], dtype=Float32, value=\n", + " [[ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02],\n", + " [ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02],\n", + " [ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02],\n", + " ...\n", + " [ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02],\n", + " [ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02],\n", + " [ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02]]),\n", + " Tensor(shape=[16], dtype=Float32, value= [ 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, \n", + " 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00]),\n", + " Tensor(shape=[16, 4], dtype=Float32, value=\n", + " [[ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02],\n", + " [ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02],\n", + " [ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02],\n", + " ...\n", + " [ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02],\n", + " [ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02],\n", + " [ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02]]),\n", + " Tensor(shape=[4], dtype=Float32, value= [ 2.00000000e+00, 2.00000000e+00, 2.00000000e+00, 2.00000000e+00]))" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grad_all = ops.composite.GradOperation(get_all=True)\n", + "grad_all(forward)(x, w1, b1, w2, b2, w3, b3)" + ] + }, + { + "cell_type": "markdown", + "id": "4fbd087e", + "metadata": {}, + "source": [ + "如果要对`ms_function`修饰的`forward`计算求导,需要提前使用`context`设置运算模式为图模式,示例如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "d5225159", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(Tensor(shape=[2, 4], dtype=Float32, value=\n", + " [[ 5.12000000e+02, 5.12000000e+02, 5.12000000e+02, 5.12000000e+02],\n", + " [ 5.12000000e+02, 5.12000000e+02, 5.12000000e+02, 5.12000000e+02]]),\n", + " Tensor(shape=[4, 8], dtype=Float32, value=\n", + " [[ 2.56000000e+02, 2.56000000e+02, 2.56000000e+02 ... 2.56000000e+02, 2.56000000e+02, 2.56000000e+02],\n", + " [ 3.84000000e+02, 3.84000000e+02, 3.84000000e+02 ... 3.84000000e+02, 3.84000000e+02, 3.84000000e+02],\n", + " [ 5.12000000e+02, 5.12000000e+02, 5.12000000e+02 ... 5.12000000e+02, 5.12000000e+02, 5.12000000e+02]\n", + " [ 6.40000000e+02, 6.40000000e+02, 6.40000000e+02 ... 6.40000000e+02, 6.40000000e+02, 6.40000000e+02]]),\n", + " Tensor(shape=[8], dtype=Float32, value= [ 1.28000000e+02, 1.28000000e+02, 1.28000000e+02, 1.28000000e+02, 1.28000000e+02, 1.28000000e+02, 1.28000000e+02, 1.28000000e+02]),\n", + " Tensor(shape=[8, 16], dtype=Float32, value=\n", + " [[ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02],\n", + " [ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02],\n", + " [ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02],\n", + " ...\n", + " [ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02],\n", + " [ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02],\n", + " [ 1.12000000e+02, 1.12000000e+02, 1.12000000e+02 ... 1.12000000e+02, 1.12000000e+02, 1.12000000e+02]]),\n", + " Tensor(shape=[16], dtype=Float32, value= [ 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, \n", + " 8.00000000e+00, 8.00000000e+00, 8.00000000e+00, 8.00000000e+00]),\n", + " Tensor(shape=[16, 4], dtype=Float32, value=\n", + " [[ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02],\n", + " [ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02],\n", + " [ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02],\n", + " ...\n", + " [ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02],\n", + " [ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02],\n", + " [ 2.24000000e+02, 2.24000000e+02, 2.24000000e+02, 2.24000000e+02]]),\n", + " Tensor(shape=[4], dtype=Float32, value= [ 2.00000000e+00, 2.00000000e+00, 2.00000000e+00, 2.00000000e+00]))" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from mindspore import ms_function, context\n", + "\n", + "context.set_context(mode=context.GRAPH_MODE)\n", + "grad_all = ops.composite.GradOperation(get_all=True)\n", + "grad_all(ms_function(forward))(x, w1, b1, w2, b2, w3, b3)" + ] + }, + { + "cell_type": "markdown", + "id": "5840307b", + "metadata": {}, + "source": [ + "更多细节可参考[API: GradOperation](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/ops/mindspore.ops.GradOperation.html)。" + ] + }, + { + "cell_type": "markdown", + "id": "13d15ab7", + "metadata": {}, + "source": [ + "### mindspore.context使用示例\n", + "\n", + "MindSpore支持多后端运算,可以通过`mindspore.context`进行设置。`mindspore.numpy` 的多数算子可以使用图模式或者PyNative模式运行,也可以运行在CPU,CPU或者Ascend等多种后端设备上。" + ] + }, + { + "cell_type": "markdown", + "id": "f55e6bd1", + "metadata": {}, + "source": [ + "```python\n", + "from mindspore import context\n", + "\n", + "# Execucation in static graph mode\n", + "context.set_context(mode=context.GRAPH_MODE)\n", + "\n", + "# Execucation in PyNative mode\n", + "context.set_context(mode=context.PYNATIVE_MODE)\n", + "\n", + "# Execucation on CPU backend\n", + "context.set_context(device_target=\"CPU\")\n", + "\n", + "# Execucation on GPU backend\n", + "context.set_context(device_target=\"GPU\")\n", + "\n", + "# Execucation on Ascend backend\n", + "context.set_context(device_target=\"Ascend\")\n", + "...\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "5888ac03", + "metadata": {}, + "source": [ + "更多细节可参考[API: mindspore.context](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.context.html)。" + ] + }, + { + "cell_type": "markdown", + "id": "8fb8c89b", + "metadata": {}, + "source": [ + "### mindspore.numpy使用示例\n", + "\n", + "这里提供一个使用`mindspore.numpy`构建网络模型的示例。\n", + "\n", + "`mindspore.numpy` 接口可以定义在`nn.Cell`代码块内进行网络的构建,示例如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "5e72c16a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 768. 768. 768. 768.]\n", + " [2816. 2816. 2816. 2816.]]\n" + ] + } + ], + "source": [ + "from mindspore import context\n", + "from mindspore.nn import Cell\n", + "\n", + "context.set_context(mode=context.GRAPH_MODE)\n", + "\n", + "x = np.arange(8).reshape(2, 4).astype('float32')\n", + "w1 = np.ones((4, 8))\n", + "b1 = np.zeros((8,))\n", + "w2 = np.ones((8, 16))\n", + "b2 = np.zeros((16,))\n", + "w3 = np.ones((16, 4))\n", + "b3 = np.zeros((4,))\n", + "\n", + "class NeuralNetwork(Cell):\n", + " def __init__(self):\n", + " super(NeuralNetwork, self).__init__()\n", + "\n", + " def construct(self, x, w1, b1, w2, b2, w3, b3):\n", + " x = np.dot(x, w1) + b1\n", + " x = np.dot(x, w2) + b2\n", + " x = np.dot(x, w3) + b3\n", + " return x\n", + "\n", + "net = NeuralNetwork()\n", + "\n", + "print(net(x, w1, b1, w2, b2, w3, b3))\n" + ] + }, + { + "cell_type": "markdown", + "id": "0a96669f", + "metadata": {}, + "source": [ + "更多构建网络的细节可以参考[MindSpore训练指导](https://www.mindspore.cn/tutorial/training/zh-CN/master/index.html)。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "MindSpore-1.1.1", + "language": "python", + "name": "mindspore-1.1.1" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/programming_guide/source_zh_cn/numpy.md b/docs/programming_guide/source_zh_cn/numpy.md deleted file mode 100644 index d441413f6c3a72766bbf12a7ff3af4d1d09fcb6b..0000000000000000000000000000000000000000 --- a/docs/programming_guide/source_zh_cn/numpy.md +++ /dev/null @@ -1,464 +0,0 @@ -# MindSpore NumPy函数 - - - -- [MindSpore NumPy函数使用](#mindspore-numpy函数使用) - - [概述](#概述) - - [算子介绍](#算子介绍) - - [张量生成](#张量生成) - - [生成具有相同元素的数组](#生成具有相同元素的数组) - - [生成具有某个范围内的数值的数组](#生成具有某个范围内的数值的数组) - - [生成特殊类型的数组](#生成特殊类型的数组) - - [张量操作](#张量操作) - - [数组维度变换](#数组维度变换) - - [数组分割](#数组分割) - - [数组拼接](#数组拼接) - - [逻辑运算](#逻辑运算) - - [数学运算](#数学运算) - - [加法](#加法) - - [矩阵乘法](#矩阵乘法) - - [求平均值](#求平均值) - - [指数](#指数) - - [MindSpore Numpy与MindSpore特性结合](#mindspore-numpy与mindspore特性结合) - - [ms_function使用示例](#ms_function使用示例) - - [GradOperation使用示例](#gradoperation使用示例) - - [mindspore.context使用示例](#mindsporecontext使用示例) - - [mindspore.numpy使用示例](#mindsporenumpy使用示例) - - - - - -## 概述 - -MindSpore NumPy工具包提供了一系列类NumPy接口。用户可以使用类NumPy语法在MindSpore上进行模型的搭建。 - -## 算子介绍 - -MindSpore Numpy具有四大功能模块:张量生成、张量操作、逻辑运算和其他常用数学运算。算子的具体相关信息可以参考[NumPy接口列表](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.numpy.html)。 - -### 张量生成 - -生成类算子用来生成和构建具有指定数值、类型和形状的数组(Tensor)。 - -构建数组代码示例: - -```python -import mindspore.numpy as np -import mindspore.ops as ops -input_x = np.array([1, 2, 3], np.float32) -print("input_x =", input_x) -print("type of input_x =", ops.typeof(input_x)) -``` - -输出如下: - -```python -input_x = [1. 2. 3.] -type of input_x = Tensor[Float32] -``` - -除了使用上述方法来创建外,也可以通过以下几种方式创建。 - -#### 生成具有相同元素的数组 - -生成具有相同元素的数组代码示例: - -```python -import mindspore.numpy as np -input_x = np.full((2, 3), 6, np.float32) -print(input_x) -``` - -输出如下: - -```python -[[6. 6. 6.] - [6. 6. 6.]] -``` - -生成指定形状的全1数组,示例: - -```python -import mindspore.numpy as np -input_x = np.ones((2, 3), np.float32) -print(input_x) -``` - -输出如下: - -```python -[[1. 1. 1.] - [1. 1. 1.]] -``` - -#### 生成具有某个范围内的数值的数组 - -生成指定范围内的等差数组代码示例: - -```python -import mindspore.numpy as np -input_x = np.arange(0, 5, 1) -print(input_x) -``` - -输出如下: - -```python -[0 1 2 3 4] -``` - -#### 生成特殊类型的数组 - -生成给定对角线处下方元素为1,上方元素为0的矩阵,示例: - -```python -import mindspore.numpy as np -input_x = np.tri(3, 3, 1) -print(input_x) -``` - -输出如下: - -```python -[[1. 1. 0.] - [1. 1. 1.] - [1. 1. 1.]] -``` - -生成对角线为1,其他元素为0的二维矩阵,示例: - -```python -import mindspore.numpy as np -input_x = np.eye(2, 2) -print(input_x) -``` - -输出如下: - -```python -[[1. 0.] - [0. 1.]] -``` - -### 张量操作 - -变换类算子主要进行数组的维度变换,分割和拼接等。 - -#### 数组维度变换 - -矩阵转置,代码示例: - -```python -import mindspore.numpy as np -input_x = np.arange(10).reshape(5, 2) -output = np.transpose(input_x) -print(output) -``` - -输出如下: - -```python -[[0 2 4 6 8] - [1 3 5 7 9]] -``` - -交换指定轴,代码示例: - -```python -import mindspore.numpy as np -input_x = np.ones((1, 2, 3)) -output = np.swapaxes(input_x, 0, 1) -print(output.shape) -``` - -输出如下: - -```python -(2, 1, 3) -``` - -#### 数组分割 - -将输入数组平均切分为多个数组,代码示例: - -```python -import mindspore.numpy as np -input_x = np.arange(9) -output = np.split(input_x, 3) -print(output) -``` - -输出如下: - -```python -(Tensor(shape=[3], dtype=Int32, value= [0, 1, 2]), - Tensor(shape=[3], dtype=Int32, value= [3, 4, 5]), - Tensor(shape=[3], dtype=Int32, value= [6, 7, 8])) -``` - -#### 数组拼接 - -将两个数组按照指定轴进行拼接,代码示例: - -```python -import mindspore.numpy as np -input_x = np.arange(0, 5) -input_y = np.arange(10, 15) -output = np.concatenate((input_x, input_y), axis=0) -print(output) -``` - -输出如下: - -```python -[ 0 1 2 3 4 10 11 12 13 14] -``` - -### 逻辑运算 - -逻辑计算类算子主要进行逻辑运算。 - -相等(equal)和小于(less)计算代码示例如下: - -```python -import mindspore.numpy as np -input_x = np.arange(0, 5) -input_y = np.arange(0, 10, 2) -output = np.equal(input_x, input_y) -print("output of equal:", output) -output = np.less(input_x, input_y) -print("output of less:", output) -``` - -输出如下: - -```python -output of equal: [ True False False False False] -output of less: [False True True True True] -``` - -### 数学运算 - -数学计算类算子主要进行各类数学计算: -加减乘除乘方,以及指数、对数等常见函数等。 - -数学计算支持类似NumPy的广播特性。 - -#### 加法 - -以下代码实现了`input_x`和`input_y`两数组相加的操作: - -```python -import mindspore.numpy as np -input_x = np.full((3, 2), [1, 2]) -input_y = np.full((3, 2), [3, 4]) -output = np.add(input_x, input_y) -print(output) -``` - -输出如下: - -```python -[[4 6] - [4 6] - [4 6]] -``` - -#### 矩阵乘法 - -以下代码实现了`input_x`和`input_y`两矩阵相乘的操作: - -```python -import mindspore.numpy as np -input_x = np.arange(2*3).reshape(2, 3).astype('float32') -input_y = np.arange(3*4).reshape(3, 4).astype('float32') -output = np.matmul(input_x, input_y) -print(output) -``` - -输出如下: - -```python -[[20. 23. 26. 29.] - [56. 68. 80. 92.]] -``` - -#### 求平均值 - -以下代码实现了求`input_x`所有元素的平均值的操作: - -```python -import mindspore.numpy as np -input_x = np.arange(6).astype('float32') -output = np.mean(input_x) -print(output) -``` - -输出如下: - -```python -2.5 -``` - -#### 指数 - -以下代码实现了自然常数`e`的`input_x`次方的操作: - -```python -import mindspore.numpy as np -input_x = np.arange(5).astype('float32') -output = np.exp(input_x) -print(output) -``` - -输出如下: - -```python -[ 1. 2.718282 7.3890557 20.085537 54.598145 ] -``` - -## MindSpore Numpy与MindSpore特性结合 - -`mindspore.numpy`能够充分利用MindSpore的强大功能,实现算子的自动微分,并使用图模式加速运算,帮助用户快速构建高效的模型。同时,MindSpore还支持多种后端设备,包括`Ascend`、`GPU`和`CPU`等,用户可以根据自己的需求灵活设置。以下提供了几种常用方法: - -- `ms_function`: 将代码包裹进图模式,用于提高代码运行效率。 -- `GradOperation`: 用于自动求导。 -- `mindspore.context`: 用于设置运行模式和后端设备等。 -- `mindspore.nn.Cell`: 用于建立深度学习模型。 - -### ms_function使用示例 - -首先,以神经网络里经常使用到的矩阵乘与矩阵加算子为例: - -```python -import mindspore.numpy as np - -x = np.arange(8).reshape(2, 4).astype('float32') -w1 = np.ones((4, 8)) -b1 = np.zeros((8,)) -w2 = np.ones((8, 16)) -b2 = np.zeros((16,)) -w3 = np.ones((16, 4)) -b3 = np.zeros((4,)) - -def forward(x, w1, b1, w2, b2, w3, b3): - x = np.dot(x, w1) + b1 - x = np.dot(x, w2) + b2 - x = np.dot(x, w3) + b3 - return x - -print(forward(x, w1, b1, w2, b2, w3, b3)) -``` - -输出如下: - -```python -[[ 768. 768. 768. 768.] - [2816. 2816. 2816. 2816.]] -``` - -对上述示例,我们可以借助`ms_function`将所有算子编译到一张静态图里以加快运行效率,示例如下: - -```python -from mindspore import ms_function - -forward_compiled = ms_function(forward) -``` - -> 目前静态图不支持在命令行模式中运行,并且有部分语法限制。`ms_function`的更多信息可参考[API: ms_function](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.html#mindspore.ms_function)。 - -### GradOperation使用示例 - -`GradOperation` 可以实现自动求导。以下示例可以实现对上述没有用`ms_function`修饰的`forward`函数定义的计算求导。 - -```python -from mindspore import ops - -grad_all = ops.composite.GradOperation(get_all=True) -grad_all(forward)(x, w1, b1, w2, b2, w3, b3) -``` - -如果要对`ms_function`修饰的`forward`计算求导,需要提前使用`context`设置运算模式为图模式,示例如下: - -```python -from mindspore import ops, ms_function, context - -context.set_context(mode=context.GRAPH_MODE) - -grad_all = ops.composite.GradOperation(get_all=True) -grad_all(ms_function(forward))(x, w1, b1, w2, b2, w3, b3) -``` - - 更多细节可参考[API: GradOperation](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/ops/mindspore.ops.GradOperation.html)。 - -### mindspore.context使用示例 - -MindSpore支持多后端运算,可以通过`mindspore.context`进行设置。`mindspore.numpy` 的多数算子可以使用图模式或者PyNative模式运行,也可以运行在CPU,CPU或者Ascend等多种后端设备上。 - -```python -from mindspore import context - -# Execucation in static graph mode -context.set_context(mode=context.GRAPH_MODE) - -# Execucation in PyNative mode -context.set_context(mode=context.PYNATIVE_MODE) - -# Execucation on CPU backend -context.set_context(device_target="CPU") - -# Execucation on GPU backend -context.set_context(device_target="GPU") - -# Execucation on Ascend backend -context.set_context(device_target="Ascend") -... -``` - - 更多细节可参考[API: mindspore.context](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.context.html)。 - -### mindspore.numpy使用示例 - -这里提供一个使用`mindspore.numpy`构建网络模型的示例。 - -`mindspore.numpy` 接口可以定义在`nn.Cell`代码块内进行网络的构建,示例如下: - -```python -import mindspore.numpy as np -from mindspore import context -from mindspore.nn import Cell - -context.set_context(mode=context.GRAPH_MODE) - -x = np.arange(8).reshape(2, 4).astype('float32') -w1 = np.ones((4, 8)) -b1 = np.zeros((8,)) -w2 = np.ones((8, 16)) -b2 = np.zeros((16,)) -w3 = np.ones((16, 4)) -b3 = np.zeros((4,)) - -class NeuralNetwork(Cell): - def __init__(self): - super(NeuralNetwork, self).__init__() - - def construct(self, x, w1, b1, w2, b2, w3, b3): - x = np.dot(x, w1) + b1 - x = np.dot(x, w2) + b2 - x = np.dot(x, w3) + b3 - return x - -net = NeuralNetwork() - -print(net(x, w1, b1, w2, b2, w3, b3)) -``` - -输出如下: - -```python -[[ 768. 768. 768. 768.] - [2816. 2816. 2816. 2816.]] -``` - -更多构建网络的细节可以参考[MindSpore训练指导](https://www.mindspore.cn/tutorial/training/zh-CN/master/index.html)。