From ecc8d07c3849e27286b1e57459cc6446835173df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=9B=B7?= Date: Thu, 15 Oct 2020 15:37:54 +0800 Subject: [PATCH 1/5] add dtype/tensor files,modify md files --- docs/programming_guide/source_zh_cn/dtype.md | 2 + .../source_zh_cn/operator.md | 78 --------- docs/programming_guide/source_zh_cn/tensor.md | 2 + .../notebook/programming_guide/dtype.ipynb | 111 +++++++++++++ .../notebook/programming_guide/tensor.ipynb | 149 ++++++++++++++++++ 5 files changed, 264 insertions(+), 78 deletions(-) create mode 100644 tutorials/notebook/programming_guide/dtype.ipynb create mode 100644 tutorials/notebook/programming_guide/tensor.ipynb diff --git a/docs/programming_guide/source_zh_cn/dtype.md b/docs/programming_guide/source_zh_cn/dtype.md index 5fc7d208e6..1cbdbdee84 100644 --- a/docs/programming_guide/source_zh_cn/dtype.md +++ b/docs/programming_guide/source_zh_cn/dtype.md @@ -9,6 +9,8 @@ +   + ## 概述 diff --git a/docs/programming_guide/source_zh_cn/operator.md b/docs/programming_guide/source_zh_cn/operator.md index 588d3a4314..7afedf1c7c 100644 --- a/docs/programming_guide/source_zh_cn/operator.md +++ b/docs/programming_guide/source_zh_cn/operator.md @@ -16,15 +16,12 @@ - [求三角函数](#求三角函数) - [向量运算](#向量运算) - [Squeeze](#squeeze) - - [求Sparse2Dense](#求sparse2dense) - [矩阵运算](#矩阵运算) - [矩阵乘法](#矩阵乘法) - [广播机制](#广播机制) - [网络操作](#网络操作) - [特征提取](#特征提取) - [激活函数](#激活函数) - - [LossFunction](#lossfunction) - - [优化算法](#优化算法) - [数组操作](#数组操作) - [DType](#dtype) - [Cast](#cast) @@ -187,7 +184,6 @@ scalar 3 import numpy as np import mindspore from mindspore import Tensor -import mindspore.ops.operations as P input_x = mindspore.Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) input_y = 3.0 print(input_x**input_y) @@ -277,29 +273,6 @@ print(output) [1. 1.] [1. 1.]] ``` -#### 求Sparse2Dense - -以下代码实现了对Sparse2Dense示例: -```python -import numpy as np -import mindspore as ms -from mindspore import Tensor -import mindspore.ops.operations as P - -indices = Tensor([[0, 1], [1, 2]]) -values = Tensor([1, 2], dtype=ms.float32) -dense_shape = (3, 4) -out = P.SparseToDense()(indices, values, dense_shape) - -print(out) -``` - -输出如下: -``` -[[0, 1, 0, 0], - [0, 0, 2, 0], - [0, 0, 0, 0]] -``` ### 矩阵运算 @@ -451,57 +424,6 @@ print(res) [0.01165623 0.03168492 0.08612854 0.23412167 0.6364086] ``` -#### LossFunction - - L1Loss - - 以下代码实现了L1 loss function: -```python -from mindspore import Tensor -import mindspore.ops.operations as P -import numpy as np -import mindspore - -loss = P.SmoothL1Loss() -input_data = Tensor(np.array([1, 2, 3]), mindspore.float32) -target_data = Tensor(np.array([1, 2, 2]), mindspore.float32) -res = loss(input_data, target_data) -print(res) -``` - - 输出如下: -``` -[0. 0. 0.5] -``` - -#### 优化算法 - - SGD - - 以下代码实现了SGD梯度下降算法的具体实现,输出是result: -```python -from mindspore import Tensor -import mindspore.ops.operations as P -import numpy as np -import mindspore - -sgd = P.SGD() -parameters = Tensor(np.array([2, -0.5, 1.7, 4]), mindspore.float32) -gradient = Tensor(np.array([1, -1, 0.5, 2]), mindspore.float32) -learning_rate = Tensor(0.01, mindspore.float32) -accum = Tensor(np.array([0.1, 0.3, -0.2, -0.1]), mindspore.float32) -momentum = Tensor(0.1, mindspore.float32) -stat = Tensor(np.array([1.5, -0.3, 0.2, -0.7]), mindspore.float32) -result = sgd(parameters, gradient, learning_rate, accum, momentum, stat) - -print(result) -``` - - 输出如下: -``` -[0. 0. 0. 0.] -``` - ### 数组操作 数组操作指操作对象是一些数组的操作。 diff --git a/docs/programming_guide/source_zh_cn/tensor.md b/docs/programming_guide/source_zh_cn/tensor.md index b7a6196404..1d8b36f592 100644 --- a/docs/programming_guide/source_zh_cn/tensor.md +++ b/docs/programming_guide/source_zh_cn/tensor.md @@ -12,6 +12,8 @@ +   + ## 概述 diff --git a/tutorials/notebook/programming_guide/dtype.ipynb b/tutorials/notebook/programming_guide/dtype.ipynb new file mode 100644 index 0000000000..a7c8bce39c --- /dev/null +++ b/tutorials/notebook/programming_guide/dtype.ipynb @@ -0,0 +1,111 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# dtype" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 概述\n", + "\n", + "MindSpore张量支持不同的数据类型,包含`int8`、`int16`、`int32`、`int64`、`uint8`、`uint16`、`uint32`、`uint64`、`float16`、`float32`、`float64`、`bool_`,与NumPy的数据类型一一对应。\n", + "\n", + "在MindSpore的运算处理流程中,Python中的`int`数会被转换为定义的`int64`类型,`float`数会被转换为定义的`float32`类型。\n", + "\n", + "详细的类型支持情况请参考https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.html#mindspore.dtype。\n", + "\n", + "以下代码,打印MindSpore的数据类型int32。" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Int32\n" + ] + } + ], + "source": [ + "from mindspore import dtype as mstype\n", + "\n", + "data_type = mstype.int32\n", + "print(data_type)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 数据类型转换接口\n", + "\n", + "MindSpore提供了以下几个接口,实现与NumPy数据类型和Python内置的数据类型间的转换。\n", + "\n", + " * `dtype_to_nptype`:将MindSpore的数据类型转换为NumPy对应的数据类型。\n", + "\n", + " * `dtype_to_pytype`:将MindSpore的数据类型转换为Python对应的内置数据类型。\n", + "\n", + " * `pytype_to_dtype`:将Python内置的数据类型转换为MindSpore对应的数据类型。\n", + "\n", + "以下代码实现了不同数据类型间的转换,并打印转换后的类型。" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Int64\n", + "\n" + ] + } + ], + "source": [ + "from mindspore import dtype as mstype\n", + "\n", + "np_type = mstype.dtype_to_nptype(mstype.int32)\n", + "ms_type = mstype.pytype_to_dtype(int)\n", + "py_type = mstype.dtype_to_pytype(mstype.float64)\n", + "\n", + "print(np_type)\n", + "print(ms_type)\n", + "print(py_type)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/tutorials/notebook/programming_guide/tensor.ipynb b/tutorials/notebook/programming_guide/tensor.ipynb new file mode 100644 index 0000000000..baf59d5601 --- /dev/null +++ b/tutorials/notebook/programming_guide/tensor.ipynb @@ -0,0 +1,149 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tensor" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 概述\n", + "\n", + "张量(Tensor)是MindSpore网络运算中的基本数据结构。张量中的数据类型可参考[dtype](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/dtype.html)。\n", + "\n", + "不同维度的张量分别表示不同的数据,0维张量表示标量,1维张量表示向量,2维张量表示矩阵,3维张量可以表示彩色图像的RGB三通道等等。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> 本文中的所有示例,支持在PyNative模式下运行,暂不支持CPU。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 张量构造\n", + "\n", + "构造张量时,支持传入`Tensor`、`float`、`int`、`bool`、`tuple`、`list`和`NumPy.array`类型。\n", + "\n", + "`Tensor`作为初始值时,可指定dtype,如果没有指定dtype,`int`、`float`、`bool`分别对应`int32`、`float32`、`bool_`,`tuple`和`list`生成的1维`Tensor`数据类型与`tuple`和`list`里存放数据的类型相对应。\n", + "\n", + "代码样例如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2]\n", + " [3 4]] \n", + "\n", + " 1 \n", + "\n", + " 2 \n", + "\n", + " True \n", + "\n", + " [1 2 3] \n", + "\n", + " [4. 5. 6.]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "from mindspore import Tensor\n", + "from mindspore.common import dtype as mstype\n", + "\n", + "x = Tensor(np.array([[1, 2], [3, 4]]), mstype.int32)\n", + "y = Tensor(1.0, mstype.int32)\n", + "z = Tensor(2, mstype.int32)\n", + "m = Tensor(True, mstype.bool_)\n", + "n = Tensor((1, 2, 3), mstype.int16)\n", + "p = Tensor([4.0, 5.0, 6.0], mstype.float64)\n", + "\n", + "print(x, \"\\n\\n\", y, \"\\n\\n\", z, \"\\n\\n\", m, \"\\n\\n\", n, \"\\n\\n\", p)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 张量的属性" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 属性\n", + "\n", + "张量的属性包括形状(shape)和数据类型(dtype)。\n", + "\n", + " * 形状:`Tensor`的shape,是一个tuple。\n", + "\n", + " * 数据类型:`Tensor`的dtype,是MindSpore的一个数据类型。\n", + "\n", + "代码样例如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 2) Int32\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "from mindspore import Tensor\n", + "from mindspore.common import dtype as mstype\n", + "\n", + "x = Tensor(np.array([[1, 2], [3, 4]]), mstype.int32)\n", + "x_shape = x.shape\n", + "x_dtype = x.dtype\n", + "\n", + "print(x_shape, x_dtype)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} -- Gitee From 4bb3717e2e9682a95f5460d31344b417702516b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=9B=B7?= Date: Thu, 15 Oct 2020 17:32:27 +0800 Subject: [PATCH 2/5] modify info --- .../source_zh_cn/operator.md | 51 ++++++++++++++++++- .../notebook/programming_guide/dtype.ipynb | 2 +- .../notebook/programming_guide/tensor.ipynb | 2 +- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/docs/programming_guide/source_zh_cn/operator.md b/docs/programming_guide/source_zh_cn/operator.md index 7afedf1c7c..514f1c8b69 100644 --- a/docs/programming_guide/source_zh_cn/operator.md +++ b/docs/programming_guide/source_zh_cn/operator.md @@ -22,6 +22,8 @@ - [网络操作](#网络操作) - [特征提取](#特征提取) - [激活函数](#激活函数) + - [LossFunction](#lossfunction) + - [优化算法](#优化算法) - [数组操作](#数组操作) - [DType](#dtype) - [Cast](#cast) @@ -184,6 +186,7 @@ scalar 3 import numpy as np import mindspore from mindspore import Tensor + input_x = mindspore.Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) input_y = 3.0 print(input_x**input_y) @@ -273,7 +276,6 @@ print(output) [1. 1.] [1. 1.]] ``` - ### 矩阵运算 矩阵运算包括矩阵乘法、矩阵范数、矩阵行列式、矩阵求特征值、矩阵分解等运算。 @@ -424,6 +426,53 @@ print(res) [0.01165623 0.03168492 0.08612854 0.23412167 0.6364086] ``` +#### LossFunction + + 以下代码实现了L1 loss function: +```python +from mindspore import Tensor +import mindspore.ops.operations as P +import numpy as np +import mindspore + +loss = P.SmoothL1Loss() +input_data = Tensor(np.array([1, 2, 3]), mindspore.float32) +target_data = Tensor(np.array([1, 2, 2]), mindspore.float32) +res = loss(input_data, target_data) +print(res) +``` + + 输出如下: +``` +[0. 0. 0.5] +``` + +#### 优化算法 + + 以下代码实现了SGD梯度下降算法的具体实现,输出是result: +```python +from mindspore import Tensor +import mindspore.ops.operations as P +import numpy as np +import mindspore + +sgd = P.SGD() +parameters = Tensor(np.array([2, -0.5, 1.7, 4]), mindspore.float32) +gradient = Tensor(np.array([1, -1, 0.5, 2]), mindspore.float32) +learning_rate = Tensor(0.01, mindspore.float32) +accum = Tensor(np.array([0.1, 0.3, -0.2, -0.1]), mindspore.float32) +momentum = Tensor(0.1, mindspore.float32) +stat = Tensor(np.array([1.5, -0.3, 0.2, -0.7]), mindspore.float32) +result = sgd(parameters, gradient, learning_rate, accum, momentum, stat) + +print(result) +``` + + 输出如下: +``` +[0. 0. 0. 0.] +``` + ### 数组操作 数组操作指操作对象是一些数组的操作。 diff --git a/tutorials/notebook/programming_guide/dtype.ipynb b/tutorials/notebook/programming_guide/dtype.ipynb index a7c8bce39c..3a4d47dcde 100644 --- a/tutorials/notebook/programming_guide/dtype.ipynb +++ b/tutorials/notebook/programming_guide/dtype.ipynb @@ -103,7 +103,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.6" + "version": "3.7.5" } }, "nbformat": 4, diff --git a/tutorials/notebook/programming_guide/tensor.ipynb b/tutorials/notebook/programming_guide/tensor.ipynb index baf59d5601..fc4e13fb3d 100644 --- a/tutorials/notebook/programming_guide/tensor.ipynb +++ b/tutorials/notebook/programming_guide/tensor.ipynb @@ -141,7 +141,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.6" + "version": "3.7.5" } }, "nbformat": 4, -- Gitee From 54420209ce95646e06b57024f8475152d9f60117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=9B=B7?= Date: Thu, 15 Oct 2020 15:37:54 +0800 Subject: [PATCH 3/5] add dtype/tensor files,modify md files --- docs/programming_guide/source_zh_cn/dtype.md | 2 + .../source_zh_cn/operator.md | 31 +--- docs/programming_guide/source_zh_cn/tensor.md | 2 + .../notebook/programming_guide/dtype.ipynb | 111 +++++++++++++ .../notebook/programming_guide/tensor.ipynb | 149 ++++++++++++++++++ 5 files changed, 265 insertions(+), 30 deletions(-) create mode 100644 tutorials/notebook/programming_guide/dtype.ipynb create mode 100644 tutorials/notebook/programming_guide/tensor.ipynb diff --git a/docs/programming_guide/source_zh_cn/dtype.md b/docs/programming_guide/source_zh_cn/dtype.md index 5fc7d208e6..1cbdbdee84 100644 --- a/docs/programming_guide/source_zh_cn/dtype.md +++ b/docs/programming_guide/source_zh_cn/dtype.md @@ -9,6 +9,8 @@ +   + ## 概述 diff --git a/docs/programming_guide/source_zh_cn/operator.md b/docs/programming_guide/source_zh_cn/operator.md index 588d3a4314..514f1c8b69 100644 --- a/docs/programming_guide/source_zh_cn/operator.md +++ b/docs/programming_guide/source_zh_cn/operator.md @@ -16,7 +16,6 @@ - [求三角函数](#求三角函数) - [向量运算](#向量运算) - [Squeeze](#squeeze) - - [求Sparse2Dense](#求sparse2dense) - [矩阵运算](#矩阵运算) - [矩阵乘法](#矩阵乘法) - [广播机制](#广播机制) @@ -187,7 +186,7 @@ scalar 3 import numpy as np import mindspore from mindspore import Tensor -import mindspore.ops.operations as P + input_x = mindspore.Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) input_y = 3.0 print(input_x**input_y) @@ -277,30 +276,6 @@ print(output) [1. 1.] [1. 1.]] ``` -#### 求Sparse2Dense - -以下代码实现了对Sparse2Dense示例: -```python -import numpy as np -import mindspore as ms -from mindspore import Tensor -import mindspore.ops.operations as P - -indices = Tensor([[0, 1], [1, 2]]) -values = Tensor([1, 2], dtype=ms.float32) -dense_shape = (3, 4) -out = P.SparseToDense()(indices, values, dense_shape) - -print(out) -``` - -输出如下: -``` -[[0, 1, 0, 0], - [0, 0, 2, 0], - [0, 0, 0, 0]] -``` - ### 矩阵运算 矩阵运算包括矩阵乘法、矩阵范数、矩阵行列式、矩阵求特征值、矩阵分解等运算。 @@ -453,8 +428,6 @@ print(res) #### LossFunction - L1Loss - 以下代码实现了L1 loss function: ```python from mindspore import Tensor @@ -476,8 +449,6 @@ print(res) #### 优化算法 - SGD - 以下代码实现了SGD梯度下降算法的具体实现,输出是result: ```python from mindspore import Tensor diff --git a/docs/programming_guide/source_zh_cn/tensor.md b/docs/programming_guide/source_zh_cn/tensor.md index b7a6196404..1d8b36f592 100644 --- a/docs/programming_guide/source_zh_cn/tensor.md +++ b/docs/programming_guide/source_zh_cn/tensor.md @@ -12,6 +12,8 @@ +   + ## 概述 diff --git a/tutorials/notebook/programming_guide/dtype.ipynb b/tutorials/notebook/programming_guide/dtype.ipynb new file mode 100644 index 0000000000..3a4d47dcde --- /dev/null +++ b/tutorials/notebook/programming_guide/dtype.ipynb @@ -0,0 +1,111 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# dtype" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 概述\n", + "\n", + "MindSpore张量支持不同的数据类型,包含`int8`、`int16`、`int32`、`int64`、`uint8`、`uint16`、`uint32`、`uint64`、`float16`、`float32`、`float64`、`bool_`,与NumPy的数据类型一一对应。\n", + "\n", + "在MindSpore的运算处理流程中,Python中的`int`数会被转换为定义的`int64`类型,`float`数会被转换为定义的`float32`类型。\n", + "\n", + "详细的类型支持情况请参考https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.html#mindspore.dtype。\n", + "\n", + "以下代码,打印MindSpore的数据类型int32。" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Int32\n" + ] + } + ], + "source": [ + "from mindspore import dtype as mstype\n", + "\n", + "data_type = mstype.int32\n", + "print(data_type)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 数据类型转换接口\n", + "\n", + "MindSpore提供了以下几个接口,实现与NumPy数据类型和Python内置的数据类型间的转换。\n", + "\n", + " * `dtype_to_nptype`:将MindSpore的数据类型转换为NumPy对应的数据类型。\n", + "\n", + " * `dtype_to_pytype`:将MindSpore的数据类型转换为Python对应的内置数据类型。\n", + "\n", + " * `pytype_to_dtype`:将Python内置的数据类型转换为MindSpore对应的数据类型。\n", + "\n", + "以下代码实现了不同数据类型间的转换,并打印转换后的类型。" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Int64\n", + "\n" + ] + } + ], + "source": [ + "from mindspore import dtype as mstype\n", + "\n", + "np_type = mstype.dtype_to_nptype(mstype.int32)\n", + "ms_type = mstype.pytype_to_dtype(int)\n", + "py_type = mstype.dtype_to_pytype(mstype.float64)\n", + "\n", + "print(np_type)\n", + "print(ms_type)\n", + "print(py_type)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "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": 4 +} diff --git a/tutorials/notebook/programming_guide/tensor.ipynb b/tutorials/notebook/programming_guide/tensor.ipynb new file mode 100644 index 0000000000..fc4e13fb3d --- /dev/null +++ b/tutorials/notebook/programming_guide/tensor.ipynb @@ -0,0 +1,149 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tensor" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 概述\n", + "\n", + "张量(Tensor)是MindSpore网络运算中的基本数据结构。张量中的数据类型可参考[dtype](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/dtype.html)。\n", + "\n", + "不同维度的张量分别表示不同的数据,0维张量表示标量,1维张量表示向量,2维张量表示矩阵,3维张量可以表示彩色图像的RGB三通道等等。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> 本文中的所有示例,支持在PyNative模式下运行,暂不支持CPU。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 张量构造\n", + "\n", + "构造张量时,支持传入`Tensor`、`float`、`int`、`bool`、`tuple`、`list`和`NumPy.array`类型。\n", + "\n", + "`Tensor`作为初始值时,可指定dtype,如果没有指定dtype,`int`、`float`、`bool`分别对应`int32`、`float32`、`bool_`,`tuple`和`list`生成的1维`Tensor`数据类型与`tuple`和`list`里存放数据的类型相对应。\n", + "\n", + "代码样例如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2]\n", + " [3 4]] \n", + "\n", + " 1 \n", + "\n", + " 2 \n", + "\n", + " True \n", + "\n", + " [1 2 3] \n", + "\n", + " [4. 5. 6.]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "from mindspore import Tensor\n", + "from mindspore.common import dtype as mstype\n", + "\n", + "x = Tensor(np.array([[1, 2], [3, 4]]), mstype.int32)\n", + "y = Tensor(1.0, mstype.int32)\n", + "z = Tensor(2, mstype.int32)\n", + "m = Tensor(True, mstype.bool_)\n", + "n = Tensor((1, 2, 3), mstype.int16)\n", + "p = Tensor([4.0, 5.0, 6.0], mstype.float64)\n", + "\n", + "print(x, \"\\n\\n\", y, \"\\n\\n\", z, \"\\n\\n\", m, \"\\n\\n\", n, \"\\n\\n\", p)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 张量的属性" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 属性\n", + "\n", + "张量的属性包括形状(shape)和数据类型(dtype)。\n", + "\n", + " * 形状:`Tensor`的shape,是一个tuple。\n", + "\n", + " * 数据类型:`Tensor`的dtype,是MindSpore的一个数据类型。\n", + "\n", + "代码样例如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 2) Int32\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "from mindspore import Tensor\n", + "from mindspore.common import dtype as mstype\n", + "\n", + "x = Tensor(np.array([[1, 2], [3, 4]]), mstype.int32)\n", + "x_shape = x.shape\n", + "x_dtype = x.dtype\n", + "\n", + "print(x_shape, x_dtype)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "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": 4 +} -- Gitee From fd65c4faa91ef150cc4579af1718d806635cf274 Mon Sep 17 00:00:00 2001 From: yingchen Date: Thu, 15 Oct 2020 11:19:30 +0800 Subject: [PATCH 4/5] change info --- docs/programming_guide/source_zh_cn/dtype.md | 2 + .../source_zh_cn/operator.md | 33 +--- docs/programming_guide/source_zh_cn/tensor.md | 2 + .../notebook/programming_guide/dtype.ipynb | 111 +++++++++++++ .../notebook/programming_guide/tensor.ipynb | 149 ++++++++++++++++++ .../advanced_use/performance_profiling_gpu.md | 36 ++--- 6 files changed, 283 insertions(+), 50 deletions(-) create mode 100644 tutorials/notebook/programming_guide/dtype.ipynb create mode 100644 tutorials/notebook/programming_guide/tensor.ipynb diff --git a/docs/programming_guide/source_zh_cn/dtype.md b/docs/programming_guide/source_zh_cn/dtype.md index 5fc7d208e6..1cbdbdee84 100644 --- a/docs/programming_guide/source_zh_cn/dtype.md +++ b/docs/programming_guide/source_zh_cn/dtype.md @@ -9,6 +9,8 @@ +   + ## 概述 diff --git a/docs/programming_guide/source_zh_cn/operator.md b/docs/programming_guide/source_zh_cn/operator.md index 588d3a4314..d71db105a2 100644 --- a/docs/programming_guide/source_zh_cn/operator.md +++ b/docs/programming_guide/source_zh_cn/operator.md @@ -16,15 +16,12 @@ - [求三角函数](#求三角函数) - [向量运算](#向量运算) - [Squeeze](#squeeze) - - [求Sparse2Dense](#求sparse2dense) - [矩阵运算](#矩阵运算) - [矩阵乘法](#矩阵乘法) - [广播机制](#广播机制) - [网络操作](#网络操作) - [特征提取](#特征提取) - [激活函数](#激活函数) - - [LossFunction](#lossfunction) - - [优化算法](#优化算法) - [数组操作](#数组操作) - [DType](#dtype) - [Cast](#cast) @@ -187,7 +184,7 @@ scalar 3 import numpy as np import mindspore from mindspore import Tensor -import mindspore.ops.operations as P + input_x = mindspore.Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) input_y = 3.0 print(input_x**input_y) @@ -277,29 +274,6 @@ print(output) [1. 1.] [1. 1.]] ``` -#### 求Sparse2Dense - -以下代码实现了对Sparse2Dense示例: -```python -import numpy as np -import mindspore as ms -from mindspore import Tensor -import mindspore.ops.operations as P - -indices = Tensor([[0, 1], [1, 2]]) -values = Tensor([1, 2], dtype=ms.float32) -dense_shape = (3, 4) -out = P.SparseToDense()(indices, values, dense_shape) - -print(out) -``` - -输出如下: -``` -[[0, 1, 0, 0], - [0, 0, 2, 0], - [0, 0, 0, 0]] -``` ### 矩阵运算 @@ -453,8 +427,6 @@ print(res) #### LossFunction - L1Loss - 以下代码实现了L1 loss function: ```python from mindspore import Tensor @@ -476,8 +448,6 @@ print(res) #### 优化算法 - SGD - 以下代码实现了SGD梯度下降算法的具体实现,输出是result: ```python from mindspore import Tensor @@ -501,7 +471,6 @@ print(result) ``` [0. 0. 0. 0.] ``` - ### 数组操作 数组操作指操作对象是一些数组的操作。 diff --git a/docs/programming_guide/source_zh_cn/tensor.md b/docs/programming_guide/source_zh_cn/tensor.md index b7a6196404..1d8b36f592 100644 --- a/docs/programming_guide/source_zh_cn/tensor.md +++ b/docs/programming_guide/source_zh_cn/tensor.md @@ -12,6 +12,8 @@ +   + ## 概述 diff --git a/tutorials/notebook/programming_guide/dtype.ipynb b/tutorials/notebook/programming_guide/dtype.ipynb new file mode 100644 index 0000000000..3a4d47dcde --- /dev/null +++ b/tutorials/notebook/programming_guide/dtype.ipynb @@ -0,0 +1,111 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# dtype" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 概述\n", + "\n", + "MindSpore张量支持不同的数据类型,包含`int8`、`int16`、`int32`、`int64`、`uint8`、`uint16`、`uint32`、`uint64`、`float16`、`float32`、`float64`、`bool_`,与NumPy的数据类型一一对应。\n", + "\n", + "在MindSpore的运算处理流程中,Python中的`int`数会被转换为定义的`int64`类型,`float`数会被转换为定义的`float32`类型。\n", + "\n", + "详细的类型支持情况请参考https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.html#mindspore.dtype。\n", + "\n", + "以下代码,打印MindSpore的数据类型int32。" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Int32\n" + ] + } + ], + "source": [ + "from mindspore import dtype as mstype\n", + "\n", + "data_type = mstype.int32\n", + "print(data_type)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 数据类型转换接口\n", + "\n", + "MindSpore提供了以下几个接口,实现与NumPy数据类型和Python内置的数据类型间的转换。\n", + "\n", + " * `dtype_to_nptype`:将MindSpore的数据类型转换为NumPy对应的数据类型。\n", + "\n", + " * `dtype_to_pytype`:将MindSpore的数据类型转换为Python对应的内置数据类型。\n", + "\n", + " * `pytype_to_dtype`:将Python内置的数据类型转换为MindSpore对应的数据类型。\n", + "\n", + "以下代码实现了不同数据类型间的转换,并打印转换后的类型。" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Int64\n", + "\n" + ] + } + ], + "source": [ + "from mindspore import dtype as mstype\n", + "\n", + "np_type = mstype.dtype_to_nptype(mstype.int32)\n", + "ms_type = mstype.pytype_to_dtype(int)\n", + "py_type = mstype.dtype_to_pytype(mstype.float64)\n", + "\n", + "print(np_type)\n", + "print(ms_type)\n", + "print(py_type)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "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": 4 +} diff --git a/tutorials/notebook/programming_guide/tensor.ipynb b/tutorials/notebook/programming_guide/tensor.ipynb new file mode 100644 index 0000000000..fc4e13fb3d --- /dev/null +++ b/tutorials/notebook/programming_guide/tensor.ipynb @@ -0,0 +1,149 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tensor" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 概述\n", + "\n", + "张量(Tensor)是MindSpore网络运算中的基本数据结构。张量中的数据类型可参考[dtype](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/dtype.html)。\n", + "\n", + "不同维度的张量分别表示不同的数据,0维张量表示标量,1维张量表示向量,2维张量表示矩阵,3维张量可以表示彩色图像的RGB三通道等等。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> 本文中的所有示例,支持在PyNative模式下运行,暂不支持CPU。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 张量构造\n", + "\n", + "构造张量时,支持传入`Tensor`、`float`、`int`、`bool`、`tuple`、`list`和`NumPy.array`类型。\n", + "\n", + "`Tensor`作为初始值时,可指定dtype,如果没有指定dtype,`int`、`float`、`bool`分别对应`int32`、`float32`、`bool_`,`tuple`和`list`生成的1维`Tensor`数据类型与`tuple`和`list`里存放数据的类型相对应。\n", + "\n", + "代码样例如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2]\n", + " [3 4]] \n", + "\n", + " 1 \n", + "\n", + " 2 \n", + "\n", + " True \n", + "\n", + " [1 2 3] \n", + "\n", + " [4. 5. 6.]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "from mindspore import Tensor\n", + "from mindspore.common import dtype as mstype\n", + "\n", + "x = Tensor(np.array([[1, 2], [3, 4]]), mstype.int32)\n", + "y = Tensor(1.0, mstype.int32)\n", + "z = Tensor(2, mstype.int32)\n", + "m = Tensor(True, mstype.bool_)\n", + "n = Tensor((1, 2, 3), mstype.int16)\n", + "p = Tensor([4.0, 5.0, 6.0], mstype.float64)\n", + "\n", + "print(x, \"\\n\\n\", y, \"\\n\\n\", z, \"\\n\\n\", m, \"\\n\\n\", n, \"\\n\\n\", p)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 张量的属性" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 属性\n", + "\n", + "张量的属性包括形状(shape)和数据类型(dtype)。\n", + "\n", + " * 形状:`Tensor`的shape,是一个tuple。\n", + "\n", + " * 数据类型:`Tensor`的dtype,是MindSpore的一个数据类型。\n", + "\n", + "代码样例如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 2) Int32\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "from mindspore import Tensor\n", + "from mindspore.common import dtype as mstype\n", + "\n", + "x = Tensor(np.array([[1, 2], [3, 4]]), mstype.int32)\n", + "x_shape = x.shape\n", + "x_dtype = x.dtype\n", + "\n", + "print(x_shape, x_dtype)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "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": 4 +} diff --git a/tutorials/training/source_en/advanced_use/performance_profiling_gpu.md b/tutorials/training/source_en/advanced_use/performance_profiling_gpu.md index dc33a9b95c..e989af7206 100644 --- a/tutorials/training/source_en/advanced_use/performance_profiling_gpu.md +++ b/tutorials/training/source_en/advanced_use/performance_profiling_gpu.md @@ -8,7 +8,7 @@ - [Overview](#overview) - [Operation Process](#operation-process) - [Preparing the Training Script](#preparing-the-training-script) - - [Launch MindInsight](#launch-mindinsight) + - [Launching MindInsight](#launching-mindinsight) - [Performance Analysis](#performance-analysis) - [Operator Performance Analysis](#operator-performance-analysis) - [Timeline Analysis](#timeline-analysis) @@ -22,7 +22,7 @@ Performance data like operators' execution time is recorded in files and can be ## Operation Process -> The GPU operation process is the same as that in Ascend chip. +> The GPU operation process is the same as that in the Ascend chip. > > @@ -33,9 +33,9 @@ Performance data like operators' execution time is recorded in files and can be ## Preparing the Training Script -To enable the performance profiling of neural networks, MindSpore Profiler APIs should be added into the script.Only the output_path in parameters is worked in GPU now. Then, at the end of the training, `Profiler.analyse` should be called to finish profiling and generate the perforamnce analyse results. +To enable the performance profiling of neural networks, MindSpore Profiler APIs should be added into the script. Only the output_path in parameters is working in GPU now. Then, at the end of the training, `Profiler.analyse` should be called to finish profiling and generate the performance analysis results. -> The sample code is the same as that in Ascend chip: +> The sample code is the same as that in the Ascend chip: > > @@ -67,16 +67,16 @@ class StopAtStep(Callback): self.profiler.analyse() ``` -The code above is just a example. Users should implement callback by themselves. +The code above is just an example. Users should implement callback by themselves. -## Launch MindInsight +## Launching MindInsight The MindInsight launch command can refer to [MindInsight Commands](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/mindinsight_commands.html). ### Performance Analysis -Users can access the Performance Profiler by selecting a specific training from the training list, and click the performance profiling link. And the Performance Profiler only support operation analysis and Timeline Analysis now, the others modules will publish soon. +Users can access the Performance Profiler by selecting a specific training from the training list, and click the performance profiling link. And the Performance Profiler only supports operation analysis and Timeline Analysis now, the others modules will be published soon. ![performance_overall.png](./images/performance_overall.png) @@ -90,7 +90,7 @@ Users can click the detail link to see the details of each components. #### Operator Performance Analysis -The operator performance analysis component is used to display the execution time of the operators during MindSpore run. +The operator performance analysis component is used to display the execution time of the operators when running MindSpore. ![gpu_op_ui_profiler.png](./images/gpu_op_ui_profiler.png) @@ -98,14 +98,14 @@ Figure 2: Statistics for Operator Types Figure 2 displays the statistics for the operator types, including: -- Choose pie or bar graph to show the proportion time occupied by each operator type. The time of one operator type is calculated by accumulating the execution time of operators belong to this type. -- Display top 20 operator types with longest average execution time, show the proportion of total time and average execution time (ms) of each operator type. +- Choose a pie or a bar graph to show the proportion time occupied by each operator type. The time of one operator type is calculated by accumulating the execution time of operators belong to this type. +- Display top 20 operator types with the longest average execution time, show the proportion of total time and average execution time (ms) of each operator type. The bottom half of Figure 2 displays the statistics table for the operators' details, including: -- Choose All: Display statistics for the operators, including operator position information, type, execution time, full scope time etc. The table will be sorted by average execution time by default. -- Choose Type: Display statistics for the operator types, including operator type name, execution time, execution frequency and proportion of total time, average execution time. Users can click on each line, querying for all the operators belong to this type. -- Search: There is a search box on the right, which can support fuzzy search for operators/operator types. +- Choose All: Display statistics for the operators, including operator position information, type, execution time, full scope time, etc. The table will be sorted by average execution time by default. +- Choose Type: Display statistics for the operator types, including operator type name, execution time, execution frequency and proportion of total time, average execution time. Users can click on each line to query for all the operators belong to this type. +- Search: There is a search box on the right, which supports fuzzy search for operators/operator types. ![gpu_activity_profiler.png](./images/gpu_activity_profiler.png) @@ -113,14 +113,14 @@ Figure 3: Statistics for Kernel Activities Figure 3 displays the statistics for the Kernel, including: -- Pie graph to show the proportion time occupied by each kernel activity. And the top 15 kernel activities with longest exection time. -- The statistical table's column include activity name, operation name, execution frequency, total time, average time. -- The search box on the right, which can support fuzzy search for activity name/operator full name. +- A pie graph to show the proportion time occupied by each kernel activity and the top 15 kernel activities with the longest execution time. +- The statistical table's column includes activity name, operation name, execution frequency, total time and average time. +- The search box on the right, which supports fuzzy search for the activity name/operator full name. #### Timeline Analysis -The usage is almost same as that in Ascend. The difference is GPU Timeline displays the operation information and CUDA activity. +The usage is almost the same as that in Ascend. The difference is GPU Timeline displays the operation information and CUDA activity. -> The usage is follow as: +> The usage is described as follows: > > \ No newline at end of file -- Gitee From 324f46f440d059889fb8ee7268260d8772f6c399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=9B=B7?= Date: Thu, 15 Oct 2020 19:34:23 +0800 Subject: [PATCH 5/5] add --- docs/programming_guide/source_zh_cn/operator.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/programming_guide/source_zh_cn/operator.md b/docs/programming_guide/source_zh_cn/operator.md index d71db105a2..514f1c8b69 100644 --- a/docs/programming_guide/source_zh_cn/operator.md +++ b/docs/programming_guide/source_zh_cn/operator.md @@ -22,6 +22,8 @@ - [网络操作](#网络操作) - [特征提取](#特征提取) - [激活函数](#激活函数) + - [LossFunction](#lossfunction) + - [优化算法](#优化算法) - [数组操作](#数组操作) - [DType](#dtype) - [Cast](#cast) @@ -274,7 +276,6 @@ print(output) [1. 1.] [1. 1.]] ``` - ### 矩阵运算 矩阵运算包括矩阵乘法、矩阵范数、矩阵行列式、矩阵求特征值、矩阵分解等运算。 @@ -471,6 +472,7 @@ print(result) ``` [0. 0. 0. 0.] ``` + ### 数组操作 数组操作指操作对象是一些数组的操作。 -- Gitee