diff --git a/docs/api_cpp/source_en/api.md b/docs/api_cpp/source_en/api.md
deleted file mode 100644
index e0c9aead9a5e5a1a083f5a37611c3436fd7ee6f0..0000000000000000000000000000000000000000
--- a/docs/api_cpp/source_en/api.md
+++ /dev/null
@@ -1,390 +0,0 @@
-# mindspore::api
-
-
-
-## Context
-
-\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
-
-The Context class is used to store environment variables during execution.
-
-### Static Public Member Function
-
-#### Instance
-
-```cpp
-static Context &Instance();
-```
-
-Obtains the MindSpore Context instance object.
-
-### Public Member Functions
-
-#### GetDeviceTarget
-
-```cpp
-const std::string &GetDeviceTarget() const;
-```
-
-Obtains the target device type.
-
-- Returns
-
- Current DeviceTarget type.
-
-#### GetDeviceID
-
-```cpp
-uint32_t GetDeviceID() const;
-```
-
-Obtains the device ID.
-
-- Returns
-
- Current device ID.
-
-#### SetDeviceTarget
-
-```cpp
-Context &SetDeviceTarget(const std::string &device_target);
-```
-
-Configures the target device.
-
-- Parameters
-
- - `device_target`: target device to be configured. The options are `kDeviceTypeAscend310` and `kDeviceTypeAscend910`.
-
-- Returns
-
- MindSpore Context instance object.
-
-#### SetDeviceID
-
-```cpp
-Context &SetDeviceID(uint32_t device_id);
-```
-
-Obtains the device ID.
-
-- Parameters
-
- - `device_id`: device ID to be configured.
-
-- Returns
-
- MindSpore Context instance object.
-
-## Serialization
-
-\#include <[serialization.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/serialization.h)>
-
-The Serialization class is used to summarize methods for reading and writing model files.
-
-### Static Public Member Function
-
-#### LoadModel
-
-- Parameters
-
- - `file`: model file path.
- - `model_type`: model file type. The options are `ModelType::kMindIR` and `ModelType::kOM`.
-
-- Returns
-
- Object for storing graph data.
-
-## Model
-
-\#include <[model.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/model.h)>
-
-A Model class is used to define a MindSpore model, facilitating computational graph management.
-
-### Constructor and Destructor
-
-```cpp
-Model(const GraphCell &graph);
-~Model();
-```
-
-`GraphCell` is a derivative of `Cell`. `Cell` is not open for use currently. `GraphCell` can be constructed from `Graph`, for example, `Model model(GraphCell(graph))`.
-
-### Public Member Functions
-
-#### Build
-
-```cpp
-Status Build(const std::map &options);
-```
-
-Builds a model so that it can run on a device.
-
-- Parameters
-
- - `options`: model build options. In the following table, Key indicates the option name, and Value indicates the corresponding option.
-
-| Key | Value |
-| --- | --- |
-| kModelOptionInsertOpCfgPath | [AIPP](https://support.huaweicloud.com/intl/en-us/adevg-ms-atlas200dkappc32/atlasadm_01_0023.html) configuration file path. |
-| kModelOptionInputFormat | Manually specifies the model input format. The options are `"NCHW"` and `"NHWC"`. |
-| kModelOptionInputShape | Manually specifies the model input shape, for example, `"input_op_name1: n1,c2,h3,w4;input_op_name2: n4,c3,h2,w1"` |
-| kModelOptionOutputType | Manually specifies the model output type, for example, `"FP16"` or `"UINT8"`. The default value is `"FP32"`. |
-| kModelOptionPrecisionMode | Model precision mode. The options are `"force_fp16"`, `"allow_fp32_to_fp16"`, `"must_keep_origin_dtype"`, and `"allow_mix_precision"`. The default value is `"force_fp16"`. |
-| kModelOptionOpSelectImplMode | Operator selection mode. The options are `"high_performance"` and `"high_precision"`. The default value is `"high_performance"`. |
-
-- Returns
-
- Status code.
-
-#### Predict
-
-```cpp
-Status Predict(const std::vector &inputs, std::vector *outputs);
-```
-
-Inference model.
-
-- Parameters
-
- - `inputs`: a `vector` where model inputs are arranged in sequence.
- - `outputs`: output parameter, which is the pointer to a `vector`. The model outputs are filled in the container in sequence.
-
-- Returns
-
- Status code.
-
-#### GetInputsInfo
-
-```cpp
-Status GetInputsInfo(std::vector *names, std::vector> *shapes, std::vector *data_types, std::vector *mem_sizes) const;
-```
-
-Obtains the model input information.
-
-- Parameters
-
- - `names`: optional output parameter, which is the pointer to a `vector` where model inputs are arranged in sequence. The input names are filled in the container in sequence. If `nullptr` is input, the attribute is not obtained.
- - `shapes`: optional output parameter, which is the pointer to a `vector` where model inputs are arranged in sequence. The input shapes are filled in the container in sequence. If `nullptr` is input, the attribute is not obtained.
- - `data_types`: optional output parameter, which is the pointer to a `vector` where model inputs are arranged in sequence. The input data types are filled in the container in sequence. If `nullptr` is input, the attribute is not obtained.
- - `mem_sizes`: optional output parameter, which is the pointer to a `vector` where model inputs are arranged in sequence. The input memory lengths (in bytes) are filled in the container in sequence. If `nullptr` is input, the attribute is not obtained.
-
-- Returns
-
- Status code.
-
-#### GetOutputsInfo
-
-```cpp
-Status GetOutputsInfo(std::vector *names, std::vector> *shapes, std::vector *data_types, std::vector *mem_sizes) const;
-```
-
-Obtains the model output information.
-
-- Parameters
-
- - `names`: optional output parameter, which is the pointer to a `vector` where model outputs are arranged in sequence. The output names are filled in the container in sequence. If `nullptr` is input, the attribute is not obtained.
- - `shapes`: optional output parameter, which is the pointer to a `vector` where model outputs are arranged in sequence. The output shapes are filled in the container in sequence. If `nullptr` is input, the attribute is not obtained.
- - `data_types`: optional output parameter, which is the pointer to a `vector` where model outputs are arranged in sequence. The output data types are filled in the container in sequence. If `nullptr` is input, the attribute is not obtained.
- - `mem_sizes`: optional output parameter, which is the pointer to a `vector` where model outputs are arranged in sequence. The output memory lengths (in bytes) are filled in the container in sequence. If `nullptr` is input, the attribute is not obtained.
-
-- Returns
-
- Status code.
-
-## Tensor
-
-\#include <[types.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/types.h)>
-
-### Constructor and Destructor
-
-```cpp
-Tensor();
-Tensor(const std::string &name, DataType type, const std::vector &shape, const void *data, size_t data_len);
-~Tensor();
-```
-
-### Static Public Member Function
-
-#### GetTypeSize
-
-```cpp
-static int GetTypeSize(api::DataType type);
-```
-
-Obtains the memory length of a data type, in bytes.
-
-- Parameters
-
- - `type`: data type.
-
-- Returns
-
- Memory length, in bytes.
-
-### Public Member Functions
-
-#### Name
-
-```cpp
-const std::string &Name() const;
-```
-
-Obtains the name of a tensor.
-
-- Returns
-
- Tensor name.
-
-#### DataType
-
-```cpp
-api::DataType DataType() const;
-```
-
-Obtains the data type of a tensor.
-
-- Returns
-
- Tensor data type.
-
-#### Shape
-
-```cpp
-const std::vector &Shape() const;
-```
-
-Obtains the shape of a tensor.
-
-- Returns
-
- Tensor shape.
-
-#### SetName
-
-```cpp
-void SetName(const std::string &name);
-```
-
-Sets the name of a tensor.
-
-- Parameters
-
- - `name`: name to be set.
-
-#### SetDataType
-
-```cpp
-void SetDataType(api::DataType type);
-```
-
-Sets the data type of a tensor.
-
-- Parameters
-
- - `type`: type to be set.
-
-#### SetShape
-
-```cpp
-void SetShape(const std::vector &shape);
-```
-
-Sets the shape of a tensor.
-
-- Parameters
-
- - `shape`: shape to be set.
-
-#### Data
-
-```cpp
-const void *Data() const;
-```
-
-Obtains the constant pointer to the tensor data.
-
-- Returns
-
- Constant pointer to the tensor data.
-
-#### MutableData
-
-```cpp
-void *MutableData();
-```
-
-Obtains the pointer to the tensor data.
-
-- Returns
-
- Pointer to the tensor data.
-
-#### DataSize
-
-```cpp
-size_t DataSize() const;
-```
-
-Obtains the memory length (in bytes) of the tensor data.
-
-- Returns
-
- Memory length of the tensor data, in bytes.
-
-#### ResizeData
-
-```cpp
-bool ResizeData(size_t data_len);
-```
-
-Adjusts the memory size of the tensor.
-
-- Parameters
-
- - `data_len`: number of bytes in the memory after adjustment.
-
-- Returns
-
- A value of bool indicates whether the operation is successful.
-
-#### SetData
-
-```cpp
-bool SetData(const void *data, size_t data_len);
-```
-
-Adjusts the memory data of the tensor.
-
-- Parameters
-
- - `data`: memory address of the source data.
- - `data_len`: length of the source data memory.
-
-- Returns
-
- A value of bool indicates whether the operation is successful.
-
-#### ElementNum
-
-```cpp
-int64_t ElementNum() const;
-```
-
-Obtains the number of elements in a tensor.
-
-- Returns
-
- Number of elements in a tensor.
-
-#### Clone
-
-```cpp
-Tensor Clone() const;
-```
-
-Performs a self copy.
-
-- Returns
-
- A deep copy.
\ No newline at end of file
diff --git a/docs/api_cpp/source_en/class_list.md b/docs/api_cpp/source_en/class_list.md
index d6f4cd6216606ff0b95bc67f0cc18ebbbb8e4f9d..a9d821824cdfbc2c47e2c33e5efc67495cfe3ea0 100644
--- a/docs/api_cpp/source_en/class_list.md
+++ b/docs/api_cpp/source_en/class_list.md
@@ -2,7 +2,7 @@
-Here is a list of all classes with links to the namespace documentation for each member:
+Here is a list of all classes with links to the namespace documentation for each member in MindSpore Lite:
| Namespace | Class Name | Description |
| --- | --- | --- |
@@ -16,3 +16,12 @@ Here is a list of all classes with links to the namespace documentation for each
| mindspore::session | [LiteSession](https://www.mindspore.cn/doc/api_cpp/en/master/session.html#litesession) | LiteSession defines sessions in MindSpore Lite for compiling Model and forwarding model. |
| mindspore::tensor | [MSTensor](https://www.mindspore.cn/doc/api_cpp/en/master/tensor.html#mstensor) | MSTensor defines tensor in MindSpore Lite. |
| mindspore::dataset | [LiteMat](https://www.mindspore.cn/doc/api_cpp/en/master/dataset.html#litemat) |LiteMat is a class used to process images. |
+
+The definitions and namespaces of classes in MindSpore are as follows:
+
+| Namespace | Class Name | Description |
+| --------- | ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- |
+| mindspore | [Context](https://www.mindspore.cn/doc/api_cpp/en/master/mindspore.html#context) | The Context class is used to store environment variables during execution. |
+| mindspore | [Serialization](https://www.mindspore.cn/doc/api_cpp/en/master/mindspore.html#serialization) | The Serialization class is used to summarize methods for reading and writing model files. |
+| mindspore | [Model](https://www.mindspore.cn/doc/api_cpp/en/master/mindspore.html#model) | The Model class is used to define a MindSpore model, facilitating computational graph management. |
+| mindspore | [MSTensor](https://www.mindspore.cn/doc/api_cpp/en/master/tensor.html#mstensor) | The MSTensor class defines a tensor in MindSpore. |
diff --git a/docs/api_cpp/source_en/dataset.md b/docs/api_cpp/source_en/dataset.md
index fb8e26148d13d66d114b11c2fc595398e5d93258..485f360eed3bdadf9d2fd6a4549e32e0a5be8329 100644
--- a/docs/api_cpp/source_en/dataset.md
+++ b/docs/api_cpp/source_en/dataset.md
@@ -2,6 +2,93 @@
+## Execute
+
+\#include <[execute.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/ccsrc/minddata/dataset/include/execute.h)>
+
+```cpp
+// shared_ptr
+Execute::Execute(std::shared_ptr op, MapTargetDevice deviceType);
+Execute::Execute(std::vector> ops, MapTargetDevice deviceType);
+
+// normal pointer
+Execute::Execute(std::reference_wrapper op, MapTargetDevice deviceType);
+Execute::Execute(std::vector> ops, MapTargetDevice deviceType);
+
+// reference_wrapper
+Execute::Execute(TensorTransform *op, MapTargetDevice deviceType);
+Execute::Execute(std::vector ops, MapTargetDevice deviceType);
+```
+
+Transform(image, text)Transform operators in eager mode executor class. Multiple constructors are supported,include shared_ptr, normal pointer and reference_wrapper.
+
+- Parameters
+
+ - `op`: Single transform operator.
+ - `ops`: A list of transform operators.
+ - `deviceType`: Runtime hardware. Options are: CPU, GPU and Ascend310.
+
+```cpp
+Status operator()(const mindspore::MSTensor &input, mindspore::MSTensor *output);
+```
+
+Eager mode execution API.
+
+- Parameters
+
+ - `input`: Tensor before transformations.
+ - `output`: Tensor after transformations.
+
+- Returns
+
+ Status code which indicate the execution result.
+
+```cpp
+std::string Execute::AippCfgGenerator()
+```
+
+Aipp module config file generator, Aipp module binds with Dvpp module. This API takes effects on case `deviceType = kAscend310`, generates Aipp config file according to the parameters of operators defined in data pre-processing.
+
+- Parameters
+
+ None.
+
+- Returns
+
+ Return a string indicates the system path of Aipp config file.
+
+## Dvpp Module
+
+Dvpp module is a hardware decoder embedded in Ascend 310 AI chip which has a better performance on image processing compare with CPU operators. Several transforms applied on JPEG format image are supported.
+
+- If let `deviceType = kAscend310` when constructing `execute` object, Dvpp operators will be applied during runtime.
+- Dvpp module supporting transforms list: `Decode(), Resize(), CenterCrop(), Normalize()`.
+- The above Dvpp operator and the CPU operator of the same function share a unified API, which is only distinguished by `deviceType`.
+
+Example:
+
+```cpp
+// Define dvpp transforms
+std::vector crop_paras = {224, 224};
+
+std::vector resize_paras = {256};
+
+std::vector mean = {0.485 * 255, 0.456 * 255, 0.406 * 255};
+
+std::vector std = {0.229 * 255, 0.224 * 255, 0.225 * 255};
+
+std::shared_ptr decode(new vision::Decode());
+
+std::shared_ptr resize(new vision::Resize(resize_paras));
+
+std::shared_ptr centercrop(new vision::CenterCrop(crop_paras));
+
+std::shared_ptr normalize(new vision::Normalize(mean, std));
+
+std::vector> trans_list = {decode, resize, centercrop, normalize};
+mindspore::dataset::Execute Transform(trans_list, MapTargetDevice::kAscend310);
+```
+
## ResizeBilinear
\#include <[image_process.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.h)>
diff --git a/docs/api_cpp/source_en/index.rst b/docs/api_cpp/source_en/index.rst
index b5f76d3c78fd947026a99ea4a9ba8afd91355ed8..779317bee1f0397ac1c5a78905b31b236f33d4f8 100644
--- a/docs/api_cpp/source_en/index.rst
+++ b/docs/api_cpp/source_en/index.rst
@@ -12,7 +12,6 @@ MindSpore C++ API
class_list
mindspore
- api
dataset
vision
lite
diff --git a/docs/api_cpp/source_en/lite_cpp_example.rst b/docs/api_cpp/source_en/lite_cpp_example.rst
index c96d72de5d67511e9389a2f0733beac7fa9af653..fd4d779e49668e94b59c95024a2b4683923038d4 100644
--- a/docs/api_cpp/source_en/lite_cpp_example.rst
+++ b/docs/api_cpp/source_en/lite_cpp_example.rst
@@ -3,6 +3,7 @@ Example
.. toctree::
:maxdepth: 1
+
Simple Demo
Implementing an Image Classification Application
High-level Usage
\ No newline at end of file
diff --git a/docs/api_cpp/source_en/mindspore.md b/docs/api_cpp/source_en/mindspore.md
index 1106b7c5875ea5928191f55e0d3c821afa131630..ca0262886f3a2505ef80be194ef9af2d7327d338 100644
--- a/docs/api_cpp/source_en/mindspore.md
+++ b/docs/api_cpp/source_en/mindspore.md
@@ -2,10 +2,631 @@
-\#include <[ms_tensor.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/lite/include/ms_tensor.h)>
+## Context
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+The Context class is used to store environment variables during execution.
+
+### Public Member Functions
+
+#### SetThreadNum
+
+```cpp
+void SetThreadNum(int32_t thread_num);
+```
+
+Set the number of threads at runtime. This option is only valid for MindSpore Lite.
+
+- Parameters
+
+ - `thread_num`: the number of threads at runtime.
+
+#### GetThreadNum
+
+```cpp
+int32_t GetThreadNum() const;
+```
+
+Get the current thread number setting.
+
+- Returns
+
+ The current thread number setting.
+
+#### SetAllocator
+
+```cpp
+void SetAllocator(const std::shared_ptr &allocator);
+```
+
+Set Allocator, which defines a memory pool for dynamic memory malloc and memory free. This option is only valid for MindSpore Lite.
+
+- Parameters
+
+ - `allocator`: A pointer to an Allocator.
+
+#### GetAllocator
+
+```cpp
+std::shared_ptr GetAllocator() const;
+```
+
+Get the current Allocator setting.
+
+- Returns
+
+ The current Allocator setting.
+
+#### MutableDeviceInfo
+
+```cpp
+std::vector> &MutableDeviceInfo();
+```
+
+Get a mutable reference of [DeviceInfoContext](#deviceinfocontext) vector in this context. Only MindSpore Lite supports heterogeneous scenarios with multiple members in the vector.
+
+- Returns
+
+ Mutable reference of DeviceInfoContext vector in this context.
+
+## DeviceInfoContext
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+DeviceInfoContext defines different device contexts.
+
+### Public Member Functions
+
+#### GetDeviceType
+
+```cpp
+virtual enum DeviceType GetDeviceType() const = 0
+```
+
+Get the type of this DeviceInfoContext.
+
+- Returns
+
+ Type of this DeviceInfoContext.
+
+ ```cpp
+ enum DeviceType {
+ kCPU = 0,
+ kMaliGPU,
+ kNvidiaGPU,
+ kKirinNPU,
+ kAscend910,
+ kAscend310,
+ // add new type here
+ kInvalidDeviceType = 100,
+ };
+ ```
+
+#### Cast
+
+```cpp
+template std::shared_ptr Cast();
+```
+
+A similar function to RTTI is provided when the `-fno-rtti` compilation option is turned on, which converts DeviceInfoContext to a shared pointer of type `T`, and returns `nullptr` if the conversion fails.
+
+- Returns
+
+ A pointer of type `T` after conversion. If the conversion fails, it will be `nullptr`.
+
+## CPUDeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+Derived from [DeviceInfoContext](#deviceinfocontext), The configuration of the model running on the CPU. This option is only valid for MindSpore Lite.
+
+### Public Member Functions
+
+| Functions | Notes |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetThreadAffinity(int mode)` | Set thread affinity mode
- `mode`: 0: no affinity, 1: big cores first, 2: little cores first. |
+| `int GetThreadAffinity() const` | - Returns: The thread affinity mode |
+| `void SetEnableFP16(bool is_fp16)` | Enables to perform the float16 inference
- `is_fp16`: Enable float16 inference or not. |
+| `bool GetEnableFP16() const` | - Returns: whether enable float16 inference. |
+
+## MaliGPUDeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+Derived from [DeviceInfoContext](#deviceinfocontext), The configuration of the model running on the GPU. This option is only valid for MindSpore Lite.
+
+### Public Member Functions
+
+| Functions | Notes |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetEnableFP16(bool is_fp16)` | Enables to perform the float16 inference
- `is_fp16`: Enable float16 inference or not. |
+| `bool GetEnableFP16() const` | - Returns: whether enable float16 inference. |
+
+## KirinNPUDeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+Derived from [DeviceInfoContext](#deviceinfocontext), The configuration of the model running on the NPU. This option is only valid for MindSpore Lite.
+
+### Public Member Functions
+
+| Functions | Notes |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetFrequency(int frequency)` | Used to set the NPU frequency
- `frequency`: can be set to 1 (low power consumption), 2 (balanced), 3 (high performance), 4 (extreme performance), default as 3. |
+| `int GetFrequency() const` | - Returns: NPU frequency |
+
+## NvidiaGPUDeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+Derived from [DeviceInfoContext](#deviceinfocontext), The configuration of the model running on the GPU. This option is invalid for MindSpore Lite.
+
+### Public Member Functions
+
+| Functions | Notes |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetDeviceID(uint32_t device_id)` | Used to set device id
- `device_id`: The device id. |
+| `uint32_t GetDeviceID() const` | - Returns: The device id. |
+
+## Ascend910DeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+Derived from [DeviceInfoContext](#deviceinfocontext), The configuration of the model running on the Ascend910. This option is invalid for MindSpore Lite.
+
+### Public Member Functions
+
+| Functions | Notes |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetDeviceID(uint32_t device_id)` | Used to set device id
- `device_id`: The device id. |
+| `uint32_t GetDeviceID() const` | - Returns: The device id. |
+
+## Ascend310DeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+Derived from [DeviceInfoContext](#deviceinfocontext), The configuration of the model running on the Ascend310. This option is invalid for MindSpore Lite.
+
+### Public Member Functions
+
+| Functions | Notes |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetDeviceID(uint32_t device_id)` | Used to set device id
- `device_id`: The device id. |
+| `uint32_t GetDeviceID() const` | - Returns: The device id. |
+| `void SetInsertOpConfigPath(const std::string &cfg_path)` | Set [AIPP](https://support.huaweicloud.com/intl/en-us/adevg-ms-atlas200dkappc32/atlasadm_01_0023.html) configuration file path |
+| `std::string GetInsertOpConfigPath()` | - Returns: [AIPP](https://support.huaweicloud.com/intl/en-us/adevg-ms-atlas200dkappc32/atlasadm_01_0023.html) configuration file path |
+| `void SetInputFormat(const std::string &format)` | Set format of model inputs
- `format`: Optional `"NCHW"`, `"NHWC"`, etc. |
+| `std::string GetInputFormat()` | - Returns: The set format of model inputs |
+| `void SetInputShape(const std::string &shape)` | Set shape of model inputs
- `shape`: e.g., `"input_op_name1: 1,2,3,4;input_op_name2: 4,3,2,1"` |
+| `std::string GetInputShape()` | - Returns: The set shape of model inputs |
+| `void SetOutputType(enum DataType output_type)` | Set type of model outputs
- `output_type`: Only uint8, fp16 and fp32 are supported |
+| `enum DataType GetOutputType()` | - Returns: The set type of model outputs |
+| `void SetPrecisionMode(const std::string &precision_mode)` | Set precision mode of model
- `precision_mode`: Optional `"force_fp16"`, `"allow_fp32_to_fp16"`, `"must_keep_origin_dtype"` and `"allow_mix_precision"`, `"force_fp16"` is set as default |
+| `std::string GetPrecisionMode(t)` | - Returns: The set precision mode |
+| `void SetOpSelectImplMode(const std::string &op_select_impl_mode)` | Set op select implementation mode
- `op_select_impl_mode`: Optional `"high_performance"` and `"high_precision"`, `"high_performance"` is set as default |
+| `std::string GetOpSelectImplMode()` | - Returns: The set op select implementation mode |
+
+## Serialization
+
+\#include <[serialization.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/serialization.h)>
+
+The Serialization class is used to summarize methods for reading and writing model files.
+
+### Static Public Member Function
+
+#### Load
+
+Loads a model file from path, is not supported on MindSpore Lite.
+
+```cpp
+Status Load(const std::string &file, ModelType model_type, Graph *graph);
+```
+
+- Parameters
+
+ - `file`: the path of model file.
+ - `model_type`:the Type of model file, options are `ModelType::kMindIR`, `ModelType::kOM`.
+ - `graph`:the output parameter, a object saves graph data.
+
+- Returns
+
+ Status code.
+
+#### Load
+
+Loads a model file from memory buffer.
+
+```cpp
+Status Load(const void *model_data, size_t data_size, ModelType model_type, Graph *graph);
+```
+
+- Parameters
+
+ - `model_data`:a buffer filled by model file.
+ - `data_size`:the size of the buffer.
+ - `model_type`:the Type of model file, options are `ModelType::kMindIR`, `ModelType::kOM`.
+ - `graph`:the output parameter, a object saves graph data.
+
+- Returns
+
+ Status code.
+
+## Model
+
+\#include <[model.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/model.h)>
+
+The Model class is used to define a MindSpore model, facilitating computational graph management.
+
+### Constructor and Destructor
+
+```cpp
+Model();
+~Model();
+```
+
+### Public Member Function
+
+#### Build
+
+```cpp
+Status Build(GraphCell graph, const std::shared_ptr &model_context);
+```
+
+Builds a model so that it can run on a device.
+
+- Parameters
+
+ - `graph`: `GraphCell` is a derivative of `Cell`. `Cell` is not available currently. `GraphCell` can be constructed from `Graph`, for example, `model.Build(GraphCell(graph), context)`.
+ - `model_context`: a [context](#context) used to store options during execution.
+
+- Returns
+
+ Status code.
+
+> Modifications to `model_context` after `Build` will no longer take effect.
+
+#### Predict
+
+```cpp
+Status Predict(const std::vector &inputs, std::vector *outputs);
+```
+
+Inference model.
+
+- Parameters
+
+ - `inputs`: a `vector` where model inputs are arranged in sequence.
+ - `outputs`: output parameter, which is a pointer to a `vector`. The model outputs are filled in the container in sequence.
+
+- Returns
+
+ Status code.
+
+#### GetInputs
+
+```cpp
+std::vector GetInputs();
+```
+
+Obtains all input tensors of the model.
+
+- Returns
+
+ The vector that includes all input tensors.
+
+#### GetInputByTensorName
+
+```cpp
+MSTensor GetInputByTensorName(const std::string &tensor_name);
+```
+
+Obtains the input tensor of the model by name.
+
+- Returns
+
+ The input tensor with the given name, if the name is not found, an invalid tensor is returned.
+
+#### GetOutputs
+
+```cpp
+std::vector GetOutputs();
+```
+
+Obtains all output tensors of the model.
+
+- Returns
+
+ A `vector` that includes all output tensors.
+
+#### GetOutputTensorNames
+
+```cpp
+std::vector GetOutputTensorNames();
+```
+
+Obtains names of all output tensors of the model.
+
+- Returns
+
+ A `vector` that includes names of all output tensors.
+
+#### GetOutputByTensorName
+
+```cpp
+MSTensor GetOutputByTensorName(const std::string &tensor_name);
+```
+
+Obtains the output tensor of the model by name.
+
+- Returns
+
+ The output tensor with the given name, if the name is not found, an invalid tensor is returned.
+
+#### Resize
+
+```cpp
+Status Resize(const std::vector &inputs, const std::vector> &dims);
+```
+
+Resizes the shapes of inputs.
+
+- Parameters
+
+ - `inputs`: a `vector` that includes all input tensors in order.
+ - `dims`: defines the new shapes of inputs, should be consistent with `inputs`.
+
+- Returns
+
+ Status code.
+
+#### CheckModelSupport
+
+```cpp
+static bool CheckModelSupport(enum DeviceType device_type, ModelType model_type);
+```
+
+Checks whether the type of device supports the type of model.
+
+- Parameters
+
+ - `device_type`: device type,options are `kMaliGPU`, `kAscend910`, etc.
+ - `model_type`: the Type of model file, options are `ModelType::kMindIR`, `ModelType::kOM`.
+
+- Returns
+
+ a bool value.
+
+## MSTensor
+
+\#include <[types.h](https://gitee.com/mindspore/mindspore/blob/r1.1/include/api/types.h)>
+
+The MSTensor class defines a tensor in MindSpore.
+
+### Constructor and Destructor
+
+```cpp
+MSTensor();
+explicit MSTensor(const std::shared_ptr &impl);
+MSTensor(const std::string &name, DataType type, const std::vector &shape, const void *data, size_t data_len);
+~MSTensor();
+```
+
+### Static Public Member Function
+
+#### CreateTensor
+
+```cpp
+MSTensor *CreateTensor(const std::string &name, DataType type, const std::vector &shape,
+ const void *data, size_t data_len) noexcept;
+```
+
+Creates a MSTensor object, whose data need to be copied before accessed by `Model`, must be used in pairs with `DestroyTensorPtr`.
+
+- Parameters
+
+ - `name`: the name of the `MSTensor`.
+ - `type`: the data type of the `MSTensor`.
+ - `shape`: the shape of the `MSTensor`.
+ - `data`: the data pointer that points to allocated memory.
+ - `data`: the length of the memory, in bytes.
+
+- Returns
+
+ An pointer of `MStensor`.
+
+#### CreateRefTensor
+
+```cpp
+MSTensor *CreateRefTensor(const std::string &name, DataType type, const std::vector &shape, void *data,
+ size_t data_len) noexcept;
+```
+
+Creates a MSTensor object, whose data can be directly accessed by `Model`, must be used in pairs with `DestroyTensorPtr`.
+
+- Parameters
+
+ - `name`: the name of the `MSTensor`.
+ - `type`: the data type of the `MSTensor`.
+ - `shape`: the shape of the `MSTensor`.
+ - `data`: the data pointer that points to allocated memory.
+ - `data`: the length of the memory, in bytes.
+
+- Returns
+
+ An pointer of `MStensor`.
+
+#### StringsToTensor
+
+```cpp
+MSTensor *StringsToTensor(const std::string &name, const std::vector &str);
+```
+
+Create a string type `MSTensor` object whose data can be accessed by `Model` only after being copied, must be used in pair with `DestroyTensorPtr`.
+
+- Parameters
+
+ - `name`: the name of the `MSTensor`.
+ - `str`:a `vector` container containing several strings.
+
+- Returns
+
+ An pointer of `MStensor`.
+
+#### TensorToStrings
+
+```cpp
+std::vector TensorToStrings(const MSTensor &tensor);
+```
+
+Parse the string type `MSTensor` object into strings.
+
+- Parameters
+
+ - `tensor`: a `MSTensor` object.
+
+- Returns
+
+ A `vector` container containing several strings.
+
+#### DestroyTensorPtr
+
+```cpp
+void DestroyTensorPtr(MSTensor *tensor) noexcept;
+```
+
+Destroy an object created by `Clone`, `StringsToTensor`, `CreateRefTensor` or `CreateTensor`. Do not use it to destroy `MSTensor` from other sources.
+
+- Parameters
+
+ - `tensor`: a pointer returned by `Clone`, `StringsToTensor`, `CreateRefTensor` or `CreateTensor`.
+
+### Public Member Functions
+
+#### Name
+
+```cpp
+std::string Name() const;
+```
+
+Obtains the name of the `MSTensor`.
+
+- Returns
+
+ The name of the `MSTensor`.
+
+#### DataType
+
+```cpp
+enum DataType DataType() const;
+```
+
+Obtains the data type of the `MSTensor`.
+
+- Returns
+
+ The data type of the `MSTensor`.
+
+#### Shape
+
+```cpp
+const std::vector &Shape() const;
+```
+
+Obtains the shape of the `MSTensor`.
+
+- Returns
+
+ A `vector` that contains the shape of the `MSTensor`.
+
+#### ElementNum
+
+```cpp
+int64_t ElementNum() const;
+```
+
+Obtains the number of elements of the `MSTensor`.
+
+- Returns
+
+ The number of elements of the `MSTensor`.
+
+#### Data
+
+```cpp
+std::shared_ptr Data() const;
+```
+
+Obtains a shared pointer to the copy of data of the `MSTensor`.
+
+- Returns
+
+ A shared pointer to the copy of data of the `MSTensor`.
+
+#### MutableData
+
+```cpp
+void *MutableData();
+```
+
+Obtains the pointer to the data of the `MSTensor`.
+
+- Returns
+
+ The pointer to the data of the `MSTensor`.
+
+#### DataSize
+
+```cpp
+size_t DataSize() const;
+```
+
+Obtains the length of the data of the `MSTensor`, in bytes.
+
+- Returns
+
+ The length of the data of the `MSTensor`, in bytes.
+
+#### IsDevice
+
+```cpp
+bool IsDevice() const;
+```
+
+Gets the boolean value that indicates whether the memory of `MSTensor` is on device.
+
+- Returns
+
+ The boolean value that indicates whether the memory of `MSTensor` is on device.
+
+#### Clone
+
+```cpp
+MSTensor *Clone() const;
+```
+
+Gets a deep copy of the `MSTensor`, must be used in pair with `DestroyTensorPtr`.
+
+- Returns
+
+ A pointer points to a deep copy of the `MSTensor`.
+
+#### operator==(std::nullptr_t)
+
+```cpp
+bool operator==(std::nullptr_t) const;
+```
+
+Gets the boolean value that indicates whether the `MSTensor` is valid.
+
+- Returns
+
+ The boolean value that indicates whether the `MSTensor` is valid.
## KernelCallBack
+\#include <[ms_tensor.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/lite/include/ms_tensor.h)>
+
```cpp
using KernelCallBack = std::function inputs, std::vector outputs, const CallBackParam &opInfo)>
```
@@ -14,6 +635,8 @@ A function wrapper. KernelCallBack defines the pointer for callback function.
## CallBackParam
+\#include <[ms_tensor.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/lite/include/ms_tensor.h)>
+
A **struct**. CallBackParam defines input arguments for callback function.
### Public Attributes
diff --git a/docs/api_cpp/source_en/session.md b/docs/api_cpp/source_en/session.md
index 370b09caaf5844fbb2a356e2a21cd380c4a11d34..a9a7aa0742786eb931a070d24e0b381a751b9697 100644
--- a/docs/api_cpp/source_en/session.md
+++ b/docs/api_cpp/source_en/session.md
@@ -218,7 +218,7 @@ Static method to create a LiteSession pointer. The returned LiteSession pointer
## TrainSession
-\#include <[train_session.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/lite/include/train_session.h)>
+\#include <[train_session.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/lite/include/train/train_session.h)>
Inherited from LiteSession, TrainSession defines the class that allows training the MindSpore model.
diff --git a/docs/api_cpp/source_zh_cn/api.md b/docs/api_cpp/source_zh_cn/api.md
deleted file mode 100644
index ed98393806abbb1699deb49af6f96b9ca2229973..0000000000000000000000000000000000000000
--- a/docs/api_cpp/source_zh_cn/api.md
+++ /dev/null
@@ -1,390 +0,0 @@
-# mindspore::api
-
-
-
-## Context
-
-\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
-
-Context类用于保存执行中的环境变量。
-
-### 静态公有成员函数
-
-#### Instance
-
-```cpp
-static Context &Instance();
-```
-
-获取MindSpore Context实例对象。
-
-### 公有成员函数
-
-#### GetDeviceTarget
-
-```cpp
-const std::string &GetDeviceTarget() const;
-```
-
-获取当前目标Device类型。
-
-- 返回值
-
- 当前DeviceTarget的类型。
-
-#### GetDeviceID
-
-```cpp
-uint32_t GetDeviceID() const;
-```
-
-获取当前Device ID。
-
-- 返回值
-
- 当前Device ID。
-
-#### SetDeviceTarget
-
-```cpp
-Context &SetDeviceTarget(const std::string &device_target);
-```
-
-配置目标Device。
-
-- 参数
-
- - `device_target`: 将要配置的目标Device,可选有`kDeviceTypeAscend310`、`kDeviceTypeAscend910`。
-
-- 返回值
-
- 该MindSpore Context实例对象。
-
-#### SetDeviceID
-
-```cpp
-Context &SetDeviceID(uint32_t device_id);
-```
-
-获取当前Device ID。
-
-- 参数
-
- - `device_id`: 将要配置的Device ID。
-
-- 返回值
-
- 该MindSpore Context实例对象。
-
-## Serialization
-
-\#include <[serialization.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/serialization.h)>
-
-Serialization类汇总了模型文件读写的方法。
-
-### 静态公有成员函数
-
-#### LoadModel
-
-- 参数
-
- - `file`: 模型文件路径。
- - `model_type`:模型文件类型,可选有`ModelType::kMindIR`、`ModelType::kOM`。
-
-- 返回值
-
- 保存图数据的对象。
-
-## Model
-
-\#include <[model.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/model.h)>
-
-Model定义了MindSpore中的模型,便于计算图管理。
-
-### 构造函数和析构函数
-
-```cpp
-Model(const GraphCell &graph);
-~Model();
-```
-
-`GraphCell`是`Cell`的一个派生,`Cell`目前没有开放使用。`GraphCell`可以由`Graph`构造,如`Model model(GraphCell(graph))`。
-
-### 公有成员函数
-
-#### Build
-
-```cpp
-Status Build(const std::map &options);
-```
-
-将模型编译至可在Device上运行的状态。
-
-- 参数
-
- - `options`: 模型编译选项,key为选项名,value为对应选项,支持的options有:
-
-| Key | Value |
-| --- | --- |
-| kModelOptionInsertOpCfgPath | [AIPP](https://support.huaweicloud.com/adevg-ms-atlas200dkappc32/atlasadm_01_0023.html)配置文件路径 |
-| kModelOptionInputFormat | 手动指定模型输入format,可选有`"NCHW"`,`"NHWC"`等 |
-| kModelOptionInputShape | 手动指定模型输入shape,如`"input_op_name1: n1,c2,h3,w4;input_op_name2: n4,c3,h2,w1"` |
-| kModelOptionOutputType | 手动指定模型输出type,如`"FP16"`,`"UINT8"`等,默认为`"FP32"` |
-| kModelOptionPrecisionMode | 模型精度模式,可选有`"force_fp16"`,`"allow_fp32_to_fp16"`,`"must_keep_origin_dtype"`或者`"allow_mix_precision"`,默认为`"force_fp16"` |
-| kModelOptionOpSelectImplMode | 算子选择模式,可选有`"high_performance"`和`"high_precision"`,默认为`"high_performance"` |
-
-- 返回值
-
- 状态码。
-
-#### Predict
-
-```cpp
-Status Predict(const std::vector &inputs, std::vector *outputs);
-```
-
-推理模型。
-
-- 参数
-
- - `inputs`: 模型输入按顺序排列的`vector`。
- - `outputs`: 输出参数,按顺序排列的`vector`的指针,模型输出会按顺序填入该容器。
-
-- 返回值
-
- 状态码。
-
-#### GetInputsInfo
-
-```cpp
-Status GetInputsInfo(std::vector *names, std::vector> *shapes, std::vector *data_types, std::vector *mem_sizes) const;
-```
-
-获取模型输入信息。
-
-- 参数
-
- - `names`: 可选输出参数,模型输入按顺序排列的`vector`的指针,模型输入的name会按顺序填入该容器,传入`nullptr`则表示不获取该属性。
- - `shapes`: 可选输出参数,模型输入按顺序排列的`vector`的指针,模型输入的shape会按顺序填入该容器,传入`nullptr`则表示不获取该属性。
- - `data_types`: 可选输出参数,模型输入按顺序排列的`vector`的指针,模型输入的数据类型会按顺序填入该容器,传入`nullptr`则表示不获取该属性。
- - `mem_sizes`: 可选输出参数,模型输入按顺序排列的`vector`的指针,模型输入的以字节为单位的内存长度会按顺序填入该容器,传入`nullptr`则表示不获取该属性。
-
-- 返回值
-
- 状态码。
-
-#### GetOutputsInfo
-
-```cpp
-Status GetOutputsInfo(std::vector *names, std::vector> *shapes, std::vector *data_types, std::vector *mem_sizes) const;
-```
-
-获取模型输出信息。
-
-- 参数
-
- - `names`: 可选输出参数,模型输出按顺序排列的`vector`的指针,模型输出的name会按顺序填入该容器,传入`nullptr`则表示不获取该属性。
- - `shapes`: 可选输出参数,模型输出按顺序排列的`vector`的指针,模型输出的shape会按顺序填入该容器,传入`nullptr`则表示不获取该属性。
- - `data_types`: 可选输出参数,模型输出按顺序排列的`vector`的指针,模型输出的数据类型会按顺序填入该容器,传入`nullptr`则表示不获取该属性。
- - `mem_sizes`: 可选输出参数,模型输出按顺序排列的`vector`的指针,模型输出的以字节为单位的内存长度会按顺序填入该容器,传入`nullptr`则表示不获取该属性。
-
-- 返回值
-
- 状态码。
-
-## Tensor
-
-\#include <[types.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/types.h)>
-
-### 构造函数和析构函数
-
-```cpp
-Tensor();
-Tensor(const std::string &name, DataType type, const std::vector &shape, const void *data, size_t data_len);
-~Tensor();
-```
-
-### 静态公有成员函数
-
-#### GetTypeSize
-
-```cpp
-static int GetTypeSize(api::DataType type);
-```
-
-获取数据类型的内存长度,以字节为单位。
-
-- 参数
-
- - `type`: 数据类型。
-
-- 返回值
-
- 内存长度,单位是字节。
-
-### 公有成员函数
-
-#### Name
-
-```cpp
-const std::string &Name() const;
-```
-
-获取Tensor的名字。
-
-- 返回值
-
- Tensor的名字。
-
-#### DataType
-
-```cpp
-api::DataType DataType() const;
-```
-
-获取Tensor的数据类型。
-
-- 返回值
-
- Tensor的数据类型。
-
-#### Shape
-
-```cpp
-const std::vector &Shape() const;
-```
-
-获取Tensor的Shape。
-
-- 返回值
-
- Tensor的Shape。
-
-#### SetName
-
-```cpp
-void SetName(const std::string &name);
-```
-
-设置Tensor的名字。
-
-- 参数
-
- - `name`: 将要设置的name。
-
-#### SetDataType
-
-```cpp
-void SetDataType(api::DataType type);
-```
-
-设置Tensor的数据类型。
-
-- 参数
-
- - `type`: 将要设置的type。
-
-#### SetShape
-
-```cpp
-void SetShape(const std::vector &shape);
-```
-
-设置Tensor的Shape。
-
-- 参数
-
- - `shape`: 将要设置的shape。
-
-#### Data
-
-```cpp
-const void *Data() const;
-```
-
-获取Tensor中的数据的const指针。
-
-- 返回值
-
- 指向Tensor中的数据的const指针。
-
-#### MutableData
-
-```cpp
-void *MutableData();
-```
-
-获取Tensor中的数据的指针。
-
-- 返回值
-
- 指向Tensor中的数据的指针。
-
-#### DataSize
-
-```cpp
-size_t DataSize() const;
-```
-
-获取Tensor中的数据的以字节为单位的内存长度。
-
-- 返回值
-
- Tensor中的数据的以字节为单位的内存长度。
-
-#### ResizeData
-
-```cpp
-bool ResizeData(size_t data_len);
-```
-
-重新调整Tensor的内存大小。
-
-- 参数
-
- - `data_len`: 调整后的内存字节数。
-
-- 返回值
-
- bool值表示是否成功。
-
-#### SetData
-
-```cpp
-bool SetData(const void *data, size_t data_len);
-```
-
-重新调整Tensor的内存数据。
-
-- 参数
-
- - `data`: 源数据内存地址。
- - `data_len`: 源数据内存长度。
-
-- 返回值
-
- bool值表示是否成功。
-
-#### ElementNum
-
-```cpp
-int64_t ElementNum() const;
-```
-
-获取Tensor中元素的个数。
-
-- 返回值
-
- Tensor中的元素个数
-
-#### Clone
-
-```cpp
-Tensor Clone() const;
-```
-
-拷贝一份自身的副本。
-
-- 返回值
-
- 深拷贝的副本。
diff --git a/docs/api_cpp/source_zh_cn/class_list.md b/docs/api_cpp/source_zh_cn/class_list.md
index 2934485fc023b42c63b20fca44a70cfe310e96c3..c51cf090214488e21af99fbac519749c4063f860 100644
--- a/docs/api_cpp/source_zh_cn/class_list.md
+++ b/docs/api_cpp/source_zh_cn/class_list.md
@@ -21,8 +21,7 @@ MindSpore中的类定义及其所属命名空间和描述:
| 命名空间 | 类 | 描述 |
| --- | --- | --- |
-| mindspore::api | [Context](https://www.mindspore.cn/doc/api_cpp/zh-CN/master/api.html#context) | Context用于保存执行期间的环境变量。 |
-| mindspore::api | [Serialization](https://www.mindspore.cn/doc/api_cpp/zh-CN/master/api.html#serialization) | Serialization汇总了模型文件读写的方法。 |
-| mindspore::api | [Model](https://www.mindspore.cn/doc/api_cpp/zh-CN/master/api.html#model) | Model定义了MindSpore中的模型,便于计算图管理。 |
-| mindspore::api | [Tensor](https://www.mindspore.cn/doc/api_cpp/zh-CN/master/api.html#tensor) | Tensor定义了MindSpore中的张量。 |
-| mindspore::api | [Buffer](https://www.mindspore.cn/doc/api_cpp/zh-CN/master/api.html#buffer) | Buffer管理了一段内存空间。 |
+| mindspore | [Context](https://www.mindspore.cn/doc/api_cpp/zh-CN/master/mindspore.html#context) | Context用于保存执行期间的环境变量。 |
+| mindspore | [Serialization](https://www.mindspore.cn/doc/api_cpp/zh-CN/master/mindspore.html#serialization) | Serialization汇总了模型文件读写的方法。 |
+| mindspore | [Model](https://www.mindspore.cn/doc/api_cpp/zh-CN/master/mindspore.html#model) | Model定义了MindSpore中的模型,便于计算图管理。 |
+| mindspore | [MSTensor](https://www.mindspore.cn/doc/api_cpp/zh-CN/master/tensor.html#mstensor) | MSTensor定义了MindSpore中的张量。 |
diff --git a/docs/api_cpp/source_zh_cn/dataset.md b/docs/api_cpp/source_zh_cn/dataset.md
index 730c9e3b59f5fd625d69877579c0d8837c930392..1e31bcdc2e3c377352a4abdd0894fe7ab4214300 100644
--- a/docs/api_cpp/source_zh_cn/dataset.md
+++ b/docs/api_cpp/source_zh_cn/dataset.md
@@ -2,6 +2,88 @@
+## Execute
+
+\#include <[execute.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/ccsrc/minddata/dataset/include/execute.h)>
+
+```cpp
+// shared_ptr
+Execute::Execute(std::shared_ptr op, MapTargetDevice deviceType);
+Execute::Execute(std::vector> ops, MapTargetDevice deviceType);
+
+// normal pointer
+Execute::Execute(std::reference_wrapper op, MapTargetDevice deviceType);
+Execute::Execute(std::vector> ops, MapTargetDevice deviceType)
+
+// reference_wrapper
+Execute::Execute(TensorTransform *op, MapTargetDevice deviceType);
+Execute::Execute(std::vector ops, MapTargetDevice deviceType);
+```
+
+Transform(图像、文本)变换算子Eager模式执行类。支持多种构造函数形式,包括智能指针,普通指针以及引用封装。
+
+- 参数
+
+ - `op`: 指定单个使用的变换算子。
+ - `ops`: 指定一个列表,包含多个使用的变换算子。
+ - `deviceType`: 指定运行硬件设备,选项为CPU,GPU以及Ascend 310。
+
+```cpp
+Status operator()(const mindspore::MSTensor &input, mindspore::MSTensor *output);
+```
+
+Eager模式执行接口。
+
+- 参数
+
+ - `input`: 待变换的Tensor张量。
+ - `output`: 变换后的Tensor张量。
+
+- 返回值
+
+ 返回一个状态码指示执行变换是否成功。
+
+```cpp
+std::string Execute::AippCfgGenerator();
+```
+
+与Dvpp相关的Aipp配置文件生成器。
+该接口在`deviceType = kAscend310`时生效,依据数据预处理算子的参数自动生成Ascend 310内置Aipp模块推理配置文件。
+
+- 参数
+
+ 无。
+
+- 返回值
+
+ 返回一个`string`表示Aipp配置文件的系统路径。
+
+## Dvpp模块
+
+Dvpp模块为Ascend 310芯片内置硬件解码器,相较于CPU拥有对图形处理更强劲的性能。支持JPEG图片的解码缩放等基础操作。
+
+- 定义`execute`对象时,若设置`deviceType = kAscend310`则会调用Dvpp模块执行数据预处理算子。
+- 当前Dvpp模块支持`Decode()`, `Resize()`, `CenterCrop()`, `Normalize()`。
+- 上述Dvpp算子与同功能CPU算子共用统一API,仅以`deviceType`区分。
+
+示例代码:
+
+```cpp
+// Define dvpp transforms
+std::vector crop_paras = {224, 224};
+std::vector resize_paras = {256};
+std::vector mean = {0.485 * 255, 0.456 * 255, 0.406 * 255};
+std::vector std = {0.229 * 255, 0.224 * 255, 0.225 * 255};
+
+std::shared_ptr decode(new vision::Decode());
+std::shared_ptr resize(new vision::Resize(resize_paras));
+std::shared_ptr centercrop(new vision::CenterCrop(crop_paras));
+std::shared_ptr normalize(new vision::Normalize(mean, std));
+
+std::vector> trans_list = {decode, resize, centercrop, normalize};
+mindspore::dataset::Execute Transform(trans_list, MapTargetDevice::kAscend310);
+```
+
## ResizeBilinear
\#include <[image_process.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.h)>
diff --git a/docs/api_cpp/source_zh_cn/index.rst b/docs/api_cpp/source_zh_cn/index.rst
index b5f76d3c78fd947026a99ea4a9ba8afd91355ed8..779317bee1f0397ac1c5a78905b31b236f33d4f8 100644
--- a/docs/api_cpp/source_zh_cn/index.rst
+++ b/docs/api_cpp/source_zh_cn/index.rst
@@ -12,7 +12,6 @@ MindSpore C++ API
class_list
mindspore
- api
dataset
vision
lite
diff --git a/docs/api_cpp/source_zh_cn/lite_cpp_example.rst b/docs/api_cpp/source_zh_cn/lite_cpp_example.rst
index daad77989b26df8436b4c1b1f19430aaa622309c..b406fdd6803f8742279afb6287e1fb31ec630857 100644
--- a/docs/api_cpp/source_zh_cn/lite_cpp_example.rst
+++ b/docs/api_cpp/source_zh_cn/lite_cpp_example.rst
@@ -3,6 +3,7 @@
.. toctree::
:maxdepth: 1
+
极简Demo
实现一个图像分类应用
高阶用法
\ No newline at end of file
diff --git a/docs/api_cpp/source_zh_cn/mindspore.md b/docs/api_cpp/source_zh_cn/mindspore.md
index f6195d8368dad856bb40c6c9e5e8f38646716c09..045651186e81c11e0747329f9f0458dd7d4e2411 100644
--- a/docs/api_cpp/source_zh_cn/mindspore.md
+++ b/docs/api_cpp/source_zh_cn/mindspore.md
@@ -2,10 +2,631 @@
-\#include <[ms_tensor.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/lite/include/ms_tensor.h)>
+## Context
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+Context类用于保存执行中的环境变量。
+
+### 公有成员函数
+
+#### SetThreadNum
+
+```cpp
+void SetThreadNum(int32_t thread_num);
+```
+
+设置运行时的线程数,该选项仅MindSpore Lite有效。
+
+- 参数
+
+ - `thread_num`: 运行时的线程数。
+
+#### GetThreadNum
+
+```cpp
+int32_t GetThreadNum() const;
+```
+
+获取当前线程数设置。
+
+- 返回值
+
+ 当前线程数设置。
+
+#### SetAllocator
+
+```cpp
+void SetAllocator(const std::shared_ptr &allocator);
+```
+
+设置Allocator,Allocator定义了用于动态内存分配和释放的内存池,该选项仅MindSpore lite有效。
+
+- 参数
+
+ - `allocator`: Allocator指针。
+
+#### GetAllocator
+
+```cpp
+std::shared_ptr GetAllocator() const;
+```
+
+获取当前Allocator设置。
+
+- 返回值
+
+ 当前Allocator的指针。
+
+#### MutableDeviceInfo
+
+```cpp
+std::vector> &MutableDeviceInfo();
+```
+
+修改该context下的[DeviceInfoContext](#deviceinfocontext)数组,仅mindspore lite支持数组中有多个成员是异构场景。
+
+- 返回值
+
+ 存储DeviceInfoContext的vector的引用。
+
+## DeviceInfoContext
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+DeviceInfoContext类定义不同硬件设备的环境信息。
+
+### 公有成员函数
+
+#### GetDeviceType
+
+```cpp
+virtual enum DeviceType GetDeviceType() const = 0
+```
+
+获取该DeviceInfoContext的类型。
+
+- 返回值
+
+ 该DeviceInfoContext的类型。
+
+ ```cpp
+ enum DeviceType {
+ kCPU = 0,
+ kMaliGPU,
+ kNvidiaGPU,
+ kKirinNPU,
+ kAscend910,
+ kAscend310,
+ // add new type here
+ kInvalidDeviceType = 100,
+ };
+ ```
+
+#### Cast
+
+```cpp
+template std::shared_ptr Cast();
+```
+
+在打开`-fno-rtti`编译选项的情况下提供类似RTTI的功能,将DeviceInfoContext转换为`T`类型的指针,若转换失败返回`nullptr`。
+
+- 返回值
+
+ 转换后`T`类型的指针,若转换失败则为`nullptr`。
+
+## CPUDeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+派生自[DeviceInfoContext](#deviceinfocontext),模型运行在CPU上的配置,仅mindspore lite支持该选项。
+
+### 公有成员函数
+
+| 函数 | 说明 |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetThreadAffinity(int mode)` | 设置线程亲和性模式
- `mode`: 0:无亲和性, 1:大核优先, 2:小核优先。 |
+| `int GetThreadAffinity() const` | - 返回值: 已配置的线程亲和性模式 |
+| `void SetEnableFP16(bool is_fp16)` | 用于指定是否以FP16精度进行推理
- `is_fp16`: 是否以FP16精度进行推理 |
+| `bool GetEnableFP16() const` | - 返回值: 已配置的精度模式 |
+
+## MaliGPUDeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+派生自[DeviceInfoContext](#deviceinfocontext),模型运行在GPU上的配置,仅mindspore lite支持该选项。
+
+### 公有成员函数
+
+| 函数 | 说明 |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetEnableFP16(bool is_fp16)` | 用于指定是否以FP16精度进行推理
- `is_fp16`: 是否以FP16精度进行推理 |
+| `bool GetEnableFP16() const` | - 返回值: 已配置的精度模式 |
+
+## KirinNPUDeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+派生自[DeviceInfoContext](#deviceinfocontext),模型运行在NPU上的配置,仅mindspore lite支持该选项。
+
+### 公有成员函数
+
+| 函数 | 说明 |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetFrequency(int frequency)` | 用于指定NPU频率
- `frequency`: 设置为1(低功耗)、2(均衡)、3(高性能)、4(极致性能),默认为3 |
+| `int GetFrequency() const` | - 返回值: 已配置的NPU频率模式 |
+
+## NvidiaGPUDeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+派生自[DeviceInfoContext](#deviceinfocontext),模型运行在GPU上的配置,mindspore lite不支持该选项。
+
+### 公有成员函数
+
+| 函数 | 说明 |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetDeviceID(uint32_t device_id)` | 用于指定设备ID
- `device_id`: 设备ID |
+| `uint32_t GetDeviceID() const` | - 返回值: 已配置的设备ID |
+
+## Ascend910DeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+派生自[DeviceInfoContext](#deviceinfocontext),模型运行在Ascend910上的配置,mindspore lite不支持该选项。
+
+### 公有成员函数
+
+| 函数 | 说明 |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetDeviceID(uint32_t device_id)` | 用于指定设备ID
- `device_id`: 设备ID |
+| `uint32_t GetDeviceID() const` | - 返回值: 已配置的设备ID |
+
+## Ascend310DeviceInfo
+
+\#include <[context.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/context.h)>
+
+派生自[DeviceInfoContext](#deviceinfocontext),模型运行在Ascend310上的配置,mindspore lite不支持该选项。
+
+### 公有成员函数
+
+| 函数 | 说明 |
+| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| `void SetDeviceID(uint32_t device_id)` | 用于指定设备ID
- `device_id`: 设备ID |
+| `uint32_t GetDeviceID() const` | - 返回值: 已配置的设备ID |
+| `void SetInsertOpConfigPath(const std::string &cfg_path)` | 模型插入[AIPP](https://support.huaweicloud.com/adevg-ms-atlas200dkappc32/atlasadm_01_0023.html)算子
- `cfg_path`: [AIPP](https://support.huaweicloud.com/adevg-ms-atlas200dkappc32/atlasadm_01_0023.html)配置文件路径 |
+| `std::string GetInsertOpConfigPath()` | - 返回值: 已配置的[AIPP](https://support.huaweicloud.com/adevg-ms-atlas200dkappc32/atlasadm_01_0023.html) |
+| `void SetInputFormat(const std::string &format)` | 指定模型输入formatt
- `format`: 可选有`"NCHW"`,`"NHWC"`等 |
+| `std::string GetInputFormat()` | - 返回值: 已配置模型输入format |
+| `void SetInputShape(const std::string &shape)` | 指定模型输入shape
- `shape`: 如`"input_op_name1:1,2,3,4;input_op_name2:4,3,2,1"` |
+| `std::string GetInputShape()` | - 返回值: 已配置模型输入shape |
+| `void SetOutputType(enum DataType output_type)` | 指定模型输出type
- `output_type`: 仅支持uint8、fp16和fp32 |
+| `enum DataType GetOutputType()` | - 返回值: 已配置模型输出type |
+| `void SetPrecisionMode(const std::string &precision_mode)` | 配置模型精度模式
- `precision_mode`: 可选有`"force_fp16"`,`"allow_fp32_to_fp16"`,`"must_keep_origin_dtype"`或者`"allow_mix_precision"`,默认为`"force_fp16"` |
+| `std::string GetPrecisionMode(t)` | - 返回值: 已配置模型精度模式 |
+| `void SetOpSelectImplMode(const std::string &op_select_impl_mode)` | 配置算子选择模式
- `op_select_impl_mode`: 可选有`"high_performance"`和`"high_precision"`,默认为`"high_performance"` |
+| `std::string GetOpSelectImplMode()` | - 返回值: 已配置算子选择模式 |
+
+## Serialization
+
+\#include <[serialization.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/serialization.h)>
+
+Serialization类汇总了模型文件读写的方法。
+
+### 静态公有成员函数
+
+#### Load
+
+从文件加载模型,MindSpore Lite未提供此功能。
+
+```cpp
+Status Load(const std::string &file, ModelType model_type, Graph *graph);
+```
+
+- 参数
+
+ - `file`: 模型文件路径。
+ - `model_type`:模型文件类型,可选有`ModelType::kMindIR`、`ModelType::kOM`。
+ - `graph`:输出参数,保存图数据的对象。
+
+- 返回值
+
+ 状态码类`Status`对象,可以使用其公有函数`StatusCode`或`ToString`函数来获取具体错误码及错误信息。
+
+#### Load
+
+从内存缓冲区加载模型。
+
+```cpp
+Status Load(const void *model_data, size_t data_size, ModelType model_type, Graph *graph);
+```
+
+- 参数
+
+ - `model_data`:模型数据指针。
+ - `data_size`:模型数据字节数。
+ - `model_type`:模型文件类型,可选有`ModelType::kMindIR`、`ModelType::kOM`。
+ - `graph`:输出参数,保存图数据的对象。
+
+- 返回值
+
+ 状态码类`Status`对象,可以使用其公有函数`StatusCode`或`ToString`函数来获取具体错误码及错误信息。
+
+## Model
+
+\#include <[model.h](https://gitee.com/mindspore/mindspore/blob/master/include/api/model.h)>
+
+Model定义了MindSpore中的模型,便于计算图管理。
+
+### 构造函数和析构函数
+
+```cpp
+Model();
+~Model();
+```
+
+### 公有成员函数
+
+#### Build
+
+```cpp
+Status Build(GraphCell graph, const std::shared_ptr &model_context);
+```
+
+将模型编译至可在Device上运行的状态。
+
+- 参数
+
+ - `graph`: `GraphCell`是`Cell`的一个派生,`Cell`目前没有开放使用。`GraphCell`可以由`Graph`构造,如`model.Build(GraphCell(graph), context)`。
+ - `model_context`: 模型[Context](#context)。
+
+- 返回值
+
+ 状态码类`Status`对象,可以使用其公有函数`StatusCode`或`ToString`函数来获取具体错误码及错误信息。
+
+> `Build`之后对`model_context`的其他修改不再生效。
+
+#### Predict
+
+```cpp
+Status Predict(const std::vector &inputs, std::vector *outputs);
+```
+
+推理模型。
+
+- 参数
+
+ - `inputs`: 模型输入按顺序排列的`vector`。
+ - `outputs`: 输出参数,按顺序排列的`vector`的指针,模型输出会按顺序填入该容器。
+
+- 返回值
+
+ 状态码类`Status`对象,可以使用其公有函数`StatusCode`或`ToString`函数来获取具体错误码及错误信息。
+
+#### GetInputs
+
+```cpp
+std::vector GetInputs();
+```
+
+获取模型所有输入张量。
+
+- 返回值
+
+ 包含模型所有输入张量的容器类型变量。
+
+#### GetInputByTensorName
+
+```cpp
+MSTensor GetInputByTensorName(const std::string &tensor_name);
+```
+
+获取模型指定名字的输入张量。
+
+- 返回值
+
+ 指定名字的输入张量,如果该名字不存在则返回非法张量。
+
+#### GetOutputs
+
+```cpp
+std::vector GetOutputs();
+```
+
+获取模型所有输出张量。
+
+- 返回值
+
+ 包含模型所有输出张量的容器类型变量。
+
+#### GetOutputTensorNames
+
+```cpp
+std::vector GetOutputTensorNames();
+```
+
+获取模型所有输出张量的名字。
+
+- 返回值
+
+ 包含模型所有输出张量名字的容器类型变量。
+
+#### GetOutputByTensorName
+
+```cpp
+MSTensor GetOutputByTensorName(const std::string &tensor_name);
+```
+
+获取模型指定名字的输出张量。
+
+- 返回值
+
+ 指定名字的输出张量,如果该名字不存在则返回非法张量。
+
+#### Resize
+
+```cpp
+Status Resize(const std::vector &inputs, const std::vector> &dims);
+```
+
+调整已编译模型的输入形状。
+
+- 参数
+
+ - `inputs`: 模型输入按顺序排列的`vector`。
+ - `dims`: 输入形状,按输入顺序排列的由形状组成的`vector`,模型会按顺序依次调整张量形状。
+
+- 返回值
+
+ 状态码类`Status`对象,可以使用其公有函数`StatusCode`或`ToString`函数来获取具体错误码及错误信息。
+
+#### CheckModelSupport
+
+```cpp
+static bool CheckModelSupport(enum DeviceType device_type, ModelType model_type);
+```
+
+检查设备是否支持该模型。
+
+- 参数
+
+ - `device_type`: 设备类型,例如`kMaliGPU`。
+ - `model_type`: 模型类型,例如`MindIR`。
+
+- 返回值
+
+ 状态码。
+
+## MSTensor
+
+\#include <[types.h](https://gitee.com/mindspore/mindspore/blob/r1.1/include/api/types.h)>
+
+`MSTensor`定义了MindSpore中的张量。
+
+### 构造函数和析构函数
+
+```cpp
+MSTensor();
+explicit MSTensor(const std::shared_ptr &impl);
+MSTensor(const std::string &name, DataType type, const std::vector &shape, const void *data, size_t data_len);
+~MSTensor();
+```
+
+### 静态公有成员函数
+
+#### CreateTensor
+
+```cpp
+MSTensor *CreateTensor(const std::string &name, DataType type, const std::vector &shape,
+ const void *data, size_t data_len) noexcept;
+```
+
+创建一个`MSTensor`对象,其数据需复制后才能由`Model`访问,必须与`DestroyTensorPtr`成对使用。
+
+- 参数
+
+ - `name`: 名称。
+ - `type`:数据类型。
+ - `shape`:形状。
+ - `data`:数据指针,指向一段已开辟的内存。
+ - `data`:数据长度,以字节为单位。
+
+- 返回值
+
+ `MStensor`指针。
+
+#### CreateRefTensor
+
+```cpp
+MSTensor *CreateRefTensor(const std::string &name, DataType type, const std::vector &shape, void *data,
+ size_t data_len) noexcept;
+```
+
+创建一个`MSTensor`对象,其数据可以直接由`Model`访问,必须与`DestroyTensorPtr`成对使用。
+
+- 参数
+
+ - `name`: 名称。
+ - `type`:数据类型。
+ - `shape`:形状。
+ - `data`:数据指针,指向一段已开辟的内存。
+ - `data`:数据长度,以字节为单位。
+
+- 返回值
+
+ `MStensor`指针。
+
+#### StringsToTensor
+
+```cpp
+MSTensor *StringsToTensor(const std::string &name, const std::vector &str);
+```
+
+创建一个字符串类型的`MSTensor`对象,其数据需复制后才能由`Model`访问,必须与`DestroyTensorPtr`成对使用。
+
+- 参数
+
+ - `name`: 名称。
+ - `str`:装有若干个字符串的`vector`容器。
+
+- 返回值
+
+ `MStensor`指针。
+
+#### TensorToStrings
+
+```cpp
+std::vector TensorToStrings(const MSTensor &tensor);
+```
+
+将字符串类型的`MSTensor`对象解析为字符串。
+
+- 参数
+
+ - `tensor`: 张量对象。
+
+- 返回值
+
+ 装有若干个字符串的`vector`容器。
+
+#### DestroyTensorPtr
+
+```cpp
+void DestroyTensorPtr(MSTensor *tensor) noexcept;
+```
+
+销毁一个由`Clone`、`StringsToTensor`、`CreateRefTensor`或`CreateTensor`所创建的对象,请勿用于销毁其他来源的`MSTensor`。
+
+- 参数
+
+ - `tensor`: 由`Clone`、`StringsToTensor`、`CreateRefTensor`或`CreateTensor`返回的指针。
+
+### 公有成员函数
+
+#### Name
+
+```cpp
+std::string Name() const;
+```
+
+获取`MSTensor`的名字。
+
+- 返回值
+
+ `MSTensor`的名字。
+
+#### DataType
+
+```cpp
+enum DataType DataType() const;
+```
+
+获取`MSTensor`的数据类型。
+
+- 返回值
+
+ `MSTensor`的数据类型。
+
+#### Shape
+
+```cpp
+const std::vector &Shape() const;
+```
+
+获取`MSTensor`的Shape。
+
+- 返回值
+
+ `MSTensor`的Shape。
+
+#### ElementNum
+
+```cpp
+int64_t ElementNum() const;
+```
+
+获取`MSTensor`的元素个数。
+
+- 返回值
+
+ `MSTensor`的元素个数。
+
+#### Data
+
+```cpp
+std::shared_ptr Data() const;
+```
+
+获取指向`MSTensor`中的数据拷贝的智能指针。
+
+- 返回值
+
+ 指向`MSTensor`中的数据拷贝的智能指针。
+
+#### MutableData
+
+```cpp
+void *MutableData();
+```
+
+获取`MSTensor`中的数据的指针。
+
+- 返回值
+
+ 指向`MSTensor`中的数据的指针。
+
+#### DataSize
+
+```cpp
+size_t DataSize() const;
+```
+
+获取`MSTensor`中的数据的以字节为单位的内存长度。
+
+- 返回值
+
+ `MSTensor`中的数据的以字节为单位的内存长度。
+
+#### IsDevice
+
+```cpp
+bool IsDevice() const;
+```
+
+判断`MSTensor`中是否在设备上。
+
+- 返回值
+
+ `MSTensor`中是否在设备上。
+
+#### Clone
+
+```cpp
+MSTensor *Clone() const;
+```
+
+拷贝一份自身的副本。
+
+- 返回值
+
+ 指向深拷贝副本的指针,必须与`DestroyTensorPtr`成对使用。
+
+#### operator==(std::nullptr_t)
+
+```cpp
+bool operator==(std::nullptr_t) const;
+```
+
+判断`MSTensor`是否合法。
+
+- 返回值
+
+ `MSTensor`是否合法。
## KernelCallBack
+\#include <[ms_tensor.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/lite/include/ms_tensor.h)>
+
```cpp
using KernelCallBack = std::function inputs, std::vector outputs, const CallBackParam &opInfo)>
```
@@ -14,6 +635,8 @@ using KernelCallBack = std::function inputs
## CallBackParam
+\#include <[ms_tensor.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/lite/include/ms_tensor.h)>
+
一个结构体。CallBackParam定义了回调函数的输入参数。
### 公有属性
diff --git a/docs/api_cpp/source_zh_cn/session.md b/docs/api_cpp/source_zh_cn/session.md
index 25a7a08d5d26493c8655f40eb3bc5a505cd02982..e767c2d53ce4a3eaa202970167ba4f1b14a82b2d 100644
--- a/docs/api_cpp/source_zh_cn/session.md
+++ b/docs/api_cpp/source_zh_cn/session.md
@@ -216,7 +216,7 @@ static LiteSession *CreateSession(const char *model_buf, size_t size, const lite
## TrainSession
-\#include <[ltrain_session.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/lite/include/train_session.h)>
+\#include <[ltrain_session.h](https://gitee.com/mindspore/mindspore/blob/master/mindspore/lite/include/train/train_session.h)>
继承于类 LiteSession,用于训练模型。
diff --git a/docs/api_python/source_en/mindquantum/mindquantum.rst b/docs/api_python/source_en/mindquantum/mindquantum.rst
index 41b1ba084d82b09ee602b7eca961188df3d47fd2..2464675921f5451b687aaa86b624ba3359230f55 100644
--- a/docs/api_python/source_en/mindquantum/mindquantum.rst
+++ b/docs/api_python/source_en/mindquantum/mindquantum.rst
@@ -33,22 +33,22 @@ The functional gates are the pre-instantiated quantum gates, which can be used d
* - functional
- gates
- * - mindspore.gate.CNOT
- - :class:`mindspore.gate.CNOTGate`
- * - mindspore.gate.I
- - :class:`mindspore.gate.IGate`
- * - mindspore.gate.H
- - :class:`mindspore.gate.HGate`
- * - mindspore.gate.S
- - :class:`mindspore.gate.PhaseShift` (numpy.pi/2)
- * - mindspore.gate.SWAP
- - :class:`mindspore.gate.SWAPGate`
- * - mindspore.gate.X
- - :class:`mindspore.gate.XGate`
- * - mindspore.gate.Y
- - :class:`mindspore.gate.YGate`
- * - mindspore.gate.Z
- - :class:`mindspore.gate.ZGate`
+ * - mindquantum.gate.CNOT
+ - :class:`mindquantum.gate.CNOTGate`
+ * - mindquantum.gate.I
+ - :class:`mindquantum.gate.IGate`
+ * - mindquantum.gate.H
+ - :class:`mindquantum.gate.HGate`
+ * - mindquantum.gate.S
+ - :class:`mindquantum.gate.PhaseShift` (numpy.pi/2)
+ * - mindquantum.gate.SWAP
+ - :class:`mindquantum.gate.SWAPGate`
+ * - mindquantum.gate.X
+ - :class:`mindquantum.gate.XGate`
+ * - mindquantum.gate.Y
+ - :class:`mindquantum.gate.YGate`
+ * - mindquantum.gate.Z
+ - :class:`mindquantum.gate.ZGate`
mindquantum.nn
--------------
diff --git a/docs/api_python/source_en/mindspore/mindspore.numpy.rst b/docs/api_python/source_en/mindspore/mindspore.numpy.rst
index dc42a370839ce164f6833f1f0995ed030d328ff5..8552ece80213ea079cda04370e69268c5af04fd7 100644
--- a/docs/api_python/source_en/mindspore/mindspore.numpy.rst
+++ b/docs/api_python/source_en/mindspore/mindspore.numpy.rst
@@ -20,6 +20,7 @@ Array Generation
mindspore.numpy.array
mindspore.numpy.asarray
mindspore.numpy.asfarray
+ mindspore.numpy.copy
mindspore.numpy.empty
mindspore.numpy.empty_like
mindspore.numpy.eye
diff --git a/docs/api_python/source_en/mindspore/mindspore.ops.rst b/docs/api_python/source_en/mindspore/mindspore.ops.rst
index 30168edb5981eed0ddd70832137ab16e92f3be82..08496ed3d50858940220d06513727b7124b2bf07 100644
--- a/docs/api_python/source_en/mindspore/mindspore.ops.rst
+++ b/docs/api_python/source_en/mindspore/mindspore.ops.rst
@@ -70,8 +70,6 @@ The functional operators are the pre-instantiated Primitive operators, which can
- :class:`mindspore.ops.Primitive` ('bool_or')
* - mindspore.ops.cast
- :class:`mindspore.ops.Cast`
- * - mindspore.ops.control_depend
- - :class:`mindspore.ops.ControlDepend`
* - mindspore.ops.distribute
- :class:`mindspore.ops.Primitive` ('distribute')
* - mindspore.ops.dtype
diff --git a/docs/api_python/source_en/mindspore/operations.rst b/docs/api_python/source_en/mindspore/operations.rst
index 4dcc5a82e9ceef9abea6daa955afa0c1f592ebc9..1a82cc29f85a602b4ab31098fb4b5d1ab51371ee 100644
--- a/docs/api_python/source_en/mindspore/operations.rst
+++ b/docs/api_python/source_en/mindspore/operations.rst
@@ -51,8 +51,6 @@ Neural Network Operators
mindspore.ops.FastGeLU
mindspore.ops.Flatten
mindspore.ops.FloorMod
- mindspore.ops.FusedBatchNorm
- mindspore.ops.FusedBatchNormEx
mindspore.ops.FusedSparseAdam
mindspore.ops.FusedSparseLazyAdam
mindspore.ops.FusedSparseProximalAdagrad
@@ -302,16 +300,6 @@ Common Operators
mindspore.ops.ReduceOp
mindspore.ops.ReduceScatter
-Control Flows
-^^^^^^^^^^^^^
-
-.. msplatformautosummary::
- :toctree: ops
- :nosignatures:
- :template: classtemplate.rst
-
- mindspore.ops.ControlDepend
-
Debug Operators
^^^^^^^^^^^^^^^
@@ -347,6 +335,43 @@ Random Operators
mindspore.ops.UniformInt
mindspore.ops.UniformReal
+Sponge Operators
+^^^^^^^^^^^^^^^^
+
+.. msplatformautosummary::
+ :toctree: ops
+ :nosignatures:
+ :template: classtemplate.rst
+
+ mindspore.ops.AngleAtomEnergy
+ mindspore.ops.AngleEnergy
+ mindspore.ops.AngleForce
+ mindspore.ops.AngleForceWithAtomEnergy
+ mindspore.ops.BondAtomEnergy
+ mindspore.ops.BondEnergy
+ mindspore.ops.BondForce
+ mindspore.ops.BondForceWithAtomEnergy
+ mindspore.ops.BondForceWithAtomVirial
+ mindspore.ops.DihedralAtomEnergy
+ mindspore.ops.DihedralEnergy
+ mindspore.ops.DihedralForce
+ mindspore.ops.DihedralForceWithAtomEnergy
+ mindspore.ops.Dihedral14CFAtomEnergy
+ mindspore.ops.Dihedral14CFEnergy
+ mindspore.ops.Dihedral14LJAtomEnergy
+ mindspore.ops.Dihedral14LJCFForceWithAtomEnergy
+ mindspore.ops.Dihedral14LJEnergy
+ mindspore.ops.Dihedral14LJForce
+ mindspore.ops.Dihedral14LJForceWithDirectCF
+ mindspore.ops.LJEnergy
+ mindspore.ops.LJForce
+ mindspore.ops.LJForceWithPMEDirectForce
+ mindspore.ops.MDIterationLeapFrog
+ mindspore.ops.NeighborListUpdate
+ mindspore.ops.PMEEnergy
+ mindspore.ops.PMEExcludedForce
+ mindspore.ops.PMEReciprocalForce
+
Inner Operators
^^^^^^^^^^^^^^^^
diff --git a/docs/api_python/source_zh_cn/mindquantum/mindquantum.rst b/docs/api_python/source_zh_cn/mindquantum/mindquantum.rst
index 41b1ba084d82b09ee602b7eca961188df3d47fd2..2464675921f5451b687aaa86b624ba3359230f55 100644
--- a/docs/api_python/source_zh_cn/mindquantum/mindquantum.rst
+++ b/docs/api_python/source_zh_cn/mindquantum/mindquantum.rst
@@ -33,22 +33,22 @@ The functional gates are the pre-instantiated quantum gates, which can be used d
* - functional
- gates
- * - mindspore.gate.CNOT
- - :class:`mindspore.gate.CNOTGate`
- * - mindspore.gate.I
- - :class:`mindspore.gate.IGate`
- * - mindspore.gate.H
- - :class:`mindspore.gate.HGate`
- * - mindspore.gate.S
- - :class:`mindspore.gate.PhaseShift` (numpy.pi/2)
- * - mindspore.gate.SWAP
- - :class:`mindspore.gate.SWAPGate`
- * - mindspore.gate.X
- - :class:`mindspore.gate.XGate`
- * - mindspore.gate.Y
- - :class:`mindspore.gate.YGate`
- * - mindspore.gate.Z
- - :class:`mindspore.gate.ZGate`
+ * - mindquantum.gate.CNOT
+ - :class:`mindquantum.gate.CNOTGate`
+ * - mindquantum.gate.I
+ - :class:`mindquantum.gate.IGate`
+ * - mindquantum.gate.H
+ - :class:`mindquantum.gate.HGate`
+ * - mindquantum.gate.S
+ - :class:`mindquantum.gate.PhaseShift` (numpy.pi/2)
+ * - mindquantum.gate.SWAP
+ - :class:`mindquantum.gate.SWAPGate`
+ * - mindquantum.gate.X
+ - :class:`mindquantum.gate.XGate`
+ * - mindquantum.gate.Y
+ - :class:`mindquantum.gate.YGate`
+ * - mindquantum.gate.Z
+ - :class:`mindquantum.gate.ZGate`
mindquantum.nn
--------------
diff --git a/docs/api_python/source_zh_cn/mindspore/mindspore.numpy.rst b/docs/api_python/source_zh_cn/mindspore/mindspore.numpy.rst
index dc42a370839ce164f6833f1f0995ed030d328ff5..8552ece80213ea079cda04370e69268c5af04fd7 100644
--- a/docs/api_python/source_zh_cn/mindspore/mindspore.numpy.rst
+++ b/docs/api_python/source_zh_cn/mindspore/mindspore.numpy.rst
@@ -20,6 +20,7 @@ Array Generation
mindspore.numpy.array
mindspore.numpy.asarray
mindspore.numpy.asfarray
+ mindspore.numpy.copy
mindspore.numpy.empty
mindspore.numpy.empty_like
mindspore.numpy.eye
diff --git a/docs/api_python/source_zh_cn/mindspore/mindspore.ops.rst b/docs/api_python/source_zh_cn/mindspore/mindspore.ops.rst
index d2bb1d855a90907ecbbe9432202bf6f51c554196..a2a28fce2f9efb4c075f8d97471ed555d671372e 100644
--- a/docs/api_python/source_zh_cn/mindspore/mindspore.ops.rst
+++ b/docs/api_python/source_zh_cn/mindspore/mindspore.ops.rst
@@ -70,8 +70,6 @@ The functional operators are the pre-instantiated Primitive operators, which can
- :class:`mindspore.ops.Primitive` ('bool_or')
* - mindspore.ops.cast
- :class:`mindspore.ops.Cast`
- * - mindspore.ops.control_depend
- - :class:`mindspore.ops.ControlDepend`
* - mindspore.ops.distribute
- :class:`mindspore.ops.Primitive` ('distribute')
* - mindspore.ops.dtype
diff --git a/docs/api_python/source_zh_cn/mindspore/operations.rst b/docs/api_python/source_zh_cn/mindspore/operations.rst
index 4dcc5a82e9ceef9abea6daa955afa0c1f592ebc9..1a82cc29f85a602b4ab31098fb4b5d1ab51371ee 100644
--- a/docs/api_python/source_zh_cn/mindspore/operations.rst
+++ b/docs/api_python/source_zh_cn/mindspore/operations.rst
@@ -51,8 +51,6 @@ Neural Network Operators
mindspore.ops.FastGeLU
mindspore.ops.Flatten
mindspore.ops.FloorMod
- mindspore.ops.FusedBatchNorm
- mindspore.ops.FusedBatchNormEx
mindspore.ops.FusedSparseAdam
mindspore.ops.FusedSparseLazyAdam
mindspore.ops.FusedSparseProximalAdagrad
@@ -302,16 +300,6 @@ Common Operators
mindspore.ops.ReduceOp
mindspore.ops.ReduceScatter
-Control Flows
-^^^^^^^^^^^^^
-
-.. msplatformautosummary::
- :toctree: ops
- :nosignatures:
- :template: classtemplate.rst
-
- mindspore.ops.ControlDepend
-
Debug Operators
^^^^^^^^^^^^^^^
@@ -347,6 +335,43 @@ Random Operators
mindspore.ops.UniformInt
mindspore.ops.UniformReal
+Sponge Operators
+^^^^^^^^^^^^^^^^
+
+.. msplatformautosummary::
+ :toctree: ops
+ :nosignatures:
+ :template: classtemplate.rst
+
+ mindspore.ops.AngleAtomEnergy
+ mindspore.ops.AngleEnergy
+ mindspore.ops.AngleForce
+ mindspore.ops.AngleForceWithAtomEnergy
+ mindspore.ops.BondAtomEnergy
+ mindspore.ops.BondEnergy
+ mindspore.ops.BondForce
+ mindspore.ops.BondForceWithAtomEnergy
+ mindspore.ops.BondForceWithAtomVirial
+ mindspore.ops.DihedralAtomEnergy
+ mindspore.ops.DihedralEnergy
+ mindspore.ops.DihedralForce
+ mindspore.ops.DihedralForceWithAtomEnergy
+ mindspore.ops.Dihedral14CFAtomEnergy
+ mindspore.ops.Dihedral14CFEnergy
+ mindspore.ops.Dihedral14LJAtomEnergy
+ mindspore.ops.Dihedral14LJCFForceWithAtomEnergy
+ mindspore.ops.Dihedral14LJEnergy
+ mindspore.ops.Dihedral14LJForce
+ mindspore.ops.Dihedral14LJForceWithDirectCF
+ mindspore.ops.LJEnergy
+ mindspore.ops.LJForce
+ mindspore.ops.LJForceWithPMEDirectForce
+ mindspore.ops.MDIterationLeapFrog
+ mindspore.ops.NeighborListUpdate
+ mindspore.ops.PMEEnergy
+ mindspore.ops.PMEExcludedForce
+ mindspore.ops.PMEReciprocalForce
+
Inner Operators
^^^^^^^^^^^^^^^^
diff --git a/docs/faq/source_en/index.rst b/docs/faq/source_en/index.rst
index c08f8a005dd0a070a83bbbca4185e2b4da41052d..9df80bfc87879ee96bfa01e47fa93b869ec642f7 100644
--- a/docs/faq/source_en/index.rst
+++ b/docs/faq/source_en/index.rst
@@ -15,6 +15,7 @@ MindSpore FAQ
network_models
platform_and_system
backend_running
+ usage_migrate_3rd
programming_language_extensions
supported_features
mindinsight_use
diff --git a/docs/faq/source_en/mindspore_cpp_library.md b/docs/faq/source_en/mindspore_cpp_library.md
index 7940d2837c69afe9ee9dcf5a5bd21138bbbc72ec..16d39b775d1e7c20ac2187c2c72a5b9486322665 100644
--- a/docs/faq/source_en/mindspore_cpp_library.md
+++ b/docs/faq/source_en/mindspore_cpp_library.md
@@ -6,13 +6,9 @@
A:Find the directory where the missing dynamic library file is located, add the path to the environment variable `LD_LIBRARY_PATH`, and refer to [Inference Using the MindIR Model on Ascend 310 AI Processors#Building Inference Code](https://www.mindspore.cn/tutorial/inference/en/master/multi_platform_inference_ascend_310_mindir.html#building-inference-code) for environment variable settings.
-**Q:What should I do when error `undefined reference to mindspore::GlobalContext::SetGlobalDeviceTarget(std::__cxx11::basic_string, std::allocator> const &)` prompts during application compiling?**
-
-A:Since MindSpore uses the old C++ ABI, applications must be the same with MindSpore and add compile definition `-D_GLIBCXX_USE_CXX11_ABI=0`, otherwise the compiling will fail. Refer to [Inference Using the MindIR Model on Ascend 310 AI Processors#Introduce to Building Script](https://www.mindspore.cn/tutorial/inference/en/master/multi_platform_inference_ascend_310_mindir.html#introduce-to-building-script) for cmake script.
-
**Q:What should I do when error `ModuleNotFoundError: No module named 'te'` prompts during application running?**
-A:First confirm whether the system environment is installed correctly and whether the whl packages such as `te` and `topi` are installed correctly. If there are multiple Python versions in the user environment, such as Conda virtual environment, you need to execute `ldd name_of_your_executable_app` to confirm whether the application link `libpython3.7m.so.1.0` is consistent with the current Python directory, if not, you need to adjust the order of the environment variable `LD_LIBRARY_PATH ` .
+A:First confirm whether the system environment is installed correctly and whether the whl packages such as `te` and `topi` are installed correctly. If there are multiple Python versions in the user environment, such as Conda virtual environment, you need to execute `ldd name_of_your_executable_app` to confirm whether the application link `libpython3.7m.so.1.0` is consistent with the current Python directory, if not, you need to adjust the order of the environment variable `LD_LIBRARY_PATH` .
**Q:What should I do when error `error while loading shared libraries: libge_compiler.so: cannot open shared object file: No such file or directory` prompts during application running?**
diff --git a/docs/faq/source_en/platform_and_system.md b/docs/faq/source_en/platform_and_system.md
index cdb6b4ec8b26a0c2099dc3ff64418beeaf894cee..95d1207daf52498b34807cea8f160362192b7bf1 100644
--- a/docs/faq/source_en/platform_and_system.md
+++ b/docs/faq/source_en/platform_and_system.md
@@ -4,6 +4,18 @@
+**Q: What is the difference between the PyNative and Graph modes?**
+
+A: In terms of efficiency, operators used in the two modes are the same. Therefore, when the same network and operators are executed in the two modes, the accuracy is the same. The network execution performance varies according to the execution mechanism. Theoretically, operators provided by MindSpore support both the PyNative and Graph modes.
+
+In terms of application scenarios, Graph mode requires the network structure to be built at the beginning, and then the framework performs entire graph optimization and execution. This mode is suitable to scenarios where the network is fixed and high performance is required.
+
+The two modes are supported on different hardware (such as `Ascend`, `GPU`, and `CPU`).
+
+In terms of code debugging, operators are executed line by line. Therefore, you can directly debug the Python code and view the `/api` output or execution result of the corresponding operator at any breakpoint in the code. In Graph mode, the network is built but not executed in the constructor function. Therefore, you cannot obtain the output of the corresponding operator at breakpoints in the `construct` function. The output can be viewed only after the network execution is complete.
+
+
+
**Q: How do I perform transfer learning in PyNative mode?**
A: PyNative mode is compatible with transfer learning. For more tutorial information, see [Code for Loading a Pre-Trained Model](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/cv_mobilenetv2_fine_tune.html#code-for-loading-a-pre-trained-model).
@@ -22,6 +34,12 @@ A: An AIR model cannot be exported from the Ascend 310. You need to load a train
+**Q: What is the limitation on the input size of a single tensor when exporting an AIR model from MindSpore?**
+
+A: For the input of a single tensor, the size of tensor should not exceed 2GB, otherwise it will be wrong when converting to air model.
+
+
+
**Q: Can a network script trained by MindSpore on a GPU be directly trained on an NPU without modification?**
A: Yes. MindSpore provides unified APIs for NPUs, GPUs, and CPUs. With the support of operators, network scripts can run across platforms without modification.
diff --git a/docs/faq/source_en/supported_operators.md b/docs/faq/source_en/supported_operators.md
index 0d66a29a4f6255d946d6278bb579dd70267ede9d..185ab579918f38e6f9e2fd167aee2109507f18a1 100644
--- a/docs/faq/source_en/supported_operators.md
+++ b/docs/faq/source_en/supported_operators.md
@@ -4,6 +4,12 @@
+**Q: What is the function of the `TransData` operator? Can the performance be optimized?**
+
+A: The `TransData` operator is used in the scenario where the data formats (such as NC1HWC0) used by interconnected operators on the network are inconsistent. In this case, the framework automatically inserts the `TransData` operator to convert the data formats into the same format and then performs computation. You can consider using the `amp` for mixed-precision training. In this way, some `FP32` operations and the invocation of some `TransData` operators can be reduced.
+
+
+
**Q: An error occurs when the `Concat` operator concatenates tuples containing multiple tensors. An error occurs when the number of `tensor list` elements entered is greater than or equal to 192. What is a better solution (running in dynamic mode) for `Concat` to concatenate tuples containing multiple Tensors?**
A: The number of tensors to be concatenated at a time cannot exceed 192 according to the bottom-layer specifications of the Ascend operator. You can try to concatenate them twice.
diff --git a/docs/faq/source_en/usage_migrate_3rd.md b/docs/faq/source_en/usage_migrate_3rd.md
new file mode 100644
index 0000000000000000000000000000000000000000..901632436abc5b594753e8faa3451382d2d90c0f
--- /dev/null
+++ b/docs/faq/source_en/usage_migrate_3rd.md
@@ -0,0 +1,34 @@
+# Migration from a Third-party Framework
+
+
+
+**Q:How do I load a pre-trained PyTorch model for fine-tuning on MindSpore?**
+
+A:Map parameters of PyTorch and MindSpore one by one. No unified conversion script is provided due to flexible network definitions.
+Customize scripts based on scenarios. For details, see [Advanced Usage of Checkpoint](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/advanced_usage_of_checkpoint.html).
+
+
+
+**Q:How do I convert a PyTorch `dataset` to a MindSpore `dataset`?**
+
+A:The custom dataset logic of MindSpore is similar to that of PyTorch. You need to define a `dataset` class containing `__init__`, `__getitem__`, and `__len__` to read your dataset, instantiate the class into an object (for example, `dataset/dataset_generator`), and transfer the instantiated object to `GeneratorDataset` (on MindSpore) or `DataLoader` (on PyTorch). Then, you are ready to load the custom dataset. MindSpore provides further `map`->`batch` operations based on `GeneratorDataset`. Users can easily add other custom operations to `map` and start `batch`.
+The custom dataset of MindSpore is loaded as follows:
+
+```python
+# 1. Perform operations such as data argumentation, shuffle, and sampler.
+class Mydata:
+ def __init__(self):
+ np.random.seed(58)
+ self.__data = np.random.sample((5, 2))
+ self.__label = np.random.sample((5, 1))
+ def __getitem__(self, index):
+ return (self.__data[index], self.__label[index])
+ def __len__(self):
+ return len(self.__data)
+dataset_generator = Mydata()
+dataset = ds.GeneratorDataset(dataset_generator, ["data", "label"], shuffle=False)
+# 2. Customize data argumentation.
+dataset = dataset.map(operations=pyFunc, {other_params})
+# 3. batch
+dataset = dataset.batch(batch_size, drop_remainder=True)
+```
diff --git a/docs/faq/source_zh_cn/mindspore_cpp_library.md b/docs/faq/source_zh_cn/mindspore_cpp_library.md
index ba20b941df2ee75af96da41f438bf895c238bd2a..ab5c9ac0d5f4fcbdb02b6631ecc7378196d82e85 100644
--- a/docs/faq/source_zh_cn/mindspore_cpp_library.md
+++ b/docs/faq/source_zh_cn/mindspore_cpp_library.md
@@ -6,10 +6,6 @@
A:寻找缺少的动态库文件所在目录,添加该路径到环境变量`LD_LIBRARY_PATH`中,环境变量设置参考[Ascend 310 AI处理器上使用MindIR模型进行推理#编译推理代码](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference_ascend_310_mindir.html#id6)。
-**Q:编译应用时出现`undefined reference to mindspore::GlobalContext::SetGlobalDeviceTarget(std::__cxx11::basic_string, std::allocator> const &)`怎么办?**
-
-A:MindSpore使用旧版的C++ ABI,因此用户程序需与MindSpore一致,添加编译选项`-D_GLIBCXX_USE_CXX11_ABI=0`,否则编译链接会失败,CMake脚本编写参考[Ascend 310 AI处理器上使用MindIR模型进行推理#构建脚本介绍](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference_ascend_310_mindir.html#id5)
-
**Q:运行应用时出现`ModuleNotFoundError: No module named 'te'`怎么办?**
A:首先确认环境安装是否正确,`te`、`topi`等whl包是否正确安装。如果用户环境中有多个Python版本,如Conda虚拟环境中,需`ldd name_of_your_executable_app`确认应用所链接的`libpython3.7m.so.1.0`是否与当前Python路径一致,如果不一致需要调整环境变量`LD_LIBRARY_PATH`顺序。
diff --git a/docs/faq/source_zh_cn/platform_and_system.md b/docs/faq/source_zh_cn/platform_and_system.md
index f1ec4e0c3b4c73b96c760b239d2e8117179abdc9..3ff12c596c9ba75e3de3c9c0eff1ab07d5cec09c 100644
--- a/docs/faq/source_zh_cn/platform_and_system.md
+++ b/docs/faq/source_zh_cn/platform_and_system.md
@@ -34,6 +34,12 @@ A:Ascend 310不能导出AIR,需要在Ascend 910加载训练好的checkpoint
+**Q:MindSpore导出AIR模型对单个Tensor输入大小有什么限制?**
+
+A:对于单个Tensor的输入,Tensor大小不能超过2GB,否则导出AIR模型会产生错误。
+
+
+
**Q:我用MindSpore在GPU上训练的网络脚本可以不做修改直接在NPU上进行训练么?**
A:可以的,MindSpore面向NPU/GPU/CPU提供统一的API,在算子支持的前提下,网络脚本可以不做修改直接跨平台运行。
diff --git a/docs/faq/source_zh_cn/usage_migrate_3rd.md b/docs/faq/source_zh_cn/usage_migrate_3rd.md
index 452c31662b494472cddc0360470f0b2eb473961d..d2343d0f6dcd4b4fc3de2af4e6c6030229cb0b97 100644
--- a/docs/faq/source_zh_cn/usage_migrate_3rd.md
+++ b/docs/faq/source_zh_cn/usage_migrate_3rd.md
@@ -1,4 +1,4 @@
-# 第三方框架迁移使用类
+# 第三方框架迁移使用类
@@ -9,7 +9,7 @@ A:需要把PyTorch和MindSpore的参数进行一一对应,因为网络定义
-**Q:怎么将PyoTrch的`dataset`转换成MindSpore的`dataset`?**
+**Q:怎么将PyTorch的`dataset`转换成MindSpore的`dataset`?**
A:MindSpore和PyTorch的自定义数据集逻辑是比较类似的,需要用户先定义一个自己的`dataset`类,该类负责定义`__init__`,`__getitem__`,`__len__`来读取自己的数据集,然后将该类实例化为一个对象(如:`dataset/dataset_generator`),最后将这个实例化对象传入`GeneratorDataset`(mindspore用法)/`DataLoader`(pytorch用法),至此即可以完成自定义数据集加载了。而mindspore在`GeneratorDataset`的基础上提供了进一步的`map`->`batch`操作,可以很方便的让用户在`map`内添加一些其他的自定义操作,并将其`batch`起来。
对应的MindSpore的自定义数据集加载如下:
@@ -28,7 +28,7 @@ class Mydata:
dataset_generator = Mydata()
dataset = ds.GeneratorDataset(dataset_generator, ["data", "label"], shuffle=False)
#2 Custom data enhancement
-dataset = dataset.map(operations=pyFunc, …)
+dataset = dataset.map(operations=pyFunc, {other_params})
#3 batch
dataset = dataset.batch(batch_size, drop_remainder=True)
-```
\ No newline at end of file
+```
diff --git a/docs/migration_guide/source_en/performance_optimization.md b/docs/migration_guide/source_en/performance_optimization.md
index 3709ccaca8bdaee96deb5e8f3a54766c2708d3af..4292cc636180e4ece383b171532941bcfa014a2b 100644
--- a/docs/migration_guide/source_en/performance_optimization.md
+++ b/docs/migration_guide/source_en/performance_optimization.md
@@ -10,7 +10,7 @@
-
+
Profiler provides performance tuning ability for MindSpore, and provides easy-to-use and rich debugging functions in operator performance, iteration performance, data processing performance, etc., helping users quickly locate and solve performance problems.
@@ -20,9 +20,9 @@ This chapter introduces the common methods and cases of performance tuning in ne
Please refer to the tutorials for the function introduction and instructions of MindSpore Profiler.
-[Performance Profiling(Ascend)](https://mindspore.cn/tutorial/training/en/master/advanced_use/performance_profiling_ascend.html)
+[Performance Profiling(Ascend)](https://mindspore.cn/tutorial/training/en/r1.2/advanced_use/performance_profiling_ascend.html)
-[Performance Profiling(GPU)](https://mindspore.cn/tutorial/training/en/master/advanced_use/performance_profiling_gpu.html)
+[Performance Profiling(GPU)](https://mindspore.cn/tutorial/training/en/r1.2/advanced_use/performance_profiling_gpu.html)
This section will introduce the common use of MindSpore Profiler through three typical cases.
diff --git a/docs/migration_guide/source_zh_cn/accuracy_optimization.md b/docs/migration_guide/source_zh_cn/accuracy_optimization.md
index a7d81071105242824a4c3549570bad626279300c..6a5e172e218f0cfd895c511e49c4a5b25eb3d509 100644
--- a/docs/migration_guide/source_zh_cn/accuracy_optimization.md
+++ b/docs/migration_guide/source_zh_cn/accuracy_optimization.md
@@ -12,7 +12,7 @@
-
+
模型训练的最终结果是为了得到一个精度达标的模型,而在AI训练过程中有时会遇到loss(模型损失值)无法下降,或者发散,metrics(模型度量指标)达不到预期等,造成无法得到一个理想精度的模型,这时候需要去进行分析训练过程中出现了什么样的问题,针对性地采用包括调整数据、调整超参、重构模型结构等方法,去解决模型精度调优过程中遇到的各种问题。
@@ -30,24 +30,24 @@ MindSpore团队总结了在AI训练过程中,遇到的造成模型精度无法
### 可视化工具
-训练过程中进行可视化数据采集时,可参考资料[收集Summary数据](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/summary_record.html)。
+训练过程中进行可视化数据采集时,可参考资料[收集Summary数据](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/summary_record.html)。
-训练过程中进行可视化数据分析时,可参考资料[训练看板](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/dashboard.html)和[溯源和对比看板](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/lineage_and_scalars_comparison.html)。
+训练过程中进行可视化数据分析时,可参考资料[训练看板](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/dashboard.html)和[溯源和对比看板](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/lineage_and_scalars_comparison.html)。
### 数据问题处理
-对数据进行标准化、归一化、通道转换等操作,在图片数据处理上,增加随机视野图片,随机旋转度图片等,另外数据混洗、batch和数据倍增等操作,可参考[数据处理](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/pipeline.html)、[数据增强](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/augmentation.html)和[自动数据增强](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/auto_augmentation.html)。
+对数据进行标准化、归一化、通道转换等操作,在图片数据处理上,增加随机视野图片,随机旋转度图片等,另外数据混洗、batch和数据倍增等操作,可参考[数据处理](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/pipeline.html)、[数据增强](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/augmentation.html)和[自动数据增强](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/auto_augmentation.html)。
### 超参问题处理
-AI训练中的超参包含全局学习率,epoch和batch等,如果需要在不同的超参下,训练过程进行可视化时,可参考资料:[可视化的超参调优](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/hyper_parameters_auto_tuning.html);如果需要设置动态学习率超参时,可参考资料:[学习率的优化算法](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/optim.html?#id3)。
+AI训练中的超参包含全局学习率,epoch和batch等,如果需要在不同的超参下,训练过程进行可视化时,可参考资料:[可视化的超参调优](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/hyper_parameters_auto_tuning.html);如果需要设置动态学习率超参时,可参考资料:[学习率的优化算法](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/optim.html?#id3)。
### 模型结构问题处理
一般的处理模型结构问题,需要庸到的操作有:模型结构的重构,选择合适的优化器或者损失函数等。
-需要重构模型结构时,可参考资料:[Cell构建及其子类](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/cell.html)。
+需要重构模型结构时,可参考资料:[Cell构建及其子类](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/cell.html)。
-选择合适的损失函数,可参考资料:[损失函数算子支持列表](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.nn.html#loss-functions)。
+选择合适的损失函数,可参考资料:[损失函数算子支持列表](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.nn.html#loss-functions)。
-选择合适的优化器时,可参考资料:[优化器算子支持列表](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.nn.html#optimizer-functions)。
+选择合适的优化器时,可参考资料:[优化器算子支持列表](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.nn.html#optimizer-functions)。
diff --git a/docs/migration_guide/source_zh_cn/images/image-20210318152607548.png b/docs/migration_guide/source_zh_cn/images/image-20210318152607548.png
new file mode 100644
index 0000000000000000000000000000000000000000..93150aa3d05917465e2c5c7aa4f35ac44ccf1cfb
Binary files /dev/null and b/docs/migration_guide/source_zh_cn/images/image-20210318152607548.png differ
diff --git a/docs/migration_guide/source_zh_cn/inference.md b/docs/migration_guide/source_zh_cn/inference.md
index 1de4af76083601e3b08715b12fb9f221205cf190..fde5b9d38a54725d523b15d46f5bde2db0437f7e 100644
--- a/docs/migration_guide/source_zh_cn/inference.md
+++ b/docs/migration_guide/source_zh_cn/inference.md
@@ -10,7 +10,7 @@
-
+
MindSpore可以基于训练好的模型,在不同的硬件平台上执行推理任务,还可以基于MindSpore Serving部署在线推理服务。
@@ -18,29 +18,29 @@ MindSpore可以基于训练好的模型,在不同的硬件平台上执行推
### 总览
-MindSpore支持保存为CheckPoint格式的[训练参数文件](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference.html#id2)和MindIR、AIR、ONNX格式的[网络模型文件](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference.html#id2)。
+MindSpore支持保存为CheckPoint格式的[训练参数文件](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference.html#id2)和MindIR、AIR、ONNX格式的[网络模型文件](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference.html#id2)。
-参考[执行推理](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference.html#id3),不仅可以直接通过`mindspore.model.predict`接口执行本机推理,还可以通过`mindspore.export`导出MindIR、AIR、ONNX格式的网络模型文件,以便于跨平台执行推理。
+参考[执行推理](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference.html#id3),不仅可以直接通过`mindspore.model.predict`接口执行本机推理,还可以通过`mindspore.export`导出MindIR、AIR、ONNX格式的网络模型文件,以便于跨平台执行推理。
-使用[MindIR格式](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference.html#id3)的模型文件消除了不同后端模型的差异,可以用于执行跨硬件平台推理,支持部署到云端Serving和端侧Lite平台。
+使用[MindIR格式](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference.html#id3)的模型文件消除了不同后端模型的差异,可以用于执行跨硬件平台推理,支持部署到云端Serving和端侧Lite平台。
### 不同硬件平台执行推理
-- Ascend硬件平台参考[Ascend 910 AI处理器上推理](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference_ascend_910.html)和[Ascend 310 AI处理器上推理](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference_ascend_310.html)。
-- GPU硬件平台参考[GPU上推理](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference_gpu.html)。
-- CPU硬件平台参考[CPU上推理](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference_cpu.html)。
+- Ascend硬件平台参考[Ascend 910 AI处理器上推理](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference_ascend_910.html)和[Ascend 310 AI处理器上推理](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference_ascend_310.html)。
+- GPU硬件平台参考[GPU上推理](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference_gpu.html)。
+- CPU硬件平台参考[CPU上推理](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference_cpu.html)。
- Lite端侧推理的相关应用参考[端侧推理](https://www.mindspore.cn/lite/docs?master)。
-> Ascend硬件平台推理的接口使用问题参考[C++接口使用类](https://www.mindspore.cn/doc/faq/zh-CN/master/mindspore_cpp_library.html)解决。
+> Ascend硬件平台推理的接口使用问题参考[C++接口使用类](https://www.mindspore.cn/doc/faq/zh-CN/r1.2/mindspore_cpp_library.html)解决。
## 基于MindSpore Serving部署在线推理服务
MindSpore Serving是一个轻量级、高性能的服务模块,旨在帮助MindSpore开发者在生产环境中高效部署在线推理服务。当用户使用MindSpore完成模型训练后,导出MindSpore模型,即可使用MindSpore Serving创建该模型的推理服务。参考以下几个样例进行部署:
-- [基于MindSpore Serving部署推理服务](https://www.mindspore.cn/tutorial/inference/zh-CN/master/serving_example.html)。
-- [基于gRPC接口访问MindSpore Serving服务](https://www.mindspore.cn/tutorial/inference/zh-CN/master/serving_grpc.html)。
-- [基于RESTful接口访问MindSpore Serving服务](https://www.mindspore.cn/tutorial/inference/zh-CN/master/serving_restful.html)。
-- [通过配置模型提供Servable](https://www.mindspore.cn/tutorial/inference/zh-CN/master/serving_model.html)。
-- [基于MindSpore Serving部署分布式推理服务](https://www.mindspore.cn/tutorial/inference/zh-CN/master/serving_distributed_example.html)。
+- [基于MindSpore Serving部署推理服务](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/serving_example.html)。
+- [基于gRPC接口访问MindSpore Serving服务](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/serving_grpc.html)。
+- [基于RESTful接口访问MindSpore Serving服务](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/serving_restful.html)。
+- [通过配置模型提供Servable](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/serving_model.html)。
+- [基于MindSpore Serving部署分布式推理服务](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/serving_distributed_example.html)。
-> MindSpore Serving部署在线推理服务的问题可以参考[MindSpore Serving类](https://www.mindspore.cn/doc/faq/zh-CN/master/mindspore_serving.html)解决。
+> MindSpore Serving部署在线推理服务的问题可以参考[MindSpore Serving类](https://www.mindspore.cn/doc/faq/zh-CN/r1.2/mindspore_serving.html)解决。
diff --git a/docs/migration_guide/source_zh_cn/neural_network_debug.md b/docs/migration_guide/source_zh_cn/neural_network_debug.md
index 4e53121f73b1765c4e1b338aa4e0fab2c46139a1..58ec4f794c8e1e59aca82239180eb813b0d0a0cb 100644
--- a/docs/migration_guide/source_zh_cn/neural_network_debug.md
+++ b/docs/migration_guide/source_zh_cn/neural_network_debug.md
@@ -19,7 +19,7 @@
-
+
本章将介绍网络调试的基本思路、常用工具,以及一些常见问题处理。
@@ -54,15 +54,15 @@
在脚本开发和网络流程调试中,我们推荐使用PyNative模式进行调试。PyNative模式支持执行单算子、普通函数和网络,以及单独求梯度的操作。在PyNative模式下,可以方便地设置断点,获取网络执行的中间结果,也可以通过pdb的方式对网络进行调试。
-在默认情况下,MindSpore处于PyNative模式,也可以通过`context.set_context(mode=context.PYNATIVE_MODE)`进行显式定义,相关示例可参考[使用PyNative模式调试](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/debug_in_pynative_mode.html#pynative)。
+在默认情况下,MindSpore处于PyNative模式,也可以通过`context.set_context(mode=context.PYNATIVE_MODE)`进行显式定义,相关示例可参考[使用PyNative模式调试](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/debug_in_pynative_mode.html#pynative)。
#### 获取更多报错信息
在网络流程调试过程中,如果需要获取更多的报错信息,可通过以下方式获得:
- 在PyNative模式下可使用pdb进行调试,利用pdb打印相关堆栈和上下文信息帮助问题定位。
-- 使用Print算子打印更多上下文信息,具体示例可参考[Print算子功能介绍](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#print)。
-- 调整日志级别获取更多报错信息,MindSpore可通过环境变量方便地调整日志级别,具体可参考[日志相关的环境变量和配置](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#id6)。
+- 使用Print算子打印更多上下文信息,具体示例可参考[Print算子功能介绍](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/custom_debugging_info.html#print)。
+- 调整日志级别获取更多报错信息,MindSpore可通过环境变量方便地调整日志级别,具体可参考[日志相关的环境变量和配置](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/custom_debugging_info.html#id6)。
#### 常见错误
@@ -72,15 +72,15 @@
网络流程调试过程中,常出现shape不匹配、dtype不支持等算子执行报错,此时应根据报错信息检查是否正确使用算子,以及算子输入数据的shape是否与预期相符,并进行相应修改。
- 相关算子支持和API介绍可参考[算子支持列表](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/operator_list.html)和[算子Python API](https://www.mindspore.cn/doc/api_python/zh-CN/master/index.html)。
+ 相关算子支持和API介绍可参考[算子支持列表](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/operator_list.html)和[算子Python API](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/index.html)。
- 相同脚本,在PyNative模式下能跑通,但Graph模式下报错
- MindSpore的Graph模式下,`construct`函数中的代码由MindSpore框架进行解析,有一些Python语法还未支持,因此导致报错。此时应当根据报错信息按照[MindSpore的语法说明](https://www.mindspore.cn/doc/note/zh-CN/master/static_graph_syntax_support.html)修改相关代码。
+ MindSpore的Graph模式下,`construct`函数中的代码由MindSpore框架进行解析,有一些Python语法还未支持,因此导致报错。此时应当根据报错信息按照[MindSpore的语法说明](https://www.mindspore.cn/doc/note/zh-CN/r1.2/static_graph_syntax_support.html)修改相关代码。
- 分布式并行训练脚本配置错误
- 分布式并行训练脚本及环境配置可参考[分布式并行训练教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/distributed_training_tutorials.html)。
+ 分布式并行训练脚本及环境配置可参考[分布式并行训练教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/distributed_training_tutorials.html)。
### loss值对比检查
@@ -134,31 +134,31 @@
- MindSpore提供了丰富的工具获取网络中间数据,可根据实际情况选用。
- - [数据Dump功能](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#dump)
- - [使用Print算子打印相关信息](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#print)
- - [使用可视化组件MindInsight](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/visualization_tutorials.html)
+ - [数据Dump功能](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/custom_debugging_info.html#dump)
+ - [使用Print算子打印相关信息](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/custom_debugging_info.html#print)
+ - [使用可视化组件MindInsight](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/visualization_tutorials.html)
### 精度调试工具
#### 自定义调试信息
-- [Callback功能](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#callback)
+- [Callback功能](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/custom_debugging_info.html#callback)
- MindSpore已提供ModelCheckpoint、LossMonitor、SummaryCollector等Callback类用于保存模型参数、监控loss值、保存训练过程信息等功能,用户也可自定义Callback函数用于实现在每个epoch和step的开始和结束运行相关功能,具体示例可参考[自定义Callback](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#id3)。
+ MindSpore已提供ModelCheckpoint、LossMonitor、SummaryCollector等Callback类用于保存模型参数、监控loss值、保存训练过程信息等功能,用户也可自定义Callback函数用于实现在每个epoch和step的开始和结束运行相关功能,具体示例可参考[自定义Callback](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/custom_debugging_info.html#id3)。
-- [MindSpore metrics功能](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#mindspore-metrics)
+- [MindSpore metrics功能](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/custom_debugging_info.html#mindspore-metrics)
当训练结束后,可以使用metrics评估训练结果的好坏。MindSpore提供了多种metrics评估指标,如:`accuracy`、`loss`、`precision`、`recall`、`F1`等。
-- [边训练边推理](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/evaluate_the_model_during_training.html)
+- [边训练边推理](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/evaluate_the_model_during_training.html)
可通过定义推理的CallBack函数的方式在训练时进行推理。
-- [自定义训练循环](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/train.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%AE%AD%E7%BB%83%E5%BE%AA%E7%8E%AF)
+- [自定义训练循环](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/train.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%AE%AD%E7%BB%83%E5%BE%AA%E7%8E%AF)
- 自定义学习率
- MindSpore提供了一些常见的动态学习率实现以及一些常见的具有自适应学习率调整功能的优化器,可参考API文档中的[Dynamic Learning Rate](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.nn.html#dynamic-learning-rate)和[Optimizer Functions](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.nn.html#optimizer-functions)。
+ MindSpore提供了一些常见的动态学习率实现以及一些常见的具有自适应学习率调整功能的优化器,可参考API文档中的[Dynamic Learning Rate](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.nn.html#dynamic-learning-rate)和[Optimizer Functions](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.nn.html#optimizer-functions)。
同时,用户可实现自定义的动态学习率,以WarmUpLR为例:
@@ -217,7 +217,7 @@
#### 使用MindOptimizer进行超参调优
-MindSpore提供了MindOptimizer工具帮助用户进行更便捷的超参调优,详细示例和使用方法可参考[使用MindOptimizer进行超参调优](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/hyper_parameters_auto_tuning.html)。
+MindSpore提供了MindOptimizer工具帮助用户进行更便捷的超参调优,详细示例和使用方法可参考[使用MindOptimizer进行超参调优](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/hyper_parameters_auto_tuning.html)。
#### loss异常定位
@@ -231,7 +231,7 @@ MindSpore提供了MindOptimizer工具帮助用户进行更便捷的超参调优
2. 造成loss值异常的原因可能由输入数据异常、算子溢出、梯度消失、梯度爆炸等原因造成。
- 排查算子溢出、梯度为0、权重异常、梯度消失和梯度爆炸等网络中间值情况,推荐使用[MindInsight调试器](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/debugger.html)设置相应检测点进行检测和调试,这种方式可较为全面地进行问题定位,可调试性也比较强。
+ 排查算子溢出、梯度为0、权重异常、梯度消失和梯度爆炸等网络中间值情况,推荐使用[MindInsight调试器](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/debugger.html)设置相应检测点进行检测和调试,这种方式可较为全面地进行问题定位,可调试性也比较强。
下面介绍几种简单的初步排查方法:
diff --git a/docs/migration_guide/source_zh_cn/overview.md b/docs/migration_guide/source_zh_cn/overview.md
index d16e7d987d63b7e8b7c59ed94cc858f20464d55f..5f3172831866bec75b44398ce180915fb21f0128 100644
--- a/docs/migration_guide/source_zh_cn/overview.md
+++ b/docs/migration_guide/source_zh_cn/overview.md
@@ -15,7 +15,7 @@
-
+
本迁移指导包含从其他机器学习框架将神经网络迁移到MindSpore的完整步骤。
diff --git a/docs/migration_guide/source_zh_cn/performance_optimization.md b/docs/migration_guide/source_zh_cn/performance_optimization.md
index c959ff60e0e394e6a3ea6c7a52ed9a957f54b4a6..05bc8953624e610dbeda451561f894aab326b736 100644
--- a/docs/migration_guide/source_zh_cn/performance_optimization.md
+++ b/docs/migration_guide/source_zh_cn/performance_optimization.md
@@ -10,7 +10,7 @@
-
+
Profiler为MindSpore提供了性能调优能力,在算子性能、迭代性能、数据处理性能等方面提供了易用、丰富的调试功能,帮助用户快速定位、解决性能问题。
@@ -20,9 +20,9 @@ Profiler为MindSpore提供了性能调优能力,在算子性能、迭代性能
Profiler的功能介绍及使用说明请参见教程:
-[性能调试(Ascend)](https://mindspore.cn/tutorial/training/zh-CN/master/advanced_use/performance_profiling_ascend.html)
+[性能调试(Ascend)](https://mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/performance_profiling_ascend.html)
-[性能调试(GPU)](https://mindspore.cn/tutorial/training/zh-CN/master/advanced_use/performance_profiling_gpu.html)
+[性能调试(GPU)](https://mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/performance_profiling_gpu.html)
本节将通过三个典型案例介绍Profiler工具的常见使用方式。
diff --git a/docs/migration_guide/source_zh_cn/preparation.md b/docs/migration_guide/source_zh_cn/preparation.md
index bd6465fa4713156b96a9920af4b014b9d3091846..fff815273009c848cdd6d29926255eb68b63e321 100644
--- a/docs/migration_guide/source_zh_cn/preparation.md
+++ b/docs/migration_guide/source_zh_cn/preparation.md
@@ -16,7 +16,7 @@
-
+
## 概述
@@ -122,14 +122,14 @@ print(ops.add(x, y))
### MindSpore编程指南
-用户可以通过参考[MindSpore教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/index.html)了解如何使用MindSpore进行训练、调试、调优、推理;也可以通过参考[MindSpore编程指南](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/index.html)了解MindSpore的基本组成和常用编程方法;也可以通过参考[MindSpore Python API](https://www.mindspore.cn/doc/api_python/zh-CN/master/index.html)详细了解MindSpore各接口的相关信息,以便于用户能够更好地使用。
+用户可以通过参考[MindSpore教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/index.html)了解如何使用MindSpore进行训练、调试、调优、推理;也可以通过参考[MindSpore编程指南](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/index.html)了解MindSpore的基本组成和常用编程方法;也可以通过参考[MindSpore Python API](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/index.html)详细了解MindSpore各接口的相关信息,以便于用户能够更好地使用。
### ModelZoo和Hub
-[ModelZoo](https://gitee.com/mindspore/mindspore/tree/master/model_zoo)是MindSpore与社区共同提供的深度优化的模型集市,向开发者提供了深度优化的模型,以便于生态中的小伙伴可以方便地基于ModelZoo中的模型进行个性化开发。当前已经覆盖了机器视觉、自然语言处理、语音等多个领域的主流模型。
+[ModelZoo](https://gitee.com/mindspore/mindspore/tree/r1.2/model_zoo)是MindSpore与社区共同提供的深度优化的模型集市,向开发者提供了深度优化的模型,以便于生态中的小伙伴可以方便地基于ModelZoo中的模型进行个性化开发。当前已经覆盖了机器视觉、自然语言处理、语音等多个领域的主流模型。
[MindSpore Hub](https://www.mindspore.cn/resources/hub)是存放MindSpore官方或者第三方开发者提供的预训练模型的平台。它向应用开发者提供了简单易用的模型加载和微调API,使得用户可以基于预训练模型进行推理或者微调,并部署到自己的应用中。用户也可以将自己训练好的模型按照指定的步骤发布到MindSpore Hub中,供其他用户下载和使用。
### 云上训练
-ModelArts是华为云提供的面向AI开发者的一站式开发平台,集成了昇腾AI处理器资源池,用户可以在该平台下体验MindSpore。相关文档可参考[云上使用MindSpore](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/use_on_the_cloud.html)和[AI开发平台ModelArts](https://support.huaweicloud.com/wtsnew-modelarts/index.html)。
+ModelArts是华为云提供的面向AI开发者的一站式开发平台,集成了昇腾AI处理器资源池,用户可以在该平台下体验MindSpore。相关文档可参考[云上使用MindSpore](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/use_on_the_cloud.html)和[AI开发平台ModelArts](https://support.huaweicloud.com/wtsnew-modelarts/index.html)。
diff --git a/docs/migration_guide/source_zh_cn/sample_code.md b/docs/migration_guide/source_zh_cn/sample_code.md
index 9ab65cefce7a6adb80b04d74da5c9a6db51e7ebe..ea3809fbaeb00b004fcf5b30b52d6f270a6648ed 100644
--- a/docs/migration_guide/source_zh_cn/sample_code.md
+++ b/docs/migration_guide/source_zh_cn/sample_code.md
@@ -28,40 +28,52 @@
-
+
本章将结合用例来介绍网络迁移的基本步骤、常用工具、定位问题的思路及解决方法。
+这里以经典网络 ResNet50 为例,结合代码来详细介绍网络迁移方法。
+
## 对标网络分析与复现
### 确定迁移目标
-网络迁移的第一步是确定迁移目标,既先找到一个合适的、可达成的标准,通常一个深度神经网络的交付目标包括以下四个部分:
+网络迁移的第一步是确定迁移目标,即先找到一个合适的、可达成的标准,通常一个深度神经网络的交付目标包括以下四个部分:
1. 网络实现:这是迁移目标中最基本的部分,有时同一个神经网络有不同的版本、同一个版本有不同的实现方式或者在相同的神经网络下使用不同的超参,这些差别会对最终的收敛精度和性能造成一定影响。通常,我们以神经网络作者本身的实现为准,也可以参考不同框架(例如TensorFlow、PyTorch等)的官方实现或其他主流开源工具箱(例如 MMDetection)。
-2. 数据集:相同的神经网络和参数,在不同的数据集上往往差别很大,因此我们需要确认迁移网络所使用的数据集。一些数据集的数据会频繁更新,确定数据集时需要注意数据集的版本、训练数据和测试数据划分比例等问题。
+2. 数据集:相同的神经网络和参数,在不同的数据集上往往差别很大,因此我们需要确认迁移网络所使用的数据集。一些数据集的数据内容会频繁更新,确定数据集时需要注意数据集的版本、训练数据和测试数据划分比例等问题。
3. 收敛精度:不同的框架、不同的GPU型号、是否为分布式训练等因素会对精度有所影响,在确定迁移目标时需要分析清楚对标的框架、硬件等信息。
-4. 训练性能:和收敛精度相同,训练性能主要受框架、GPU本身和是否为分布式训练因素影响。
+4. 训练性能:和收敛精度相同,训练性能主要受网络脚本、框架性能、GPU硬件本身和是否为分布式训练等因素影响。
+
+#### ResNet50 迁移示例
+
+ResNet50 是 CV 中经典的深度神经网络,有较多开发者关注和复现,而 PyTorch 的语法和 MindSpore 较为相似,因此,我们选择 PyTorch 作为对标框架。
+
+PyTorch 官方实现脚本可参考 [torchvision model](https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py) 或者 [英伟达 PyTorch 实现脚本](https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/Classification/ConvNets/resnet50v1.5),其中包括了主流 ResNet 系列网络的实现(ResNet18、ResNet34、ResNet50、ResNet101、ResNet152)。ResNet50 所使用的数据集为 ImageNet2012,收敛精度可参考 [PyTorch Hub](https://pytorch.org/hub/pytorch_vision_resnet/#model-description)。
+
+开发者可以基于 PyTorch 的 ResNet50 脚本直接在对标的硬件环境下运行,然后计算出性能数据,也可以参考同硬件环境下的官方数据。例如,当我们对标 Nvidia DGX-1 32GB(8x V100 32GB) 硬件时,可参考 [Nvidia 官方发布的 ResNet50 性能数据](https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/Classification/ConvNets/resnet50v1.5#training-performance-nvidia-dgx-1-32gb-8x-v100-32gb)。
### 复现迁移目标
-网络迁移目标确定完成后,接下来要做的就是复现这些指标。复现标杆数据对后续精度和性能调优十分重要,当我们在 MindSpore开发的网络和对标脚本有精度/性能差距时,很多时候都是以标杆数据作为基准,一步一步地分析迁移脚本和对标脚本的差别,如果对标脚本无法复现指标,那我们以此为基准开发的MindSpore脚本就很难达到迁移目标。复现迁移指标时,不仅要复现训练阶段,推理阶段也同样重要。
+网络迁移目标确定完成后,接下来要做的就是复现指标。复现标杆数据对后续精度和性能调优十分重要,当我们在 MindSpore 开发的网络和对标脚本有精度/性能差距时,很多时候都是以标杆数据作为基准,一步一步地分析迁移脚本和对标脚本的差别,如果对标脚本无法复现指标,那我们以此为基准开发的 MindSpore 脚本就很难达到迁移目标。复现迁移指标时,不仅要复现训练阶段,推理阶段也同样重要。
-需要注意的是对于部分网络,使用相同的硬件环境和脚本训练,最终达到的收敛精度和性能也可能与原作者提出的结果有细微差别,这属于正常的波动范围,我们在迁移网络时要把这种波动考虑在内。
+需要注意的是,对于部分网络,使用相同的硬件环境和脚本,最终达到的收敛精度和性能也可能与原作者提出的结果有细微差别,这属于正常的波动范围,我们在迁移网络时要把这种波动考虑在内。
### 复现单Step结果
-复现单Step结果主要是为了接下来的脚本开发和网络调优。对于复杂的神经网络,完整的训练需要耗时几天甚至几个月,如果仅以最终的训练精度和结果做参考,会极大地降低开发效率。因此,我们需要提前复现单Step的运行结果,并以此为对照展开后续的开发工作。
+复现单 Step 结果主要是为了接下来的脚本开发和网络调优。对于复杂的神经网络,完整的训练需要耗时几天甚至几个月,如果仅以最终的训练精度和结果做参考,会极大地降低开发效率。因此,我们需要提前复现单 Step 的运行结果,即获取只执行第一个 Step 后网络的状态(该状态是经历了数据预处理、权重初始化、正向计算、loss 计算、反向梯度计算和优化器更新之后的结果,覆盖了网络训练的全部环节),并以此为对照展开后续的开发工作。
## 脚本开发
### 脚本开发前分析
-在开始真正的开发脚本前,需要进行对标脚本分析。脚本分析的目的是识别出MindSpore与对标框架相比缺失的算子或功能。MindSpore已支持绝大多数常用[功能](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/index.html)和[算子](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/operator_list.html)。MindSpore既支持动态图(PyNative)模式,又支持静态图(Graph) 模式,动态图模式灵活、易于调试,因此动态图模式主要用于网络调试,静态图模式性能好,主要用于整网训练,在分析缺失算子和功能时,要分别分析这两种模式。
+在开始真正的开发脚本前,需要进行对标脚本分析。脚本分析的目的是识别出 MindSpore 与对标框架相比缺失的算子或功能。具体方法可以参考[脚本评估教程](https://gitee.com/mindspore/docs/blob/r1.2/docs/migration_guide/source_zh_cn/script_analysis.md)。
+
+MindSpore 已支持绝大多数常用 [功能](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/index.html) 和 [算子](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/operator_list.html)。MindSpore 既支持动态图(PyNative)模式,又支持静态图(Graph)模式,动态图模式灵活、易于调试,因此动态图模式主要用于网络调试,静态图模式性能好,主要用于整网训练,在分析缺失算子和功能时,要分别分析这两种模式。
-如果发现有缺失的算子和功能,首先可考虑基于当前算子或功能来组合出缺失的算子和功能,对于主流的CV和NLP类网络,新的缺失算子一般都可以通过组合已有算子的方式来解决。
+如果发现有缺失的算子和功能,首先可考虑基于当前算子或功能来组合出缺失的算子和功能,对于主流的 CV 和 NLP 类网络,新的缺失算子一般都可以通过组合已有算子的方式来解决。
-组合的算子可以通过Cell的方式实现,在MindSpore中,[nn类算子](https://gitee.com/mindspore/mindspore/tree/master/mindspore/nn) 就是通过这种方式实现的。例如下面的`ReduceSumExp`算子,它是由已有的`Exp`、`ReduceSum`、`Log`小算子组合而成:
+组合的算子可以通过 Cell 的方式实现,在 MindSpore 中,[nn类算子](https://gitee.com/mindspore/mindspore/tree/r1.2/mindspore/nn) 就是通过这种方式实现的。例如下面的 `ReduceSumExp` 算子,它是由已有的`Exp`、`ReduceSum`、`Log`小算子组合而成:
```python
class ReduceLogSumExp(Cell):
@@ -122,26 +134,340 @@ class ReduceLogSumExp(Cell):
return logsumexp
```
-如果缺失的功能和算子无法规避,或者组合算子性能较差,严重影响网络的训练和推理,可联系[MindSpore社区](https://gitee.com/mindspore/mindspore/issues) 反馈,我们会有专门的工作人员为您解决。
+如果缺失的功能和算子无法规避,或者组合算子性能较差,严重影响网络的训练和推理,可联系 [MindSpore社区](https://gitee.com/mindspore/mindspore/issues) 反馈,我们会有专门的工作人员为您解决。
+
+#### ResNet50 迁移示例
+
+以下为 ResNet 系列网络结构:
+
+
+
+使用 PyTorch 实现的 ResNet50 脚本如下:
+
+```python
+def conv3x3(in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, dilation: int = 1) -> nn.Conv2d:
+ """3x3 convolution with padding"""
+ return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
+ padding=dilation, groups=groups, bias=False, dilation=dilation)
+
+
+def conv1x1(in_planes: int, out_planes: int, stride: int = 1) -> nn.Conv2d:
+ """1x1 convolution"""
+ return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
+
+
+class BasicBlock(nn.Module):
+ expansion: int = 1
+
+ def __init__(
+ self,
+ inplanes: int,
+ planes: int,
+ stride: int = 1,
+ downsample: Optional[nn.Module] = None,
+ groups: int = 1,
+ base_width: int = 64,
+ dilation: int = 1,
+ norm_layer: Optional[Callable[..., nn.Module]] = None
+ ) -> None:
+ super(BasicBlock, self).__init__()
+ if norm_layer is None:
+ norm_layer = nn.BatchNorm2d
+ if groups != 1 or base_width != 64:
+ raise ValueError('BasicBlock only supports groups=1 and base_width=64')
+ if dilation > 1:
+ raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
+ # Both self.conv1 and self.downsample layers downsample the input when stride != 1
+ self.conv1 = conv3x3(inplanes, planes, stride)
+ self.bn1 = norm_layer(planes)
+ self.relu = nn.ReLU(inplace=True)
+ self.conv2 = conv3x3(planes, planes)
+ self.bn2 = norm_layer(planes)
+ self.downsample = downsample
+ self.stride = stride
+
+ def forward(self, x: Tensor) -> Tensor:
+ identity = x
+
+ out = self.conv1(x)
+ out = self.bn1(out)
+ out = self.relu(out)
+
+ out = self.conv2(out)
+ out = self.bn2(out)
+
+ if self.downsample is not None:
+ identity = self.downsample(x)
+
+ out += identity
+ out = self.relu(out)
+
+ return out
+
+
+class Bottleneck(nn.Module):
+ # Bottleneck in torchvision places the stride for downsampling at 3x3 convolution(self.conv2)
+ # while original implementation places the stride at the first 1x1 convolution(self.conv1)
+ # according to "Deep residual learning for image recognition"https://arxiv.org/abs/1512.03385.
+ # This variant is also known as ResNet V1.5 and improves accuracy according to
+ # https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch.
+
+ expansion: int = 4
+
+ def __init__(
+ self,
+ inplanes: int,
+ planes: int,
+ stride: int = 1,
+ downsample: Optional[nn.Module] = None,
+ groups: int = 1,
+ base_width: int = 64,
+ dilation: int = 1,
+ norm_layer: Optional[Callable[..., nn.Module]] = None
+ ) -> None:
+ super(Bottleneck, self).__init__()
+ if norm_layer is None:
+ norm_layer = nn.BatchNorm2d
+ width = int(planes * (base_width / 64.)) * groups
+ # Both self.conv2 and self.downsample layers downsample the input when stride != 1
+ self.conv1 = conv1x1(inplanes, width)
+ self.bn1 = norm_layer(width)
+ self.conv2 = conv3x3(width, width, stride, groups, dilation)
+ self.bn2 = norm_layer(width)
+ self.conv3 = conv1x1(width, planes * self.expansion)
+ self.bn3 = norm_layer(planes * self.expansion)
+ self.relu = nn.ReLU(inplace=True)
+ self.downsample = downsample
+ self.stride = stride
+
+ def forward(self, x: Tensor) -> Tensor:
+ identity = x
+
+ out = self.conv1(x)
+ out = self.bn1(out)
+ out = self.relu(out)
+
+ out = self.conv2(out)
+ out = self.bn2(out)
+ out = self.relu(out)
+
+ out = self.conv3(out)
+ out = self.bn3(out)
+
+ if self.downsample is not None:
+ identity = self.downsample(x)
+
+ out += identity
+ out = self.relu(out)
+
+ return out
+
+
+class ResNet(nn.Module):
+
+ def __init__(
+ self,
+ block: Type[Union[BasicBlock, Bottleneck]],
+ layers: List[int],
+ num_classes: int = 1000,
+ zero_init_residual: bool = False,
+ groups: int = 1,
+ width_per_group: int = 64,
+ replace_stride_with_dilation: Optional[List[bool]] = None,
+ norm_layer: Optional[Callable[..., nn.Module]] = None
+ ) -> None:
+ super(ResNet, self).__init__()
+ if norm_layer is None:
+ norm_layer = nn.BatchNorm2d
+ self._norm_layer = norm_layer
+
+ self.inplanes = 64
+ self.dilation = 1
+ if replace_stride_with_dilation is None:
+ # each element in the tuple indicates if we should replace
+ # the 2x2 stride with a dilated convolution instead
+ replace_stride_with_dilation = [False, False, False]
+ if len(replace_stride_with_dilation) != 3:
+ raise ValueError("replace_stride_with_dilation should be None "
+ "or a 3-element tuple, got {}".format(replace_stride_with_dilation))
+ self.groups = groups
+ self.base_width = width_per_group
+ self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3,
+ bias=False)
+ self.bn1 = norm_layer(self.inplanes)
+ self.relu = nn.ReLU(inplace=True)
+ self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
+ self.layer1 = self._make_layer(block, 64, layers[0])
+ self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
+ dilate=replace_stride_with_dilation[0])
+ self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
+ dilate=replace_stride_with_dilation[1])
+ self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
+ dilate=replace_stride_with_dilation[2])
+ self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
+ self.fc = nn.Linear(512 * block.expansion, num_classes)
+
+ for m in self.modules():
+ if isinstance(m, nn.Conv2d):
+ nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
+ elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
+ nn.init.constant_(m.weight, 1)
+ nn.init.constant_(m.bias, 0)
+
+ # Zero-initialize the last BN in each residual branch,
+ # so that the residual branch starts with zeros, and each residual block behaves like an identity.
+ # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
+ if zero_init_residual:
+ for m in self.modules():
+ if isinstance(m, Bottleneck):
+ nn.init.constant_(m.bn3.weight, 0) # type: ignore[arg-type]
+ elif isinstance(m, BasicBlock):
+ nn.init.constant_(m.bn2.weight, 0) # type: ignore[arg-type]
+
+ def _make_layer(self, block: Type[Union[BasicBlock, Bottleneck]], planes: int, blocks: int,
+ stride: int = 1, dilate: bool = False) -> nn.Sequential:
+ norm_layer = self._norm_layer
+ downsample = None
+ previous_dilation = self.dilation
+ if dilate:
+ self.dilation *= stride
+ stride = 1
+ if stride != 1 or self.inplanes != planes * block.expansion:
+ downsample = nn.Sequential(
+ conv1x1(self.inplanes, planes * block.expansion, stride),
+ norm_layer(planes * block.expansion),
+ )
+
+ layers = []
+ layers.append(block(self.inplanes, planes, stride, downsample, self.groups,
+ self.base_width, previous_dilation, norm_layer))
+ self.inplanes = planes * block.expansion
+ for _ in range(1, blocks):
+ layers.append(block(self.inplanes, planes, groups=self.groups,
+ base_width=self.base_width, dilation=self.dilation,
+ norm_layer=norm_layer))
+
+ return nn.Sequential(*layers)
+
+ def _forward_impl(self, x: Tensor) -> Tensor:
+ # See note [TorchScript super()]
+ x = self.conv1(x)
+ x = self.bn1(x)
+ x = self.relu(x)
+ x = self.maxpool(x)
+
+ x = self.layer1(x)
+ x = self.layer2(x)
+ x = self.layer3(x)
+ x = self.layer4(x)
+
+ x = self.avgpool(x)
+ x = torch.flatten(x, 1)
+ x = self.fc(x)
+
+ return x
+
+ def forward(self, x: Tensor) -> Tensor:
+ return self._forward_impl(x)
+
+
+def _resnet(
+ arch: str,
+ block: Type[Union[BasicBlock, Bottleneck]],
+ layers: List[int],
+ pretrained: bool,
+ progress: bool,
+ **kwargs: Any
+) -> ResNet:
+ model = ResNet(block, layers, **kwargs)
+ if pretrained:
+ state_dict = load_state_dict_from_url(model_urls[arch],
+ progress=progress)
+ model.load_state_dict(state_dict)
+ return model
+
+
+def resnet50(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> ResNet:
+ r"""ResNet-50 model from
+ `"Deep Residual Learning for Image Recognition" `_.
+ Args:
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
+ progress (bool): If True, displays a progress bar of the download to stderr
+ """
+ return _resnet('resnet50', Bottleneck, [3, 4, 6, 3], pretrained, progress,
+ **kwargs)
+```
+
+我们可以基于算子和功能两个方面分析:
+
+- 算子分析
+
+| PyTorch 使用算子 | MindSpore 对应算子 | 是否支持该算子所需功能 |
+| ---------------------- | ------------------ | ---------------------- |
+| `nn.Conv2D` | `nn.Conv2d` | 是 |
+| `nn.BatchNorm2D` | `nn.BatchNom2d` | 是 |
+| `nn.ReLU` | `nn.ReLU` | 是 |
+| `nn.MaxPool2D` | `nn.MaxPool2d` | 是 |
+| `nn.AdaptiveAvgPool2D` | 无 | 不支持 |
+| `nn.Linear` | `nn.Dense` | 是 |
+| `torch.flatten` | `nn.Flatten` | 是 |
+
+注:对于 PyTorch 脚本,MindSpore 提供了 [PyTorch 算子映射工具](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/index.html#operator_api),可直接查询该算子是否支持。
+
+- 功能分析
+
+| Pytorch 使用功能 | MindSpore 对应功能 |
+| ------------------------- | ------------------------------------- |
+| `nn.init.kaiming_normal_` | `initializer(init='HeNormal')` |
+| `nn.init.constant_` | `initializer(init='Constant')` |
+| `nn.Sequential` | `nn.SequentialCell` |
+| `nn.Module` | `nn.Cell` |
+| `nn.distibuted` | `context.set_auto_parallel_context` |
+| `torch.optim.SGD` | `nn.optim.SGD` or `nn.optim.Momentum` |
+
+(由于MindSpore 和 PyTorch 在接口设计上不完全一致,这里仅列出关键功能的比对)
+
+经过算子和功能分析,我们发现,相比 PyTorch,MindSpore 功能上没有缺失,但算子上缺失 `nn.AdaptiveAvgPool` ,这时我们需要更一步的分析,该缺失算子是否有可替代方案。在 ResNet50 网络中,输入的图片 shape 是固定的,统一为 `N,3,224,224`,其中 N 为 batch size,3 为通道的数量,224 和 224 分别为图片的宽和高,网络中改变图片大小的算子有 `Conv2d` 和 `Maxpool2d`,这两个算子对shape 的影响是固定的,因此,`nn.AdaptiveAvgPool2D` 的输入和输出 shape 是可以提前确定的,只要我们计算出 `nn.AdaptiveAvgPool2D` 的输入和输出 shape,就可以通过 `nn.AvgPool` 或 `nn.ReduceMean` 来实现,所以该算子的缺失是可替代的,并不影响网络的训练。
### 数据预处理
-要理解一个神经网络的实现,首先要清楚网络的输入数据,因此,数据预处理是脚本开发的第一个环节。MindSpore设计了一个专门进行数据处理的模块 - MindData,使用MindData进行数据预处理主要包括以下几个步骤:
+要理解一个神经网络的实现,首先要清楚网络的输入数据,因此,数据预处理是脚本开发的第一个环节。MindSpore 设计了一个专门进行数据处理的模块 - MindData,使用 MindData 进行数据预处理主要包括以下几个步骤:
1. 传入数据路径,读取数据文件。
2. 解析数据。
3. 数据处理(如常见数据切分、shuffle、数据增强等操作)。
-4. 数据分发(以batch_size为单位分发数据,分布式训练涉及多机分发)。
+4. 数据分发(以 batch_size 为单位分发数据,分布式训练涉及多机分发)。
+
+在读取和解析数据过程中,MindSpore 提供了一种更友好的数据格式 - [MindRecord](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/convert_dataset.html)。用户可以将常规格式的数据集转换为 MindSpore数据格式,即 MindRecord,从而方便地加载到 MindSpore 中进行训练。同时,MindSpore 在部分场景做了性能优化,使用 MindRecord 数据格式可以获得更好的性能。
-在读取和解析数据过程中,MindSpore提供了一种更友好的数据格式 - [MindRecord](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/convert_dataset.html)。用户可以将常规格式的数据集转换为 MindSpore数据格式,即MindRecord,从而方便地加载到MindSpore中进行训练。同时,MindSpore在部分场景做了性能优化,使用MindSpore数据格式可以获得更好的性能。
+数据处理通常是数据准备中最耗时的阶段,大部分对数据的操作都被包含在这一步骤里,例如 CV 类网络中的Resize、Rescale、Crop 等操作。MindSpore 提供了一套常用的数据处理集成接口,用户可以不用自己实现而直接调用这些接口,这些集成接口不仅可以提升用户的易用性,还可以提升数据预处理的性能,减少训练过程中数据准备的耗时。具体可以参考[数据预处理教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/optimize_data_processing.html)。
-数据处理通常是数据准备中最耗时的阶段,大部分对数据的操作都被包含在这一步骤里,例如CV类网络中的Resize、Rescale、Crop等操作。MindSpore提供了一套常用的数据处理集成接口,用户可以不用自己实现而直接调用这些接口,这些集成接口不仅可以提升用户的易用性,还可以提升数据预处理的性能,减少训练过程中数据准备的耗时。具体可以参考。
+在数据分发环节,MindData 提供了极为简洁的 API,可以通过直接调用 batch、repeat 等操作完成数据的 batch 组合、重复等操作。
-在数据分发环节,MindData提供了极为简洁的API,可以通过直接调用batch、repeat等操作完成数据的batch组合、重复等操作。
+当完成以上4个步骤后,我们理论上使用 MindSpore 脚本和对标脚本处理数据集后,可以得到完全相同的数据(如果有引入随机情况的操作需要去除)。
-当完成以上4个步骤后,我们使用MindSpore脚本和对标脚本处理数据集后,可以得到完全相同的数据(如果有引入随机情况的操作需要去除)。
+#### ResNet50 迁移示例
-以[ResNet50网络](https://gitee.com/mindspore/mindspore/blob/master/model_zoo/official/cv/resnet/src/dataset.py)和CIFAR-10数据预处理为例:
+ResNet50 网络使用的是 ImageNet2012 数据集,其数据预处理的 PyTorch 代码如下:
+
+```python
+# sample execution (requires torchvision)
+from PIL import Image
+from torchvision import transforms
+input_image = Image.open(filename)
+preprocess = transforms.Compose([
+ transforms.Resize(256),
+ transforms.CenterCrop(224),
+ transforms.ToTensor(),
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
+])
+input_tensor = preprocess(input_image)
+input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
+```
+
+通过观察以上代码,我们发现 ResNet50 的数据预处理主要做了 Resize、CenterCrop、Normalize 操作,在 MindSpore 中实现这些操作有两种方式,一是使用 MindSpore 的数据处理模块 MindData 来调用已封装好的数据预处理接口,二是通过 [自定义数据集](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/dataset_loading.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%95%B0%E6%8D%AE%E9%9B%86%E5%8A%A0%E8%BD%BD) 进行加载。这里更建议开发者选择第一种方式,这样不仅可以减少重复代码的开发,减少错误的引入,还可以得到更好的数据处理性能。更多关于 MindData 数据处理的介绍,可参考 [MindData API](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/data_pipeline.html) 介绍。
+
+以下是基于 MindData 开发的数据处理函数:
```python
def create_dataset(dataset_path, do_train, repeat_num=1, batch_size=32, target="Ascend", distribute=False):
@@ -152,85 +478,788 @@ def create_dataset(dataset_path, do_train, repeat_num=1, batch_size=32, target="
do_train(bool): whether dataset is used for train or eval.
repeat_num(int): the repeat times of dataset. Default: 1
batch_size(int): the batch size of dataset. Default: 32
- target(str): the device target. Default: Ascend
distribute(bool): data for distribute or not. Default: False
Returns:
dataset
"""
- if target == "Ascend":
- device_num, rank_id = _get_rank_info()
+ # device number: total number of devices of training
+ # rank_id: the sequence of current device of training
+ device_num, rank_id = _get_rank_info()
+ if distribute:
+ init()
+ rank_id = get_rank()
+ device_num = get_group_size()
else:
- if distribute:
- init()
- rank_id = get_rank()
- device_num = get_group_size()
- else:
- device_num = 1
+ device_num = 1
if device_num == 1:
- # 单机训练
- # num_paralel_workers: 并行数据处理的并行度
- # shuffle: 是否打乱原数据顺序
- data_set = ds.Cifar10Dataset(dataset_path, num_parallel_workers=8, shuffle=True)
+ # standalone training
+ # num_paralel_workers: parallel degree of data process
+ # shuffle: whether shuffle data or not
+ data_set = ds.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True)
else:
- # 多机训练 (num_parallel_workers, shuffle 意义同上)
- # num_shards: 分布式训练的总机器数量,即数据要被切分的总数量
- # shard_id: 当前机器在所有训练机器的序列,该机器只能获取第shard_id份数据
- data_set = ds.Cifar10Dataset(dataset_path, num_parallel_workers=8, shuffle=True,
- num_shards=device_num, shard_id=rank_id)
+ # distributing traing (meaning of num_parallel_workers and shuffle is same as above)
+ # num_shards: total number devices for distribute training, which equals number shard of data
+ # shard_id: the sequence of current device in all distribute training devices, which equals the data shard sequence for current device
+ data_set = ds.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True,
+ num_shards=device_num, shard_id=rank)
- # define map operations
+ # define data operations
trans = []
if do_train:
trans += [
- # 声明要使用的MindData内置数据处理接口,提升处理性能(注意,这两个操作会引入随机情况)
- C.RandomCrop((32, 32), (4, 4, 4, 4)),
C.RandomHorizontalFlip(prob=0.5)
]
trans += [
- # 声明要使用的MindData内置数据处理接口,提升处理性能
- C.Resize((224, 224)),
+ C.Resize((256, 256)),
+ C.CenterCrop(224),
C.Rescale(1.0 / 255.0, 0.0),
- C.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]),
+ C.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
C.HWC2CHW()
]
type_cast_op = C2.TypeCast(mstype.int32)
- # 调用已定义好的内置预处理接口
+ # call data operations by map
data_set = data_set.map(operations=type_cast_op, input_columns="label", num_parallel_workers=8)
data_set = data_set.map(operations=trans, input_columns="image", num_parallel_workers=8)
- # 将处理好的数据已经batch操作
+ # batchinng data
data_set = data_set.batch(batch_size, drop_remainder=True)
- # 将已做过batch的数据进行repeat操作,repeat_num代表数据被重复的次数
+ # repeat data, usually repeat_num equals epoch_size
data_set = data_set.repeat(repeat_num)
return data_set
```
+在以上代码中我们可以发现,针对常用的经典数据集(如 ImageNet2012),MindData 也为我们提供了 `ImageFolderDataset` 接口直接读取原始数据,省去了手写代码读取文件的工作量。需要注意的是,单机训练和多机分布式训练时 MindData 创建数据集的参数是不一样的,分布式训练需要额外指定 `num_shard` 和 `shard_id` 两个参数。
+
### 子网开发
-通常子网开发包含两个部分:训练子网和loss子网,其中训练子网可根据网络的复杂程度决定是否继续划分。直接开发一个大型的神经网络脚本可能会让我们无从下手,因此,我们可以将网络中不同模块或子模块作为一个个子网抽离出来单独开发,这样可以保证各个子网并行开发,互相不受干扰。子网开发完成后,还可以固定子网输入和权重,与对标脚本的子网代码形成对比,作为后续网络开发的测试用例。
+通常子网开发包含两个部分:训练子网和 loss 子网,其中训练子网可根据网络的复杂程度决定是否继续划分。直接开发一个大型的神经网络脚本可能会让我们无从下手,因此,我们可以将网络中不同模块或子模块作为一个个子网抽离出来单独开发,这样可以保证各个子网并行开发,互相不受干扰。子网开发完成后,还可以固定子网输入和权重,与对标脚本的子网代码形成对比,作为后续网络开发的测试用例。
+
+在精度调优阶段,我们常常会遇到精度不达标的情况,这时我们会重新审视已开发的脚本并逐行排查。而使用子网方式开发脚本并形成测试用例可以高效地帮助我们排除怀疑点,从几十个算子里寻找可疑点,要比从成百上千个算子中找可疑点轻松得多,尤其是在很多时候,同一个子网会被重复调用多次,当我们以子网为单位排查时,可以减少很多工作量。
+
+#### ResNet50 迁移示例
+
+分析 ResNet50 网络代码,主要可以分成以下几个子网:
+
+- conv1x1、conv3x3:定义了不同 kernel_size 的卷积。
+- BasicBlock:ResNet 系列网络中 ResNet18 和 ResNet34 的最小子网,由 Conv、BN、ReLU 和 残差组成。
+- BottleNeck:ResNet 系列网络中 ResNet50、ResNet101 和 ResNet152 的最小子网,相比 BasicBlock 多了一层 Conv、BN 和 ReLU的结构,下采样的卷积位置也做了改变。
+- ResNet:封装了 BasiclBlock、BottleNeck 和 Layer 结构的网络,传入不同的参数即可构造不同的ResNet系列网络。在该结构中,也使用了一些 PyTorch 自定义的初始化功能。
+
+基于以上子网划分,我们结合 MindSpore 语法,重新完成上述开发。
+
+重新开发权重初始化(也可以直接使用 [MindSpore 已定义的权重初始化方法](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.common.initializer.html)):
+
+```python
+def _conv_variance_scaling_initializer(in_channel, out_channel, kernel_size):
+ fan_in = in_channel * kernel_size * kernel_size
+ scale = 1.0
+ scale /= max(1., fan_in)
+ stddev = (scale ** 0.5) / .87962566103423978
+ mu, sigma = 0, stddev
+ weight = truncnorm(-2, 2, loc=mu, scale=sigma).rvs(out_channel * in_channel * kernel_size * kernel_size)
+ weight = np.reshape(weight, (out_channel, in_channel, kernel_size, kernel_size))
+ return Tensor(weight, dtype=mstype.float32)
+
+
+def _weight_variable(shape, factor=0.01):
+ init_value = np.random.randn(*shape).astype(np.float32) * factor
+ return Tensor(init_value)
+
+
+def calculate_gain(nonlinearity, param=None):
+ """calculate_gain"""
+ linear_fns = ['linear', 'conv1d', 'conv2d', 'conv3d', 'conv_transpose1d', 'conv_transpose2d', 'conv_transpose3d']
+ res = 0
+ if nonlinearity in linear_fns or nonlinearity == 'sigmoid':
+ res = 1
+ elif nonlinearity == 'tanh':
+ res = 5.0 / 3
+ elif nonlinearity == 'relu':
+ res = math.sqrt(2.0)
+ elif nonlinearity == 'leaky_relu':
+ if param is None:
+ negative_slope = 0.01
+ elif not isinstance(param, bool) and isinstance(param, int) or isinstance(param, float):
+ # True/False are instances of int, hence check above
+ negative_slope = param
+ else:
+ raise ValueError("negative_slope {} not a valid number".format(param))
+ res = math.sqrt(2.0 / (1 + negative_slope ** 2))
+ else:
+ raise ValueError("Unsupported nonlinearity {}".format(nonlinearity))
+ return res
+
+
+def _calculate_fan_in_and_fan_out(tensor):
+ """_calculate_fan_in_and_fan_out"""
+ dimensions = len(tensor)
+ if dimensions < 2:
+ raise ValueError("Fan in and fan out can not be computed for tensor with fewer than 2 dimensions")
+ if dimensions == 2: # Linear
+ fan_in = tensor[1]
+ fan_out = tensor[0]
+ else:
+ num_input_fmaps = tensor[1]
+ num_output_fmaps = tensor[0]
+ receptive_field_size = 1
+ if dimensions > 2:
+ receptive_field_size = tensor[2] * tensor[3]
+ fan_in = num_input_fmaps * receptive_field_size
+ fan_out = num_output_fmaps * receptive_field_size
+ return fan_in, fan_out
+
+
+def _calculate_correct_fan(tensor, mode):
+ mode = mode.lower()
+ valid_modes = ['fan_in', 'fan_out']
+ if mode not in valid_modes:
+ raise ValueError("Mode {} not supported, please use one of {}".format(mode, valid_modes))
+ fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor)
+ return fan_in if mode == 'fan_in' else fan_out
+
+
+def kaiming_normal(inputs_shape, a=0, mode='fan_in', nonlinearity='leaky_relu'):
+ fan = _calculate_correct_fan(inputs_shape, mode)
+ gain = calculate_gain(nonlinearity, a)
+ std = gain / math.sqrt(fan)
+ return np.random.normal(0, std, size=inputs_shape).astype(np.float32)
+```
+
+重新开发卷积核为 3x3 和 1x1 的卷积算子:
+
+```python
+# conv3x3 and conv1x1
+def _conv3x3(in_channel, out_channel, stride=1):
+ weight_shape = (out_channel, in_channel, 3, 3)
+ # unlike pytorch, weight initialization is introduced when define conv2d
+ weight = Tensor(kaiming_normal(weight_shape, mode="fan_out", nonlinearity='relu'))
+ return nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=stride,
+ padding=0, pad_mode='same', weight_init=weight)
+
+
+def _conv1x1(in_channel, out_channel, stride=1):
+ # unlike pytorch, weight initialization is introduced when define conv2d
+ weight_shape = (out_channel, in_channel, 1, 1)
+ weight = Tensor(kaiming_normal(weight_shape, mode="fan_out", nonlinearity='relu'))
+ return nn.Conv2d(in_channel, out_channel, kernel_size=1, stride=stride,
+ padding=0, pad_mode='same', weight_init=weight)
+```
+
+重新开发 BasicBlock 和 BottleNeck:
+
+```python
+class BasicBlock(nn.Cell):
+ """
+ ResNet V1 residual block definition.
+
+ Args:
+ in_channel (int): Input channel.
+ out_channel (int): Output channel.
+ stride (int): Stride size for the first convolutional layer. Default: 1.
+ Returns:
+ Tensor, output tensor.
+
+ Examples:
+ >>> BasicBlock(3, 256, stride=2)
+ """
+
+ def __init__(self,
+ in_channel,
+ out_channel,
+ stride=1):
+ super(BasicBlock, self).__init__()
+ self.conv1 = _conv3x3(in_channel, out_channel, stride=stride)
+ self.bn1d = _bn(out_channel)
+ self.conv2 = _conv3x3(out_channel, out_channel, stride=1)
+ self.bn2d = _bn(out_channel)
+ self.relu = nn.ReLU()
+
+ self.down_sample = False
+ if stride != 1 or in_channel != out_channel:
+ self.down_sample = True
+
+ self.down_sample_layer = None
+ if self.down_sample:
+ self.down_sample_layer = nn.SequentialCell([_conv1x1(in_channel, out_channel, stride,),
+ _bn(out_channel)])
+
+ def construct(self, x):
+ identity = x
+
+ out = self.conv1(x)
+ out = self.bn1d(out)
+ out = self.relu(out)
+
+ out = self.conv2(out)
+ out = self.bn2d(out)
+
+ if self.down_sample:
+ identity = self.down_sample_layer(identity)
+
+ out = out + identity
+ out = self.relu(out)
+
+ return out
+
+
+class BottleNeck(nn.Cell):
+ """
+ ResNet V1.5 residual block definition.
+
+ Args:
+ in_channel (int): Input channel.
+ out_channel (int): Output channel.
+ stride (int): Stride size for the first convolutional layer. Default: 1.
+
+ Returns:
+ Tensor, output tensor.
+
+ Examples:
+ >>> ResidualBlock(3, 256, stride=2)
+ """
+ expansion = 4
+
+ def __init__(self,
+ in_channel,
+ out_channel,
+ stride=1):
+ super(BottleNeck, self).__init__()
+ self.stride = stride
+ channel = out_channel // self.expansion
+ self.conv1 = _conv1x1(in_channel, channel, stride=1)
+ self.bn1 = _bn(channel)
+ self.conv2 = _conv3x3(channel, channel, stride=stride)
+ self.bn2 = _bn(channel)
+ self.conv3 = _conv1x1(channel, out_channel, stride=1)
+ self.bn3 = _bn_last(out_channel)
+ self.relu = nn.ReLU()
+ self.down_sample = False
+ if stride != 1 or in_channel != out_channel:
+ self.down_sample = True
+ self.down_sample_layer = None
+ if self.down_sample:
+ self.down_sample_layer = nn.SequentialCell([_conv1x1(in_channel, out_channel, stride), _bn(out_channel)])
+
+ def construct(self, x):
+ identity = x
+ out = self.conv1(x)
+ out = self.bn1(out)
+ out = self.relu(out)
+ out = self.conv2(out)
+ out = self.bn2(out)
+ out = self.relu(out)
+ out = self.conv3(out)
+ out = self.bn3(out)
+ if self.down_sample:
+ identity = self.down_sample_layer(identity)
+
+ out = out + identity
+ out = self.relu(out)
+ return out
+```
+
+重新开发 ResNet 系列整网:
+
+```python
+class ResNet(nn.Cell):
+ """
+ ResNet architecture.
+
+ Args:
+ block (Cell): Block for network.
+ layer_nums (list): Numbers of block in different layers.
+ in_channels (list): Input channel in each layer.
+ out_channels (list): Output channel in each layer.
+ strides (list): Stride size in each layer.
+ num_classes (int): The number of classes that the training images are belonging to.
+
+ Returns:
+ Tensor, output tensor.
+
+ Examples:
+ >>> ResNet(ResidualBlock,
+ >>> [3, 4, 6, 3],
+ >>> [64, 256, 512, 1024],
+ >>> [256, 512, 1024, 2048],
+ >>> [1, 2, 2, 2],
+ >>> 10)
+ """
+
+ def __init__(self,
+ block,
+ layer_nums,
+ in_channels,
+ out_channels,
+ strides,
+ num_classes):
+ super(ResNet, self).__init__()
+
+ if not len(layer_nums) == len(in_channels) == len(out_channels) == 4:
+ raise ValueError("the length of layer_num, in_channels, out_channels list must be 4!")
+
+ self.conv1 = _conv7x7(3, 64, stride=2)
+ self.bn1 = _bn(64)
+ self.relu = P.ReLU()
+ self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="same")
+
+ self.layer1 = self._make_layer(block,
+ layer_nums[0],
+ in_channel=in_channels[0],
+ out_channel=out_channels[0],
+ stride=strides[0])
+ self.layer2 = self._make_layer(block,
+ layer_nums[1],
+ in_channel=in_channels[1],
+ out_channel=out_channels[1],
+ stride=strides[1])
+ self.layer3 = self._make_layer(block,
+ layer_nums[2],
+ in_channel=in_channels[2],
+ out_channel=out_channels[2],
+ stride=strides[2])
+ self.layer4 = self._make_layer(block,
+ layer_nums[3],
+ in_channel=in_channels[3],
+ out_channel=out_channels[3],
+ stride=strides[3])
+
+ self.mean = P.ReduceMean(keep_dims=True)
+ self.flatten = nn.Flatten()
+ self.end_point = _fc(out_channels[3], num_classes)
+
+ def _make_layer(self, block, layer_num, in_channel, out_channel, stride):
+ """
+ Make stage network of ResNet.
+
+ Args:
+ block (Cell): Resnet block.
+ layer_num (int): Layer number.
+ in_channel (int): Input channel.
+ out_channel (int): Output channel.
+ stride (int): Stride size for the first convolutional layer.
+ Returns:
+ SequentialCell, the output layer.
+
+ Examples:
+ >>> _make_layer(ResidualBlock, 3, 128, 256, 2)
+ """
+ layers = []
+
+ resnet_block = block(in_channel, out_channel, stride=stride)
+ layers.append(resnet_block)
+ for _ in range(1, layer_num):
+ resnet_block = block(out_channel, out_channel, stride=1)
+ layers.append(resnet_block)
+ return nn.SequentialCell(layers)
+
+ def construct(self, x):
+ x = self.conv1(x)
+ x = self.bn1(x)
+ x = self.relu(x)
+ c1 = self.maxpool(x)
+
+ c2 = self.layer1(c1)
+ c3 = self.layer2(c2)
+ c4 = self.layer3(c3)
+ c5 = self.layer4(c4)
-在精度调优阶段,我们常常会遇到精度不达标的情况,这时我们会重新审视已开发的脚本并逐行排查。而使用子网方式开发脚本并形成测试用例可以高效地帮助我们的排除怀疑点,从几十个算子里寻找可疑点,要比从成百上千个算子中找可疑点轻松得多,尤其是在很多时候,同一个子网会被重复调用多次,当我们以子网为单位排查时,可以减少很多工作量。
+ out = self.mean(c5, (2, 3))
+ out = self.flatten(out)
+ out = self.end_point(out)
+
+ return out
+```
+
+传入 ResNet50 层数信息,构造 ResNet50 整网:
+
+```python
+def resnet50(class_num=1000):
+ """
+ Get ResNet50 neural network.
+
+ Args:
+ class_num (int): Class number.
+
+ Returns:
+ Cell, cell instance of ResNet50 neural network.
+
+ Examples:
+ >>> net = resnet50(10)
+ """
+ return ResNet(ResidualBlock,
+ [3, 4, 6, 3],
+ [64, 256, 512, 1024],
+ [256, 512, 1024, 2048],
+ [1, 2, 2, 2],
+ class_num)
+```
+
+经过以上步骤,基于 MindSpore 的 ResNet50 整网结构和各子网结构已经开发完成,接下来就是开发其他模块。
### 其他模块
其他模块通常包括:反向构造、梯度裁剪、优化器、学习率生成等,这些模块要么本身结构单一,要么依赖已开发完成的子网结果才能和对标脚本形成对比。相比子网开发,这些模块的脚本开发难度更小一些。
+#### ResNet50 迁移示例
+
+关于其他训练配置,可以参考 [英伟达训练 ResNet50 的配置信息](https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/Classification/ConvNets/resnet50v1.5#default-configuration),ResNet50 的训练主要涉及以下几项:
+
+- 使用了 SGD + Momentum 优化器
+- 使用了 WeightDecay 功能(但 BatchNorm 的 gamma 和 bias 没有使用)
+- 使用了 cosine LR schedule
+- 使用了 Label Smoothing
+
+实现 cosine LR schedule:
+
+```python
+def _generate_cosine_lr(lr_init, lr_end, lr_max, total_steps, warmup_steps):
+ """
+ Applies cosine decay to generate learning rate array.
+
+ Args:
+ lr_init(float): init learning rate.
+ lr_end(float): end learning rate
+ lr_max(float): max learning rate.
+ total_steps(int): all steps in training.
+ warmup_steps(int): all steps in warmup epochs.
+
+ Returns:
+ np.array, learning rate array.
+ """
+ decay_steps = total_steps - warmup_steps
+ lr_each_step = []
+ for i in range(total_steps):
+ if i < warmup_steps:
+ lr_inc = (float(lr_max) - float(lr_init)) / float(warmup_steps)
+ lr = float(lr_init) + lr_inc * (i + 1)
+ else:
+ linear_decay = (total_steps - i) / decay_steps
+ cosine_decay = 0.5 * (1 + math.cos(math.pi * 2 * 0.47 * i / decay_steps))
+ decayed = linear_decay * cosine_decay + 0.00001
+ lr = lr_max * decayed
+ lr_each_step.append(lr)
+ return lr_each_step
+```
+
+实现带 Momentum 的 SGD 优化器,除 BN 的 gamma 和 bias 外,其他权重应用 WeightDecay :
+
+```python
+from mindspore.nn.optim import Momentum
+
+net = resnet50(class_num=1000)
+lr = _generate_cosine_lr()
+momentum = 0.875
+weight_decay = 1/32768
+
+decayed_params = []
+no_decayed_params = []
+for param in net.trainable_params():
+ if 'beta' not in param.name and 'gamma' not in param.name and 'bias' not in param.name:
+ decayed_params.append(param)
+ else:
+ no_decayed_params.append(param)
+
+group_params = [{'params': decayed_params, 'weight_decay': weight_decay},
+ {'params': no_decayed_params},
+ {'order_params': net.trainable_params()}]
+opt = Momentum(group_params, lr momentum)
+```
+
+定义 Loss 函数和实现 Label Smoothing:
+
+```python
+import mindspore.nn as nn
+from mindspore import Tensor
+from mindspore.common import dtype as mstype
+from mindspore.nn.loss.loss import _Loss
+from mindspore.ops import functional as F
+from mindspore.ops import operations as P
+
+# define cross entropy loss
+class CrossEntropySmooth(_Loss):
+ """CrossEntropy"""
+ def __init__(self, sparse=True, reduction='mean', smooth_factor=0., num_classes=1000):
+ super(CrossEntropySmooth, self).__init__()
+ self.onehot = P.OneHot()
+ self.sparse = sparse
+ self.on_value = Tensor(1.0 - smooth_factor, mstype.float32)
+ self.off_value = Tensor(1.0 * smooth_factor / (num_classes - 1), mstype.float32)
+ self.ce = nn.SoftmaxCrossEntropyWithLogits(reduction=reduction)
+
+ def construct(self, logit, label):
+ if self.sparse:
+ label = self.onehot(label, F.shape(logit)[1], self.on_value, self.off_value)
+ loss = self.ce(logit, label)
+ return loss
+
+# define loss with label smooth
+label_smooth_factor = 0.1
+loss = CrossEntropySmooth(sparse=True, reduction="mean",smooth_factor=label_smooth_factor, num_classes=1000)
+```
+
### 超参对比
-当各子网已经打通,最后一步要做的是和对标脚本对齐超参,保证网络结构一致。
+当各子网已经打通,最后一步要做的是和对标脚本对齐超参,保证网络结构一致。需要注意的是,在不同的框架上,同一套超参可能有不同的精度表现,在迁移网络时不一定要严格按照对标脚本的超参进行设置,可在不改变网络结构的情况下进行微调。
+
+#### ResNet50 迁移示例
+
+在 ResNet50 的训练中,主要涉及以下超参:
+
+- momentum =0.875
+- batch_size = 256
+- learning rate = 0.256
+- learing rate schedule = cosine
+- weight_decay = 1/32768
+- label_smooth = 0.1
+- epoch size = 90
-## 精度调试
+## 流程打通
-### 训练
+经过以上步骤后,我们已经开发完了网络迁移的必备脚本,接下来就是打通单机训练、分布式训练、推理流程。
### 单机训练
-### 多机训练精度调优
+#### ResNet50 迁移示例
+
+为了更好的阅读代码,建议按照以下结构组织脚本:
+
+```txt
+.
+└──resnet
+ ├── README.md
+ ├── scripts
+ ├── run_distribute_train.sh # 启动Ascend分布式训练(8卡)
+ ├── run_eval.sh # 启动Ascend评估
+ ├── run_standalone_train.sh # 启动Ascend单机训练(单卡)
+ ├── src
+ ├── config.py # 参数配置
+ ├── dataset.py # 数据预处理
+ ├── CrossEntropySmooth.py # ImageNet2012数据集的损失定义
+ ├── lr_generator.py # 生成每个步骤的学习率
+ └── resnet.py # ResNet骨干网络
+ ├── eval.py # 评估网络
+ └── train.py # 训练网络
+```
+
+其中 train.py 定义如下:
+
+```python
+import os
+import argparse
+import ast
+from mindspore import context
+from mindspore import Tensor
+from mindspore.nn.optim import Momentum
+from mindspore.train.model import Model
+from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
+from mindspore.train.loss_scale_manager import FixedLossScaleManager
+from mindspore.train.serialization import load_checkpoint, load_param_into_net
+from mindspore.communication.management import init, get_rank, get_group_size
+from mindspore.common import set_seed
+import mindspore.nn as nn
+import mindspore.common.initializer as weight_init
+from src.lr_generator import get_lr
+from src.CrossEntropySmooth import CrossEntropySmooth
+from src.config import cfg
+
+parser = argparse.ArgumentParser(description='Image classification')
+parser.add_argument('--net', type=str, default=None, help='Resnet Model, resnet50')
+parser.add_argument('--dataset', type=str, default=None, help='Dataset, imagenet2012')
+parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
+args_opt = parser.parse_args()
+set_seed(1)
+
+from src.resnet import resnet50 as resnet
+from src.config import config
+from src.dataset import create_dataset as create_dataset
+
+if __name__ == '__main__':
+ ckpt_save_dir = config.save_checkpoint_path
+
+ # init context
+ context.set_context(mode=context.GRAPH_MODE, save_graphs=False)
+ # create dataset
+ dataset = create_dataset(dataset_path=args_opt.dataset_path, do_train=True, repeat_num=1,
+ batch_size=config.batch_size)
+ step_size = dataset.get_dataset_size()
+
+ # define net
+ net = resnet(class_num=config.class_num)
+ for _, cell in net.cells_and_names():
+ if isinstance(cell, nn.Conv2d):
+ cell.weight.set_data(weight_init.initializer(weight_init.XavierUniform(),
+ cell.weight.shape,
+ cell.weight.dtype))
+ if isinstance(cell, nn.Dense):
+ cell.weight.set_data(weight_init.initializer(weight_init.TruncatedNormal(),
+ cell.weight.shape,
+ cell.weight.dtype))
+ lr = get_lr(lr_init=config.lr_init, lr_end=config.lr_end, lr_max=config.lr_max, warmup_epochs=config.warmup_epochs, total_epochs=config.epoch_size, steps_per_epoch=step_size,
+ lr_decay_mode=config.lr_decay_mode)
+ lr = Tensor(lr)
+
+ # define opt
+ decayed_params = []
+ no_decayed_params = []
+ for param in net.trainable_params():
+ if 'beta' not in param.name and 'gamma' not in param.name and 'bias' not in param.name:
+ decayed_params.append(param)
+ else:
+ no_decayed_params.append(param)
+
+ group_params = [{'params': decayed_params, 'weight_decay': config.weight_decay},
+ {'params': no_decayed_params},
+ {'order_params': net.trainable_params()}]
+ opt = Momentum(group_params, lr, config.momentum, loss_scale=config.loss_scale)
+ # define loss, model
+ loss = CrossEntropySmooth(sparse=True, reduction="mean", smooth_factor=config.label_smooth_factor, num_classes=config.class_num)
+ loss_scale = FixedLossScaleManager(config.loss_scale, drop_overflow_update=False)
+ model = Model(net, loss_fn=loss, optimizer=opt, loss_scale_manager=loss_scale, metrics={'acc'},
+ amp_level="O2", keep_batchnorm_fp32=False)
+ # define callbacks
+ time_cb = TimeMonitor(data_size=step_size)
+ loss_cb = LossMonitor()
+ cb = [time_cb, loss_cb]
+ if config.save_checkpoint:
+ config_ck = CheckpointConfig(save_checkpoint_steps=config.save_checkpoint_epochs * step_size,
+ keep_checkpoint_max=config.keep_checkpoint_max)
+ ckpt_cb = ModelCheckpoint(prefix="resnet", directory=ckpt_save_dir, config=config_ck)
+ cb += [ckpt_cb]
+
+ # train model
+ dataset_sink_mode = True
+ model.train(config.epoch_size, dataset, callbacks=cb, sink_size=dataset.get_dataset_size(), dataset_sink_mode=dataset_sink_mode)
+```
+
+注意:关于目录中其他文件的代码,可以参考 MindSpore model_zoo 的 [ResNet50 实现](https://gitee.com/mindspore/mindspore/tree/r1.2/model_zoo/official/cv/resnet)(该脚本融合了其他 ResNet 系列网络及ResNet-SE 网络,具体实现可能和对标脚本有差异)。
+
+### 分布式训练
+
+分布式训练相比单机训练对网络结构没有影响,可以通过调用 MindSpore 提供的分布式训练接口改造单机脚本即可完成分布式训练,具体可参考 [分布式训练教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/distributed_training_tutorials.html)。
+
+#### ResNet50 迁移示例
+
+对单机训练脚本添加以下接口:
+
+```python
+import os
+import argparse
+import ast
+from mindspore import context
+from mindspore.communication.management import init, get_rank, get_group_size
+from src.config import cfg
+
+# ....
+parser = argparse.ArgumentParser(description='Image classification')
+# add two new options to support both standalone and distribute training
+parser.add_argument('--run_distribute', type=ast.literal_eval, default=False, help='Run distribute')
+parser.add_argument('--device_num', type=int, default=1, help='Device num.')
+# ...
+device_id = int(os.getenv('DEVICE_ID')) # get the current device id
+context.set_context(device_id=device_id)
+# enable distribute training
+context.set_auto_parallel_context(device_num=args_opt.device_num, parallel_mode=ParallelMode.DATA_PARALLEL,
+ gradients_mean=True)
+# init distribute training
+init()
+```
+
+修改 create_dataset 接口,使数据加载时对数据进行 shard 操作以支持分布式训练:
+
+```python
+import mindspore.dataset as ds
+from mindspore.communication.management import init, get_rank, get_group_size
+# ....
+device_num, rank_id = _get_rank_info()
+if device_num == 1:
+ # standalone training
+ data_set = ds.Cifar10Dataset(dataset_path, num_parallel_workers=8, shuffle=True)
+else:
+ # distribute training
+ data_set = ds.Cifar10Dataset(dataset_path, num_parallel_workers=8, shuffle=True,
+ num_shards=device_num, shard_id=rank_id)
+# ...
+```
+
+### 推理
+
+推理流程与训练相比有以下不同:
+
+- 无需定义loss 和 优化器
+- 无需在构造数据集时进行 repeat 操作
+- 网络定义后需要加载已训练好的 CheckPoint
+- 定义计算推理精度的 metric
+
+#### ResNet50 迁移示例
+
+修改后的推理脚本:
+
+```python
+import os
+import argparse
+from mindspore import context
+from mindspore.common import set_seed
+from mindspore.train.model import Model
+from mindspore.train.serialization import load_checkpoint, load_param_into_net
+
+parser = argparse.ArgumentParser(description='Image classification')
+parser.add_argument('--net', type=str, default=None, help='Resnet Model, either resnet18, '
+ 'resnet50 or resnet101')
+parser.add_argument('--dataset', type=str, default=None, help='Dataset, either cifar10 or imagenet2012')
+
+parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
+parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
+parser.add_argument('--device_target', type=str, default='Ascend', choices=("Ascend", "GPU", "CPU"),
+ help="Device target, support Ascend, GPU and CPU.")
+args_opt = parser.parse_args()
+
+set_seed(1)
+
+from src.resnet import resnet50 as resnet
+from src.dataset import create_dataset
+from src.config import config as config
+
+
+if __name__ == '__main__':
+ target = args_opt.device_target
+
+ # init context
+ context.set_context(mode=context.GRAPH_MODE, device_target=target, save_graphs=False)
+ device_id = int(os.getenv('DEVICE_ID'))
+ context.set_context(device_id=device_id)
+
+ # create dataset
+ dataset = create_dataset(dataset_path=args_opt.dataset_path, do_train=False, batch_size=config.batch_size)
+ step_size = dataset.get_dataset_size()
+
+ # define net
+ net = resnet(class_num=config.class_num)
+
+ # load checkpoint
+ param_dict = load_checkpoint(args_opt.checkpoint_path)
+ load_param_into_net(net, param_dict)
+ net.set_train(False)
+
+ # define model
+ model = Model(net, metrics={'top_1_accuracy', 'top_5_accuracy'})
+
+ # eval model
+ res = model.eval(dataset)
+ print("result:", res, "ckpt=", args_opt.checkpoint_path)
+```
+
+### 问题定位
+
+在流程打通中可能会遇到一些中断训练的问题,可以参考 [网络训练调试教程](https://gitee.com/mindspore/docs/blob/r1.2/docs/migration_guide/source_zh_cn/neural_network_debug.md) 定位和解决。
+
+## 精度调优
+
+在打通流程后,就可以通过训练和推理两个步骤获得网络训练的精度。通常情况下,我们很难一次就复现对标脚本的精度,需要通过精度调优来逐渐提高精度,精度调优相比性能调优不够直观,效率低,工作量大。开发者可将我们提供的 [精度调优教程](https://gitee.com/mindspore/docs/blob/r1.2/docs/migration_guide/source_zh_cn/accuracy_optimization.md) 作为参考。
## 性能调优
@@ -240,32 +1269,32 @@ def create_dataset(dataset_path, do_train, repeat_num=1, batch_size=32, target="
### 分析Profiling数据
-分析Profiling数据是性能调优阶段必不可少的步骤,MindSpore的性能和精度调优工具[MindInsight](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/visualization_tutorials.html)提供了丰富的性能和精度调优方法,对于性能调优,最重要的信息就是Profiling数据。Profiling可以收集整网训练过程中端到端的详细性能数据,包含数据准备和迭代轨迹。在迭代轨迹中,你可以看到每个算子的起始运行时间、结束运行时间、调用次数和调用顺序等非常详细的信息,这对我们性能调优非常有帮助。生成Profiling数据的方式如下:
+分析Profiling数据是性能调优阶段必不可少的步骤,MindSpore 的性能和精度调优工具 [MindInsight](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/visualization_tutorials.html) 提供了丰富的性能和精度调优方法,对于性能调优,最重要的信息就是Profiling数据。Profiling可以收集整网训练过程中端到端的详细性能数据,包含数据准备和迭代轨迹。在迭代轨迹中,你可以看到每个算子的起始运行时间、结束运行时间、调用次数和调用顺序等非常详细的信息,这对我们性能调优非常有帮助。生成Profiling数据的方式如下:
```python
from mindspore.profiler import Profiler
from mindspore import Model, nn, context
-# 初始化context
+# init context
context.set_context(mode=context.GRAPH_MODE, device_target='Ascend', device_id=int(os.environ["DEVICE_ID"]))
-# 初始化profiler,默认数据会保存在当前路径的data目录下
+# init profiler, profiling data will be stored under folder ./data by default
profiler = Profiler()
-# 训练
+# start training
Model.train()
-# 训练结束,解析profiling数据成可读文本
+# end training,parse profiling data to readable text
profiler.analyse()
```
-关于Profiling更详细的方法,可以参考[性能调优教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/performance_profiling.html)。
+关于Profiling更详细的使用方法,可以参考 [Profiling 性能分析方法](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/performance_profiling.html)。
-获取到Profiling数据后,我们可以分析出性能瓶颈阶段和算子,然后使用以下手段优化性能。
+获取到 Profiling 数据后,我们可以分析出性能瓶颈阶段和算子,然后进行性能优化,可以参考 [性能调优指导](https://gitee.com/mindspore/docs/blob/r1.2/docs/migration_guide/source_zh_cn/performance_optimization.md)。
### 常见问题及相应优化方法
-#### MindData性能问题
+#### MindData 性能问题
单Step性能抖动、数据队列一段时间内持续为空的情况都是由于数据预处理部分性能较差,使得数据处理速度跟不上单Step迭代速度导致,这两个现象通常成对出现。
@@ -273,11 +1302,10 @@ profiler.analyse()
#### 多机同步性能问题
-当进行分布式训练时,在一个Step的训练过程中,完成前向传播和梯度计算后,各个机器开始进行AllReduce梯度同步,AllReduce同步时间主要受权重数量、机器数量影响,对于越复杂、机器规模越大的网络,其AllReduce梯度更新时间也越久,此时我们可以进行AllReduce切分来优化这部分耗时。
-
-正常情况下,AllReduce梯度同步会等所有反向算子执行结束,也就是对所有权重都计算出梯度后再一次性同步所有机器的梯度,而使用AllReduce切分后,我们可以在计算出一部分权重的梯度后,就立刻进行这部分权重的梯度同步,这样梯度同步和剩余算子的梯度计算可以并行执行,也就隐藏了这部分AllReduce梯度同步时间。
+当进行分布式训练时,在一个Step的训练过程中,完成前向传播和梯度计算后,各个机器开始进行AllReduce梯度同步,AllReduce同步时间主要受权重数量、机器数量影响,对于越复杂、机器规模越大的网络,其 AllReduce 梯度更新时间也越久,此时我们可以进行AllReduce 切分来优化这部分耗时。
-以 [ResNet50网络](https://gitee.com/mindspore/mindspore/blob/master/model_zoo/official/cv/resnet/train.py)为例:
+正常情况下,AllReduce 梯度同步会等所有反向算子执行结束,也就是对所有权重都计算出梯度后再一次性同步所有机器的梯度,而使用AllReduce切分后,我们可以在计算出一部分权重的梯度后,就立刻进行这部分权重的梯度同步,这样梯度同步和剩余算子的梯度计算可以并行执行,也就隐藏了这部分 AllReduce 梯度同步时间。切分策略通常是手动尝试,寻找一个最优的方案(支持切分大于两段)。
+以 [ResNet50网络](https://gitee.com/mindspore/mindspore/blob/r1.2/model_zoo/official/cv/resnet/train.py) 为例,该网络共有 160 个 权重, [85, 160] 表示第 0 至 85个权重计算完梯度后立刻进行梯度同步,第 86 至 160 个 权重计算完后再进行梯度同步,这里共切分两段,因此需要进行两次梯度同步。代码实现如下:
```python
from mindspore import context
@@ -289,23 +1317,20 @@ context.set_auto_parallel_context(device_num=args_opt.device_num, parallel_mode=
gradients_mean=True)
set_algo_parameters(elementwise_op_strategy_follow=True)
if args_opt.net == "resnet50" or args_opt.net == "se-resnet50":
- # AllReduce 切分
- # [85, 160] 表示前 0-85, 86-160个parameter计算完梯度后立刻进行梯度同步。resnet50共有160个parameter,因此这里进行两次梯度同步
- # 切分策略通常是手动尝试,寻找一个最优的方案(支持切分大于两段)
+ # AllReduce split
context.set_auto_parallel_context(all_reduce_fusion_config=[85, 160])
else:
- # 不同的网络结构可以设置不同的切分策略
+ # Another split stratety
context.set_auto_parallel_context(all_reduce_fusion_config=[180, 313])
init()
```
#### 算子性能问题
-单算子耗时久、对于同一种算子在不同shape或者不同datatype下性能差异较大的情况主要是由算子性能问题引起,通常有以下两个解决思路:
+单算子耗时久、对于同一种算子在不同shape或者不同 datatype 下性能差异较大的情况主要是由算子性能问题引起,通常有以下两个解决思路:
-1. 使用计算量更小的数据类型。例如,同一个算子在float16和float32下精度无明显差别,可使用计算量更小的float16格式。
-2. 使用Ascend芯片更亲和的Format。为了充分发挥Ascend芯片的算力,我们设计了几种Ascend亲和的Format,可以尝试使用这些Format。
-3. 使用算法相同的其他算子规避。
+1. 使用计算量更小的数据类型。例如,同一个算子在 float16 和 float32 下精度无明显差别,可使用计算量更小的 float16 格式。
+2. 使用算法相同的其他算子规避。
如果您发现有性能较差的算子时,建议联系 [MindSpore社区](https://gitee.com/mindspore/mindspore/issues) 反馈,我们确认为性能问题后会及时优化。
@@ -319,14 +1344,14 @@ init()
- 使用自动混合精度
- 混合精度训练方法是通过混合使用单精度和半精度数据格式来加速深度神经网络训练的过程,同时保持了单精度训练所能达到的网络精度。混合精度训练能够加速计算过程,同时减少内存使用和存取,并使得在特定的硬件上可以训练更大的模型或batch size。
+ 混合精度训练方法是通过混合使用单精度和半精度数据格式来加速深度神经网络训练的过程,同时保持了单精度训练所能达到的网络精度。混合精度训练能够加速计算过程,同时减少内存使用和存取,并使得在特定的硬件上可以训练更大的模型或 batch size。
- 具体可参考[混合精度教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/enable_mixed_precision.html)。
+ 具体可参考 [混合精度教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/enable_mixed_precision.html)。
- 使能图算融合
- 图算融合是MindSpore特有的网络性能优化技术。它可以通过自动分析和优化现有网络计算图逻辑,并结合目标硬件能力,对计算图进行计算化简和替代、算子拆分和融合、算子特例化编译等优化,以提升设备计算资源利用率,实现对网络性能的整体优化。相比传统优化技术,图算融合具有多算子跨边界联合优化、与算子编译跨层协同、基于Polyhedral的算子即时编译等独特优势。另外,图算融合只需要用户打开对应配置后,整个优化过程即可自动完成,不需要网络开发人员进行其它额外感知,使得用户可以聚焦网络算法实现。
+ 图算融合是 MindSpore 特有的网络性能优化技术。它可以通过自动分析和优化现有网络计算图逻辑,并结合目标硬件能力,对计算图进行计算化简和替代、算子拆分和融合、算子特例化编译等优化,以提升设备计算资源利用率,实现对网络性能的整体优化。相比传统优化技术,图算融合具有多算子跨边界联合优化、与算子编译跨层协同、基于Polyhedral的算子即时编译等独特优势。另外,图算融合只需要用户打开对应配置后,整个优化过程即可自动完成,不需要网络开发人员进行其它额外感知,使得用户可以聚焦网络算法实现。
图算融合的适用场景包括:对网络执行时间具有较高性能要求的场景;通过拼接基本算子实现自定义组合算子,并希望对这些基本算子进行自动融合,以提升自定义组合算子性能的场景。
- 具体可参考[图片融合教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.1/advanced_use/enable_graph_kernel_fusion.html)。
+ 具体可参考 [图算融合教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/enable_graph_kernel_fusion.html)。
diff --git a/docs/migration_guide/source_zh_cn/script_analysis.md b/docs/migration_guide/source_zh_cn/script_analysis.md
index 31a4924d3e40fd3783fe7c0887b2ea9cb84945e2..2ec95bd0df92da94340a69297bceff6a7a3e8f1c 100644
--- a/docs/migration_guide/source_zh_cn/script_analysis.md
+++ b/docs/migration_guide/source_zh_cn/script_analysis.md
@@ -13,7 +13,7 @@
-
+
## 算子评估
@@ -41,11 +41,11 @@ MindSpore API由各种Python/C++ API算子组成,可以大致分为:
### 查询算子映射表
-在代码库找到网络结构及实现训练功能的Python文件(名称一般为train.py model.py等等),在脚本文件中查找所有相关算子(含数据框架类、数据预处理、网络结构算子),并与[MindSpore算子API](https://www.mindspore.cn/doc/note/zh-CN/master/operator_list_ms.html)对比,查找`mindspore.nn`或者`mindspore.ops.operations`下算子的平台支持情况,目前支持Ascend、CPU与GPU。
+在代码库找到网络结构及实现训练功能的Python文件(名称一般为train.py model.py等等),在脚本文件中查找所有相关算子(含数据框架类、数据预处理、网络结构算子),并与[MindSpore算子API](https://www.mindspore.cn/doc/note/zh-CN/r1.2/operator_list_ms.html)对比,查找`mindspore.nn`或者`mindspore.ops.operations`下算子的平台支持情况,目前支持Ascend、CPU与GPU。
-若该网页均未能找到对应的ME算子,则可继续在[MindSpore API列表](https://www.mindspore.cn/doc/api_python/zh-CN/master/index.html)中搜索算子名称。
+若该网页均未能找到对应的ME算子,则可继续在[MindSpore API列表](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/index.html)中搜索算子名称。
-若源码为PyTorch脚本,则可以直接查询[MindSpore与PyTorch的算子映射](https://www.mindspore.cn/doc/note/zh-CN/master/index.html#operator_api)找到对应的MindSpore算子。注意,针对相同功能的算子,MindSpore的命名可能与其他框架不同,同名算子参数与功能也可能与其他框架有区别,均以官方描述为准。
+若源码为PyTorch脚本,则可以直接查询[MindSpore与PyTorch的算子映射](https://www.mindspore.cn/doc/note/zh-CN/r1.2/index.html#operator_api)找到对应的MindSpore算子。注意,针对相同功能的算子,MindSpore的命名可能与其他框架不同,同名算子参数与功能也可能与其他框架有区别,均以官方描述为准。
### 缺失算子处理策略
@@ -57,7 +57,7 @@ MindSpore API由各种Python/C++ API算子组成,可以大致分为:
MindSpore提供`GRAPH_MODE`和`PYNATIVE_MODE`两种模式。语法限制仅出现在GRAPH_MODE中,PyNative模式下网络的执行办法与一般Python代码无异。
-语法限制问题通常出现在GRAPH_MODE和PYNATIVE_MODE的反向构图中。在这两种情况下,需要对Python代码进行图编译操作,而这一步操作中MindSpore目前还未能支持完整的Python语法全集,所以`construct`函数的编写会存在部分限制。具体限制内容可以参考[MindSpore静态图语法](https://www.mindspore.cn/doc/note/zh-CN/master/static_graph_syntax_support.html)。
+语法限制问题通常出现在GRAPH_MODE和PYNATIVE_MODE的反向构图中。在这两种情况下,需要对Python代码进行图编译操作,而这一步操作中MindSpore目前还未能支持完整的Python语法全集,所以`construct`函数的编写会存在部分限制。具体限制内容可以参考[MindSpore静态图语法](https://www.mindspore.cn/doc/note/zh-CN/r1.2/static_graph_syntax_support.html)。
### 常见限制原则
diff --git a/docs/migration_guide/source_zh_cn/script_development.md b/docs/migration_guide/source_zh_cn/script_development.md
index 37f30aea4dc1527e8986bb361d8b258177bb8849..518eb6dc6ef22bc78e0116d3913eee85bfc10fd0 100644
--- a/docs/migration_guide/source_zh_cn/script_development.md
+++ b/docs/migration_guide/source_zh_cn/script_development.md
@@ -444,7 +444,7 @@
return out
```
- PyTorch和MindSpore在一些基础API的定义上比较相似,比如[mindspore.nn.SequentialCell](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/nn/mindspore.nn.SequentialCell.html?highlight=sequentialcell#mindspore.nn.SequentialCell)和[torch.nn.Sequential](https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html?highlight=sequential#torch.nn.Sequential),另外,一些算子API可能不尽相同,此处列举一些常见的API对照,更多信息可以参考MindSpore官网的[算子列表](https://www.mindspore.cn/doc/note/zh-CN/master/index.html#operator_api)。
+ PyTorch和MindSpore在一些基础API的定义上比较相似,比如[mindspore.nn.SequentialCell](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/nn/mindspore.nn.SequentialCell.html#mindspore.nn.SequentialCell)和[torch.nn.Sequential](https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html#torch.nn.Sequential),另外,一些算子API可能不尽相同,此处列举一些常见的API对照,更多信息可以参考MindSpore官网的[算子列表](https://www.mindspore.cn/doc/note/zh-CN/r1.2/index.html#operator_api)。
| PyTorch | MindSpore |
| :-------------------------------: | :------------------------------------------------: |
diff --git a/docs/migration_guide/source_zh_cn/torch_bert_migration_case_of_mindconverter.ipynb b/docs/migration_guide/source_zh_cn/torch_bert_migration_case_of_mindconverter.ipynb
index 1cf73d2e01e32a39de9e202b2e969bdce9e7cbaa..afb1f57c1b9665338ce59b715d1a1dbd1946b38a 100644
--- a/docs/migration_guide/source_zh_cn/torch_bert_migration_case_of_mindconverter.ipynb
+++ b/docs/migration_guide/source_zh_cn/torch_bert_migration_case_of_mindconverter.ipynb
@@ -8,7 +8,7 @@
"# PyTorch BERT迁移案例\n",
"`Linux` `Ascend` `GPU` `CPU` `模型迁移` `初级` `中级` `高级`\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/migration_guide/source_zh_cn/torch_bert_migration_case_of_mindconverter.ipynb)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/migration_guide/source_zh_cn/torch_bert_migration_case_of_mindconverter.ipynb)"
]
},
{
diff --git a/docs/note/source_en/design/mindspore/mindir.md b/docs/note/source_en/design/mindspore/mindir.md
index 791f507db3b418223dce1c792a7ba0e3c46c6ed6..030fb3521cca73b3e6784ccad082d9aca87e3401 100644
--- a/docs/note/source_en/design/mindspore/mindir.md
+++ b/docs/note/source_en/design/mindspore/mindir.md
@@ -1,7 +1,7 @@
# MindSpore IR (MindIR)
-`Linux` `Windows` `Framework Development` `Intermediate` `Expert` `Contributor`
+`Linux` `Windows` `Ascend` `GPU` `Framework Development` `Intermediate` `Model Development` `Expert` `Contributor`
@@ -10,6 +10,7 @@
- [Syntax](#syntax)
- [Example](#example)
- [Saving IR](#saving-ir)
+ - [IR File Contents Introduction](#ir-file-contents-introduction)
- [Function-style Semantics](#function-style-semantics)
- [Higher-Order Functions](#higher-order-functions)
- [Control Flows](#control-flows)
@@ -18,13 +19,21 @@
-
+
## Overview
An intermediate representation (IR) is a representation of a program between the source and target languages, which facilitates program analysis and optimization for the compiler. Therefore, the IR design needs to consider the difficulty in converting the source language to the target language, as well as the ease-of-use and performance of program analysis and optimization.
-MindSpore IR (MindIR) is a function-style IR based on graph representation. Its core purpose is to serve automatic differential transformation. Automatic differentiation uses the transformation method based on the function-style programming framework. Therefore, IR uses the semantics close to that of the ANF function. In addition, a manner of representation based on an explicit dependency graph is used by referring to excellent designs of Sea of Nodes[1] and Thorin[2].
+MindSpore IR (MindIR) is a function-style IR based on graph representation. Its core purpose is to serve automatic differential transformation. Automatic differentiation uses the transformation method based on the function-style programming framework. Therefore, IR uses the semantics close to that of the ANF function. In addition, a manner of representation based on an explicit dependency graph is used by referring to excellent designs of Sea of Nodes[1] and Thorin[2]. For the specific introduction of ANF-IR, please refer to [MindSpore IR Syntax](#syntax).
+
+When a model compiled using MindSpore runs in the graph mode `context.set_context(mode=context.GRAPH_MODE)` and `context.set_context(save_graphs=True)` is set in the configuration, some intermediate files will be generated during graph compliation. These intermediate files are called IR files. Currently, there are three IR files:
+
+- .ir file: An IR file that describes the model structure in text format and can be directly viewed using any text editors. We will also introduce how to view it in the following sections.
+
+- .dat file: An IR file that describes the model structure more strictly than the .ir file. It contains more contents and can be directly viewed using any text editors.
+
+- .dot file: An IR file that describes the topology relationships between different nodes. You can use this file by [graphviz](http://graphviz.org/) as the input to generate images for users to view the model structure. For models with multiple operators, it is recommended using the visualization component [MindInsight](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/dashboard.html#computational-graph-visualization) to visualize computing graphs.
## Syntax
@@ -102,6 +111,136 @@ In the ANF, each expression is bound as a variable by using the let expression,
You can run the graphviz command to convert a .dot file to the picture format. For example, you can run the `dot -Tpng *.dot -o *.png` command to convert a .dot file to a .png file.
+Add the following code to `train.py`, When running the script, MindSpore will automatically store the IR files generated during compilation under the specified path.
+
+```python
+if __name__ == "__main__":
+ context.set_context(save_graphs=True, save_graphs_path="path/to/ir/files")
+```
+
+Here, we run the training script on stand-alone computing device. When running on multiple computing devices, MindSpore will generate separate processes for each computing device. So, in multiple computing devices scenario, you are advised to read the ID of the current computing device from the training script and set an independent `save_graphs_path` for each decive to save the IR files to a different path. For example:
+
+```python
+device_id = os.getenv("DEVICE_ID")
+context.set_context(save_graphs=True, save_graphs_path="path/to/ir/files"+device_id)
+```
+
+After the training command is executed, the following files are generated in the specified directory: the IR files starting with digits and underscores are generated during the ME diagram compilation. The calculation diagram is saved in each phase of the `pipeline`. Let's see the important phases, For examples, the `parse` phase parses the `construct` function of the entrance. The `symbol_resolve` phase recursively parses other functions and objects directly or indirectly referenced by the entry function. The `abstract_specialize` phase, type derivation and `shape` derivation are performed. The `optimize` phase, hardware-independent optimization is performed, The automatic differential and automatic parallel functions are also performed. The `validate` phase, the compiled compute graph is verified. The `task_emit` phase, the computing graph is transferred to the backend for further processing. The calculation graph is executed in the `execute` phase.
+
+```bash
+.
+├── 00_parse_[xxxx].ir
+├── 00_parse.dat
+├── 00_parse.dot
+├── 01_symbol_resolve_[xxxx].ir
+├── 01_symbol_resolve.dat
+├── 01_symbol_resolve.dot
+├── 02_combine_like_graphs_[xxxx].ir
+├── 02_combine_like_graphs.dat
+├── 02_combine_like_graphs.dot
+├── 03_inference_opt_prepare_[xxxx].ir
+├── 03_inference_opt_prepare.dat
+├── 03_inference_opt_prepare.dot
+├── 04_abstract_specialize_[xxxx].ir
+├── 04_abstract_specialize.dat
+├── 04_abstract_specialize.dot
+├── 05_inline_[xxxx].ir
+├── 05_inline.dat
+├── 05_inline.dot
+├── 06_py_pre_ad_[xxxx].ir
+├── 06_py_pre_ad.dat
+├── 06_py_pre_ad.dot
+├── 07_pipeline_split_[xxxx].ir
+├── 07_pipeline_split.dat
+├── 07_pipeline_split.dot
+├── 08_optimize_[xxxx].ir
+├── 08_optimize.dat
+├── 08_optimize.dot
+├── 09_py_opt_[xxxx].ir
+├── 09_py_opt.dat
+├── 09_py_opt.dot
+├── 10_validate_[xxxx].ir
+├── 10_validate.dat
+├── 10_validate.dot
+├── 11_task_emit_[xxxx].ir
+├── 11_task_emit.dat
+├── 11_task_emit.dot
+├── 12_execute_[xxxx].ir
+├── 12_execute.dat
+├── 12_execute.dot
+...
+```
+
+## IR File Contents Introduction
+
+The following is an example to describe the contents of the IR file.
+
+```python
+import mindspore.context as context
+import mindspore.nn as nn
+from mindspore import Tensor
+from mindspore import dtype as mstype
+
+context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
+context.set_context(save_graphs=True, save_graphs_path="./ir_files")
+
+class Net(nn.Cell):
+ def __init__(self):
+ super().__init__()
+
+ def construct(self, x, y):
+ x = x + y
+ x = x * y
+ return x
+
+x = Tensor(3, mstype.float32)
+y = Tensor(2, mstype.float32)
+net = Net()
+out = net(x, y)
+print(out)
+```
+
+Use a text editing software (for example, vi) to open the `12_execute_[xxxx].ir` file. The file contents are as follows:
+
+```text
+ 1 #IR entry : @6_5_1_construct_wrapper.15
+ 2 #attrs :
+ 3 check_set_strategy_valid_once_only : 1
+ 4 #Total params : 2
+ 5
+ 6 %para1_x :
+ 7 %para2_y :
+ 8
+ 9 #Total subgraph : 1
+10
+11 subgraph attr:
+12 check_set_strategy_valid_once_only : 1
+13 subgraph @6_5_1_construct_wrapper.15() {
+14 %0([CNode]8) = Add(%para1_x, %para2_y) primitive_attrs: {output_names: [output], input_names: [x, y]}
+15 : (, ) -> ()
+16 # In file /home/workspace/mindspore/mindspore/ops/composite/multitype_ops/add_impl.py(129)/ return F.add(x, y)/
+17 # In file demo.py(14)/ x = x + y/
+18 %1([CNode]10) = Mul(%0, %para2_y) primitive_attrs: {output_names: [output], input_names: [x, y]}
+19 : (, ) -> ()
+20 # In file /home/workspace/mindspore/mindspore/ops/composite/multitype_ops/mul_impl.py(48)/ return F.tensor_mul(x, y)/
+21 # In file demo.py(15)/ x = x * y/
+22 return(%1)
+23 : ()
+24 }
+```
+
+The above contents can be divided into two parts, the first part is the input list and the second part is the graph structure. The first line tells us the name of the top MindSpore graph about the network, `@6_5_1_construct_wrapper.15`, or the entry graph. Line 4 tells us how many inputs are in the network. Line 6 to 7 are the input list, which is in the format of `%para[No.]_[name] : <[data_type]x[shape]>`. Line 9 tells us the number of subgraphs parsed by the network. Line 11 to 24 indicate the graph structure, which contains several nodes, namely, `CNode`. In this example, there are only two nodes: `Add` in row 14 and `Mul` in row 18.
+
+The `CNode` information format is as follows: including the node name, attribute, input node, output information, format, and source code parsing call stack. The ANF diagram is a unidirectional acyclic graph. So, the connection between nodes is displayed only based on the input relationshape. The source code parsing call stack reflects the relationship between the `CNode` and the script source code. For example, line 20 is parsed from line 21, and line 21 corresponds to `x = x * y` of the script.
+
+```text
+ %[No.]([debug_name]) = [OpName]([arg], ...) primitive_attrs: {[key]: [value], ...}
+ : (<[input data_type]x[input shape]>, ...) -> (<[output data_type]x[output shape]>, ...)
+ # Call stack for source code parsing
+```
+
+> After several optimizations by the compiler, the node may undergo several changes (such as operator splitting and operator merging). The source code parsing call stack information of the node may not be in a one-to-one correspondence with the script. This is only an auxiliary method.
+
## Function-style Semantics
Compared with traditional computational graphs, MindIR can not only express data dependency between operators, but also express rich function-style semantics.
diff --git a/docs/note/source_en/env_var_list.md b/docs/note/source_en/env_var_list.md
index d8d93cfb9c70613dcd0837c79d1484a23f37eb2c..cf02804fdff7766dbec7ad76eb49cc876db9b918 100644
--- a/docs/note/source_en/env_var_list.md
+++ b/docs/note/source_en/env_var_list.md
@@ -21,8 +21,10 @@ MindSpore environment variables are as follows:
|RANK_TABLE_FILE|MindSpore|Specifies the file to which a path points, including `DEVICE_IP`s corresponding to multiple Ascend AI Processor `DEVICE_ID`s. |String|File path, which can be a relative path or an absolute path.|This variable is used together with RANK_SIZE. |Mandatory (when the Ascend AI Processor is used)|
|RANK_SIZE|MindSpore|Specifies the number of Ascend AI Processors to be called during deep learning. |Integer|The number of Ascend AI Processors to be called ranges from 1 to 8. | This variable is used together with RANK_TABLE_FILE |Mandatory (when the Ascend AI Processor is used) |
|RANK_ID|MindSpore|Specifies the logical ID of the Ascend AI Processor called during deep learning.|Integer|The value ranges from 0 to 7. When multiple servers are running concurrently, `DEVICE_ID`s in different servers may be the same. RANK_ID can be used to avoid this problem. (RANK_ID = SERVER_ID * DEVICE_NUM + DEVICE_ID) |None|Optional|
-|MS_SUBMODULE_LOG_v|MindSpore| For details about the function and usage, see [MS_SUBMODULE_LOG_v](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/custom_debugging_info.html?highlight=ms_submodule_log_v#log-related-environment-variables-and-configurations)|Dict{String:Integer...}|LogLevel: 0-DEBUG, 1-INFO, 2-WARNING, 3-ERROR
SubModual: COMMON, MD, DEBUG, DEVICE, COMMON, IR...|None | Optional
+|MS_SUBMODULE_LOG_v|MindSpore| For details about the function and usage, see [MS_SUBMODULE_LOG_v](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/custom_debugging_info.html#log-related-environment-variables-and-configurations)|Dict{String:Integer...}|LogLevel: 0-DEBUG, 1-INFO, 2-WARNING, 3-ERROR
SubModual: COMMON, MD, DEBUG, DEVICE, COMMON, IR...|None | Optional
|OPTION_PROTO_LIB_PATH|MindSpore|Specifies the RPOTO dependent library path. |String|File path, which can be a relative path or an absolute path.|None|Optional|
+|MS_RDR_ENABLE|MindSpore|Determines whether to enable running data recorder (RDR). If a running exception occurs in MindSpore, the pre-recorded data in MindSpore is automatically exported to assist in locating the cause of the running exception.|Integer|1:enables RDR
0:disables RDR|This variable is used together with MS_RDR_PATH|Optional|
+|MS_RDR_PATH|MindSpore|Specifies the system path for storing the data recorded by running data recorder (RDR).|String|Directory path, which should be an absolute path.|This variable is used together with MS_RDR_ENABLE=1|Optional|
|GE_USE_STATIC_MEMORY|GraphEngine| When a network model has too many layers, the intermediate computing data of a feature map may exceed 25 GB, for example, on the BERT24 network. In the multi-device scenario, to ensure efficient memory collaboration, set this variable to 1, indicating that static memory allocation mode is used. For other networks, dynamic memory allocation mode is used by default.
In static memory allocation mode, the default allocation is 31 GB, which is determined by the sum of graph_memory_max_size and variable_memory_max_size. In dynamic memory allocation mode, the allocation is within the sum of graph_memory_max_size and variable_memory_max_size. |Integer|1: static memory allocation mode
0: dynamic memory allocation mode|None|Optional|
|DUMP_GE_GRAPH|GraphEngine|Outputs the graph description information of each phase in the entire process to a file. This environment variable controls contents of the dumped graph. |Integer|1: full dump
2: basic dump without data such as weight
3: simplified dump with only node relationships displayed|None|Optional|
|DUMP_GRAPH_LEVEL|GraphEngine|Outputs the graph description information of each phase in the entire process to a file. This environment variable controls the number of dumped graphs. |Integer|1: dumps all graphs.
2: dumps all graphs except subgraphs.
3: dumps the last generated graph. |None |Optional |
diff --git a/docs/note/source_en/network_list_ms.md b/docs/note/source_en/network_list_ms.md
index 8eaf23753b5680408664346aa5cf81c8874a468b..2c6ef04a88e39433a89e1bbf3d8980077c56bb98 100644
--- a/docs/note/source_en/network_list_ms.md
+++ b/docs/note/source_en/network_list_ms.md
@@ -19,7 +19,8 @@
|:------ |:------| :----------- |:------ |:------ |:------ |:------ |:----- |:-----
|Computer Vision (CV) | Image Classification | [AlexNet](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/alexnet) | Supported | Supported | Supported | Supported | Doing | Doing
| Computer Vision (CV) | Image Classification | [CNN](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/cnn_direction_model) | Supported | Doing | Doing | Doing | Doing | Doing
-| Computer Vision (CV) | Image Classification | [DenseNet121](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/densenet121) | Supported | Supported | Doing | Doing | Doing | Doing
+| Computer Vision (CV) | Image Classification | [DenseNet100](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/densenet) | Doing | Doing | Doing | Doing | Supported | Supported
+| Computer Vision (CV) | Image Classification | [DenseNet121](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/densenet) | Supported | Supported | Doing | Doing | Doing | Doing
| Computer Vision (CV) | Image Classification | [EfficientNet-B0](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/efficientnet) | Doing | Doing | Supported | Supported | Doing | Doing
| Computer Vision (CV) | Image Classification | [GoogLeNet](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/googlenet) | Supported | Supported | Supported | Supported | Doing | Doing
| Computer Vision (CV) | Image Classification | [InceptionV3](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/inceptionv3) | Supported | Doing | Doing | Doing | Doing | Doing
@@ -60,6 +61,7 @@
| Computer Vision (CV) | Text Recognition | [CNN+CTC](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/cnnctc) | Supported | Supported | Doing | Doing | Doing | Doing
| Computer Vision (CV) | Semantic Segmentation | [DeepLabV3](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/deeplabv3) | Supported | Doing | Doing | Doing | Supported | Doing
| Computer Vision (CV) | Semantic Segmentation | [U-Net2D (Medical)](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/unet) | Supported | Doing | Doing | Doing | Doing | Doing
+| Computer Vision (CV) | Semantic Segmentation | [U-Net3D (Medical)](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/unet3d) | Supported | Doing | Doing | Doing | Doing | Doing
| Computer Vision (CV) | Keypoint Detection | [OpenPose](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/openpose) | Supported | Doing | Doing | Doing | Doing | Doing
| Computer Vision (CV) | Keypoint Detection | [SimplePoseNet](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/simple_pose) | Supported | Doing | Doing | Doing | Doing | Doing
| Computer Vision (CV) | Optical Character Recognition | [CRNN](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/crnn) | Supported | Doing | Doing | Doing | Doing | Doing
diff --git a/docs/note/source_en/static_graph_syntax_support.md b/docs/note/source_en/static_graph_syntax_support.md
index 168730eb8c55261a348b28bfe570bdecafe716e3..a2565ab693044f88289c581581c6ae4e078759fb 100644
--- a/docs/note/source_en/static_graph_syntax_support.md
+++ b/docs/note/source_en/static_graph_syntax_support.md
@@ -25,14 +25,17 @@
- [Identity Operators](#identity-operators)
- [Expressions](#expressions)
- [Conditional Control Statements](#conditional-control-statements)
- - [if](#if)
+ - [single if](#single-if)
+ - [side-by-side if](#side-by-side-if)
+ - [if in if](#if-in-if)
- [Loop Statements](#loop-statements)
- [for](#for)
- [while](#while)
- - [Process Control Statements](#process-control-statements)
- - [break](#break)
- - [continue](#continue)
- - [pass](#pass)
+ - [side-by-side while](#side-by-side-while)
+ - [while in while](#while-in-while)
+ - [Conditional Control Statements in Loop Statements](#conditional-control-statements-in-loop-statements)
+ - [if in for](#if-in-for)
+ - [if in while](#if-in-while)
- [Function Definition Statements](#function-definition-statements)
- [def Keyword](#def-keyword)
- [lambda Expression](#lambda-expression)
@@ -780,7 +783,7 @@ For details about the rules, click y:
+ out = x
+else:
+ out = y
+if z > x:
+ out = out + 1
+return out
+```
+
+#### if in if
+
+Usage:
+
+- `if (cond1):if (cond2):statements...`
+
+Parameters: `cond1` and `cond2` -- Consistent with `single if`.
+
+Restrictions:
+
+- Inherit all restrictions of `single if`.
+
+- The total number of `if` in calculating graph can not exceed 50.
+
+- Too many `if` will cause the compilation time to be too long. Reducing the number of `if` will help improve compilation efficiency.
+
+Example:
+
+```python
+if x > y:
+ z = z + 1
+ if z > x:
+ return m
+else:
+ return n
+```
### Loop Statements
#### for
-Usage: `for i in sequence`
+Usage:
+
+- `for i in sequence`
-For example:
+Parameter: `sequence` --Iterative sequences (`Tuple` and `List`).
+
+Restrictions:
+
+- The total number of graph operations is a multiple of number of iterations of the `for` loop. Excessive number of iterations of the `for` loop may cause the graph to occupy more memory than usage limit.
+
+Example:
```python
z = Tensor(np.ones((2, 3)))
@@ -843,82 +913,166 @@ The result is as follows:
z: Tensor(shape=[2, 3], dtype=Int64, value=[[7, 7], [7, 7], [7, 7]])
```
-Parameter: `sequence` --Traverses sequences (`Tuple` and `List`).
+#### single while
-#### while
+Usage:
-Usage: `while(cond)`
+- `while (cond)`
-Parameter: `cond` -- Consistent with `if`.
+Parameter: `cond` -- Consistent with `single if`.
Restrictions:
- During graph building, if `while` is not eliminated, the data type and `shape` of `return` inside `while` must be the same as those outside `while`.
-- The data type and `shape` of the updated variables in `while` must be the same as those before the update.
+- The data type and shape of the updated variables in `while` must be the same as those before the update.
+
+- Does not support training scenarios.
Example 1:
```python
-while x > y:
+while x < y:
x += 1
return m
return n
```
-The `m` data type returned inside `while` inside and `n` data type returned outside `while` must be the same as those of `shape`.
+The `m` data type returned inside `while` inside and `n` data type returned outside `while` must be the same as those of shape.
Example 2:
```python
out = m
-while x > y:
+while x < y:
x += 1
- out = n
+ out = out + 1
return out
```
-In `while`, the data types of `out` before and after update must be the same as those of `shape`.
+In `while`, the data types of `out` before and after update must be the same as those of shape.
-### Process Control Statements
+#### side-by-side while
-The current process control statements support `break`, `continue`, and `pass`.
+Usage:
-#### break
+- `while (cond1):statements while (cond2):statemetns...`
-It can be used in the `for` and `while` code blocks to terminate the entire loop.
+Parameters: `cond1` and `cond2` -- Consistent with `single if`.
-For example:
+Restrictions:
+
+- Inherit all restrictions of `single while`.
+
+- The total number of `while` in calculating graph can not exceed 50.
+
+- Too many `while` will cause the compilation time to be too long. Reducing the number of `while` will help improve compilation efficiency.
+
+Example:
```python
-for i in x:
- if i == 2:
- break
- statement_a
-statement_b
+out = m
+while x < y:
+ x += 1
+ out = out + 1
+while out > 10:
+ out -= 10
+return out
```
-When `i == 2`, the loop ends and `statement_b` is executed.
+#### while in while
-#### continue
+Usage:
-It can be used in the `for` and `while` statement blocks to terminate the current loop and directly enter the next loop.
+-`while (cond1):while (cond2):statements...`
-For example:
+Parameters: `cond1` and `cond2` -- Consistent with `single if`.
+
+Restrictions:
+
+- Inherit all restrictions of `single while`.
+
+- The total number of `while` in calculating graph can not exceed 50.
+
+- Too many `while` will cause the compilation time to be too long. Reducing the number of `while` will help improve compilation efficiency.
+
+Example:
+
+```python
+out = m
+while x < y:
+ while z < y:
+ z += 1
+ out = out + 1
+ x += 1
+return out
+```
+
+### Conditional Control Statements in Loop Statements
+
+#### if in for
+
+Usage:
+
+- for i in sequence:if (cond)`
+
+Parameters:
+
+- `cond` -- Consistent with `single if`.
+
+- `sequence` -- Iterative sequence(`Tuple`、`List`)
+
+Restrictions:
+
+- Inherit all restrictions of `single if`.
+
+- Inherit all restrictions of `for`.
+
+- If `cond` is variable, it is forbidden to use `if (cond):return`,`if (cond):continue`,`if (cond):break` statements.
+
+- The total number of `if` is a multiple of number of iterations of the `for` loop. Excessive number of iterations of the `for` loop may cause the compilation time to be too long.
+
+Example:
```python
+z = Tensor(np.ones((2, 3)))
+x = (1, 2, 3)
for i in x:
- if i == 2:
- continue
- statement_a
-statement_b
- ```
+ if i < 3:
+ z += i
+return z
+```
-When `i == 2`, the current loop ends and the `statement_a` statement is not executed. The next loop starts.
+The result is as follows:
-#### pass
+```text
+z: Tensor(shape=[2, 3], dtype=Int64, value=[[4, 4], [4, 4], [4, 4]])
+```
+
+#### if in while
-It is used as a placeholder statement.
+Usage:
+
+- `while (cond1):if (cond2)`
+
+Parameters: `cond1` and `cond2` -- Consistent with `single if`.
+
+Restrictions:
+
+- Inherit all restrictions of `single if` and `single while`.
+
+- If `cond2` is variable, it is forbidden to use `if (cond2):return`,`if (cond2):continue`,`if (cond2):break` statements.
+
+Example:
+
+```python
+out = m
+while x < y:
+ if z > 2*x:
+ out = out + 1
+ x += 1
+return out
+```
### Function Definition Statements
diff --git a/docs/note/source_zh_cn/design/mindspore/mindir.md b/docs/note/source_zh_cn/design/mindspore/mindir.md
index 01fd8b8ab770c1db0a3749607f368199f41e36bc..92b49f4c26f492c8e09b4481d2c81650e0a6add8 100644
--- a/docs/note/source_zh_cn/design/mindspore/mindir.md
+++ b/docs/note/source_zh_cn/design/mindspore/mindir.md
@@ -1,6 +1,6 @@
# MindSpore IR(MindIR)
-`Linux` `Windows` `框架开发` `中级` `高级` `贡献者`
+`Linux` `Windows` `Ascend` `GPU` `框架开发` `中级` `模型开发` `高级` `贡献者`
@@ -9,6 +9,7 @@
- [文法定义](#文法定义)
- [示例](#示例)
- [如何保存IR](#如何保存ir)
+ - [IR文件内容介绍](#ir文件内容介绍)
- [函数式语义](#函数式语义)
- [高阶函数](#高阶函数)
- [控制流](#控制流)
@@ -17,13 +18,19 @@
-
+
## 简介
中间表示(IR)是程序编译过程中介于源语言和目标语言之间的程序表示,以方便编译器进行程序分析和优化,因此IR的设计需要考虑从源语言到目标语言的转换难度,同时考虑程序分析和优化的易用性和性能。
-MindIR是一种基于图表示的函数式IR,其最核心的目的是服务于自动微分变换。自动微分采用的是基于函数式编程框架的变换方法,因此IR采用了接近于ANF函数式的语义。此外,借鉴Sea of Nodes[1]和Thorin[2]的优秀设计,采用了一种基于显性依赖图的表示方式。
+MindIR是一种基于图表示的函数式IR,其最核心的目的是服务于自动微分变换。自动微分采用的是基于函数式编程框架的变换方法,因此IR采用了接近于ANF函数式的语义。此外,借鉴Sea of Nodes[1]和Thorin[2]的优秀设计,采用了一种基于显性依赖图的表示方式。关于ANF-IR的具体介绍,可以参考[MindSpore IR文法定义](#id2)。
+
+在图模式`context.set_context(mode=context.GRAPH_MODE)`下运行用MindSpore编写的模型时,若配置中设置了`context.set_context(save_graphs=True)`,运行时会输出一些图编译过程中生成的一些中间文件,我们称为IR文件。当前主要有三种格式的IR文件:
+
+- ir后缀结尾的IR文件:一种比较直观易懂的以文本格式描述模型结构的文件,可以直接用文本编辑软件查看。在下文中我们也将介绍此文件的查看方式。
+- dat后缀结尾的IR文件:一种相对于ir后缀结尾的文件格式定义更为严谨的描述模型结构的文件,包含的内容更为丰富,可以直接用文本编辑软件查看。
+- dot后缀结尾的IR文件:描述了不同节点间的拓扑关系,可以用[graphviz](http://graphviz.org)将此文件作为输入生成图片,方便用户直观地查看模型结构。对于算子比较多的模型,推荐使用可视化组件[MindInsight](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/dashboard.html#id5)对计算图进行可视化。
## 文法定义
@@ -87,7 +94,7 @@ lambda (x, y)
c end
```
-对应的MindIR为[ir.dot](https://gitee.com/mindspore/docs/blob/master/docs/note/source_zh_cn/design/mindspore/images/ir/ir.dot):
+对应的MindIR为[ir.dot](https://gitee.com/mindspore/docs/blob/r1.2/docs/note/source_zh_cn/design/mindspore/images/ir/ir.dot):

@@ -101,6 +108,136 @@ lambda (x, y)
DOT文件可以通过graphviz转换为图片格式来查看,例如将dot转换为png的命令是`dot -Tpng *.dot -o *.png`。
+在训练脚本`train.py`中,我们在`set_context`函数中添加如下代码,运行训练脚本时,MindSpore会自动将编译过程中产生的IR文件存放到指定路径。
+
+```python
+if __name__ == "__main__":
+ context.set_context(save_graphs=True, save_graphs_path="path/to/ir/files")
+```
+
+此处为单机版本的训练脚本。当运行的脚本使用多个计算设备时,MindSpore会为每一个计算设备生成一个独立的进程。因此我们建议用户在多卡版本的训练脚本中读取当前的计算设id,从而为每个设备设置独立的`save_graphs_path`实现将每个设备的IR文件保存在不同的路径下。例如:
+
+```python
+device_id = os.getenv("DEVICE_ID")
+context.set_context(save_graphs=True, save_graphs_path="path/to/ir/files"+device_id)
+```
+
+执行训练命令后,在指定的目录生成如下文件。其中以数字下划线开头的IR文件是在ME编译图过程中输出的,`pipeline`各阶段分别会保存一次计算图。下面介绍比较重要的阶段,例如`parse`阶段会解析入口的`construct`函数;`symbol_resolve`阶段会递归解析入口函数直接或间接引用到的其他函数和对象;`abstract_specialize`阶段会做类型推导和`shape`推导;`optimize`阶段主要是进行和硬件无关的优化,自动微分与自动并行功能也是在该阶段展开;`validate`阶段会校验编译出来的计算图;`task_emit`阶段将计算图传给后端进一步处理;`execute`阶段会执行该计算图。
+
+```bash
+.
+├── 00_parse_[xxxx].ir
+├── 00_parse.dat
+├── 00_parse.dot
+├── 01_symbol_resolve_[xxxx].ir
+├── 01_symbol_resolve.dat
+├── 01_symbol_resolve.dot
+├── 02_combine_like_graphs_[xxxx].ir
+├── 02_combine_like_graphs.dat
+├── 02_combine_like_graphs.dot
+├── 03_inference_opt_prepare_[xxxx].ir
+├── 03_inference_opt_prepare.dat
+├── 03_inference_opt_prepare.dot
+├── 04_abstract_specialize_[xxxx].ir
+├── 04_abstract_specialize.dat
+├── 04_abstract_specialize.dot
+├── 05_inline_[xxxx].ir
+├── 05_inline.dat
+├── 05_inline.dot
+├── 06_py_pre_ad_[xxxx].ir
+├── 06_py_pre_ad.dat
+├── 06_py_pre_ad.dot
+├── 07_pipeline_split_[xxxx].ir
+├── 07_pipeline_split.dat
+├── 07_pipeline_split.dot
+├── 08_optimize_[xxxx].ir
+├── 08_optimize.dat
+├── 08_optimize.dot
+├── 09_py_opt_[xxxx].ir
+├── 09_py_opt.dat
+├── 09_py_opt.dot
+├── 10_validate_[xxxx].ir
+├── 10_validate.dat
+├── 10_validate.dot
+├── 11_task_emit_[xxxx].ir
+├── 11_task_emit.dat
+├── 11_task_emit.dot
+├── 12_execute_[xxxx].ir
+├── 12_execute.dat
+├── 12_execute.dot
+...
+```
+
+## IR文件内容介绍
+
+下面以一个简单的例子来说明IR文件的内容,执行以下一段训练代码:
+
+```python
+import mindspore.context as context
+import mindspore.nn as nn
+from mindspore import Tensor
+from mindspore import dtype as mstype
+
+context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
+context.set_context(save_graphs=True, save_graphs_path="./ir_files")
+
+class Net(nn.Cell):
+ def __init__(self):
+ super().__init__()
+
+ def construct(self, x, y):
+ x = x + y
+ x = x * y
+ return x
+
+x = Tensor(3, mstype.float32)
+y = Tensor(2, mstype.float32)
+net = Net()
+out = net(x, y)
+print(out)
+```
+
+使用文本编辑软件(例如`vi`)打开执行完后输出的IR文件`12_execute_[xxxx].ir`,内容如下所示:
+
+```text
+ 1 #IR entry : @6_5_1_construct_wrapper.15
+ 2 #attrs :
+ 3 check_set_strategy_valid_once_only : 1
+ 4 #Total params : 2
+ 5
+ 6 %para1_x :
+ 7 %para2_y :
+ 8
+ 9 #Total subgraph : 1
+10
+11 subgraph attr:
+12 check_set_strategy_valid_once_only : 1
+13 subgraph @6_5_1_construct_wrapper.15() {
+14 %0([CNode]8) = Add(%para1_x, %para2_y) primitive_attrs: {output_names: [output], input_names: [x, y]}
+15 : (, ) -> ()
+16 # In file /home/workspace/mindspore/mindspore/ops/composite/multitype_ops/add_impl.py(129)/ return F.add(x, y)/
+17 # In file demo.py(14)/ x = x + y/
+18 %1([CNode]10) = Mul(%0, %para2_y) primitive_attrs: {output_names: [output], input_names: [x, y]}
+19 : (, ) -> ()
+20 # In file /home/workspace/mindspore/mindspore/ops/composite/multitype_ops/mul_impl.py(48)/ return F.tensor_mul(x, y)/
+21 # In file demo.py(15)/ x = x * y/
+22 return(%1)
+23 : ()
+24 }
+```
+
+以上内容可分为两个部分,第一部分为输入列表,第二部分为图结构。 其中第1行告诉了我们该网络的顶图名称`@6_5_1_construct_wrapper.15`,也就是入口图。 第4行告诉了我们该网络有多少个输入。 第6-7行为输入列表,遵循`%para[序号]_[name] : <[data_type]x[shape]>`的格式。 第9行告诉我们该网络解析出来的子图数量。 第11-24行为图结构,含有若干节点,即`CNode`。该示例中只有2个节点,分别为14行的`Add`和18行的`Mul`。
+
+`CNode`的信息遵循如下格式,包含节点名称、属性、输入节点、输出信息、格式、源码解析调用栈等信息,由于ANF图为单向无环图,所以这里仅根据输入关系体现节点与节点的连接关系。源码解析调用栈则体现了`CNode`与脚本源码之间的关系,例如第20行由第21行解析而来,而第21行能对应到脚本的`x = x * y`。
+
+```text
+%[序号]([debug_name]) = [OpName]([arg], ...) primitive_attrs: {[key]: [value], ...}
+ : (<[输入data_type]x[输入shape]>, ...) -> (<[输出data_type]x[输出shape]>, ...)
+ # 源码解析调用栈
+```
+
+> 需要注意的是经过编译器的若干优化处理后,节点可能经过了若干变幻(如算子拆分、算子融合等),节点的源码解析调用栈信息与脚本可能无法完全一一对应,这里仅作为辅助手段。
+
## 函数式语义
MindIR较传统计算图的一个重要特性是不仅可以表达算子之间的数据依赖,还可以表达丰富的函数式语义。
@@ -121,7 +258,7 @@ def hof(x):
return res
```
-对应的MindIR为[hof.dot](https://gitee.com/mindspore/docs/blob/master/docs/note/source_zh_cn/design/mindspore/images/ir/hof.dot):
+对应的MindIR为[hof.dot](https://gitee.com/mindspore/docs/blob/r1.2/docs/note/source_zh_cn/design/mindspore/images/ir/hof.dot):

@@ -144,7 +281,7 @@ def fibonacci(n):
return fibonacci(n-1) + fibonacci(n-2)
```
-对应的MindIR为[cf.dot](https://gitee.com/mindspore/docs/blob/master/docs/note/source_zh_cn/design/mindspore/images/ir/cf.dot):
+对应的MindIR为[cf.dot](https://gitee.com/mindspore/docs/blob/r1.2/docs/note/source_zh_cn/design/mindspore/images/ir/cf.dot):

@@ -171,7 +308,7 @@ def ms_closure():
return out1, out2
```
-对应的MindIR为[closure.dot](https://gitee.com/mindspore/docs/blob/master/docs/note/source_zh_cn/design/mindspore/images/ir/closure.dot):
+对应的MindIR为[closure.dot](https://gitee.com/mindspore/docs/blob/r1.2/docs/note/source_zh_cn/design/mindspore/images/ir/closure.dot):

diff --git a/docs/note/source_zh_cn/env_var_list.md b/docs/note/source_zh_cn/env_var_list.md
index cbbc400691ba2f9e4205ebb91310a9c6758bb603..cc869957e608fff3e780c55a78ae6be0bee6ca39 100644
--- a/docs/note/source_zh_cn/env_var_list.md
+++ b/docs/note/source_zh_cn/env_var_list.md
@@ -21,8 +21,10 @@
|RANK_TABLE_FILE|MindSpore|路径指向文件,包含指定多Ascend AI处理器环境中Ascend AI处理器的"device_id"对应的"device_ip"。|String|文件路径,支持相对路径与绝对路径|与RANK_SIZE配合使用|必选(使用Ascend AI处理器时)|
|RANK_SIZE|MindSpore|指定深度学习时调用Ascend AI处理器的数量|Integer|1~8,调用Ascend AI处理器的数量|与RANK_TABLE_FILE配合使用|必选(使用Ascend AI处理器时)|
|RANK_ID|MindSpore|指定深度学习时调用Ascend AI处理器的逻辑ID|Integer|0~7,多机并行时不同server中DEVICE_ID会有重复,使用RANK_ID可以避免这个问题(多机并行时 RANK_ID = SERVER_ID * DEVICE_NUM + DEVICE_ID|无|可选|
-|MS_SUBMODULE_LOG_v|MindSpore|[MS_SUBMODULE_LOG_v功能与用法]()|Dict{String:Integer...}|LogLevel: 0-DEBUG, 1-INFO, 2-WARNING, 3-ERROR
SubModual: COMMON, MD, DEBUG, DEVICE, COMMON, IR...|无|可选
-|OPTION_PROTO_LIB_PATH|MindSpore|RPOTO依赖库库路径|String|文件路径,支持相对路径与绝对路径|无|可选|
+|MS_SUBMODULE_LOG_v|MindSpore|[MS_SUBMODULE_LOG_v功能与用法](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#id9)|Dict{String:Integer...}|LogLevel: 0-DEBUG, 1-INFO, 2-WARNING, 3-ERROR
SubModual: COMMON, MD, DEBUG, DEVICE, COMMON, IR...|无|可选
+|OPTION_PROTO_LIB_PATH|MindSpore|RPOTO依赖库库路径|String|目录路径,支持相对路径与绝对路径|无|可选|
+|MS_RDR_ENABLE|MindSpore|是否开启程序运行数据记录器(RDR),如果MindSpore出现了运行异常,会自动导出MindSpore中预先记录的数据以辅助定位运行异常的原因|Integer|1:开启RDR功能
0:关闭RDR功能|与MS_RDR_PATH一起使用|可选|
+|MS_RDR_PATH|MindSpore|配置程序运行数据记录器(RDR)的文件导出路径|String|文件路径,仅支持绝对路径|与MS_RDR_ENABLE=1一起使用|可选|
|GE_USE_STATIC_MEMORY|GraphEngine|当网络模型层数过大时,特征图中间计算数据可能超过25G,例如BERT24网络。多卡场景下为保证通信内存高效协同,需要配置为1,表示使用内存静态分配方式,其他网络暂时无需配置,默认使用内存动态分配方式。
静态内存默认配置为31G,如需要调整可以通过网络运行参数graph_memory_max_size和variable_memory_max_size的总和指定;动态内存是动态申请,最大不会超过graph_memory_max_size和variable_memory_max_size的总和。|Integer|1:使用内存静态分配方式
0:使用内存动态分配方式|无|可选|
|DUMP_GE_GRAPH|GraphEngine|把整个流程中各个阶段的图描述信息打印到文件中,此环境变量控制dump图的内容多少|Integer|1:全量dump
2:不含有权重等数据的基本版dump
3:只显示节点关系的精简版dump|无|可选|
|DUMP_GRAPH_LEVEL|GraphEngine|把整个流程中各个阶段的图描述信息打印到文件中,此环境变量可以控制dump图的个数|Integer|1:dump所有图
2:dump除子图外的所有图
3:dump最后的生成图|无|可选|
diff --git a/docs/note/source_zh_cn/network_list_ms.md b/docs/note/source_zh_cn/network_list_ms.md
index d4685b9f0f6ed831410d5ebb536de97d1e6fb0a9..7e8ad93a968986466878e5b1d6e20060481e0497 100644
--- a/docs/note/source_zh_cn/network_list_ms.md
+++ b/docs/note/source_zh_cn/network_list_ms.md
@@ -19,7 +19,8 @@
|:---- |:------- |:---- |:---- |:---- |:---- |:---- |:---- |:----
|计算机视觉(CV) | 图像分类(Image Classification) | [AlexNet](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/alexnet) | Supported | Supported | Supported | Supported | Doing | Doing
| 计算机视觉(CV) | 图像分类(Image Classification) | [CNN](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/cnn_direction_model) | Supported | Doing | Doing | Doing | Doing | Doing
-| 计算机视觉(CV) | 图像分类(Image Classification) | [DenseNet121](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/densenet121) | Supported | Supported | Doing | Doing | Doing | Doing
+| 计算机视觉(CV) | 图像分类(Image Classification) | [DenseNet100](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/densenet) | Doing | Doing | Doing | Doing | Supported | Supported
+| 计算机视觉(CV) | 图像分类(Image Classification) | [DenseNet121](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/densenet) | Supported | Supported | Doing | Doing | Doing | Doing
| 计算机视觉(CV) | 图像分类(Image Classification) | [EfficientNet-B0](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/efficientnet) | Doing | Doing | Supported | Supported | Doing | Doing
| 计算机视觉(CV) | 图像分类(Image Classification) | [GoogLeNet](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/googlenet) | Supported | Supported | Supported | Supported | Doing | Doing
| 计算机视觉(CV) | 图像分类(Image Classification) | [InceptionV3](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/inceptionv3) | Supported | Doing | Doing | Doing | Doing | Doing
@@ -60,6 +61,7 @@
| 计算机视觉(CV) | 文本识别(Text Recognition) | [CNN+CTC](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/cnnctc) | Supported | Supported | Doing | Doing | Doing | Doing
| 计算机视觉(CV) | 语义分割(Semantic Segmentation) | [DeepLabV3](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/deeplabv3) | Supported | Doing | Doing | Doing | Supported | Doing
| 计算机视觉(CV) | 语义分割(Semantic Segmentation) | [U-Net2D (Medical)](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/unet) | Supported | Doing | Doing | Doing | Doing | Doing
+| 计算机视觉(CV) | 语义分割(Semantic Segmentation) | [U-Net3D (Medical)](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/unet3d) | Supported | Doing | Doing | Doing | Doing | Doing
| 计算机视觉(CV) | 关键点检测(Keypoint Detection) |[OpenPose](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/openpose) | Supported | Doing | Doing | Doing | Doing | Doing
| 计算机视觉(CV) | 关键点检测(Keypoint Detection) |[SimplePoseNet](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/simple_pose) | Supported | Doing | Doing | Doing | Doing | Doing
| 计算机视觉(CV) | 光学字符识别(Optical Character Recognition) |[CRNN](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/crnn) | Supported | Doing | Doing | Doing | Doing | Doing
diff --git a/docs/note/source_zh_cn/static_graph_syntax_support.md b/docs/note/source_zh_cn/static_graph_syntax_support.md
index 08738e39797bb6cea228a8ec280414143e69f62b..5cf5b6a3476c13955a4d6798a47f93b5ed3e0d08 100644
--- a/docs/note/source_zh_cn/static_graph_syntax_support.md
+++ b/docs/note/source_zh_cn/static_graph_syntax_support.md
@@ -25,14 +25,17 @@
- [身份运算符](#身份运算符)
- [表达式](#表达式)
- [条件控制语句](#条件控制语句)
- - [if](#if)
+ - [单if](#单if)
+ - [并列if](#并列if)
+ - [嵌套if](#嵌套if)
- [循环语句](#循环语句)
- [for](#for)
- [while](#while)
- - [流程控制语句](#流程控制语句)
- - [break](#break)
- - [continue](#continue)
- - [pass](#pass)
+ - [并列while](#并列while)
+ - [嵌套while](#嵌套while)
+ - [循环嵌套条件控制语句](#循环嵌套条件控制语句)
+ - [if in for](#if-in-for)
+ - [if in while](#if-in-while)
- [函数定义语句](#函数定义语句)
- [def关键字](#def关键字)
- [lambda表达式](#lambda表达式)
@@ -782,7 +785,7 @@ def generate_tensor():
### 条件控制语句
-#### if
+#### 单if
使用方式:
@@ -794,11 +797,15 @@ def generate_tensor():
限制:
-- 在构图时,如果`if`未能消除,则`if`分支`return`的数据类型和`shape`,与`if`分支外`return`的数据类型和`shape`必须一致。
+- 在构图时,如果`if`未能消除,则`if`分支`return`的数据类型和shape,与`if`分支外`return`的数据类型和shape必须一致。
-- 当只有`if`时,`if`分支变量更新后数据类型和`shape`,与更新前数据类型和`shape`必须一致。
+- 当只有`if`时,`if`分支变量更新后数据类型和shape,与更新前数据类型和shape必须一致。
-- 当即有`if`又有`else`时,`if`分支变量更新后数据类型和`shape`,与`else`分支更新后数据类型和`shape`必须一致。
+- 当即有`if`又有`else`时,`if`分支变量更新后数据类型和shape,与`else`分支更新后数据类型和shape必须一致。
+
+- 不支持高阶微分场景。
+
+- 不支持`elif`语句。
示例1:
@@ -809,7 +816,7 @@ else:
return n
```
-`if`分支返回的`m`和`else`分支返回的`n`,二者数据类型和`shape`必须一致。
+`if`分支返回的`m`和`else`分支返回的`n`,二者数据类型和shape必须一致。
示例2:
@@ -821,15 +828,78 @@ else:
return out
```
-`if`分支更新后`out`和`else`分支更新后`out`,二者数据类型和`shape`必须一致。
+`if`分支更新后`out`和`else`分支更新后`out`,二者数据类型和shape必须一致。
+
+#### 并列if
+
+使用方式:
+
+- `if (cond1):statements else:statements...if (cond2):statements...`
+
+参数:`cond1`、 `cond2`-- 与`单if`一致。
+
+限制:
+
+- 继承`单if`所有限制。
+
+- 计算图总`if`数量不超过50个。
+
+- `if`数量过多会导致编译时间过长,减少`if`数量有助于提升编译效率。
+
+示例:
+
+```python
+if x > y:
+ out = x
+else:
+ out = y
+if z > x:
+ out = out + 1
+return out
+```
+
+#### 嵌套if
+
+使用方式:
+
+- `if (cond1):if (cond2):statements...`
+
+参数:`cond1`、 `cond2`-- 与`单if`一致。
+
+限制:
+
+- 继承`单if`所有限制。
+
+- 计算图`if`数量不超过50个。
+
+- `if`数量过多会导致编译时间过长,减少`if`数量有助于提升编译效率。
+
+示例:
+
+```python
+if x > y:
+ z = z + 1
+ if z > x:
+ return m
+else:
+ return n
+```
### 循环语句
#### for
-使用方式:`for i in sequence`
+使用方式:
+
+- `for i in sequence`
-示例如下:
+参数:`sequence` -- 遍历序列(`Tuple`、`List`)
+
+限制:
+
+- 图的算子数量和`for`循环的迭代次数成倍数关系,`for`循环迭代次数过大可能会导致图占用内存超过使用限制。
+
+示例:
```python
z = Tensor(np.ones((2, 3)))
@@ -845,82 +915,166 @@ return z
z: Tensor(shape=[2, 3], dtype=Int64, value=[[7, 7], [7, 7], [7, 7]])
```
-参数:`sequence` -- 遍历序列(`Tuple`、`List`)
+#### 单while
-#### while
+使用方式:
-使用方式:`while(cond)`
+- `while (cond)`
-参数:`cond` -- 与`if`一致。
+参数:`cond` -- 与`单if`一致。
限制:
-- 在构图时,如果`while`未能消除,则`while`内`return`的数据类型和`shape`,与`while`外`return`的数据类型和`shape`必须一致。
+- 在构图时,如果`while`未能消除,则`while`内`return`的数据类型和shape,与`while`外`return`的数据类型和shape必须一致。
-- `while`内变量更新后数据类型和`shape`,与更新前数据类型和`shape`必须一致。
+- `while`内变量更新后数据类型和shape,与更新前数据类型和shape必须一致。
+
+- 不支持训练场景。
示例1:
```python
-while x > y:
+while x < y:
x += 1
return m
return n
```
-`while`内返回的`m`和`while`外返回的`n`数据类型必须和`shape`一致。
+`while`内返回的`m`和`while`外返回的`n`数据类型必须和shape一致。
示例2:
```python
out = m
-while x > y:
+while x < y:
x += 1
- out = n
+ out = out + 1
return out
```
-`while`内,`out`更新后和更新前的数据类型和`shape`必须一致。
+`while`内,`out`更新后和更新前的数据类型和shape必须一致。
-### 流程控制语句
+#### 并列while
-当前流程控制语句支持了`break`、`continue`和`pass`。
+使用方式:
-#### break
+- `while (cond1):statements while (cond2):statemetns...`
-可用于`for`和`while`代码块里,用于终止整个循环。
+参数:`cond1`、 `cond2`-- 与`单if`一致。
-示例如下:
+限制:
+
+- 继承`单while`所有限制。
+
+- 并列`while`总数不超过50个。
+
+- `while`数量过多会导致编译时间过长,减少`while`数量有助于提升编译效率。
+
+示例:
```python
-for i in x:
- if i == 2:
- break
- statement_a
-statement_b
+out = m
+while x < y:
+ x += 1
+ out = out + 1
+while out > 10:
+ out -= 10
+return out
+```
+
+#### 嵌套while
+
+使用方式:
+
+- `while (cond1):while (cond2):statements...`
+
+参数:`cond1`、 `cond2`-- 与`单if`一致。
+
+限制:
+
+- 继承`单while`所有限制。
+
+- 嵌套`while`总数不超过50个。
+
+- `while`数量过多会导致编译时间过长,减少`while`数量有助于提升编译效率。
+
+示例:
+
+```python
+out = m
+while x < y:
+ while z < y:
+ z += 1
+ out = out + 1
+ x += 1
+return out
```
-当 `i == 2`时,循环终止,执行`statement_b`。
+### 循环嵌套条件控制语句
-#### continue
+#### if in for
-可用于`for`和`while`语句块里,用于终止本轮循环,直接进入下一轮循环。
+使用方式:
+
+- `for i in sequence:if (cond)`
+
+参数:
+
+`cond` -- 与`单if`一致。
+
+`sequence` -- 遍历序列(`Tuple`、`List`)
+
+限制:
+
+- 继承`单if`所有限制。
+
+- 继承`for`所有限制。
+
+- `cond`为变量时,不能有`if (cond):return`、`if (cond):continue`、`if (cond):break`语句。
+
+- `if`数量和`for`循环的迭代次数成倍数关系,`for`循环迭代次数过大可能会导致编译时间过长。
示例如下:
```python
+z = Tensor(np.ones((2, 3)))
+x = (1, 2, 3)
for i in x:
- if i == 2:
- continue
- statement_a
-statement_b
- ```
+ if i < 3:
+ z += i
+return z
+```
-当 `i == 2`时,本轮循环终止,不会往下执行`statement_a`,进入下一轮循环。
+结果如下:
-#### pass
+```text
+z: Tensor(shape=[2, 3], dtype=Int64, value=[[4, 4], [4, 4], [4, 4]])
+```
+
+#### if in while
-不做任何事情,占位语句。
+使用方式:
+
+- `while (cond1):if (cond2)`
+
+参数:`cond1`、 `cond2`-- 与`单if`一致。
+
+限制:
+
+- 继承`单if`、`单while`所有限制。
+
+- `cond2`为变量时,不能有`if (cond2):return`、`if (cond2):continue`、`if (cond2):break`语句。
+
+示例:
+
+```python
+out = m
+while x < y:
+ if z > 2*x:
+ out = out + 1
+ x += 1
+return out
+```
### 函数定义语句
diff --git a/docs/programming_guide/source_en/advanced_usage_of_checkpoint.md b/docs/programming_guide/source_en/advanced_usage_of_checkpoint.md
index caec70c9835d9b79ef24cb09eaafed98b2d919f1..49e96edc957d2c052c7c675bb0b2e17dbc1b0599 100644
--- a/docs/programming_guide/source_en/advanced_usage_of_checkpoint.md
+++ b/docs/programming_guide/source_en/advanced_usage_of_checkpoint.md
@@ -2,4 +2,4 @@
No English version right now, welcome to contribute.
-
+
diff --git a/docs/programming_guide/source_en/api_structure.md b/docs/programming_guide/source_en/api_structure.md
index 6df689bd542165ad963fc18f93c1785dfb150dd9..ed107dad895ee8025964c3883c5e81f411d8e479 100644
--- a/docs/programming_guide/source_en/api_structure.md
+++ b/docs/programming_guide/source_en/api_structure.md
@@ -9,19 +9,19 @@
-
+
## Overall Architecture
MindSpore is a deep learning framework in all scenarios, aiming to achieve easy development, efficient execution, and all-scenario coverage. Easy development features include API friendliness and low debugging difficulty. Efficient execution includes computing efficiency, data preprocessing efficiency, and distributed training efficiency. All-scenario coverage means that the framework supports cloud, edge, and device scenarios.
-The overall architecture of MindSpore consists of the Mind Expression (ME), Graph Engine (GE), and backend runtime. ME provides user-level APIs for scientific computing, building and training neural networks, and converting Python code of users into graphs. GE is a manager of operators and hardware resources, and is responsible for controlling execution of graphs received from ME. Backend runtime includes efficient running environments, such as the CPU, GPU, Ascend AI processors, and Android/iOS, on the cloud, edge, and device. For more information about the overall architecture, see [Overall Architecture](https://www.mindspore.cn/doc/note/en/master/design/mindspore/architecture.html).
+The overall architecture of MindSpore consists of the Mind Expression (ME), Graph Engine (GE), and backend runtime. ME provides user-level APIs for scientific computing, building and training neural networks, and converting Python code of users into graphs. GE is a manager of operators and hardware resources, and is responsible for controlling execution of graphs received from ME. Backend runtime includes efficient running environments, such as the CPU, GPU, Ascend AI processors, and Android/iOS, on the cloud, edge, and device. For more information about the overall architecture, see [Overall Architecture](https://www.mindspore.cn/doc/note/en/r1.2/design/mindspore/architecture.html).
## Design Concept
MindSpore originates from the best practices of the entire industry and provides unified model training, inference, and export APIs for data scientists and algorithm engineers. It supports flexible deployment in different scenarios such as the device, edge, and cloud, and promotes the prosperity of domains such as deep learning and scientific computing.
-MindSpore provides the Python programming paradigm. Users can use the native control logic of Python to build complex neural network models, simplifying AI programming. For details, see [Implementing an Image Classification Application](https://www.mindspore.cn/tutorial/training/en/master/quick_start/quick_start.html).
+MindSpore provides the Python programming paradigm. Users can use the native control logic of Python to build complex neural network models, simplifying AI programming. For details, see [Implementing an Image Classification Application](https://www.mindspore.cn/tutorial/training/en/r1.2/quick_start/quick_start.html).
Currently, there are two execution modes of a mainstream deep learning framework: a static graph mode and a dynamic graph mode. The static graph mode has a relatively high training performance, but is difficult to debug. On the contrary, the dynamic graph mode is easy to debug, but is difficult to execute efficiently. MindSpore provides an encoding mode that unifies dynamic and static graphs, which greatly improves the compatibility between static and dynamic graphs. Instead of developing multiple sets of code, users can switch between the dynamic and static graph modes by changing only one line of code. For example, set `context.set_context(mode=context.PYNATIVE_MODE)` to switch to the dynamic graph mode, or set `context.set_context(mode=context.GRAPH_MODE)` to switch to the static graph mode, which facilitates development and debugging, and improves performance experience.
@@ -62,11 +62,11 @@ In the first step, a function (computational graph) is defined. In the second st
In addition, the SCT can convert Python code into an intermediate representation (IR) of a MindSpore function. The IR constructs a computational graph that can be parsed and executed on different devices. Before the computational graph is executed, a plurality of software and hardware collaborative optimization technologies are used, and performance and efficiency in different scenarios such as device, edge, and cloud, are improved.
-Improving the data processing capability to match the computing power of AI chips is the key to ensure the ultimate performance of AI chips. MindSpore provides multiple data processing operators and uses automatic data acceleration technology to implement high-performance pipelines, including data loading, data demonstration, and data conversion. It supports data processing capabilities in all scenarios, such as CV, NLP, and GNN. MindRecord is a self-developed data format of MindSpore. It features efficient read and write and easy distributed processing. Users can convert non-standard and common datasets to the MindRecord format to obtain better performance experience. For details about the conversion, see [MindSpore Data Format Conversion](https://www.mindspore.cn/doc/programming_guide/en/master/dataset_conversion.html). MindSpore supports the loading of common datasets and datasets in multiple data storage formats. For example, users can use `dataset=dataset.Cifar10Dataset("Cifar10Data/")` to load the CIFAR-10 dataset. `Cifar10Data/` indicates the local directory of the dataset, and users can also use `GeneratorDataset` to customize the dataset loading mode. Data augmentation is a method of generating new data based on (limited) data, which can reduce the overfitting phenomenon of network model and improve the generalization ability of the model. In addition to user-defined data augmentation, MindSpore provides automatic data augmentation, making data augmentation more flexible. For details, see [Automatic Data Augmentation](https://www.mindspore.cn/doc/programming_guide/en/master/auto_augmentation.html).
+Improving the data processing capability to match the computing power of AI chips is the key to ensure the ultimate performance of AI chips. MindSpore provides multiple data processing operators and uses automatic data acceleration technology to implement high-performance pipelines, including data loading, data demonstration, and data conversion. It supports data processing capabilities in all scenarios, such as CV, NLP, and GNN. MindRecord is a self-developed data format of MindSpore. It features efficient read and write and easy distributed processing. Users can convert non-standard and common datasets to the MindRecord format to obtain better performance experience. For details about the conversion, see [MindSpore Data Format Conversion](https://www.mindspore.cn/doc/programming_guide/en/r1.2/dataset_conversion.html). MindSpore supports the loading of common datasets and datasets in multiple data storage formats. For example, users can use `dataset=dataset.Cifar10Dataset("Cifar10Data/")` to load the CIFAR-10 dataset. `Cifar10Data/` indicates the local directory of the dataset, and users can also use `GeneratorDataset` to customize the dataset loading mode. Data augmentation is a method of generating new data based on (limited) data, which can reduce the overfitting phenomenon of network model and improve the generalization ability of the model. In addition to user-defined data augmentation, MindSpore provides automatic data augmentation, making data augmentation more flexible. For details, see [Automatic Data Augmentation](https://www.mindspore.cn/doc/programming_guide/en/r1.2/auto_augmentation.html).
-The deep learning neural network model usually contains many hidden layers for feature extraction. However, the feature extraction is random and the debugging process is invisible, which limits the trustworthiness and optimization of the deep learning technology. MindSpore supports visualized debugging and optimization (MindInsight) and provides functions such as training dashboard, lineage, performance analysis, and debugger to help users detect deviations during model training and easily debug and optimize models. For example, before initializing the network, users can use `profiler=Profiler()` to initialize the `Profiler` object, automatically collect information such as the operator time consumption during training, and record the information in a file. After the training is complete, call `profiler.analyse()` to stop collecting data and generate performance analysis results. Users can view and analyze the visualized results to more efficiently debug network performance. For details about debugging and optimization, see [Training Process Visualization](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/visualization_tutorials.html).
+The deep learning neural network model usually contains many hidden layers for feature extraction. However, the feature extraction is random and the debugging process is invisible, which limits the trustworthiness and optimization of the deep learning technology. MindSpore supports visualized debugging and optimization (MindInsight) and provides functions such as training dashboard, lineage, performance analysis, and debugger to help users detect deviations during model training and easily debug and optimize models. For example, before initializing the network, users can use `profiler=Profiler()` to initialize the `Profiler` object, automatically collect information such as the operator time consumption during training, and record the information in a file. After the training is complete, call `profiler.analyse()` to stop collecting data and generate performance analysis results. Users can view and analyze the visualized results to more efficiently debug network performance. For details about debugging and optimization, see [Training Process Visualization](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/visualization_tutorials.html).
-As a scale of neural network models and datasets continuously increases, parallel distributed training becomes a common practice of neural network training. However, policy selection and compilation of parallel distributed training are very complex, which severely restricts training efficiency of a deep learning model and hinders development of deep learning. MindSpore unifies the encoding methods of standalone and distributed training. Developers do not need to write complex distributed policies. Instead, they can implement distributed training by adding a small amount of codes to the standalone code. For example, after `context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL)` is set, a cost model can be automatically established, and a better parallel mode can be selected for users. This improves the training efficiency of neural networks, greatly decreases the AI development difficulty, and enables users to quickly implement model. For more information, see [Distributed Training](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/distributed_training_tutorials.html).
+As a scale of neural network models and datasets continuously increases, parallel distributed training becomes a common practice of neural network training. However, policy selection and compilation of parallel distributed training are very complex, which severely restricts training efficiency of a deep learning model and hinders development of deep learning. MindSpore unifies the encoding methods of standalone and distributed training. Developers do not need to write complex distributed policies. Instead, they can implement distributed training by adding a small amount of codes to the standalone code. For example, after `context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL)` is set, a cost model can be automatically established, and a better parallel mode can be selected for users. This improves the training efficiency of neural networks, greatly decreases the AI development difficulty, and enables users to quickly implement model. For more information, see [Distributed Training](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/distributed_training_tutorials.html).
## Level Structure
diff --git a/docs/programming_guide/source_en/augmentation.md b/docs/programming_guide/source_en/augmentation.md
index 98da63d3e13a962f12b3c2b39367ccc12168af12..3ff234f1573f42f15b76f3d642c978bf580f63c5 100644
--- a/docs/programming_guide/source_en/augmentation.md
+++ b/docs/programming_guide/source_en/augmentation.md
@@ -16,7 +16,7 @@
-
+
## Overview
@@ -29,7 +29,7 @@ MindSpore provides the `c_transforms` and `py_transforms` modules for data augme
| c_transforms | Implemented based on C++. | This module provides high performance. |
| py_transforms | Implemented based on Python PIL | This module provides multiple image augmentation methods and can convert PIL images to NumPy arrays. |
-The following table lists the common data augmentation operators supported by MindSpore. For details about more data augmentation operators, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.dataset.vision.html).
+The following table lists the common data augmentation operators supported by MindSpore. For details about more data augmentation operators, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.dataset.vision.html).
| Module | Operator | Description |
| ------------- | -------------------- | ------------------------------------------------------------------ |
@@ -63,13 +63,18 @@ The following example uses a sequential sampler to load the CIFAR-10 dataset [1]
Download [CIFAR-10 dataset](https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz) and decompress it to the specified path, execute the following command:
```bash
-!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/cifar10.zip
-!unzip -o cifar10.zip -d ./datasets
-!tree ./datasets/cifar10
+!wget -N https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/cifar-10-binary.tar.gz
+!mkdir -p datasets
+!tar -xzf cifar-10-binary.tar.gz -C datasets
+!mkdir -p datasets/cifar-10-batches-bin/train datasets/cifar-10-batches-bin/test
+!mv -f datasets/cifar-10-batches-bin/test_batch.bin datasets/cifar-10-batches-bin/test
+!mv -f datasets/cifar-10-batches-bin/data_batch*.bin datasets/cifar-10-batches-bin/batches.meta.txt datasets/cifar-10-batches-bin/train
+!tree ./datasets/cifar-10-batches-bin
```
```text
-./datasets/cifar10
+./datasets/cifar-10-batches-bin
+├── readme.html
├── test
│ └── test_batch.bin
└── train
@@ -80,7 +85,7 @@ Download [CIFAR-10 dataset](https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar
├── data_batch_4.bin
└── data_batch_5.bin
-2 directories, 7 files
+2 directories, 8 files
```
```python
@@ -91,7 +96,7 @@ import mindspore.dataset.vision.c_transforms as c_trans
ds.config.set_seed(5)
ds.config.set_num_parallel_workers(1)
-DATA_DIR = "./datasets/cifar10/train/"
+DATA_DIR = "./datasets/cifar-10-batches-bin/train/"
sampler = ds.SequentialSampler(num_samples=3)
dataset1 = ds.Cifar10Dataset(DATA_DIR, sampler=sampler)
@@ -161,7 +166,7 @@ import mindspore.dataset.vision.c_transforms as c_trans
ds.config.set_seed(6)
ds.config.set_num_parallel_workers(1)
-DATA_DIR = "./datasets/cifar10/train/"
+DATA_DIR = "./datasets/cifar-10-batches-bin/train/"
sampler = ds.RandomSampler(num_samples=4)
dataset1 = ds.Cifar10Dataset(DATA_DIR, sampler=sampler)
@@ -199,8 +204,8 @@ The output is as follows:
Source image Shape : (32, 32, 3) , Source label : 3
Flipped image Shape: (32, 32, 3) , Flipped label: 3
------
-Source image Shape : (32, 32, 3) , Source label : 6
-Flipped image Shape: (32, 32, 3) , Flipped label: 6
+Source image Shape : (32, 32, 3) , Source label : 3
+Flipped image Shape: (32, 32, 3) , Flipped label: 3
------
Source image Shape : (32, 32, 3) , Source label : 6
Flipped image Shape: (32, 32, 3) , Flipped label: 6
@@ -228,13 +233,16 @@ The following example loads the MNIST dataset [2], resizes the loaded image to (
Download and decompress the MNIST dataset, store it in the `./datasets/MNIST_data/` path, execute the following command:
```bash
-!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/MNIST_Data.zip
-!unzip -o MNIST_Data.zip -d ./datasets
-!tree ./datasets/MNIST_Data/
+!mkdir -p ./datasets/MNIST_Data/train ./datasets/MNIST_Data/test
+!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-labels-idx1-ubyte
+!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-images-idx3-ubyte
+!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-labels-idx1-ubyte
+!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-images-idx3-ubyte
+!tree ./datasets/MNIST_Data
```
```text
-./datasets/MNIST_Data/
+./datasets/MNIST_Data
├── test
│ ├── t10k-images-idx3-ubyte
│ └── t10k-labels-idx1-ubyte
@@ -317,7 +325,7 @@ import mindspore.dataset.vision.c_transforms as c_trans
ds.config.set_seed(8)
-DATA_DIR = "./datasets/cifar10/train/"
+DATA_DIR = "./datasets/cifar-10-batches-bin/train/"
dataset1 = ds.Cifar10Dataset(DATA_DIR, num_samples=4, shuffle=True)
@@ -352,17 +360,17 @@ plt.show()
The output is as follows:
```text
-Source image Shape : (32, 32, 3) , Source label : 4
-Flipped image Shape: (32, 32, 3) , Flipped label: 4
+Source image Shape : (32, 32, 3) , Source label : 7
+Flipped image Shape: (101, 101, 3) , Flipped label: 7
------
-Source image Shape : (32, 32, 3) , Source label : 9
-Flipped image Shape: (32, 32, 3) , Flipped label: 9
+Source image Shape : (32, 32, 3) , Source label : 0
+Flipped image Shape: (101, 101, 3) , Flipped label: 0
------
-Source image Shape : (32, 32, 3) , Source label : 6
-Flipped image Shape: (32, 32, 3) , Flipped label: 6
+Source image Shape : (32, 32, 3) , Source label : 2
+Flipped image Shape: (101, 101, 3) , Flipped label: 2
------
-Source image Shape : (32, 32, 3) , Source label : 5
-Flipped image Shape: (32, 32, 3) , Flipped label: 5
+Source image Shape : (32, 32, 3) , Source label : 1
+Flipped image Shape: (101, 101, 3) , Flipped label: 1
------
```
@@ -391,9 +399,10 @@ from PIL import Image
ds.config.set_seed(8)
-DATA_DIR = "./datasets/cifar10/train/"
+DATA_DIR = "./datasets/cifar-10-batches-bin/train/"
dataset1 = ds.Cifar10Dataset(DATA_DIR, num_samples=5, shuffle=True)
+
def decode(image):
return Image.fromarray(image)
@@ -422,11 +431,11 @@ plt.show()
The output is as follows:
```text
-Transformed image Shape: (3, 200, 200) , Transformed label: 4
-Transformed image Shape: (3, 200, 200) , Transformed label: 9
-Transformed image Shape: (3, 200, 200) , Transformed label: 6
-Transformed image Shape: (3, 200, 200) , Transformed label: 5
Transformed image Shape: (3, 200, 200) , Transformed label: 7
+Transformed image Shape: (3, 200, 200) , Transformed label: 0
+Transformed image Shape: (3, 200, 200) , Transformed label: 2
+Transformed image Shape: (3, 200, 200) , Transformed label: 1
+Transformed image Shape: (3, 200, 200) , Transformed label: 6
```
The following shows the processed image.
@@ -435,8 +444,8 @@ The following shows the processed image.
## Eager Mode
-All data augmentation operators we introduced above need to be run under pipeline mode. That is, we have to
-define a `map` operator which helps us to apply these augmentations, for example:
+All data augmentation operators `c_transform` and `py_transform` we introduced above need to be run under pipeline mode. That is, we have to
+define a `map` operator which helps us to start and execute the given data augmentation operator, and to map and transfor the data of the data pipeline, for example:
```python
random_crop = c_trans.RandomCrop([10, 10])
@@ -446,19 +455,18 @@ dataset = dataset.map(operations=random_crop, input_columns=["image"])
However, the pipeline code seems heavy while we sometime just want to do a little experiment (e.g. model inference).
Thus, MindSpore provides a simple way to execute these augmentation operators, calls `Eager Mode`.
-To achieve the augmented result, you can write code easily as following:
+To use Eager mode, you only need to use the data enhancement operator itself as an executable function, you can write code easily as following:
```python
-import os
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import mindspore.dataset.vision.c_transforms as C
import mindspore.dataset.vision.py_transforms as P
-os.system("wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/banana.jpg")
-img = Image.open("banana.jpg").convert("RGB")
-print("Image.type: {}, Image.shape: {}".format(type(img), img.size))
+!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/banana.jpg
+img_ori = Image.open("banana.jpg").convert("RGB")
+print("Image.type: {}, Image.shape: {}".format(type(img_ori), img_ori.size))
# Define a Resize op from c_transform and execute it immediately
op1 = C.Resize(size=(320))
@@ -501,16 +509,18 @@ The following shows the processed image.

- Augmentation operators that support to be run in Eager Mode are listed in the following files:
- - [mindspore.dataset.vision.c_transforms](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.dataset.vision.html#mindspore-dataset-vision-c-transforms)
+ - [mindspore.dataset.vision.c_transforms](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.dataset.vision.html#mindspore-dataset-vision-c-transforms)
- - [mindspore.dataset.vision.py_transforms](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.dataset.vision.html#mindspore-dataset-vision-py-transforms)
+ - [mindspore.dataset.vision.py_transforms](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.dataset.vision.html#mindspore-dataset-vision-py-transforms)
- - [mindspore.dataset.text.transforms](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.dataset.text.html#mindspore-dataset-text-transforms)
+ - [mindspore.dataset.text.transforms](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.dataset.text.html#mindspore-dataset-text-transforms)
## Usage Instructions
Do not use `c_transforms` and `py_transforms` together because they apply to images in different ways and using them together will reduce the processing performance (Except for Eager Mode).
+(Note: The mixed use of `c_transforms` and `py_transforms` in Eager mode is not affected by differences in operating modes.)
+

Using both C++ and Python will cause the cost of switching between them. You are advised not to use operators of the two modules together. However, it is acceptable to use a proper number of operators together.
diff --git a/docs/programming_guide/source_en/auto_augmentation.md b/docs/programming_guide/source_en/auto_augmentation.md
index d2ce902d1d7ff5e90649fedda3d2874243a39963..9d27378ae7fed280d19eebadc6997690ccc17c83 100644
--- a/docs/programming_guide/source_en/auto_augmentation.md
+++ b/docs/programming_guide/source_en/auto_augmentation.md
@@ -12,7 +12,7 @@
-
+
## Overview
@@ -24,7 +24,7 @@ Auto augmentation can be implemented based on probability or callback parameters
MindSpore provides a series of probability-based auto augmentation APIs. You can randomly select and combine various data augmentation operations to make data augmentation more flexible.
-For details about APIs, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.dataset.transforms.html).
+For details about APIs, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.dataset.transforms.html).
### RandomApply
diff --git a/docs/programming_guide/source_en/auto_parallel.md b/docs/programming_guide/source_en/auto_parallel.md
index 6c26cbce8789849041a7efdd75c3198f67077c23..93196a326fbf4556056aa210734d0aa4d401bc62 100644
--- a/docs/programming_guide/source_en/auto_parallel.md
+++ b/docs/programming_guide/source_en/auto_parallel.md
@@ -34,7 +34,7 @@
-
+
## Overview
@@ -102,7 +102,7 @@ context.get_auto_parallel_context("gradients_mean")
- `semi_auto_parallel`: semi-automatic parallel mode. In this mode, you can use the `shard` method to configure a segmentation policy for an operator. If no policy is configured, the data parallel policy is used by default.
- `auto_parallel`: automatic parallel mode. In this mode, the framework automatically creates a cost model and selects the optimal segmentation policy for users.
-The complete examples of `auto_parallel` and `data_parallel` are provided in [Distributed Training](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/distributed_training_tutorials.html).
+The complete examples of `auto_parallel` and `data_parallel` are provided in [Distributed Training](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/distributed_training_tutorials.html).
The following is a code example:
@@ -384,10 +384,10 @@ x = Parameter(Tensor(np.ones([2, 2])), layerwise_parallel=True)
Data parallel refers to the parallel mode in which data is segmented. Generally, data is segmented by batch and distributed to each computing unit (worker) for model calculation. In data parallel mode, datasets must be imported in data parallel mode, and `parallel_mode` must be set to `data_parallel`.
-For details about the test cases, see [Distributed Training](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/distributed_training_tutorials.html).
+For details about the test cases, see [Distributed Training](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/distributed_training_tutorials.html).
## Automatic Parallel
Automatic parallel is a distributed parallel mode that integrates data parallel, model parallel, and hybrid parallel. It can automatically establish a cost model and select a parallel mode for users. The cost model refers to modeling the training time based on the memory computing overhead and the communication overhead, and designing an efficient algorithm to find a parallel policy with a relatively short training time. In automatic parallel mode, datasets must be imported in data parallel mode, and `parallel_mode` must be set to `auto_parallel`.
-For details about the test cases, see the [Distributed Training](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/distributed_training_tutorials.html).
+For details about the test cases, see the [Distributed Training](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/distributed_training_tutorials.html).
diff --git a/docs/programming_guide/source_en/cache.md b/docs/programming_guide/source_en/cache.md
index 4bd90412e25a814e95729c7f067dccc72b41a503..42e6f1e3935678f7f190758c1fc6d2b804c92f1c 100644
--- a/docs/programming_guide/source_en/cache.md
+++ b/docs/programming_guide/source_en/cache.md
@@ -11,7 +11,7 @@
-
+
## Overview
@@ -170,7 +170,7 @@ Currently, the cache service supports only single-node cache. That is, the clien
Note that you need to create a cache instance for each of the two examples according to step 4, and use the created `test_cache` as the `cache` parameter in the dataset loading operator or map operator.
- CIFAR-10 dataset is used in the following two examples. Before running the sample, download and store the CIFAR-10 dataset by referring to [Loading Dataset](https://www.mindspore.cn/doc/programming_guide/en/master/dataset_loading.html#cifar-10-100).
+ CIFAR-10 dataset is used in the following two examples. Before running the sample, download and store the CIFAR-10 dataset by referring to [Loading Dataset](https://www.mindspore.cn/doc/programming_guide/en/r1.2/dataset_loading.html#cifar-10-100).
- Cache the original loaded dataset.
@@ -329,11 +329,11 @@ During the single-node multi-device distributed training, the cache operator all
done
```
- > Complete sample code: [cache.sh](https://gitee.com/mindspore/docs/blob/master/tutorials/tutorial_code/cache/cache.sh)
+ > Complete sample code: [cache.sh](https://gitee.com/mindspore/docs/blob/r1.2/tutorials/tutorial_code/cache/cache.sh)
4. Create and apply a cache instance.
- CIFAR-10 dataset is used in the following example. Before running the sample, download and store the CIFAR-10 dataset by referring to [Loading Dataset](https://www.mindspore.cn/doc/programming_guide/en/master/dataset_loading.html#cifar-10-100). The directory structure is as follows:
+ CIFAR-10 dataset is used in the following example. Before running the sample, download and store the CIFAR-10 dataset by referring to [Loading Dataset](https://www.mindspore.cn/doc/programming_guide/en/r1.2/dataset_loading.html#cifar-10-100). The directory structure is as follows:
```text
├─cache.sh
@@ -372,7 +372,7 @@ During the single-node multi-device distributed training, the cache operator all
print("Got {} samples on device {}".format(num_iter, args_opt.device))
```
- > Complete sample code: [my_training_script.py](https://gitee.com/mindspore/docs/blob/master/tutorials/tutorial_code/cache/my_training_script.py)
+ > Complete sample code: [my_training_script.py](https://gitee.com/mindspore/docs/blob/r1.2/tutorials/tutorial_code/cache/my_training_script.py)
5. Execute the training script.
diff --git a/docs/programming_guide/source_en/callback.md b/docs/programming_guide/source_en/callback.md
index 793d1637fa558241b0cd2d650935d75f108350ae..e5b6dc680434fc0fc065b9d6e90eba5665d930b9 100644
--- a/docs/programming_guide/source_en/callback.md
+++ b/docs/programming_guide/source_en/callback.md
@@ -9,7 +9,7 @@
-
+
## Overview
@@ -23,19 +23,19 @@ In MindSpore, the callback mechanism is generally used in the network training p
This function is combined with the model training process, and saves the model and network parameters after training to facilitate re-inference or re-training. `ModelCheckpoint` is generally used together with `CheckpointConfig`. `CheckpointConfig` is a parameter configuration class that can be used to customize the checkpoint storage policy.
- For details, see [Saving Models](https://www.mindspore.cn/tutorial/training/en/master/use/save_model.html).
+ For details, see [Saving Models](https://www.mindspore.cn/tutorial/training/en/r1.2/use/save_model.html).
- SummaryCollector
This function collects common information, such as loss, learning rate, computational graph, and parameter weight, helping you visualize the training process and view information. In addition, you can perform the summary operation to collect data from the summary file.
- For details, see [Collecting Summary Record](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/summary_record.html).
+ For details, see [Collecting Summary Record](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/summary_record.html).
- LossMonitor
This function monitors the loss change during training. When the loss is NAN or INF, the training is terminated in advance. Loss information can be recorded in logs for you to view.
- For details, see the [Custom Debugging Information](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/custom_debugging_info.html#mindsporecallback).
+ For details, see the [Custom Debugging Information](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/custom_debugging_info.html#mindsporecallback).
- TimeMonitor
@@ -51,6 +51,6 @@ The following examples are used to introduce the custom callback functions:
2. Save the checkpoint file with the highest accuracy during training. You can customize the function to save a model with the highest accuracy after each epoch.
-For details, see [Custom Callback](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/custom_debugging_info.html#custom-callback).
+For details, see [Custom Callback](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/custom_debugging_info.html#custom-callback).
According to the tutorial, you can easily customize other callback functions. For example, customize a function to output the detailed training information, including the training progress, training step, training name, and loss value, after each training is complete; terminate training when the loss or model accuracy reaches a certain value by setting the loss or model accuracy threshold. When the loss or model accuracy reaches the threshold, the training is terminated in advance.
diff --git a/docs/programming_guide/source_en/cell.md b/docs/programming_guide/source_en/cell.md
index 97cbf49319badf76cf84581b3d790b3ab8f015fc..1e495d5443bd7c19e4bb5106394d41323b1f6fb5 100644
--- a/docs/programming_guide/source_en/cell.md
+++ b/docs/programming_guide/source_en/cell.md
@@ -21,7 +21,7 @@
-
+
## Overview
@@ -64,7 +64,7 @@ class Net(nn.Cell):
The `parameters_dict` method is used to identify all parameters in the network structure and return `OrderedDict` with key as the parameter name and value as the parameter value.
-There are many other methods for returning parameters in the `Cell` class, such as `get_parameters` and `trainable_params`. For details, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/nn/mindspore.nn.Cell.html).
+There are many other methods for returning parameters in the `Cell` class, such as `get_parameters` and `trainable_params`. For details, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/nn/mindspore.nn.Cell.html).
A code example is as follows:
@@ -330,7 +330,7 @@ In this case, two pieces of tensor data are built. The `nn.L1Loss` API is used t
## Optimization Algorithms
-`mindspore.nn.optim` is a module that implements various optimization algorithms in the MindSpore framework. For details, see [Optimization Algorithms](https://www.mindspore.cn/doc/programming_guide/en/master/optim.html)
+`mindspore.nn.optim` is a module that implements various optimization algorithms in the MindSpore framework. For details, see [Optimization Algorithms](https://www.mindspore.cn/doc/programming_guide/en/r1.2/optim.html)
## Building a Customized Network
diff --git a/docs/programming_guide/source_en/context.md b/docs/programming_guide/source_en/context.md
index f30b97885d019f683e46faa02d2a480754426b44..ea6ba9647070c01107ae66a849844301dc14457b 100644
--- a/docs/programming_guide/source_en/context.md
+++ b/docs/programming_guide/source_en/context.md
@@ -11,12 +11,12 @@
- [Distributed Management](#distributed-management)
- [Maintenance and Test Management](#maintenance-and-test-management)
- [Profiling Data Collection](#profiling-data-collection)
- - [Saving MindIR](#saving mindir)
+ - [Saving MindIR](#saving-mindir)
- [Print Operator Disk Flushing](#print-operator-disk-flushing)
-
+
## Overview
@@ -60,11 +60,38 @@ conv = nn.Conv2d(3, 4, 3, bias_init='zeros')
input_data = Tensor(np.ones([1, 3, 5, 5]).astype(np.float32))
conv(input_data)
context.set_context(mode=context.PYNATIVE_MODE)
+
conv(input_data)
```
+```text
+Tensor(shape=[1, 4, 5, 5], dtype=Float32, value=
+[[[[ 1.64782144e-02, 5.31007685e-02, 5.31007685e-02, 5.31007685e-02, 5.11828624e-02],
+ [ 3.00714076e-02, 6.57572001e-02, 6.57572001e-02, 6.57572001e-02, 4.35083285e-02],
+ [ 3.00714076e-02, 6.57572001e-02, 6.57572001e-02, 6.57572001e-02, 4.35083285e-02]
+ [ 3.00714076e-02, 6.57572001e-02, 6.57572001e-02, 6.57572001e-02, 4.35083285e-02],
+ [ 1.84759758e-02, 4.71352898e-02, 4.71352898e-02, 4.71352898e-02, 3.72093469e-02]],
+ [[-3.36203352e-02, -6.12429380e-02, -6.12429380e-02, -6.12429380e-02, -4.33492810e-02],
+ [-2.67659649e-02, -8.04031491e-02, -8.04031491e-02, -8.04031491e-02, -6.84653893e-02],
+ [-2.67659649e-02, -8.04031491e-02, -8.04031491e-02, -8.04031491e-02, -6.84653893e-02]
+ [-2.67659649e-02, -8.04031491e-02, -8.04031491e-02, -8.04031491e-02, -6.84653893e-02],
+ [-5.57974726e-03, -6.80863336e-02, -6.80863336e-02, -6.80863336e-02, -8.38923305e-02]],
+ [[-1.60222687e-02, 2.26615220e-02, 2.26615220e-02, 2.26615220e-02, 6.03060052e-02],
+ [-6.76476881e-02, -2.96694487e-02, -2.96694487e-02, -2.96694487e-02, 4.86185402e-02],
+ [-6.76476881e-02, -2.96694487e-02, -2.96694487e-02, -2.96694487e-02, 4.86185402e-02]
+ [-6.76476881e-02, -2.96694487e-02, -2.96694487e-02, -2.96694487e-02, 4.86185402e-02],
+ [-6.52819276e-02, -3.50066647e-02, -3.50066647e-02, -3.50066647e-02, 2.85858363e-02]]
+ [[-3.10218725e-02, -3.84682454e-02, -3.84682454e-02, -3.84682454e-02, -8.58424231e-03],
+ [-4.27014455e-02, -7.07850009e-02, -7.07850009e-02, -7.07850009e-02, -5.36267459e-02],
+ [-4.27014455e-02, -7.07850009e-02, -7.07850009e-02, -7.07850009e-02, -5.36267459e-02]
+ [-4.27014455e-02, -7.07850009e-02, -7.07850009e-02, -7.07850009e-02, -5.36267459e-02],
+ [-1.23060495e-02, -4.99926135e-02, -4.99926135e-02, -4.99926135e-02, -4.71802950e-02]]]])
+```
+
In the preceding example, the running mode is set to `GRAPH_MODE` and then switched to `PYNATIVE_MODE`.
+> This code is applicable to GPU environment.
+
## Hardware Management
Hardware management involves the `device_target` and `device_id` parameters.
@@ -86,7 +113,7 @@ context.set_context(device_target="Ascend", device_id=6)
The context contains the context.set_auto_parallel_context API that is used to configure parallel training parameters. This API must be called before the network is initialized.
-> For details about distributed management, see [Parallel Distributed Training](https://www.mindspore.cn/doc/programming_guide/en/master/auto_parallel.html).
+> For details about distributed management, see [Parallel Distributed Training](https://www.mindspore.cn/doc/programming_guide/en/r1.2/auto_parallel.html).
## Maintenance and Test Management
@@ -96,27 +123,39 @@ To facilitate maintenance and fault locating, the context provides a large numbe
The system can collect profiling data during training and use the profiling tool for performance analysis. Currently, the following profiling data can be collected:
-- `enable_profiling`: indicates whether to enable the profiling function. If this parameter is set to True, the profiling function is enabled, and profiling options are read from enable_options. If this parameter is set to False, the profiling function is disabled and only training_trace is collected.
+- `enable_profiling`: indicates whether to enable the profiling function. If this parameter is set to True, the profiling function is enabled, and profiling options are read from `enable_options`. If this parameter is set to False, the profiling function is disabled and only training_trace is collected.
- `profiling_options`: profiling collection options. The values are as follows. Multiple data items can be collected.
- result_path: saving the path of the profiling collection result file. The directory spectified by this parameter needs to be created in advance on the training environment (container or host side) and ensure that the running user configured during installation has read and write permissions. It supports the configuration of absolute or relative paths(relative to the current path when executing the command line). The absolute path configuration starts with '/', for example:/home/data/output. The relative path configuration directly starts with the directory name, for example:output;
- training_trace: collect iterative trajectory data, that is, the training task and software information of the AI software stack, to achieve performance analysis of the training task, focusing on data enhancement, forward and backward calculation, gradient aggregation update and other related data. The value is on/off;
- task_trace: collect task trajectory data, that is, the hardware information of the HWTS/AICore of the Ascend 910 processor, and analyze the information of beginning and ending of the task. The value is on/off;
- aicpu_trace: collect profiling data enhanced by aicpu data. The value is on/off;
- fp_point: specify the start position of the forward operator of the training network iteration trajectory, which is used to record the start timestamp of the forward calculation. The configuration value is the name of the first operator specified in the forward direction. when the value is empty, the system will automatically obtain the forward operator name;
- bp_point: specify the end position of the iteration trajectory reversal operator of the training network, record the end timestamp of the backward calculation. The configuration value is the name of the operator after the specified reverse. when the value is empty, the system will automatically obtain the backward operator name;
- ai_core_metrics: the values are as follows:
- - ArithmeticUtilization: percentage statistics of various calculation indicators;
- - PipeUtilization: the time-consuming ratio of calculation unit and handling unit, this item is the default value;
- - Memory: percentage of external memory read and write instructions;
- - MemoryL0: percentage of internal memory read and write instructions;
- - ResourceConflictRatio: proportion of pipline queue instructions.
+
+ - `result_path`: saving the path of the profiling collection result file. The directory spectified by this parameter needs to be created in advance on the training environment (container or host side) and ensure that the running user configured during installation has read and write permissions. It supports the configuration of absolute or relative paths(relative to the current path when executing the command line). The absolute path configuration starts with '/', for example:/home/data/output. The relative path configuration directly starts with the directory name, for example:output;
+
+ - `training_trace`: collect iterative trajectory data, that is, the training task and software information of the AI software stack, to achieve performance analysis of the training task, focusing on data enhancement, forward and backward calculation, gradient aggregation update and other related data. The value is on/off;
+
+ - `task_trace`: collect task trajectory data, that is, the hardware information of the HWTS/AICore of the Ascend 910 processor, and analyze the information of beginning and ending of the task. The value is on/off;
+
+ - `aicpu_trace`: collect profiling data enhanced by aicpu data. The value is on/off;
+
+ - `fp_point`: specify the start position of the forward operator of the training network iteration trajectory, which is used to record the start timestamp of the forward calculation. The configuration value is the name of the first operator specified in the forward direction. when the value is empty, the system will automatically obtain the forward operator name;
+
+ - `bp_point`: specify the end position of the iteration trajectory reversal operator of the training network, record the end timestamp of the backward calculation. The configuration value is the name of the operator after the specified reverse. when the value is empty, the system will automatically obtain the backward operator name;
+
+ - `ai_core_metrics` the values are as follows:
+
+ - ArithmeticUtilization: percentage statistics of various calculation indicators;
+
+ - PipeUtilization: the time-consuming ratio of calculation unit and handling unit, this item is the default value;
+
+ - Memory: percentage of external memory read and write instructions;
+
+ - MemoryL0: percentage of internal memory read and write instructions;
+
+ - ResourceConflictRatio: proportion of pipline queue instructions.
A code example is as follows:
```python
from mindspore import context
-context.set_context(enable_profiling=True, profiling_options='{"result_path":"/home/data/output","training_trace":"on"}')
+context.set_context(enable_profiling=True, profiling_options= '{"result_path":"/home/data/output","training_trace":"on"}')
```
### Saving MindIR
@@ -134,13 +173,13 @@ from mindspore import context
context.set_context(save_graphs=True)
```
-> For details about the debugging method, see [Asynchronous Dump](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/custom_debugging_info.html#asynchronous-dump).
+> For a detailed introduction of MindIR, please refer to [MindSpore IR(MindIR)](https://www.mindspore.cn/doc/note/en/r1.2/design/mindspore/mindir.html).
### Print Operator Disk Flushing
By default, the MindSpore self-developed print operator can output the tensor or character string information entered by users. Multiple character string inputs, multiple tensor inputs, and hybrid inputs of character strings and tensors are supported. The input parameters are separated by commas (,).
-> For details about the print function, see [MindSpore Print Operator](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/custom_debugging_info.html#mindspore-print-operator).
+> For details about the print function, see [MindSpore Print Operator](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/custom_debugging_info.html#mindspore-print-operator).
- `print_file_path`: saves the print operator data to a file and disables the screen printing function. If the file to be saved exists, a timestamp suffix is added to the file. Saving data to a file can solve the problem that the data displayed on the screen is lost when the data volume is large.
@@ -151,4 +190,4 @@ from mindspore import context
context.set_context(print_file_path="print.pb")
```
-> For details about the context API, see [mindspore.context](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.context.html).
+> For details about the context API, see [mindspore.context](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.context.html).
diff --git a/docs/programming_guide/source_en/customized.rst b/docs/programming_guide/source_en/customized.rst
index c32e781c7c56a12f091135e194808e4c4ff05179..83f931eeb663f81d2505954e40065ec522eb8996 100644
--- a/docs/programming_guide/source_en/customized.rst
+++ b/docs/programming_guide/source_en/customized.rst
@@ -4,6 +4,6 @@ Custom Operators
.. toctree::
:maxdepth: 1
- Custom Operators (Ascend)
- Custom Operators (GPU)
- Custom Operators (CPU)
+ Custom Operators (Ascend)
+ Custom Operators (GPU)
+ Custom Operators (CPU)
diff --git a/docs/programming_guide/source_en/dataset_conversion.md b/docs/programming_guide/source_en/dataset_conversion.md
index 84320bf41642fbbd4e18c4f2d6e1a50cd7277aa6..e600f2d318ce0aa7c6f8addc13efaf9b3cb5990e 100644
--- a/docs/programming_guide/source_en/dataset_conversion.md
+++ b/docs/programming_guide/source_en/dataset_conversion.md
@@ -15,7 +15,7 @@
-
+
## Overview
@@ -81,6 +81,10 @@ Create a MindRecord file containing 100 records, whose samples include the `file
writer.commit()
```
+ ```text
+ MSRStatus.SUCCESS
+ ```
+
**Parameter description:**
- `MINDRECORD_FILE`: path of the output MindRecord file.
@@ -96,6 +100,10 @@ Create a MindRecord file containing 100 records, whose samples include the `file
print("Got {} samples".format(count))
```
+ ```text
+ Got 100 samples
+ ```
+
### Converting NLP Dataset
This example describes how to convert an NLP dataset into MindRecord and use `MindDataset` to load the dataset. The process of converting the text into the lexicographic order is omitted in this example.
@@ -123,13 +131,13 @@ Create a MindRecord file containing 100 records, whose samples include eight fie
writer = FileWriter(file_name=MINDRECORD_FILE, shard_num=1)
nlp_schema = {"source_sos_ids": {"type": "int64", "shape": [-1]},
- "source_sos_mask": {"type": "int64", "shape": [-1]},
- "source_eos_ids": {"type": "int64", "shape": [-1]},
- "source_eos_mask": {"type": "int64", "shape": [-1]},
- "target_sos_ids": {"type": "int64", "shape": [-1]},
- "target_sos_mask": {"type": "int64", "shape": [-1]},
- "target_eos_ids": {"type": "int64", "shape": [-1]},
- "target_eos_mask": {"type": "int64", "shape": [-1]}}
+ "source_sos_mask": {"type": "int64", "shape": [-1]},
+ "source_eos_ids": {"type": "int64", "shape": [-1]},
+ "source_eos_mask": {"type": "int64", "shape": [-1]},
+ "target_sos_ids": {"type": "int64", "shape": [-1]},
+ "target_sos_mask": {"type": "int64", "shape": [-1]},
+ "target_eos_ids": {"type": "int64", "shape": [-1]},
+ "target_eos_mask": {"type": "int64", "shape": [-1]}}
writer.add_schema(nlp_schema, "it is a preprocessed nlp dataset")
data = []
@@ -137,13 +145,13 @@ Create a MindRecord file containing 100 records, whose samples include eight fie
i += 1
sample = {"source_sos_ids": np.array([i, i + 1, i + 2, i + 3, i + 4], dtype=np.int64),
- "source_sos_mask": np.array([i * 1, i * 2, i * 3, i * 4, i * 5, i * 6, i * 7], dtype=np.int64),
- "source_eos_ids": np.array([i + 5, i + 6, i + 7, i + 8, i + 9, i + 10], dtype=np.int64),
- "source_eos_mask": np.array([19, 20, 21, 22, 23, 24, 25, 26, 27], dtype=np.int64),
- "target_sos_ids": np.array([28, 29, 30, 31, 32], dtype=np.int64),
- "target_sos_mask": np.array([33, 34, 35, 36, 37, 38], dtype=np.int64),
- "target_eos_ids": np.array([39, 40, 41, 42, 43, 44, 45, 46, 47], dtype=np.int64),
- "target_eos_mask": np.array([48, 49, 50, 51], dtype=np.int64)}
+ "source_sos_mask": np.array([i * 1, i * 2, i * 3, i * 4, i * 5, i * 6, i * 7], dtype=np.int64),
+ "source_eos_ids": np.array([i + 5, i + 6, i + 7, i + 8, i + 9, i + 10], dtype=np.int64),
+ "source_eos_mask": np.array([19, 20, 21, 22, 23, 24, 25, 26, 27], dtype=np.int64),
+ "target_sos_ids": np.array([28, 29, 30, 31, 32], dtype=np.int64),
+ "target_sos_mask": np.array([33, 34, 35, 36, 37, 38], dtype=np.int64),
+ "target_eos_ids": np.array([39, 40, 41, 42, 43, 44, 45, 46, 47], dtype=np.int64),
+ "target_eos_mask": np.array([48, 49, 50, 51], dtype=np.int64)}
data.append(sample)
if i % 10 == 0:
@@ -156,6 +164,10 @@ Create a MindRecord file containing 100 records, whose samples include eight fie
writer.commit()
```
+ ```text
+ MSRStatus.SUCCESS
+ ```
+
**Parameter description:**
- `MINDRECORD_FILE`: path of the output MindRecord file.
@@ -169,6 +181,10 @@ Create a MindRecord file containing 100 records, whose samples include eight fie
print("Got {} samples".format(count))
```
+ ```text
+ Got 100 samples
+ ```
+
## Converting Common Dataset to MindRecord
MindSpore provides tool classes for converting common datasets to MindRecord. The following table lists part of common datasets and their corresponding tool classes.
@@ -180,29 +196,39 @@ MindSpore provides tool classes for converting common datasets to MindRecord. Th
| TFRecord | TFRecordToMR |
| CSV File | CsvToMR |
-For details about dataset conversion, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.mindrecord.html).
+For details about dataset conversion, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.mindrecord.html).
### Converting the CIFAR-10 Dataset
You can use the `Cifar10ToMR` class to convert the original CIFAR-10 data to MindRecord and use `MindDataset` to load the data.
-1. Download and decompress the [CIFAR-10 dataset](https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz). The directory structure is as follows:
+1. Download and decompress the [CIFAR-10 dataset](https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz). Execute the following command:
+
+ ```bash
+ !wget -N https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/cifar-10-python.tar.gz
+ !mkdir -p datasets
+ !tar -xzf cifar-10-python.tar.gz -C datasets
+ !tree ./datasets/cifar-10-batches-py
+ ```
```text
- └─cifar-10-batches-py
- ├─batches.meta
- ├─data_batch_1
- ├─data_batch_2
- ├─data_batch_3
- ├─data_batch_4
- ├─data_batch_5
- ├─readme.html
- └─test_batch
+ ./datasets/cifar-10-batches-py
+ ├── batches.meta
+ ├── data_batch_1
+ ├── data_batch_2
+ ├── data_batch_3
+ ├── data_batch_4
+ ├── data_batch_5
+ ├── readme.html
+ └── test_batch
+
+ 0 directories, 8 files
```
2. Import related modules.
```python
+ import os
import mindspore.dataset as ds
import mindspore.dataset.vision.c_transforms as vision
from mindspore.mindrecord import Cifar10ToMR
@@ -211,10 +237,19 @@ You can use the `Cifar10ToMR` class to convert the original CIFAR-10 data to Min
3. Create the `Cifar10ToMR` object and call the `transform` API to convert the CIFAR-10 dataset to MindRecord.
```python
- CIFAR10_DIR = "./cifar-10-batches-py"
- MINDRECORD_FILE = "./cifar10.mindrecord"
+ ds_target_path = "./datasets/mindspore_dataset_conversion/"
+ # clean old run files
+ os.system("rm -f {}*".format(ds_target_path))
+ os.system("mkdir -p {}".format(ds_target_path))
+
+ CIFAR10_DIR = "./datasets/cifar-10-batches-py"
+ MINDRECORD_FILE = "./datasets/mindspore_dataset_conversion/cifar10.mindrecord"
cifar10_transformer = Cifar10ToMR(CIFAR10_DIR, MINDRECORD_FILE)
cifar10_transformer.transform(['label'])
+ ```
+
+ ```text
+ MSRStatus.SUCCESS
```
**Parameter description:**
@@ -233,6 +268,10 @@ You can use the `Cifar10ToMR` class to convert the original CIFAR-10 data to Min
print("Got {} samples".format(count))
```
+ ```text
+ Got 50000 samples
+ ```
+
### Converting the ImageNet Dataset
You can use the `ImageNetToMR` class to convert the original ImageNet data (images and annotations) to MindRecord and use `MindDataset` to load the data.
@@ -268,7 +307,7 @@ You can use the `ImageNetToMR` class to convert the original ImageNet data (imag
```python
IMAGENET_MAP_FILE = "./labels_map.txt"
- IMAGENET_IMAGE_DIR = "./images/"
+ IMAGENET_IMAGE_DIR = "./images"
MINDRECORD_FILE = "./imagenet.mindrecord"
imagenet_transformer = ImageNetToMR(IMAGENET_MAP_FILE, IMAGENET_IMAGE_DIR, MINDRECORD_FILE, partition_number=1)
imagenet_transformer.transform()
@@ -350,10 +389,24 @@ Create a CSV file containing 5 records, convert the CSV file to MindRecord using
print("Got {} samples".format(count))
```
+ ```text
+ Got 5 samples
+ ```
+
### Converting TFRecord Dataset
> Currently, only TensorFlow 1.13.0-rc1 and later versions are supported.
+In this part of the example, TensorFlow needs to be installed in advance. If it is not installed, execute the following command to install it. For example, when this document is running as a Notebook, after the installation is complete, you need to restart the kernel to execute the subsequent code.
+
+```python
+os.system('pip install tensorflow') if os.system('python -c "import tensorflow"') else print("TensorFlow installed")
+```
+
+```text
+0
+```
+
Use TensorFlow to create a TFRecord file and convert the file to MindRecord using the `TFRecordToMR` tool class. Read the file using `MindDataset` and decode the `image_bytes` field using the `Decode` operator.
1. Import related modules.
@@ -429,6 +482,10 @@ Use TensorFlow to create a TFRecord file and convert the file to MindRecord usin
generate_tfrecord()
```
+ ```text
+ Write 10 rows in tfrecord.
+ ```
+
**Parameter description:**
- `TFRECORD_FILE`: path of the TFRecord file.
- `MINDRECORD_FILE`: path of the output MindRecord file.
@@ -466,3 +523,7 @@ Use TensorFlow to create a TFRecord file and convert the file to MindRecord usin
count += 1
print("Got {} samples".format(count))
```
+
+ ```text
+ Got 10 samples
+ ```
diff --git a/docs/programming_guide/source_en/dataset_loading.md b/docs/programming_guide/source_en/dataset_loading.md
index 20419d06565cfc291beaf76c3809fd2e63239dd6..ea14fc0d8a2cdc5a45b425aa1ab41039a42afbfa 100644
--- a/docs/programming_guide/source_en/dataset_loading.md
+++ b/docs/programming_guide/source_en/dataset_loading.md
@@ -21,7 +21,7 @@
-
+
## Overview
@@ -55,7 +55,7 @@ MindSpore also supports user-defined dataset loading using `GeneratorDataset`. Y
| GeneratorDataset | User defined class or function to load and process dataset. |
| NumpySlicesDataset | User defined data source to construct dataset using NumPy. |
-> For details about the API for dataset loading, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.dataset.html).
+> For details about the API for dataset loading, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.dataset.html).
## Loading Common Dataset
@@ -65,16 +65,30 @@ The following describes how to load common datasets.
Download [CIFAR-10 dataset](https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz) and decompress it, the directory structure is as follows:
+```bash
+!wget -N https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/cifar-10-binary.tar.gz
+!mkdir -p datasets
+!tar -xzf cifar-10-binary.tar.gz -C datasets
+!mkdir -p datasets/cifar-10-batches-bin/train datasets/cifar-10-batches-bin/test
+!mv -f datasets/cifar-10-batches-bin/test_batch.bin datasets/cifar-10-batches-bin/test
+!mv -f datasets/cifar-10-batches-bin/data_batch*.bin datasets/cifar-10-batches-bin/batches.meta.txt datasets/cifar-10-batches-bin/train
+!tree ./datasets/cifar-10-batches-bin
+```
+
```text
-└─cifar-10-batches-bin
- ├── batches.meta.txt
- ├── data_batch_1.bin
- ├── data_batch_2.bin
- ├── data_batch_3.bin
- ├── data_batch_4.bin
- ├── data_batch_5.bin
- ├── readme.html
- └── test_batch.bin
+./datasets/cifar-10-batches-bin
+├── readme.html
+├── test
+│ └── test_batch.bin
+└── train
+ ├── batches.meta.txt
+ ├── data_batch_1.bin
+ ├── data_batch_2.bin
+ ├── data_batch_3.bin
+ ├── data_batch_4.bin
+ └── data_batch_5.bin
+
+2 directories, 8 files
```
The following example uses the `Cifar10Dataset` API to load the CIFAR-10 dataset, uses the sequential sampler to obtain five samples, and displays the shape and label of the corresponding image.
@@ -84,7 +98,7 @@ The methods for loading the CIFAR-100 and MNIST datasets are similar.
```python
import mindspore.dataset as ds
-DATA_DIR = "cifar-10-batches-bin/"
+DATA_DIR = "./datasets/cifar-10-batches-bin/train/"
sampler = ds.SequentialSampler(num_samples=5)
dataset = ds.Cifar10Dataset(DATA_DIR, sampler=sampler)
@@ -210,49 +224,121 @@ The following describes how to load dataset files in specific formats.
MindRecord is a data format defined by MindSpore. Using MindRecord can improve performance.
-> For details about how to convert a dataset into the MindRecord data format, see [Data Format Conversion](https://www.mindspore.cn/doc/programming_guide/en/master/dataset_conversion.html).
+> For details about how to convert a dataset into the MindRecord data format, see [Data Format Conversion](https://www.mindspore.cn/doc/programming_guide/en/r1.2/dataset_conversion.html).
+
+Before executing this example, you need to download the corresponding test data `test_mindrecord.zip` and unzip it to the specified location, execute the following command:
+
+```bash
+!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/test_mindrecord.zip
+!unzip -o ./test_mindrecord.zip -d ./datasets/mindspore_dataset_loading/
+!tree ./datasets/mindspore_dataset_loading/
+```
+
+```text
+./datasets/mindspore_dataset_loading/
+├── test.mindrecord
+└── test.mindrecord.db
+
+0 directories, 2 files
+```
The following example uses the `MindDataset` API to load MindRecord files, and displays labels of the loaded data.
```python
import mindspore.dataset as ds
-DATA_FILE = ["mindrecord_file_0", "mindrecord_file_1", "mindrecord_file_2"]
+DATA_FILE = ["./datasets/mindspore_dataset_loading/test.mindrecord"]
mindrecord_dataset = ds.MindDataset(DATA_FILE)
for data in mindrecord_dataset.create_dict_iterator(output_numpy=True):
- print(data["label"])
+ print(data.keys())
+```
+
+```text
+dict_keys(['chinese', 'english'])
+dict_keys(['chinese', 'english'])
+dict_keys(['chinese', 'english'])
```
### Manifest
Manifest is a data format file supported by Huawei ModelArts. For details, see [Specifications for Importing the Manifest File](https://support.huaweicloud.com/en-us/engineers-modelarts/modelarts_23_0009.html).
+In this example, you need to download the test data `test_manifest.zip` and unzip it to the specified location, and execute the following command:
+
+```bash
+!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/test_manifest.zip
+!unzip -o ./test_manifest.zip -d ./datasets/mindspore_dataset_loading/test_manifest/
+!tree ./datasets/mindspore_dataset_loading/test_manifest/
+```
+
+```text
+./datasets/mindspore_dataset_loading/test_manifest/
+├── eval
+│ ├── 1.JPEG
+│ └── 2.JPEG
+├── test_manifest.json
+└── train
+ ├── 1.JPEG
+ └── 2.JPEG
+
+2 directories, 5 files
+```
+
The following example uses the `ManifestDataset` API to load a Manifest file, and displays labels of the loaded data.
```python
import mindspore.dataset as ds
-DATA_FILE = "manifest_file"
+DATA_FILE = "./datasets/mindspore_dataset_loading/test_manifest/test_manifest.json"
manifest_dataset = ds.ManifestDataset(DATA_FILE)
for data in manifest_dataset.create_dict_iterator():
print(data["label"])
```
+```text
+0
+1
+```
+
### TFRecord
TFRecord is a binary data file format defined by TensorFlow.
The following example uses the `TFRecordDataset` API to load TFRecord files and introduces two methods for setting the format of datasets.
-1. Specify the dataset path or TFRecord file list to create a `TFRecordDataset` object.
+Download the `tfrecord` test data `test_tftext.zip` and unzip it to the specified location, execute the following command:
+
+```bash
+!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/test_tftext.zip
+!unzip -o ./test_tftext.zip -d ./datasets/mindspore_dataset_loading/test_tfrecord/
+!tree ./datasets/mindspore_dataset_loading/test_tfrecord/
+```
+
+```text
+./datasets/mindspore_dataset_loading/test_tfrecord/
+└── test_tftext.tfrecord
+
+0 directories, 1 file
+```
+
+1. Specify the dataset path or TFRecord file list to create a `TFRecordDataset` object, this example uses test_tftext.tfrecord.
```python
import mindspore.dataset as ds
- DATA_FILE = ["tfrecord_file_0", "tfrecord_file_1", "tfrecord_file_2"]
+ DATA_FILE = "./datasets/mindspore_dataset_loading/test_tfrecord/test_tftext.tfrecord"
tfrecord_dataset = ds.TFRecordDataset(DATA_FILE)
+
+ for tf_data in tfrecord_dataset.create_dict_iterator():
+ print(tf_data.keys())
+ ```
+
+ ```text
+ dict_keys(['chinese', 'line', 'words'])
+ dict_keys(['chinese', 'line', 'words'])
+ dict_keys(['chinese', 'line', 'words'])
```
2. Compile a schema file or create a schema object to set the dataset format and features.
@@ -261,45 +347,87 @@ The following example uses the `TFRecordDataset` API to load TFRecord files and
Write the dataset format and features to the schema file in JSON format. The following is an example:
- ```json
- {
- "columns": {
- "image": {
- "type": "uint8",
- "rank": 1
- },
- "label" : {
- "type": "string",
- "rank": 1
- }
- "id" : {
- "type": "int64",
- "rank": 0
- }
- }
- }
- ```
-
- - `columns`: column information field, which needs to be defined based on the actual column name of the dataset. In the preceding example, the dataset columns are `image`, `label`, and `id`.
+ - `columns`: column information field, which needs to be defined based on the actual column name of the dataset. In the preceding example, the dataset columns are `image`, `label`, and `id`.
When creating `TFRecordDataset`, transfer the path of the schema file.
```python
+ import os
+ import json
+
+ data_json = {
+ "columns": {
+ "chinese": {
+ "type": "uint8",
+ "rank": 1
+ },
+ "line" : {
+ "type": "int8",
+ "rank": 1
+ },
+ "words" : {
+ "type": "uint8",
+ "rank": 0
+ }
+ }
+ }
+
+ if not os.path.exists("dataset_schema_path"):
+ os.mkdir("dataset_schema_path")
SCHEMA_DIR = "dataset_schema_path/schema.json"
+ with open(SCHEMA_DIR, "w") as f:
+ json.dump(data_json,f,indent=4)
+
tfrecord_dataset = ds.TFRecordDataset(DATA_FILE, schema=SCHEMA_DIR)
+
+ for tf_data in tfrecord_dataset.create_dict_iterator():
+ print(tf_data.values())
```
- - Create a schema object.
+ ```text
+ dict_values([Tensor(shape=[57], dtype=UInt8, value= [230, 177, 159, 229, 183, 158, 229, 184, 130, 233, 149, 191, 230, 177, 159, 229, 164, 167, 230, 161, 165, 229, 143, 130,
+ 229, 138, 160, 228, 186, 134, 233, 149, 191, 230, 177, 159, 229, 164, 167, 230, 161, 165, 231, 154, 132, 233, 128, 154,
+ 232, 189, 166, 228, 187, 170, 229, 188, 143]), Tensor(shape=[22], dtype=Int8, value= [ 71, 111, 111, 100, 32, 108, 117, 99, 107, 32, 116, 111, 32, 101, 118, 101, 114, 121, 111, 110, 101, 46]), Tensor(shape=[32], dtype=UInt8, value= [229, 165, 179, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 101, 118, 101, 114, 121, 111, 110, 101,
+ 99, 32, 32, 32, 32, 32, 32, 32])])
+ dict_values([Tensor(shape=[12], dtype=UInt8, value= [231, 148, 183, 233, 187, 152, 229, 165, 179, 230, 179, 170]), Tensor(shape=[19], dtype=Int8, value= [ 66, 101, 32, 104, 97, 112, 112, 121, 32, 101, 118, 101, 114, 121, 32, 100, 97, 121, 46]), Tensor(shape=[20], dtype=UInt8, value= [ 66, 101, 32, 32, 32, 104, 97, 112, 112, 121, 100, 97, 121, 32, 32, 98, 32, 32, 32, 32])])
+ dict_values([Tensor(shape=[48], dtype=UInt8, value= [228, 187, 138, 229, 164, 169, 229, 164, 169, 230, 176, 148, 229, 164, 170, 229, 165, 189, 228, 186, 134, 230, 136, 145,
+ 228, 187, 172, 228, 184, 128, 232, 181, 183, 229, 142, 187, 229, 164, 150, 233, 157, 162, 231, 142, 169, 229, 144, 167
+ ]), Tensor(shape=[20], dtype=Int8, value= [ 84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 120, 116, 32, 102, 105, 108, 101, 46]), Tensor(shape=[16], dtype=UInt8, value= [ 84, 104, 105, 115, 116, 101, 120, 116, 102, 105, 108, 101, 97, 32, 32, 32])])
+ ```
- Create a schema object, add user-defined fields to the schema object, and pass the schema object when creating a dataset object.
+3. Create a schema object.
- ```python
- from mindspore import dtype as mstype
- schema = ds.Schema()
- schema.add_column('image', de_type=mstype.uint8)
- schema.add_column('label', de_type=mstype.int32)
- tfrecord_dataset = ds.TFRecordDataset(DATA_FILE, schema=schema)
- ```
+ Create a schema object, add user-defined fields to the schema object, and pass the schema object when creating a dataset object.
+
+ ```python
+ from mindspore import dtype as mstype
+ schema = ds.Schema()
+ schema.add_column('chinese', de_type=mstype.uint8)
+ schema.add_column('line', de_type=mstype.uint8)
+ tfrecord_dataset = ds.TFRecordDataset(DATA_FILE, schema=schema)
+
+ for tf_data in tfrecord_dataset.create_dict_iterator():
+ print(tf_data)
+ ```
+
+ ```text
+ {'chinese': Tensor(shape=[12], dtype=UInt8, value= [231, 148, 183, 233, 187, 152, 229, 165, 179, 230, 179, 170]), 'line': Tensor(shape=[19], dtype=UInt8, value= [ 66, 101, 32, 104, 97, 112, 112, 121, 32, 101, 118, 101, 114, 121, 32, 100, 97, 121, 46])}
+ {'chinese': Tensor(shape=[48], dtype=UInt8, value= [228, 187, 138, 229, 164, 169, 229, 164, 169, 230, 176, 148, 229, 164, 170, 229, 165, 189, 228, 186, 134, 230, 136, 145,
+ 228, 187, 172, 228, 184, 128, 232, 181, 183, 229, 142, 187, 229, 164, 150, 233, 157, 162, 231, 142, 169, 229, 144, 167
+ ]), 'line': Tensor(shape=[20], dtype=UInt8, value= [ 84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 120, 116, 32, 102, 105, 108, 101, 46])}
+ {'chinese': Tensor(shape=[57], dtype=UInt8, value= [230, 177, 159, 229, 183, 158, 229, 184, 130, 233, 149, 191, 230, 177, 159, 229, 164, 167, 230, 161, 165, 229, 143, 130,
+ 229, 138, 160, 228, 186, 134, 233, 149, 191, 230, 177, 159, 229, 164, 167, 230, 161, 165, 231, 154, 132, 233, 128, 154,
+ 232, 189, 166, 228, 187, 170, 229, 188, 143]), 'line': Tensor(shape=[22], dtype=UInt8, value= [ 71, 111, 111, 100, 32, 108, 117, 99, 107, 32, 116, 111, 32, 101, 118, 101, 114, 121, 111, 110, 101, 46])}
+ ```
+
+Comparing step 2 and step 3 above, we can see:
+
+|step|chinese|line|words
+|:---|:---|:---|:---
+| 2|UInt8 |Int8|UInt8
+| 3|UInt8 |UInt8|
+
+The data in the columns in the example step 2 has changed from chinese (UInt8), line (Int8) and words (UInt8) to the chinese (UInt8) and line (UInt8) in the example step 3. Through the Schema object, set the data type and characteristics of the dataset, so that the data type and characteristics in the column are changed accordingly.
### NumPy
@@ -362,8 +490,8 @@ The following examples describe how to use `NumpySlicesDataset` to load array, l
dataset = ds.NumpySlicesDataset(data1, column_names=["col1", "col2"], shuffle=False)
- for data in dataset.create_dict_iterator():
- print(data)
+ for np_dic_data in dataset.create_dict_iterator():
+ print(np_dic_data)
```
The output is as follows:
@@ -377,16 +505,39 @@ The following examples describe how to use `NumpySlicesDataset` to load array, l
The following example uses `CSVDataset` to load CSV dataset files, and displays labels of the loaded data.
+Download the test data `test_csv.zip` and unzip it to the specified location, execute the following command:
+
+```bash
+!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/test_csv.zip
+!unzip -o ./test_csv.zip -d ./datasets/mindspore_dataset_loading/test_csv/
+!tree ./datasets/mindspore_dataset_loading/test_csv/
+```
+
+```text
+./datasets/mindspore_dataset_loading/test_csv/
+├── test1.csv
+└── test2.csv
+
+0 directories, 2 files
+```
+
The method of loading a text dataset file is similar to that of loading a CSV file.
```python
import mindspore.dataset as ds
-DATA_FILE = ["csv_file_0", "csv_file_1", "csv_file_2"]
+DATA_FILE = ["./datasets/mindspore_dataset_loading/test_csv/test1.csv","./datasets/mindspore_dataset_loading/test_csv/test2.csv"]
csv_dataset = ds.CSVDataset(DATA_FILE)
-for data in csv_dataset.create_dict_iterator(output_numpy=True):
- print(data["1"])
+for csv_data in csv_dataset.create_dict_iterator(output_numpy=True):
+ print(csv_data.keys())
+```
+
+```text
+dict_keys(['a', 'b', 'c', 'd'])
+dict_keys(['a', 'b', 'c', 'd'])
+dict_keys(['a', 'b', 'c', 'd'])
+dict_keys(['a', 'b', 'c', 'd'])
```
## Loading User-defined Dataset
@@ -411,8 +562,8 @@ def GeneratorFunc():
dataset = ds.GeneratorDataset(GeneratorFunc, ["data", "label"])
-for sample in dataset.create_dict_iterator():
- print(sample["data"], sample["label"])
+for item in dataset.create_dict_iterator():
+ print(item["data"], item["label"])
```
The output is as follows:
diff --git a/docs/programming_guide/source_en/dtype.md b/docs/programming_guide/source_en/dtype.md
index 29437cc5de0d6dfbf696f4548167d62f04f3d682..d7a3ff75cbd0cd175ab51524d8248772c0df5975 100644
--- a/docs/programming_guide/source_en/dtype.md
+++ b/docs/programming_guide/source_en/dtype.md
@@ -8,7 +8,7 @@
-
+
## Overview
@@ -16,7 +16,7 @@ MindSpore tensors support different data types, including `int8`, `int16`, `int3
In the computation process of MindSpore, the `int` data type in Python is converted into the defined `int64` type, and the `float` data type is converted into the defined `float32` type.
-For details about the supported types, see .
+For details about the supported types, see .
In the following code, the data type of MindSpore is int32.
diff --git a/docs/programming_guide/source_en/infer.md b/docs/programming_guide/source_en/infer.md
index 8687404e37573338750728c54ffebe15d5bc87fe..52f4edbb59f5a7de4b8d59ca0473400647a1f25a 100644
--- a/docs/programming_guide/source_en/infer.md
+++ b/docs/programming_guide/source_en/infer.md
@@ -6,19 +6,19 @@
-
+
Based on the model trained by MindSpore, it supports the execution of inferences on various platforms such as Ascend 910 AI processor, Ascend 310 AI processor, GPU, CPU, and device side. For more details, please refer to the following tutorials:
-- [Inference on the Ascend 910 AI processor](https://www.mindspore.cn/tutorial/inference/en/master/multi_platform_inference_ascend_910.html)
-- [Inference on the Ascend 310 AI processor](https://www.mindspore.cn/tutorial/inference/en/master/multi_platform_inference_ascend_310.html)
-- [Inference on a GPU](https://www.mindspore.cn/tutorial/inference/en/master/multi_platform_inference_gpu.html)
-- [Inference on a CPU](https://www.mindspore.cn/tutorial/inference/en/master/multi_platform_inference_cpu.html)
-- [Inference on the device side](https://www.mindspore.cn/tutorial/lite/en/master/quick_start/quick_start.html)
+- [Inference on the Ascend 910 AI processor](https://www.mindspore.cn/tutorial/inference/en/r1.2/multi_platform_inference_ascend_910.html)
+- [Inference on the Ascend 310 AI processor](https://www.mindspore.cn/tutorial/inference/en/r1.2/multi_platform_inference_ascend_310.html)
+- [Inference on a GPU](https://www.mindspore.cn/tutorial/inference/en/r1.2/multi_platform_inference_gpu.html)
+- [Inference on a CPU](https://www.mindspore.cn/tutorial/inference/en/r1.2/multi_platform_inference_cpu.html)
+- [Inference on the device side](https://www.mindspore.cn/tutorial/lite/en/r1.2/quick_start/quick_start.html)
At the same time, MindSpore offers a lightweight and high-performance module called "MindSpore Serving", which helps MindSpore developers effectively deploy online inferences in a production environment. For more details, please refer to the following tutorials:
-- [MindSpore Serving-based Inference Service Deployment](https://www.mindspore.cn/tutorial/inference/en/master/serving_example.html)
-- [gRPC-based MindSpore Serving Access](https://www.mindspore.cn/tutorial/inference/en/master/serving_grpc.html)
-- [RESTful-based MindSpore Serving Access](https://www.mindspore.cn/tutorial/inference/en/master/serving_restful.html)
-- [Servable Provided Through Model Configuration](https://www.mindspore.cn/tutorial/inference/en/master/serving_model.html)
+- [MindSpore Serving-based Inference Service Deployment](https://www.mindspore.cn/tutorial/inference/en/r1.2/serving_example.html)
+- [gRPC-based MindSpore Serving Access](https://www.mindspore.cn/tutorial/inference/en/r1.2/serving_grpc.html)
+- [RESTful-based MindSpore Serving Access](https://www.mindspore.cn/tutorial/inference/en/r1.2/serving_restful.html)
+- [Servable Provided Through Model Configuration](https://www.mindspore.cn/tutorial/inference/en/r1.2/serving_model.html)
diff --git a/docs/programming_guide/source_en/network_component.md b/docs/programming_guide/source_en/network_component.md
index 13be4e5b3c07c9db9daf1637c3bb2fe76f5fcddb..7d0b70eb96235a871e5d955a821e9cf04332251b 100644
--- a/docs/programming_guide/source_en/network_component.md
+++ b/docs/programming_guide/source_en/network_component.md
@@ -10,7 +10,7 @@
-
+
## Overview
@@ -22,7 +22,7 @@ The following describes three network components, `GradOperation`, `WithLossCell
## GradOperation
-GradOperation is used to generate the gradient of the input function. The `get_all`, `get_by_list`, and `sens_param` parameters are used to control the gradient calculation method. For details, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/ops/mindspore.ops.GradOperation.html)
+GradOperation is used to generate the gradient of the input function. The `get_all`, `get_by_list`, and `sens_param` parameters are used to control the gradient calculation method. For details, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/ops/mindspore.ops.GradOperation.html)
The following is an example of using GradOperation:
```python
@@ -30,15 +30,14 @@ import numpy as np
import mindspore.nn as nn
from mindspore import Tensor, Parameter
from mindspore import dtype as mstype
-from mindspore.ops import composite as C
-from mindspore.ops import operations as P
+import mindspore.ops as ops
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
- self.matmul = P.MatMul()
- self.z = Parameter(Tensor(np.array([1.0], np.float32)))
+ self.matmul = ops.MatMul()
+ self.z = Parameter(Tensor(np.array([1.0], np.float32)), name='z')
def construct(self, x, y):
x = x * self.z
out = self.matmul(x, y)
@@ -48,7 +47,7 @@ class GradNetWrtX(nn.Cell):
def __init__(self, net):
super(GradNetWrtX, self).__init__()
self.net = net
- self.grad_op = C.GradOperation()
+ self.grad_op = ops.GradOperation()
def construct(self, x, y):
gradient_function = self.grad_op(self.net)
return gradient_function(x, y)
@@ -58,15 +57,14 @@ y = Tensor([[0.01, 0.3, 1.1], [0.1, 0.2, 1.3], [2.1, 1.2, 3.3]], dtype=mstype.fl
GradNetWrtX(Net())(x, y)
```
-The preceding example is used to calculate the gradient value of `Net` to x. You need to define the network `Net` as the input of `GradOperation`. The instance creates `GradNetWrtX` that contains the gradient operation. Calling `GradNetWrtX` transfers the network to `GradOperation` to generate a gradient function, and transfers the input data to the gradient function to return the final result.
-
-The following information is displayed:
-
```text
-[[1.4100001 1.5999999 6.6 ]
- [1.4100001 1.5999999 6.6 ]]
+Tensor(shape=[2, 3], dtype=Float32, value=
+[[1.41000009e+000, 1.60000002e+000, 6.59999943e+000],
+ [1.41000009e+000, 1.60000002e+000, 6.59999943e+000]])
```
+The preceding example is used to calculate the gradient value of `Net` to x. You need to define the network `Net` as the input of `GradOperation`. The instance creates `GradNetWrtX` that contains the gradient operation. Calling `GradNetWrtX` transfers the network to `GradOperation` to generate a gradient function, and transfers the input data to the gradient function to return the final result.
+
All other components, such as `WithGradCell` and `TrainOneStepCell`, involved in gradient calculation use `GradOperation`.
You can view the internal implementation of these APIs.
@@ -78,26 +76,27 @@ The following uses an example to describe how to use this function. First, you n
```python
import numpy as np
+
import mindspore.context as context
import mindspore.nn as nn
from mindspore import Tensor
from mindspore.nn import TrainOneStepCell, WithLossCell
from mindspore.nn.optim import Momentum
-from mindspore.ops import operations as P
+import mindspore.ops as ops
-context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
+context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
class LeNet(nn.Cell):
def __init__(self):
super(LeNet, self).__init__()
- self.relu = P.ReLU()
+ self.relu = ops.ReLU()
self.batch_size = 32
self.conv1 = nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=0, has_bias=False, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=0, has_bias=False, pad_mode='valid')
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
- self.reshape = P.Reshape()
+ self.reshape = ops.Reshape()
self.fc1 = nn.Dense(400, 120)
self.fc2 = nn.Dense(120, 84)
self.fc3 = nn.Dense(84, 10)
@@ -162,22 +161,20 @@ for i in range(5):
print(res)
```
-In the case, an optimizer and a `WithLossCell` instance are built, and then a training network is initialized in `TrainOneStepCell`. The case is repeated for five times, that is, the network is trained for five times, and the loss result of each time is output, the result shows that the loss value gradually decreases after each training.
-
-The following information is displayed:
-
```text
+++++++++result:0++++++++++++
2.302585
+++++++++result:1++++++++++++
-2.2935824
+2.2935712
+++++++++result:2++++++++++++
-2.2765071
+2.2764661
+++++++++result:3++++++++++++
-2.2522228
+2.2521412
+++++++++result:4++++++++++++
-2.2215357
+2.2214084
```
+In the case, an optimizer and a `WithLossCell` instance are built, and then a training network is initialized in `TrainOneStepCell`. The case is repeated for five times, that is, the network is trained for five times, and the loss result of each time is output, the result shows that the loss value gradually decreases after each training.
+
The following content will describe how MindSpore uses more advanced encapsulation APIs, that is, the `train` method in the `Model` class to train a model. Many network components, such as `TrainOneStepCell` and `WithLossCell`, will be used in the internal implementation.
You can view the internal implementation of these components.
diff --git a/docs/programming_guide/source_en/network_list.rst b/docs/programming_guide/source_en/network_list.rst
index 5118f160b0b99ba8edf4e7cc9f3aba11a24a15a9..cac0e51504647e04e0cc16e8e4ee85342ff2debb 100644
--- a/docs/programming_guide/source_en/network_list.rst
+++ b/docs/programming_guide/source_en/network_list.rst
@@ -4,4 +4,4 @@ Network List
.. toctree::
:maxdepth: 1
- MindSpore Network List
\ No newline at end of file
+ MindSpore Network List
\ No newline at end of file
diff --git a/docs/programming_guide/source_en/numpy.md b/docs/programming_guide/source_en/numpy.md
index dfeed88c32e76064085c1f1ee9cfece3e9e2d9d4..a44ecdc8e97a267c8667970c8d5a0af5f5f5d2e3 100644
--- a/docs/programming_guide/source_en/numpy.md
+++ b/docs/programming_guide/source_en/numpy.md
@@ -27,7 +27,7 @@
-
+
## Overview
@@ -35,7 +35,7 @@ MindSpore Numpy package contains a set of Numpy-like interfaces, which allows de
## Operator Functions
-Mindspore Numpy operators can be classified into four functional modules: `array generation`, `array operation`, `logic operation` and `math operation`. For details about the supported operators on the Ascend AI processors, GPU, and CPU, see [Numpy Interface List](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.numpy.html).
+Mindspore Numpy operators can be classified into four functional modules: `array generation`, `array operation`, `logic operation` and `math operation`. For details about the supported operators on the Ascend AI processors, GPU, and CPU, see [Numpy Interface List](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.numpy.html).
### Array Generations
@@ -362,7 +362,7 @@ from mindspore import ms_function
forward_compiled = ms_function(forward)
```
-> Currently, static graph cannot run in command line mode and not all python types can be passed into functions decorated with `ms_function`. For details about the static graph syntax support, see [Syntax Support](https://www.mindspore.cn/doc/note/en/master/static_graph_syntax_support.html). For details about how to use `ms_function`, see [API: ms_function](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.html?highlight=ms_function#mindspore.ms_function).
+> Currently, static graph cannot run in command line mode and not all python types can be passed into functions decorated with `ms_function`. For details about the static graph syntax support, see [Syntax Support](https://www.mindspore.cn/doc/note/en/r1.2/static_graph_syntax_support.html). For details about how to use `ms_function`, see [API: ms_function](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.html#mindspore.ms_function).
### Use GradOperation to compute deratives
@@ -386,7 +386,7 @@ grad_all = ops.composite.GradOperation(get_all=True)
grad_all(ms_function(forward))(x, w1, b1, w2, b2, w3, b3)
```
- For more details, see [API: GradOperation](https://www.mindspore.cn/doc/api_python/en/master/mindspore/ops/mindspore.ops.GradOperation.html).
+ For more details, see [API: GradOperation](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/ops/mindspore.ops.GradOperation.html).
### Use mindspore.context to control execution mode
@@ -413,7 +413,7 @@ context.set_context(device_target="Ascend")
...
```
- For more details, see [API: mindspore.context](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.context.html).
+ For more details, see [API: mindspore.context](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.context.html).
### Use mindspore.numpy in MindSpore Deep Learning Models
@@ -455,4 +455,4 @@ The output is:
[2816. 2816. 2816. 2816.]]
```
-For more details on building Neural Network with MindSpore, see [MindSpore Training Guide](https://www.mindspore.cn/tutorial/training/en/master/index.html).
+For more details on building Neural Network with MindSpore, see [MindSpore Training Guide](https://www.mindspore.cn/tutorial/training/en/r1.2/index.html).
diff --git a/docs/programming_guide/source_en/operator_list.rst b/docs/programming_guide/source_en/operator_list.rst
index 32c5db52166ff05588edd965e94d4969243dcd6d..d9e98029c97a4731709e3da820fffc91028b418a 100644
--- a/docs/programming_guide/source_en/operator_list.rst
+++ b/docs/programming_guide/source_en/operator_list.rst
@@ -4,6 +4,6 @@ Operator List
.. toctree::
:maxdepth: 1
- MindSpore Operator List
- MindSpore Implicit Type Conversion
- MindSpore Distributed Operator List
\ No newline at end of file
+ MindSpore Operator List
+ MindSpore Implicit Type Conversion
+ MindSpore Distributed Operator List
\ No newline at end of file
diff --git a/docs/programming_guide/source_en/operators.md b/docs/programming_guide/source_en/operators.md
index 948e7025e9e22543c94ecc6b3f4fe46259359e5f..db66a078098979b63d5d308b793099f2c6bf63f0 100644
--- a/docs/programming_guide/source_en/operators.md
+++ b/docs/programming_guide/source_en/operators.md
@@ -40,7 +40,7 @@
-
+
## Overview
@@ -56,7 +56,7 @@ APIs related to operators include operations, functional, and composite. Operato
### mindspore.ops.operations
-The operations API provides all primitive operator APIs, which are the lowest-order operator APIs open to users. For details about the supported operators, see [Operator List](https://www.mindspore.cn/doc/note/en/master/operator_list.html).
+The operations API provides all primitive operator APIs, which are the lowest-order operator APIs open to users. For details about the supported operators, see [Operator List](https://www.mindspore.cn/doc/note/en/r1.2/operator_list.html).
Primitive operators directly encapsulate the implementation of operators at bottom layers such as Ascend, GPU, AICPU, and CPU, providing basic operator capabilities for users.
@@ -85,7 +85,7 @@ output = [ 1. 8. 64.]
### mindspore.ops.functional
-To simplify the calling process of operators without attributes, MindSpore provides the functional version of some operators. For details about the input parameter requirements, see the input and output requirements of the original operator. For details about the supported operators, see [Operator List](https://www.mindspore.cn/doc/note/en/master/operator_list_ms.html#mindspore-ops-functional).
+To simplify the calling process of operators without attributes, MindSpore provides the functional version of some operators. For details about the input parameter requirements, see the input and output requirements of the original operator. For details about the supported operators, see [Operator List](https://www.mindspore.cn/doc/note/en/r1.2/operator_list_ms.html#mindspore-ops-functional).
For example, the functional version of the `P.Pow` operator is `F.tensor_pow`.
@@ -168,7 +168,7 @@ tensor [[2.4 4.2]
scalar 3
```
-In addition, the high-order function `GradOperation` provides the method of computing the gradient function corresponding to the input function. For details, see [mindspore.ops](https://www.mindspore.cn/doc/api_python/en/master/mindspore/ops/mindspore.ops.GradOperation.html).
+In addition, the high-order function `GradOperation` provides the method of computing the gradient function corresponding to the input function. For details, see [mindspore.ops](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/ops/mindspore.ops.GradOperation.html).
### Combination usage of operations/functional/composite three types of operators
@@ -190,7 +190,7 @@ pow = ops.Pow()
## Operator Functions
-Operators can be classified into seven functional modules: tensor operations, network operations, array operations, image operations, encoding operations, debugging operations, and quantization operations. For details about the supported operators on the Ascend AI processors, GPU, and CPU, see [Operator List](https://www.mindspore.cn/doc/note/en/master/operator_list.html).
+Operators can be classified into seven functional modules: tensor operations, network operations, array operations, image operations, encoding operations, debugging operations, and quantization operations. For details about the supported operators on the Ascend AI processors, GPU, and CPU, see [Operator List](https://www.mindspore.cn/doc/note/en/r1.2/operator_list.html).
### Tensor Operations
@@ -265,7 +265,7 @@ print(res)
The following information is displayed:
```text
-[4. 10. 18]
+[4. 10. 18.]
```
#### Trigonometric Function
@@ -403,6 +403,16 @@ The following information is displayed:
```text
[[[[288. 288. 288. ... 288. 288. 288.]
+ [288. 288. 288. ... 288. 288. 288.]
+ [288. 288. 288. ... 288. 288. 288.]
+ ...
+ [288. 288. 288. ... 288. 288. 288.]
+ [288. 288. 288. ... 288. 288. 288.]
+ [288. 288. 288. ... 288. 288. 288.]]]
+
+ ...
+
+ [[288. 288. 288. ... 288. 288. 288.]
[288. 288. 288. ... 288. 288. 288.]
[288. 288. 288. ... 288. 288. 288.]
...
@@ -410,6 +420,13 @@ The following information is displayed:
[288. 288. 288. ... 288. 288. 288.]
[288. 288. 288. ... 288. 288. 288.]]
+
+ ...
+
+
+ [[288. 288. 288. ... 288. 288. 288.]
+ [288. 288. 288. ... 288. 288. 288.]
+ [288. 288. 288. ... 288. 288. 288.]
...
[288. 288. 288. ... 288. 288. 288.]
[288. 288. 288. ... 288. 288. 288.]
@@ -438,21 +455,23 @@ print(res)
The following information is displayed:
```text
-[[[[ 32. 64. 96. ... 96. 64. 32.]
- [ 64. 128. 192. ... 192. 128. 64.]
- [ 96. 192. 288. ... 288. 192. 96.]
+[[[[ 32. 64. 96. ... 96. 64. 32.]
+ [ 64. 128. 192. ... 192. 128. 64.]
+ [ 96. 192. 288. ... 288. 192. 96.]
...
- [ 96. 192. 288. ... 288. 192. 96.]
- [ 64. 128. 192. ... 192. 128. 64.]
- [ 32. 64. 96. ... 96. 64. 32.]]
+ [ 96. 192. 288. ... 288. 192. 96.]
+ [ 64. 128. 192. ... 192. 128. 64.]
+ [ 32. 64. 96. ... 96. 64. 32.]]
+
+ ...
- [[ 32. 64. 96. ... 96. 64. 32.]
- [ 64. 128. 192. ... 192. 128. 64.]
- [ 96. 192. 288. ... 288. 192. 96.]
+ [[ 32. 64. 96. ... 96. 64. 32.]
+ [ 64. 128. 192. ... 192. 128. 64.]
+ [ 96. 192. 288. ... 288. 192. 96.]
...
- [ 96. 192. 288. ... 288. 192. 96.]
- [ 64. 128. 192. ... 192. 128. 64.]
- [ 32. 64. 96. ... 96. 64. 32.]]]]
+ [ 96. 192. 288. ... 288. 192. 96.]
+ [ 64. 128. 192. ... 192. 128. 64.]
+ [ 32. 64. 96. ... 96. 64. 32.]]]]
```
#### Activation Function
@@ -475,7 +494,7 @@ print(res)
The following information is displayed:
```text
-[0.01165623 0.03168492 0.08612854 0.23412167 0.6364086]
+[0.01165623 0.03168492 0.08612853 0.23412164 0.63640857]
```
#### Loss Function
@@ -505,8 +524,6 @@ print(res)
#### Optimization Algorithm
- SGD
-
The following code implements the stochastic gradient descent (SGD) algorithm. The output is stored in result.
```python
@@ -530,7 +547,7 @@ print(result)
The following information is displayed:
```text
-(Tensor(shape=[4], dtype=Float32, value= [ 1.98989999e+00, -4.90300000e-01, 1.69520009e+00, 3.98009992e+00]),)
+(Tensor(shape=[4], dtype=Float32, value= [ 1.99000001e+00, -4.90300000e-01, 1.69500005e+00, 3.98009992e+00]),)
```
### Array Operations
@@ -678,8 +695,8 @@ from mindspore import Tensor
import mindspore.ops as ops
import mindspore
-anchor_box = Tensor([[2, 2, 2, 3], [2, 2, 2, 3]],mindspore.float32)
-groundtruth_box = Tensor([[1, 2, 1, 4], [1, 2, 1, 4]],mindspore.float32)
+anchor_box = Tensor([[2,2,2,3],[2,2,2,3]],mindspore.float32)
+groundtruth_box = Tensor([[1,2,1,4],[1,2,1,4]],mindspore.float32)
boundingbox_encode = ops.BoundingBoxEncode(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0))
res = boundingbox_encode(anchor_box, groundtruth_box)
print(res)
@@ -688,8 +705,8 @@ print(res)
The following information is displayed:
```text
-[[ -1. 0.25 0. 0.40551758]
- [ -1. 0.25 0. 0.40551758]]
+[[-1. 0.25 0. 0.40546513]
+ [-1. 0.25 0. 0.40546513]]
```
#### BoundingBoxDecode
@@ -713,15 +730,15 @@ print(res)
The following information is displayed:
```text
-[[4.1953125 0. 0. 5.1953125]
- [2.140625 0. 3.859375 60.59375]]
+[[ 4.194528 0. 0. 5.194528 ]
+ [ 2.1408591 0. 3.8591409 60.59815 ]]
```
#### IOU Computing
Computes the proportion of the intersection area and union area of the box where the predicted object is located and the box where the real object is located. It is often used as a loss function to optimize the model.
- The following code implements the IOU computing between anchor_boxes and gt_boxes. The output is stored in out:
+The following code implements the IOU computing between `anchor_boxes` and `gt_boxes`. The output is stored in out:
```python
from mindspore import Tensor
@@ -739,9 +756,9 @@ print(out)
The following information is displayed:
```text
-[[0. -0. 0.]
- [0. -0. 0.]
- [0. 0. 0.]]
+[[ 0. -0. 0.]
+ [ 0. -0. 0.]
+ [ 0. 0. 0.]]
```
### Debugging Operations
@@ -752,7 +769,7 @@ The debugging operations refer to some common operators and operations used to d
Displays the gradient of intermediate variables. It is a common operator. Currently, only the PyNative mode is supported.
- The following code implements the function of printing the gradient of the intermediate variable (x,y in this example):
+The following code implements the function of printing the gradient of the intermediate variable (x,y in this example):
```python
from mindspore import Tensor
@@ -775,11 +792,12 @@ def hook_test(x, y):
def backward(x, y):
return grad_all(hook_test)(Tensor(x, mstype.float32), Tensor(y, mstype.float32))
-backward(1, 2)
+print(backward(1, 2))
```
The following information is displayed:
```text
-(Tensor(shape=[], dtype=Float32, value=2),)
+(Tensor(shape=[], dtype=Float32, value= 2),)
+(Tensor(shape=[], dtype=Float32, value= 4), Tensor(shape=[], dtype=Float32, value= 4))
```
diff --git a/docs/programming_guide/source_en/optim.md b/docs/programming_guide/source_en/optim.md
index 7a697d2df242ef9cb06b369230f312f81b29426a..674cbd07cd1d3b035b73b254e58e3f519c59c8e4 100644
--- a/docs/programming_guide/source_en/optim.md
+++ b/docs/programming_guide/source_en/optim.md
@@ -13,7 +13,7 @@
-
+
## Overview
diff --git a/docs/programming_guide/source_en/parameter.md b/docs/programming_guide/source_en/parameter.md
index 3041bdededd2bc613268a74fa7acb5a3494a5410..1453576d0bd5a129287bc6e2e943ac28155c2eeb 100644
--- a/docs/programming_guide/source_en/parameter.md
+++ b/docs/programming_guide/source_en/parameter.md
@@ -11,7 +11,7 @@
-
+
## Overview
@@ -35,7 +35,7 @@ To update a parameter, set `requires_grad` to `True`.
When `layerwise_parallel` is set to True, this parameter will be filtered out during parameter broadcast and parameter gradient aggregation.
-For details about the configuration of distributed parallelism, see .
+For details about the configuration of distributed parallelism, see .
In the following example, `Parameter` objects are built using three different data types. All the three `Parameter` objects need to be updated, and layerwise parallelism is not used.
@@ -55,11 +55,11 @@ print(x, "\n\n", y, "\n\n", z)
The following information is displayed:
```text
-Parameter (name=x, shape=(2, 3), dtype=Int64, requires_grad=True)
+Parameter (name=x)
-Parameter (name=y, shape=(1, 2, 3), dtype=Float32, requires_grad=True)
+Parameter (name=y)
-Parameter (name=z, shape=(), dtype=Float32, requires_grad=True)
+Parameter (name=z)
```
## Attributes
@@ -119,7 +119,7 @@ data: Parameter (name=Parameter, shape=(2, 3), dtype=Int64, requires_grad=True)
- `set_data`: sets the data saved by `Parameter`. `Tensor`, `Initializer`, `int`, and `float` can be input for setting.
When the input parameter `slice_shape` of the method is set to True, the shape of `Parameter` can be changed. Otherwise, the configured shape must be the same as the original shape of `Parameter`.
-- `set_param_ps`: controls whether training parameters are trained by using the [Parameter Server](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/apply_parameter_server_training.html).
+- `set_param_ps`: controls whether training parameters are trained by using the [Parameter Server](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/apply_parameter_server_training.html).
- `clone`: clones `Parameter`. You can specify the parameter name after cloning.
@@ -177,7 +177,7 @@ print(params_copy)
The following information is displayed:
```text
-(Parameter (name=x, shape=(2, 3), dtype=Int64, requires_grad=True), Parameter (name=y, shape=(1, 2, 3), dtype=Float32, requires_grad=True), Parameter (name=z, shape=(), dtype=Float32, requires_grad=True))
+(Parameter (name=x), Parameter (name=y), Parameter (name=z))
-(Parameter (name=params_copy.x, shape=(2, 3), dtype=Int64, requires_grad=True), Parameter (name=params_copy.y, shape=(1, 2, 3), dtype=Float32, requires_grad=True), Parameter (name=params_copy.z, shape=(), dtype=Float32, requires_grad=True))
+(Parameter (name=params_copy.x), Parameter (name=params_copy.y), Parameter (name=params_copy.z))
```
diff --git a/docs/programming_guide/source_en/performance_optimization.md b/docs/programming_guide/source_en/performance_optimization.md
index b946226a2785368f3911646876b456c7ef2f5819..11f8208c89d1c647dbe168ee5c263af7111645b0 100644
--- a/docs/programming_guide/source_en/performance_optimization.md
+++ b/docs/programming_guide/source_en/performance_optimization.md
@@ -6,14 +6,14 @@
-
+
MindSpore provides a variety of performance optimization methods, users can use them to improve the performance of training and inference according to the actual situation.
| Optimization Stage | Optimization Method | Supported |
| --- | --- | --- |
-| Training | [Distributed Training](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/distributed_training_tutorials.html) | Ascend, GPU |
-| | [Mixed Precision](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/enable_mixed_precision.html) | Ascend, GPU |
-| | [Graph Kernel Fusion](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/enable_graph_kernel_fusion.html) | Ascend, GPU |
-| | [Gradient Accumulation](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/apply_gradient_accumulation.html) | GPU |
-| Inference | [Quantization After Training](https://www.mindspore.cn/tutorial/lite/en/master/use/post_training_quantization.html) | Lite |
+| Training | [Distributed Training](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/distributed_training_tutorials.html) | Ascend, GPU |
+| | [Mixed Precision](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/enable_mixed_precision.html) | Ascend, GPU |
+| | [Graph Kernel Fusion](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/enable_graph_kernel_fusion.html) | Ascend, GPU |
+| | [Gradient Accumulation](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/apply_gradient_accumulation.html) | GPU |
+| Inference | [Quantization After Training](https://www.mindspore.cn/tutorial/lite/en/r1.2/use/post_training_quantization.html) | Lite |
diff --git a/docs/programming_guide/source_en/pipeline.md b/docs/programming_guide/source_en/pipeline.md
index 178ad149f7cebe5a9389f84ee332a6d2f1e0c411..41c9c02452b96e2722493594b090f3c7928cb66f 100644
--- a/docs/programming_guide/source_en/pipeline.md
+++ b/docs/programming_guide/source_en/pipeline.md
@@ -14,7 +14,7 @@
-
+
## Overview
@@ -22,7 +22,7 @@ Data is the basis of deep learning. Good data input can play a positive role in
Each dataset class of MindSpore provides multiple data processing operators. You can build a data processing pipeline to define the data processing operations to be used. In this way, data can be continuously transferred to the training system through the data processing pipeline during the training process.
-The following table lists part of the common data processing operators supported by MindSpore. For more data processing operations, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.dataset.html).
+The following table lists part of the common data processing operators supported by MindSpore. For more data processing operations, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.dataset.html).
| Data Processing Operator | Description |
| ---- | ---- |
@@ -65,18 +65,18 @@ for data in dataset1.create_dict_iterator():
The output is as follows:
```text
-{'data': Tensor(shape=[3], dtype=Int64, value=[0, 1, 2])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[2, 3, 4])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[3, 4, 5])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[1, 2, 3])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[4, 5, 6])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [0, 1, 2])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [2, 3, 4])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [3, 4, 5])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [1, 2, 3])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [4, 5, 6])}
```
### map
-Applies a specified function or operator to specified columns in a dataset to implement data mapping. You can customize the mapping function or use operators in c_transforms or py_transforms to augment image and text data.
+Applies a specified function or operator to specified columns in a dataset to implement data mapping. You can customize the mapping function or use operators in `c_transforms` or `py_transforms` to augment image and text data.
-> For details about how to use data augmentation, see [Data Augmentation](https://www.mindspore.cn/doc/programming_guide/en/master/augmentation.html) in the Programming Guide.
+> For details about how to use data augmentation, see [Data Augmentation](https://www.mindspore.cn/doc/programming_guide/en/r1.2/augmentation.html) in the Programming Guide.

@@ -109,17 +109,17 @@ for data in dataset.create_dict_iterator():
The output is as follows:
```text
-{'data': Tensor(shape=[3], dtype=Int64, value=[0, 1, 2])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[1, 2, 3])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[2, 3, 4])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[3, 4, 5])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[4, 5, 6])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [0, 1, 2])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [1, 2, 3])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [2, 3, 4])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [3, 4, 5])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [4, 5, 6])}
------ after processing ------
-{'data': Tensor(shape=[3], dtype=Int64, value=[0, 2, 4])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[2, 4, 6])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[4, 6, 8])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[ 6, 8, 10])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[ 8, 10, 12])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [0, 2, 4])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [2, 4, 6])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [4, 6, 8])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [ 6, 8, 10])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [ 8, 10, 12])}
```
### batch
@@ -156,12 +156,21 @@ for data in dataset2.create_dict_iterator():
The output is as follows:
```text
-{'data': Tensor(shape=[2, 3], dtype=Int64, value=[[0, 1, 2], [1, 2, 3]])}
-{'data': Tensor(shape=[2, 3], dtype=Int64, value=[[2, 3, 4], [3, 4, 5]])}
-{'data': Tensor(shape=[1, 3], dtype=Int64, value=[[4, 5, 6]])}
+{'data': Tensor(shape=[2, 3], dtype=Int64, value=
+[[0, 1, 2],
+ [1, 2, 3]])}
+{'data': Tensor(shape=[2, 3], dtype=Int64, value=
+[[2, 3, 4],
+ [3, 4, 5]])}
+{'data': Tensor(shape=[1, 3], dtype=Int64, value=
+[[4, 5, 6]])}
------ drop remainder ------
-{'data': Tensor(shape=[2, 3], dtype=Int64, value=[[0, 1, 2], [1, 2, 3]])}
-{'data': Tensor(shape=[2, 3], dtype=Int64, value=[[2, 3, 4], [3, 4, 5]])}
+{'data': Tensor(shape=[2, 3], dtype=Int64, value=
+[[0, 1, 2],
+ [1, 2, 3]])}
+{'data': Tensor(shape=[2, 3], dtype=Int64, value=
+[[2, 3, 4],
+ [3, 4, 5]])}
```
### repeat
@@ -192,16 +201,16 @@ for data in dataset1.create_dict_iterator():
The output is as follows:
```text
-{'data': Tensor(shape=[3], dtype=Int64, value=[0, 1, 2])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[1, 2, 3])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[2, 3, 4])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[3, 4, 5])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[4, 5, 6])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[0, 1, 2])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[1, 2, 3])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[2, 3, 4])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[3, 4, 5])}
-{'data': Tensor(shape=[3], dtype=Int64, value=[4, 5, 6])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [0, 1, 2])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [1, 2, 3])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [2, 3, 4])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [3, 4, 5])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [4, 5, 6])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [0, 1, 2])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [1, 2, 3])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [2, 3, 4])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [3, 4, 5])}
+{'data': Tensor(shape=[3], dtype=Int64, value= [4, 5, 6])}
```
### zip
diff --git a/docs/programming_guide/source_en/probability.md b/docs/programming_guide/source_en/probability.md
index 52e2e474953986dc6396f944184c8ce757421257..df524a4fdcafeb735e4a5634bd3cd8a4ad0c8605 100644
--- a/docs/programming_guide/source_en/probability.md
+++ b/docs/programming_guide/source_en/probability.md
@@ -47,7 +47,7 @@
-
+
MindSpore deep probabilistic programming is to combine Bayesian learning with deep learning, including probability distribution, probability distribution mapping, deep probability network, probability inference algorithm, Bayesian layer, Bayesian conversion, and Bayesian toolkit. For professional Bayesian learning users, it provides probability sampling, inference algorithms, and model build libraries. On the other hand, advanced APIs are provided for users who are unfamiliar with Bayesian deep learning, so that they can use Bayesian models without changing the deep learning programming logic.
@@ -851,7 +851,7 @@ decoder = Decoder()
cvae = ConditionalVAE(encoder, decoder, hidden_size=400, latent_size=20, num_classes=10)
```
-Load a dataset, for example, Mnist. For details about the data loading and preprocessing process, see [Implementing an Image Classification Application](https://www.mindspore.cn/tutorial/training/en/master/quick_start/quick_start.html). The create_dataset function is used to create a data iterator.
+Load a dataset, for example, Mnist. For details about the data loading and preprocessing process, see [Implementing an Image Classification Application](https://www.mindspore.cn/tutorial/training/en/r1.2/quick_start/quick_start.html). The create_dataset function is used to create a data iterator.
```python
ds_train = create_dataset(image_path, 128, 1)
@@ -915,7 +915,7 @@ If you want the generated sample to be better and clearer, you can define a more
The following uses the APIs in `nn.probability.bnn_layers` of MindSpore to implement the BNN image classification model. The APIs in `nn.probability.bnn_layers` of MindSpore include `NormalPrior`, `NormalPosterior`, `ConvReparam`, `DenseReparam`, `DenseLocalReparam` and `WithBNNLossCell`. The biggest difference between BNN and DNN is that the weight and bias of the BNN layer are not fixed values, but follow a distribution. `NormalPrior` and `NormalPosterior` are respectively used to generate a prior distribution and a posterior distribution that follow a normal distribution. `ConvReparam` and `DenseReparam` are the Bayesian convolutional layer and fully connected layers implemented by using the reparameterization method, respectively. `DenseLocalReparam` is the Bayesian fully connected layers implemented by using the local reparameterization method. `WithBNNLossCell` is used to encapsulate the BNN and loss function.
-For details about how to use the APIs in `nn.probability.bnn_layers` to build a Bayesian neural network and classify images, see [Applying the Bayesian Network](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/apply_deep_probability_programming.html#id3).
+For details about how to use the APIs in `nn.probability.bnn_layers` to build a Bayesian neural network and classify images, see [Applying the Bayesian Network](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/apply_deep_probability_programming.html#id3).
## Bayesian Conversion
@@ -971,7 +971,7 @@ The `trainable_bnn` parameter is a trainable DNN model packaged by `TrainOneStep
```
- `get_dense_args` specifies the parameters to be obtained from the fully connected layer of the DNN model. The default value is the common parameters of the fully connected layers of the DNN and BNN models. For details about the parameters, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/nn/mindspore.nn.Dense.html). `get_conv_args` specifies the parameters to be obtained from the convolutional layer of the DNN model. The default value is the common parameters of the convolutional layers of the DNN and BNN models. For details about the parameters, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/nn/mindspore.nn.Conv2d.html). `add_dense_args` and `add_conv_args` specify the new parameter values to be specified for the BNN layer. Note that the parameters in `add_dense_args` cannot be the same as those in `get_dense_args`. The same rule applies to `add_conv_args` and `get_conv_args`.
+ `get_dense_args` specifies the parameters to be obtained from the fully connected layer of the DNN model. The default value is the common parameters of the fully connected layers of the DNN and BNN models. For details about the parameters, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/nn/mindspore.nn.Dense.html). `get_conv_args` specifies the parameters to be obtained from the convolutional layer of the DNN model. The default value is the common parameters of the convolutional layers of the DNN and BNN models. For details about the parameters, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/nn/mindspore.nn.Conv2d.html). `add_dense_args` and `add_conv_args` specify the new parameter values to be specified for the BNN layer. Note that the parameters in `add_dense_args` cannot be the same as those in `get_dense_args`. The same rule applies to `add_conv_args` and `get_conv_args`.
- Function 2: Convert a specific layer.
@@ -997,7 +997,7 @@ The `trainable_bnn` parameter is a trainable DNN model packaged by `TrainOneStep
`Dnn_layer` specifies a DNN layer to be converted into a BNN layer, and `bnn_layer` specifies a BNN layer to be converted into a DNN layer, and `get_args` and `add_args` specify the parameters obtained from the DNN layer and the parameters to be re-assigned to the BNN layer, respectively.
-For details about how to use `TransformToBNN` in MindSpore, see [DNN-to-BNN Conversion with One Click](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/apply_deep_probability_programming.html#dnnbnn).
+For details about how to use `TransformToBNN` in MindSpore, see [DNN-to-BNN Conversion with One Click](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/apply_deep_probability_programming.html#dnnbnn).
## Bayesian Toolbox
@@ -1075,7 +1075,7 @@ The interface of the VAE-based anomaly detection toolbox is as follows:
Use Encoder and Decoder, set hidden_size and latent_size, initialize the class, and then pass the dataset to detect abnormal points.
```python
-from mindspore.nn.probability.toolbox.vae_anomaly_detection import VAEAnomalyDetection
+from mindspore.nn.probability.toolbox import VAEAnomalyDetection
if __name__ == '__main__':
encoder = Encoder()
diff --git a/docs/programming_guide/source_en/run.md b/docs/programming_guide/source_en/run.md
index 33179b18e82a16b79c1b0151cbf07a191a723c05..92b24bbc6a40ab334877018e7bf88f40f8dd381a 100644
--- a/docs/programming_guide/source_en/run.md
+++ b/docs/programming_guide/source_en/run.md
@@ -12,12 +12,14 @@
-
+
## Overview
There are three execution modes: single operator, common function, and network training model.
+> Note: This document is applicable to GPU and Ascend environments.
+
## Executing a Single Operator
Execute a single operator and output the result.
@@ -65,6 +67,8 @@ The output is as follows:
[ 0.01015155 0.00781826 0.00781826 0.00781826 -0.02884173]]]]
```
+> Note: Due to random factors in weight initialization, the actual output results may be different, which is for reference only.
+
## Executing a Common Function
Combine multiple operators into a function, execute these operators by calling the function, and output the result.
@@ -99,11 +103,34 @@ The output is as follows:
## Executing a Network Model
-The [Model API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.html#mindspore.Model) of MindSpore is an advanced API used for training and validation. Layers with the training or inference function can be combined into an object. The training, inference, and prediction functions can be implemented by calling the train, eval, and predict APIs, respectively.
+The [Model API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.html#mindspore.Model) of MindSpore is an advanced API used for training and validation. Layers with the training or inference function can be combined into an object. The training, inference, and prediction functions can be implemented by calling the train, eval, and predict APIs, respectively.
You can transfer the initialized Model APIs such as the network, loss function, and optimizer as required. You can also configure amp_level to implement mixed precision and configure metrics to implement model evaluation.
-> Executing a network model will produce a `kernel_meta` directory in the current directory, and all the cache of operations compiled during the executing process will be stored in it. If user executes the same model again, or a model with some differences, MindSpore will automatically call the reusable cache in `kernel_meta` to reduce the compilation time of the whole model. It has a significant improvement in performance.
+> Executing the network model will generate a `kernel_meta` directory under the execution directory, and save the operator cache files generated by network compilation to this directory during execution, including `.o`, `.info` and `.json` files. If the user executes the same network model again, or only some changes are made, MindSpore will automatically call the reusable operator cache file in the `kernel_meta` directory, which significantly reduces network compilation time and improves execution performance. For details, please refer to [Incremental Operator Build](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/incremental_operator_build.html)
+
+Before executing the network, download and unzip the required dataset to the specified directory:
+
+```bash
+!mkdir -p ./datasets/MNIST_Data/train ./datasets/MNIST_Data/test
+!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-labels-idx1-ubyte
+!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-images-idx3-ubyte
+!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-labels-idx1-ubyte
+!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-images-idx3-ubyte
+!tree ./datasets/MNIST_Data
+```
+
+```text
+./datasets/MNIST_Data
+├── test
+│ ├── t10k-images-idx3-ubyte
+│ └── t10k-labels-idx1-ubyte
+└── train
+ ├── train-images-idx3-ubyte
+ └── train-labels-idx1-ubyte
+
+2 directories, 4 files
+```
### Executing a Training Model
@@ -113,20 +140,15 @@ A code example is as follows:
```python
import os
-
import mindspore.dataset.vision.c_transforms as CV
from mindspore.dataset.vision import Inter
-
import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as CT
-import mindspore.dataset.vision.c_transforms as CV
import mindspore.nn as nn
from mindspore import context, Model
from mindspore import dtype as mstype
from mindspore.common.initializer import Normal
-from mindspore.common.initializer import TruncatedNormal
-from mindspore.dataset.vision import Inter
-from mindspore.train.callback import LossMonitor
+from mindspore.train.callback import LossMonitor, ModelCheckpoint, CheckpointConfig
def create_dataset(data_path, batch_size=32, repeat_size=1,
@@ -166,26 +188,6 @@ def create_dataset(data_path, batch_size=32, repeat_size=1,
return mnist_ds
-def conv(in_channels, out_channels, kernel_size, stride=1, padding=0):
- """weight initial for conv layer"""
- weight = weight_variable()
- return nn.Conv2d(in_channels, out_channels,
- kernel_size=kernel_size, stride=stride, padding=padding,
- weight_init=weight, has_bias=False, pad_mode="valid")
-
-
-def fc_with_initialize(input_channels, out_channels):
- """weight initial for fc layer"""
- weight = weight_variable()
- bias = weight_variable()
- return nn.Dense(input_channels, out_channels, weight, bias)
-
-
-def weight_variable():
- """weight initial"""
- return TruncatedNormal(0.02)
-
-
class LeNet5(nn.Cell):
"""
Lenet network
@@ -224,32 +226,36 @@ class LeNet5(nn.Cell):
if __name__ == "__main__":
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
- ds_train = create_dataset(os.path.join("/home/workspace/mindspore_dataset/MNIST_Data/", "train"), 32)
+
+ model_path = "./models/ckpt/mindspore_run/"
+ os.system("rm -rf {0}*.ckpt {0}*.meta {0}*.pb".format(model_path))
+
+ ds_train_path = "./datasets/MNIST_Data/train/"
+ ds_train = create_dataset(ds_train_path, 32)
network = LeNet5(10)
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
+ config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=5)
+ ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet", directory=model_path, config=config_ck)
model = Model(network, net_loss, net_opt)
print("============== Starting Training ==============")
- model.train(1, ds_train, callbacks=[LossMonitor()], dataset_sink_mode=False)
+ model.train(1, ds_train, callbacks=[LossMonitor(375), ckpoint_cb], dataset_sink_mode=True)
```
-> For details about how to obtain the MNIST dataset used in the example, see [Downloading the Dataset](https://www.mindspore.cn/tutorial/training/en/master/quick_start/quick_start.html#downloading-the-dataset).
-
-The output is as follows:
-
-```python
-epoch: 1 step: 1, loss is 2.300784
-epoch: 1 step: 2, loss is 2.3076947
-epoch: 1 step: 3, loss is 2.2993166
-...
-epoch: 1 step: 1873, loss is 0.13014838
-epoch: 1 step: 1874, loss is 0.0346688
-epoch: 1 step: 1875, loss is 0.017264696
+```text
+============== Starting Training ==============
+epoch: 1 step: 375, loss is 2.2898183
+epoch: 1 step: 750, loss is 2.2777305
+epoch: 1 step: 1125, loss is 0.27802905
+epoch: 1 step: 1500, loss is 0.032973606
+epoch: 1 step: 1875, loss is 0.06105463
```
-> Use the PyNative mode for debugging, including the execution of single operator, common function, and network training model. For details, see [Debugging in PyNative Mode](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/debug_in_pynative_mode.html).
+> For details about how to obtain the MNIST dataset used in the example, see [Downloading the Dataset](https://www.mindspore.cn/tutorial/training/en/r1.2/quick_start/quick_start.html#downloading-the-dataset).
+> Use the PyNative mode for debugging, including the execution of single operator, common function, and network training model. For details, see [Debugging in PyNative Mode](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/debug_in_pynative_mode.html).
+> To use free control loop iterations, traversing data sets, etc., you can refer to the "Customizing a Training Cycle" part of the official website programming guide "[Training](https://www.mindspore.cn/doc/programming_guide/en/r1.2/train.html#customizing-a-training-cycle)".
### Executing an Inference Model
@@ -274,8 +280,6 @@ Formula: $$F_\beta = (1 + \beta^2) \cdot \frac{precisiont \cdot recall}{(\beta^2
A code example is as follows:
```python
-import os
-
import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as CT
import mindspore.dataset.vision.c_transforms as CV
@@ -363,6 +367,8 @@ def create_dataset(data_path, batch_size=32, repeat_size=1,
if __name__ == "__main__":
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
+ model_path = "./models/ckpt/mindspore_run/"
+ ds_eval_path = "./datasets/MNIST_Data/test/"
network = LeNet5(10)
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
repeat_size = 1
@@ -370,24 +376,24 @@ if __name__ == "__main__":
model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy(), "Precision": Precision()})
print("============== Starting Testing ==============")
- param_dict = load_checkpoint("./ckpt/checkpoint_lenet-1_1875.ckpt")
+ param_dict = load_checkpoint(model_path+"checkpoint_lenet-1_1875.ckpt")
load_param_into_net(network, param_dict)
- ds_eval = create_dataset(os.path.join("/home/workspace/mindspore_dataset/MNIST_Data", "test"), 32, repeat_size)
+ ds_eval = create_dataset(ds_eval_path, 32, repeat_size)
+
acc = model.eval(ds_eval, dataset_sink_mode=True)
print("============== {} ==============".format(acc))
```
+```text
+============== Starting Testing ==============
+============== {'Accuracy': 0.960136217948718, 'Precision': array([0.95763547, 0.98059965, 0.99153439, 0.93333333, 0.97322348,
+ 0.99385749, 0.98502674, 0.93179724, 0.8974359 , 0.97148676])} ==============
+```
+
In the preceding information:
- `load_checkpoint`: loads the checkpoint model parameter file and returns a parameter dictionary.
- `checkpoint_lenet-1_1875.ckpt`: name of the saved checkpoint model file.
- `load_param_into_net`: loads parameters to the network.
-> For details about how to save the `checkpoint_lenet-1_1875.ckpt` file, see [Training the Network](https://www.mindspore.cn/tutorial/training/en/master/quick_start/quick_start.html#training-the-network).
-
-The output is as follows:
-
-```python
-============== {'Accuracy': 0.96875, 'Precision': array([0.97782258, 0.99451052, 0.98031496, 0.92723881, 0.98352214,
- 0.97165533, 0.98726115, 0.9472196 , 0.9394551 , 0.98236515])} ==============
-```
+> For details about how to save the `checkpoint_lenet-1_1875.ckpt` file, see [Training the Network](https://www.mindspore.cn/tutorial/training/en/r1.2/quick_start/quick_start.html#training-the-network).
diff --git a/docs/programming_guide/source_en/sampler.md b/docs/programming_guide/source_en/sampler.md
index 9bd30092017e50aa5fc805122a090b31eed590ab..2d4f0d1aef828fde056edf06c60208a8ff0bef0f 100644
--- a/docs/programming_guide/source_en/sampler.md
+++ b/docs/programming_guide/source_en/sampler.md
@@ -14,13 +14,13 @@
-
+
## Overview
MindSpore provides multiple samplers to help you sample datasets for various purposes to meet training requirements and solve problems such as oversized datasets and uneven distribution of sample categories. You only need to import the sampler object when loading the dataset for sampling the data.
-The following table lists part of the common samplers supported by MindSpore. In addition, you can define your own sampler class as required. For more samplers, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.dataset.html).
+The following table lists part of the common samplers supported by MindSpore. In addition, you can define your own sampler class as required. For more samplers, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.dataset.html).
| Sampler | Description |
| ---- | ---- |
@@ -32,18 +32,34 @@ The following table lists part of the common samplers supported by MindSpore. In
## MindSpore Samplers
-The following uses the CIFAR-10 as an example to introduce several common MindSpore samplers. Download [CIFAR-10 dataset](https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz) and decompress it. The directory structure is as follows:
+The following uses the CIFAR-10 as an example to introduce several common MindSpore samplers.
+
+Download the CIFAR-10 data set and unzip it to the specified path, execute the following command:
+
+```bash
+!wget -N https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/cifar-10-binary.tar.gz
+!mkdir -p datasets
+!tar -xzf cifar-10-binary.tar.gz -C datasets
+!mkdir -p datasets/cifar-10-batches-bin/train datasets/cifar-10-batches-bin/test
+!mv -f datasets/cifar-10-batches-bin/test_batch.bin datasets/cifar-10-batches-bin/test
+!mv -f datasets/cifar-10-batches-bin/data_batch*.bin datasets/cifar-10-batches-bin/batches.meta.txt datasets/cifar-10-batches-bin/train
+!tree ./datasets/cifar-10-batches-bin
+```
```text
-└─cifar-10-batches-bin
+./datasets/cifar-10-batches-bin
+├── readme.html
+├── test
+│ └── test_batch.bin
+└── train
├── batches.meta.txt
├── data_batch_1.bin
├── data_batch_2.bin
├── data_batch_3.bin
├── data_batch_4.bin
- ├── data_batch_5.bin
- ├── readme.html
- └── test_batch.bin
+ └── data_batch_5.bin
+
+2 directories, 8 files
```
### RandomSampler
@@ -57,7 +73,7 @@ import mindspore.dataset as ds
ds.config.set_seed(0)
-DATA_DIR = "cifar-10-batches-bin/"
+DATA_DIR = "./datasets/cifar-10-batches-bin/train/"
print("------ Without Replacement ------")
@@ -82,15 +98,15 @@ The output is as follows:
------ Without Replacement ------
Image shape: (32, 32, 3) , Label: 1
Image shape: (32, 32, 3) , Label: 6
-Image shape: (32, 32, 3) , Label: 7
+Image shape: (32, 32, 3) , Label: 6
Image shape: (32, 32, 3) , Label: 0
Image shape: (32, 32, 3) , Label: 4
------ With Replacement ------
-Image shape: (32, 32, 3) , Label: 4
-Image shape: (32, 32, 3) , Label: 6
+Image shape: (32, 32, 3) , Label: 0
Image shape: (32, 32, 3) , Label: 9
-Image shape: (32, 32, 3) , Label: 1
-Image shape: (32, 32, 3) , Label: 5
+Image shape: (32, 32, 3) , Label: 3
+Image shape: (32, 32, 3) , Label: 9
+Image shape: (32, 32, 3) , Label: 6
```
### WeightedRandomSampler
@@ -104,7 +120,7 @@ import mindspore.dataset as ds
ds.config.set_seed(1)
-DATA_DIR = "cifar-10-batches-bin/"
+DATA_DIR = "./datasets/cifar-10-batches-bin/train/"
weights = [1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
sampler = ds.WeightedRandomSampler(weights, num_samples=6)
@@ -136,7 +152,7 @@ import mindspore.dataset as ds
ds.config.set_seed(2)
-DATA_DIR = "cifar-10-batches-bin/"
+DATA_DIR = "./datasets/cifar-10-batches-bin/train/"
indices = [0, 1, 2, 3, 4, 5]
sampler = ds.SubsetRandomSampler(indices, num_samples=3)
@@ -165,7 +181,7 @@ import mindspore.dataset as ds
ds.config.set_seed(3)
-DATA_DIR = "cifar-10-batches-bin/"
+DATA_DIR = "./datasets/cifar-10-batches-bin/train/"
sampler = ds.PKSampler(num_val=2, class_column='label', num_samples=20)
dataset = ds.Cifar10Dataset(DATA_DIR, sampler=sampler)
@@ -240,7 +256,7 @@ class MySampler(ds.Sampler):
for i in range(0, 10, 2):
yield i
-DATA_DIR = "cifar-10-batches-bin/"
+DATA_DIR = "./datasets/cifar-10-batches-bin/train/"
dataset = ds.Cifar10Dataset(DATA_DIR, sampler=MySampler())
diff --git a/docs/programming_guide/source_en/security_and_privacy.md b/docs/programming_guide/source_en/security_and_privacy.md
index c76a39dbb3386eb265eaaf549f0979148421dc8b..56fcef337ed2cb759f78507fcbff8444852eb552 100644
--- a/docs/programming_guide/source_en/security_and_privacy.md
+++ b/docs/programming_guide/source_en/security_and_privacy.md
@@ -8,16 +8,18 @@
- [Attack](#attack)
- [Defense](#defense)
- [Detector](#detector)
-- [Model Security Test](#model-security-test)
+ - [Model Security Test](#model-security-test)
- [Fuzzer](#fuzzer)
- [Differential Privacy Training](#differential-privacy-training)
- [DPModel](#dpmodel)
+ - [Suppress Privacy Training](#suppress-privacy-training)
+ - [SuppressModel](#suppressmodel)
- [Privacy Breach Risk Assessment](#privacy-breach-risk-assessment)
- [Membership Inference](#membership-inference)
-
+
## Overview
@@ -37,7 +39,7 @@ The `Defense` base class defines the interface for adversarial training. Its sub
The `Detector` base class defines the interface for adversarial sample detection. Its subclasses implement various specific detection algorithms to enhance the adversarial robustness of the models.
-For details, see [Improving Model Security with NAD Algorithm](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/improve_model_security_nad.html).
+For details, see [Improving Model Security with NAD Algorithm](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/improve_model_security_nad.html).
## Model Security Test
@@ -45,7 +47,7 @@ For details, see [Improving Model Security with NAD Algorithm](https://www.minds
The `Fuzzer` class controls the fuzzing process based on the neuron coverage gain. It uses natural perturbation and adversarial sample generation methods as the mutation policy to activate more neurons to explore different types of model output results and error behavior, helping users enhance model robustness.
-For details, see [Testing Model Security Using Fuzz Testing](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/test_model_security_fuzzing.html).
+For details, see [Testing Model Security Using Fuzz Testing](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/test_model_security_fuzzing.html).
## Differential Privacy Training
@@ -53,7 +55,7 @@ For details, see [Testing Model Security Using Fuzz Testing](https://www.mindspo
`DPModel` inherits `mindspore.Model` and provides the entry function for differential privacy training.
-For details, see [Protecting User Privacy with Differential Privacy Mechanism](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/protect_user_privacy_with_differential_privacy.html).
+For details, see [Protecting User Privacy with Differential Privacy Mechanism](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/protect_user_privacy_with_differential_privacy.html).
## Suppress Privacy Training
@@ -61,7 +63,7 @@ For details, see [Protecting User Privacy with Differential Privacy Mechanism](h
`SuppressModel` inherits `mindspore.Model` and provides the entry function for suppress privacy training.
-For details, see [Protecting User Privacy with Suppress Privacy Mechanism](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/protect_user_privacy_with_suppress_privacy.html).
+For details, see [Protecting User Privacy with Suppress Privacy Mechanism](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/protect_user_privacy_with_suppress_privacy.html).
## Privacy Breach Risk Assessment
@@ -69,4 +71,4 @@ For details, see [Protecting User Privacy with Suppress Privacy Mechanism](https
The `MembershipInference` class provides a reverse analysis method. It can infer whether a sample is in the training set of a model based on the prediction information of the model on the sample to evaluate the privacy breach risk of the model.
-For details, see [Testing Model Security with Membership Inference](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/test_model_security_membership_inference.html).
+For details, see [Testing Model Security with Membership Inference](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/test_model_security_membership_inference.html).
diff --git a/docs/programming_guide/source_en/syntax_list.rst b/docs/programming_guide/source_en/syntax_list.rst
index b562453c3a01e1426a505095d0c088766bce8a4f..e6ea2d636d8a8223e909d4d348d7d4af6129f8d8 100644
--- a/docs/programming_guide/source_en/syntax_list.rst
+++ b/docs/programming_guide/source_en/syntax_list.rst
@@ -4,4 +4,4 @@ Syntax list
.. toctree::
:maxdepth: 1
- Static Graph Syntax Support .
+ Static Graph Syntax Support .
diff --git a/docs/programming_guide/source_en/tensor.md b/docs/programming_guide/source_en/tensor.md
index 6ca70599626ed667af8fcdac9710e0c527d74449..ba19f6c75195686675c2f167c38904e6abf11d7c 100644
--- a/docs/programming_guide/source_en/tensor.md
+++ b/docs/programming_guide/source_en/tensor.md
@@ -11,11 +11,11 @@
-
+
## Overview
-Tensor is a basic data structure in the MindSpore network computing. For details about data types in tensors, see [dtype](https://www.mindspore.cn/doc/programming_guide/en/master/dtype.html).
+Tensor is a basic data structure in the MindSpore network computing. For details about data types in tensors, see [dtype](https://www.mindspore.cn/doc/programming_guide/en/r1.2/dtype.html).
Tensors of different dimensions represent different data. For example, a 0-dimensional tensor represents a scalar, a 1-dimensional tensor represents a vector, a 2-dimensional tensor represents a matrix, and a 3-dimensional tensor may represent the three channels of RGB images.
diff --git a/docs/programming_guide/source_en/tokenizer.md b/docs/programming_guide/source_en/tokenizer.md
index f3d000874bc4646c8cc48dd3a7f0b79b6610f845..f0d638f409f5e3f2918bc89c5446a0a3d0e09ffc 100644
--- a/docs/programming_guide/source_en/tokenizer.md
+++ b/docs/programming_guide/source_en/tokenizer.md
@@ -14,7 +14,7 @@
-
+
## Overview
@@ -36,7 +36,7 @@ MindSpore provides the following tokenizers. In addition, you can customize toke
| WhitespaceTokenizer | Performs tokenization on scalar text data based on spaces. |
| WordpieceTokenizer | Performs tokenization on scalar text data based on the word set. |
-For details about tokenizers, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.dataset.text.html).
+For details about tokenizers, see [MindSpore API](https://www.mindspore.cn/doc/api_python/en/r1.2/mindspore/mindspore.dataset.text.html).
## MindSpore Tokenizers
@@ -103,6 +103,24 @@ I am making small mistakes during working hours
`JiebaTokenizer` performs Chinese tokenization based on Jieba.
+Download the dictionary files `hmm_model.utf8` and `jieba.dict.utf8` and put them in the specified location.
+
+```bash
+!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/hmm_model.utf8
+!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/jieba.dict.utf8
+!mkdir -p ./datasets/tokenizer/
+!mv hmm_model.utf8 jieba.dict.utf8 -t ./datasets/tokenizer/
+!tree ./datasets/tokenizer/
+```
+
+```text
+./datasets/tokenizer/
+├── hmm_model.utf8
+└── jieba.dict.utf8
+
+0 directories, 2 files
+```
+
The following example builds a text dataset, uses the HMM and MP dictionary files to create a `JiebaTokenizer` object, performs tokenization on the dataset, and displays the text results before and after tokenization.
```python
@@ -118,8 +136,8 @@ for data in dataset.create_dict_iterator(output_numpy=True):
print(text.to_str(data['text']))
# files from open source repository https://github.com/yanyiwu/cppjieba/tree/master/dict
-HMM_FILE = "hmm_model.utf8"
-MP_FILE = "jieba.dict.utf8"
+HMM_FILE = "./datasets/tokenizer/hmm_model.utf8"
+MP_FILE = "./datasets/tokenizer/jieba.dict.utf8"
jieba_op = text.JiebaTokenizer(HMM_FILE, MP_FILE)
dataset = dataset.map(operations=jieba_op, input_columns=["text"], num_parallel_workers=1)
@@ -142,6 +160,22 @@ The output is as follows:
`SentencePieceTokenizer` performs tokenization based on an open-source natural language processing tool package [SentencePiece](https://github.com/google/sentencepiece).
+Download the text dataset file `botchan.txt` and place it in the specified location.
+
+```bash
+!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/botchan.txt
+!mkdir -p ./datasets/tokenizer/
+!mv botchan.txt ./datasets/tokenizer/
+!tree ./datasets/tokenizer/
+```
+
+```text
+./datasets/tokenizer/
+└── botchan.txt
+
+0 directories, 1 files
+```
+
The following example builds a text dataset, creates a `vocab` object from the `vocab_file` file, uses `SentencePieceTokenizer` to perform tokenization on the dataset, and displays the text results before and after tokenization.
```python
@@ -157,8 +191,8 @@ print("------------------------before tokenization----------------------------")
for data in dataset.create_dict_iterator(output_numpy=True):
print(text.to_str(data['text']))
-# file from MindSpore repository https://gitee.com/mindspore/mindspore/blob/master/tests/ut/data/dataset/test_sentencepiece/botchan.txt
-vocab_file = "botchan.txt"
+# file from MindSpore repository https://gitee.com/mindspore/mindspore/blob/r1.2/tests/ut/data/dataset/test_sentencepiece/botchan.txt
+vocab_file = "./datasets/tokenizer/botchan.txt"
vocab = text.SentencePieceVocab.from_file([vocab_file], 5000, 0.9995, SentencePieceModel.UNIGRAM, {})
tokenizer_op = text.SentencePieceTokenizer(vocab, out_type=SPieceTokenizerOutType.STRING)
dataset = dataset.map(operations=tokenizer_op)
@@ -188,7 +222,7 @@ The following example builds a text dataset, uses `UnicodeCharTokenizer` to perf
import mindspore.dataset as ds
import mindspore.dataset.text as text
-input_list = ["Welcome to Beijing!", "北京欢迎您! ", "我喜欢English!"]
+input_list = ["Welcome to Beijing!", "北京欢迎您!", "我喜欢English!"]
dataset = ds.NumpySlicesDataset(input_list, column_names=["text"], shuffle=False)
print("------------------------before tokenization----------------------------")
@@ -214,7 +248,7 @@ Welcome to Beijing!
我喜欢English!
------------------------after tokenization-----------------------------
['W', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', 'B', 'e', 'i', 'j', 'i', 'n', 'g', '!']
-['北', '京', '欢', '迎', '您', '! ']
+['北', '京', '欢', '迎', '您', '!']
['我', '喜', '欢', 'E', 'n', 'g', 'l', 'i', 's', 'h', '!']
```
@@ -228,7 +262,7 @@ The following example builds a text dataset, uses `WhitespaceTokenizer` to perfo
import mindspore.dataset as ds
import mindspore.dataset.text as text
-input_list = ["Welcome to Beijing!", "北京欢迎您! ", "我喜欢English!"]
+input_list = ["Welcome to Beijing!", "北京欢迎您!", "我喜欢English!"]
dataset = ds.NumpySlicesDataset(input_list, column_names=["text"], shuffle=False)
print("------------------------before tokenization----------------------------")
@@ -254,7 +288,7 @@ Welcome to Beijing!
我喜欢English!
------------------------after tokenization-----------------------------
['Welcome', 'to', 'Beijing!']
-['北京欢迎您! ']
+['北京欢迎您!']
['我喜欢English!']
```
diff --git a/docs/programming_guide/source_en/train.md b/docs/programming_guide/source_en/train.md
index 1de44dbf01f2baae7e1ebc8fcea8cb4c85c66a9b..ce9549af195ef00ae9689a31234a7b7dfe015011 100644
--- a/docs/programming_guide/source_en/train.md
+++ b/docs/programming_guide/source_en/train.md
@@ -13,23 +13,25 @@
-
+
## Overview
MindSpore provides a large number of network models such as object detection and natural language processing in ModelZoo for users to directly use. However, some senior users may want to design networks or customize training cycles. The following describes how to customize a training network, how to customize a training cycle, and how to conduct inference while training. In addition, the on-device execution mode is also described in detail.
+> Note: This document is applicable to GPU and Ascend environments.
+
## Customizing a Training Network
Before customizing a training network, you need to understand the network support of MindSpore, constraints on network construction using Python, and operator support.
-- Network support: Currently, MindSpore supports multiple types of networks, including computer vision, natural language processing, recommender, and graph neural network. For details, see [Network List](https://www.mindspore.cn/doc/note/en/master/network_list.html). If the existing networks cannot meet your requirements, you can define your own network as required.
+- Network support: Currently, MindSpore supports multiple types of networks, including computer vision, natural language processing, recommender, and graph neural network. For details, see [Network List](https://www.mindspore.cn/doc/note/en/r1.2/network_list.html). If the existing networks cannot meet your requirements, you can define your own network as required.
-- Constraints on network construction using Python: MindSpore does not support the conversion of any Python source code into computational graphs. Therefore, the source code has the syntax and network definition constraints. These constraints may change as MindSpore evolves.
+- Constraints on network construction using Python: MindSpore does not support the conversion of any Python source code into computational graphs. Therefore, the source code has the syntax and network definition constraints. For details, please refer to [Static Graph Syntax Support](https://www.mindspore.cn/doc/note/en/r1.2/static_graph_syntax_support.html). These constraints may change as MindSpore evolves.
-- Operator support: As the name implies, the network is based on operators. Therefore, before customizing a training network, you need to understand the operators supported by MindSpore. For details about operator implementation on different backends (Ascend, GPU, and CPU), see [Operator List](https://www.mindspore.cn/doc/note/en/master/operator_list.html).
+- Operator support: As the name implies, the network is based on operators. Therefore, before customizing a training network, you need to understand the operators supported by MindSpore. For details about operator implementation on different backends (Ascend, GPU, and CPU), see [Operator List](https://www.mindspore.cn/doc/note/en/r1.2/operator_list.html).
-> When the built-in operators of the network cannot meet the requirements, you can refer to [Custom Operators(Ascend)](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/custom_operator_ascend.html) to quickly expand the custom operators of the Ascend AI processor.
+> When the built-in operators of the network cannot meet the requirements, you can refer to [Custom Operators(Ascend)](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/custom_operator_ascend.html) to quickly expand the custom operators of the Ascend AI processor.
The following is a code example:
@@ -38,14 +40,14 @@ import numpy as np
from mindspore import Tensor
from mindspore.nn import Cell, Dense, SoftmaxCrossEntropyWithLogits, Momentum, TrainOneStepCell, WithLossCell
-from mindspore.ops import operations as P
+import mindspore.ops as ops
class ReLUReduceMeanDense(Cell):
def __init__(self, kernel, bias, in_channel, num_class):
super().__init__()
- self.relu = P.ReLU()
- self.mean = P.ReduceMean(keep_dims=False)
+ self.relu = ops.ReLU()
+ self.mean = ops.ReduceMean(keep_dims=False)
self.dense = Dense(in_channel, num_class, kernel, bias)
def construct(self, x):
@@ -85,7 +87,30 @@ The output is as follows:
## Customizing a Training Cycle
-If you do not want to use the `Model` interface provided by MindSpore, you can use the `train` interface to control the number of iterations and the number of steps for each epoch.
+Before performing a custom training cycle, download the MNIST dataset that needs to be used, and decompress and place it at the specified location:
+
+```bash
+!mkdir -p ./datasets/MNIST_Data/train ./datasets/MNIST_Data/test
+!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-labels-idx1-ubyte
+!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-images-idx3-ubyte
+!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-labels-idx1-ubyte
+!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-images-idx3-ubyte
+!tree ./datasets/MNIST_Data
+```
+
+```text
+./datasets/MNIST_Data
+├── test
+│ ├── t10k-images-idx3-ubyte
+│ └── t10k-labels-idx1-ubyte
+└── train
+ ├── train-images-idx3-ubyte
+ └── train-labels-idx1-ubyte
+
+2 directories, 4 files
+```
+
+If you do not want to use the `Model` interface provided by MindSpore, you can also refer to the following examples to freely control the number of iterations, traverse the dataset, and so on.
The following is a code example:
@@ -102,9 +127,7 @@ from mindspore.common.initializer import TruncatedNormal
from mindspore import ParameterTuple
from mindspore.dataset.vision import Inter
from mindspore.nn import WithLossCell
-from mindspore.ops import composite as C
-from mindspore.ops import functional as F
-from mindspore.ops import operations as P
+import mindspore.ops as ops
def create_dataset(data_path, batch_size=32, repeat_size=1,
@@ -188,7 +211,7 @@ class LeNet5(nn.Cell):
self.fc3 = fc_with_initialize(84, self.num_class)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
- self.reshape = P.Reshape()
+ self.reshape = ops.Reshape()
def construct(self, x):
x = self.conv1(x)
@@ -212,7 +235,7 @@ class TrainOneStepCell(nn.Cell):
self.network = network
self.weights = ParameterTuple(network.trainable_params())
self.optimizer = optimizer
- self.grad = C.GradOperation(get_by_list=True, sens_param=True)
+ self.grad = ops.GradOperation(get_by_list=True, sens_param=True)
self.sens = sens
def set_sens(self, value):
@@ -221,53 +244,61 @@ class TrainOneStepCell(nn.Cell):
def construct(self, data, label):
weights = self.weights
loss = self.network(data, label)
- sens = P.Fill()(P.DType()(loss), P.Shape()(loss), self.sens)
+ sens = ops.Fill()(ops.DType()(loss), ops.Shape()(loss), self.sens)
grads = self.grad(self.network, weights)(data, label, sens)
- return F.depend(loss, self.optimizer(grads))
+ return ops.depend(loss, self.optimizer(grads))
if __name__ == "__main__":
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- ds_train = create_dataset(os.path.join("/home/workspace/mindspore_dataset/MNIST_Data/", "train"), 32)
+ context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
+
+ ds_data_path = "./datasets/MNIST_Data/train/"
+ ds_train = create_dataset(ds_data_path, 32)
network = LeNet5(10)
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
net = WithLossCell(network, net_loss)
net = TrainOneStepCell(net, net_opt)
- dataset_helper = DatasetHelper(ds_train, dataset_sink_mode=True, sink_size=100, epoch_num=10)
- net = connect_network_with_dataset(net, dataset_helper)
network.set_train()
print("============== Starting Training ==============")
epoch = 10
for step in range(epoch):
- for inputs in dataset_helper:
+ for inputs in ds_train:
output = net(*inputs)
print("epoch: {0}/{1}, losses: {2}".format(step + 1, epoch, output.asnumpy(), flush=True))
```
-> For details about how to obtain the MNIST dataset used in the example, see [Downloading the Dataset](https://www.mindspore.cn/tutorial/training/en/master/quick_start/quick_start.html#downloading-the-dataset).
-
The output is as follows:
-```python
-epoch: 1/10, losses: 2.294034719467163
-epoch: 2/10, losses: 2.3150298595428467
-epoch: 3/10, losses: 2.3107073307037354
-epoch: 4/10, losses: 2.3155436515808105
-epoch: 5/10, losses: 2.28973388671875
-epoch: 6/10, losses: 2.3108928203582764
-epoch: 7/10, losses: 2.293713092803955
-epoch: 8/10, losses: 2.29837703704834
-epoch: 9/10, losses: 2.305952548980713
-epoch: 10/10, losses: 1.4282708168029785
+```text
+============== Starting Training ==============
+epoch: 1/10, losses: 2.3086986541748047
+epoch: 1/10, losses: 2.309938430786133
+epoch: 1/10, losses: 2.302298069000244
+epoch: 1/10, losses: 2.310209035873413
+epoch: 1/10, losses: 2.3002336025238037
+epoch: 1/10, losses: 2.3022992610931396
+... ...
+epoch: 1/10, losses: 0.18848800659179688
+epoch: 1/10, losses: 0.15532201528549194
+epoch: 2/10, losses: 0.179201140999794
+epoch: 2/10, losses: 0.20995387434959412
+epoch: 2/10, losses: 0.4867479205131531
+... ...
+epoch: 10/10, losses: 0.027243722230196
+epoch: 10/10, losses: 0.07665436714887619
+epoch: 10/10, losses: 0.005962767638266087
+epoch: 10/10, losses: 0.026364721357822418
+epoch: 10/10, losses: 0.0003102901973761618
```
-> The typical application scenario is gradient accumulation. For details, see [Applying Gradient Accumulation Algorithm](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/apply_gradient_accumulation.html).
+> For details about how to obtain the MNIST dataset used in the example, see [Downloading the Dataset](https://www.mindspore.cn/tutorial/training/en/r1.2/quick_start/quick_start.html#downloading-the-dataset).
+> The typical application scenario is gradient accumulation. For details, see [Applying Gradient Accumulation Algorithm](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/apply_gradient_accumulation.html).
## Conducting Inference While Training
-For some complex networks with a large data volume and a relatively long training time, to learn the change of model accuracy in different training phases, the model accuracy may be traced in a manner of inference while training. For details, see [Evaluating the Model during Training](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/evaluate_the_model_during_training.html).
+For some complex networks with a large data volume and a relatively long training time, to learn the change of model accuracy in different training phases, the model accuracy may be traced in a manner of inference while training. For details, see [Evaluating the Model during Training](https://www.mindspore.cn/tutorial/training/en/r1.2/advanced_use/evaluate_the_model_during_training.html).
## On-Device Execution
@@ -315,7 +346,7 @@ from mindspore import dtype as mstype
from mindspore.common.initializer import TruncatedNormal
from mindspore.dataset.vision import Inter
from mindspore.nn import Accuracy
-from mindspore.ops import operations as P
+import mindspore.ops as ops
from mindspore.train.callback import LossMonitor
@@ -400,7 +431,7 @@ class LeNet5(nn.Cell):
self.fc3 = fc_with_initialize(84, self.num_class)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
- self.reshape = P.Reshape()
+ self.reshape = ops.Reshape()
def construct(self, x):
x = self.conv1(x)
@@ -419,8 +450,9 @@ class LeNet5(nn.Cell):
if __name__ == "__main__":
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- ds_train = create_dataset(os.path.join("/home/workspace/mindspore_dataset/MNIST_Data/", "train"), 32)
+ context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
+ ds_train_path = "./datasets/MNIST_Data/train/"
+ ds_train = create_dataset(ds_train_path, 32)
network = LeNet5(10)
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
@@ -431,21 +463,22 @@ if __name__ == "__main__":
model.train(epoch=10, train_dataset=ds_train, callbacks=[LossMonitor()], dataset_sink_mode=True, sink_size=1000)
```
-When `batch_size` is 32, the size of the dataset is 1875. When `sink_size` is set to 1000, each `epoch` sinks 1000 batches of data, the number of sinks is `epoch` (=10), and the total sunk data volume is `epoch` x `sink_size` = 10000.
-
The output is as follows:
```python
-epoch: 1 step: 1000, loss is 0.5399815
-epoch: 2 step: 1000, loss is 0.033433747
-epoch: 3 step: 1000, loss is 0.054761313
-epoch: 4 step: 1000, loss is 0.007882872
-epoch: 5 step: 1000, loss is 0.00658499
-epoch: 6 step: 1000, loss is 0.0413095
-epoch: 7 step: 1000, loss is 0.13373856
-epoch: 8 step: 1000, loss is 0.015793817
-epoch: 9 step: 1000, loss is 0.00017951085
-epoch: 10 step: 1000, loss is 0.01490275
+============== Starting Training ==============
+epoch: 1 step: 1000, loss is 0.110185064
+epoch: 2 step: 1000, loss is 0.12088283
+epoch: 3 step: 1000, loss is 0.15903473
+epoch: 4 step: 1000, loss is 0.030054657
+epoch: 5 step: 1000, loss is 0.013846226
+epoch: 6 step: 1000, loss is 0.052161213
+epoch: 7 step: 1000, loss is 0.0050197737
+epoch: 8 step: 1000, loss is 0.17207858
+epoch: 9 step: 1000, loss is 0.010310417
+epoch: 10 step: 1000, loss is 0.000672762
```
+When `batch_size` is 32, the size of the dataset is 1875. When `sink_size` is set to 1000, each `epoch` sinks 1000 batches of data, the number of sinks is `epoch` (=10), and the total sunk data volume is `epoch` x `sink_size` = 10000.
+
> When `dataset_sink_mode` is set to False, the `sink_size` parameter is invalid.
diff --git a/docs/programming_guide/source_zh_cn/advanced_usage_of_checkpoint.ipynb b/docs/programming_guide/source_zh_cn/advanced_usage_of_checkpoint.ipynb
new file mode 100644
index 0000000000000000000000000000000000000000..6f82db2acf3f5b48bc669bf52f16dd6bc87a4afc
--- /dev/null
+++ b/docs/programming_guide/source_zh_cn/advanced_usage_of_checkpoint.ipynb
@@ -0,0 +1,572 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 保存、加载与转化模型\n",
+ "\n",
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/advanced_usage_of_checkpoint.ipynb)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 概述\n",
+ "\n",
+ "在模型训练或者加载模型的过程中,有时需要替换掉模型文件中某些优化器或者其他超参数以及分类函数中的全连接层改动,但是又不想改动太大,或者从0开始训练模型,针对这种情况,MindSpore提供了只调整模型部分权重的CheckPoint进阶用法,并将方法应用在模型调优过程中。\n",
+ "\n",
+ "基础用法可参考:[保存加载参数](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/use/save_model.html#checkpoint)。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 准备工作"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "本篇以LeNet网络为例子,介绍在MindSpore中对模型进行保存,加载和转化等操作方法。\n",
+ "\n",
+ "在进行操作前,需做好如下准备好以下几个文件:\n",
+ "\n",
+ "- MNIST数据集。\n",
+ "- LeNet网络的预训练模型文件`checkpoint-lenet_1-1875.ckpt`。\n",
+ "- 数据增强文件`dataset_process.py`,使用其中的数据增强方法`create_dataset`,可参考官网[实现一个图片分类应用](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/quick_start/quick_start.html)中定义的数据增强方法`create_dataset`。\n",
+ "- 定义LeNet网络。\n",
+ "\n",
+ "执行下述代码,完成前3项准备工作。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2021-03-19T07:26:35.335788Z",
+ "start_time": "2021-03-19T07:26:35.312679Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "!mkdir -p ./datasets/MNIST_Data/train ./datasets/MNIST_Data/test\n",
+ "!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-labels-idx1-ubyte \n",
+ "!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-images-idx3-ubyte\n",
+ "!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-labels-idx1-ubyte\n",
+ "!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-images-idx3-ubyte\n",
+ "!wget https://mindspore-website.obs.myhuaweicloud.com/notebook/source-codes/dataset_process.py -N\n",
+ "!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/checkpoint_lenet-1_1875.zip\n",
+ "!unzip -o checkpoint_lenet-1_1875.zip"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "定义LeNet网络模型,具体定义过程如下。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from mindspore.common.initializer import Normal\n",
+ "import mindspore.nn as nn\n",
+ "\n",
+ "class LeNet5(nn.Cell):\n",
+ " \"\"\"Lenet network structure.\"\"\"\n",
+ " # define the operator required\n",
+ " def __init__(self, num_class=10, num_channel=1):\n",
+ " super(LeNet5, self).__init__()\n",
+ " self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')\n",
+ " self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')\n",
+ " self.fc1 = nn.Dense(16 * 5 * 5, 120, weight_init=Normal(0.02))\n",
+ " self.fc2 = nn.Dense(120, 84, weight_init=Normal(0.02))\n",
+ " self.fc3 = nn.Dense(84, num_class, weight_init=Normal(0.02))\n",
+ " self.relu = nn.ReLU()\n",
+ " self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)\n",
+ " self.flatten = nn.Flatten()\n",
+ "\n",
+ " # use the preceding operators to construct networks\n",
+ " def construct(self, x):\n",
+ " x = self.max_pool2d(self.relu(self.conv1(x)))\n",
+ " x = self.max_pool2d(self.relu(self.conv2(x)))\n",
+ " x = self.flatten(x)\n",
+ " x = self.relu(self.fc1(x))\n",
+ " x = self.relu(self.fc2(x))\n",
+ " x = self.fc3(x)\n",
+ " return x"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 高级用法\n",
+ "\n",
+ "### 保存\n",
+ "\n",
+ "#### 手动保存CheckPoint\n",
+ "\n",
+ "使用`save_checkpoint`,手动保存CheckPoint文件。\n",
+ "\n",
+ "应用场景: \n",
+ "\n",
+ "1. 保存网络的初始值。\n",
+ "2. 手动保存指定网络。\n",
+ "\n",
+ "执行以下代码,在对预训练模型`checkpoint_lenet-1_1875.ckpt`训练过100个batch的数据集后,使用`save_checkpoint`手动保存出模型`mindspore_lenet.ckpt`。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[Parameter (name=conv1.weight), Parameter (name=conv2.weight), Parameter (name=fc1.weight), Parameter (name=fc1.bias), Parameter (name=fc2.weight), Parameter (name=fc2.bias), Parameter (name=fc3.weight), Parameter (name=fc3.bias), Parameter (name=learning_rate), Parameter (name=momentum), Parameter (name=moments.conv1.weight), Parameter (name=moments.conv2.weight), Parameter (name=moments.fc1.weight), Parameter (name=moments.fc1.bias), Parameter (name=moments.fc2.weight), Parameter (name=moments.fc2.bias), Parameter (name=moments.fc3.weight), Parameter (name=moments.fc3.bias)]\n"
+ ]
+ }
+ ],
+ "source": [
+ "from mindspore import Model, load_checkpoint, save_checkpoint, load_param_into_net\n",
+ "from mindspore import context, Tensor\n",
+ "from dataset_process import create_dataset\n",
+ "import mindspore.nn as nn\n",
+ "\n",
+ "network = LeNet5()\n",
+ "net_opt = nn.Momentum(network.trainable_params(), learning_rate=0.01, momentum=0.9)\n",
+ "net_loss = nn.loss.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')\n",
+ "\n",
+ "params = load_checkpoint(\"checkpoint_lenet-1_1875.ckpt\")\n",
+ "load_param_into_net(network, params)\n",
+ "\n",
+ "net_with_criterion = nn.WithLossCell(network, net_loss)\n",
+ "train_net = nn.TrainOneStepCell(net_with_criterion, net_opt)\n",
+ "train_net.set_train()\n",
+ "\n",
+ "train_path = \"./datasets/MNIST_Data/train\"\n",
+ "ds_train = create_dataset(train_path)\n",
+ "\n",
+ "count = 0\n",
+ "for item in ds_train.create_dict_iterator():\n",
+ " input_data = item[\"image\"]\n",
+ " labels = item[\"label\"]\n",
+ " train_net(input_data, labels)\n",
+ " count += 1\n",
+ " if count==100:\n",
+ " print(train_net.trainable_params())\n",
+ " save_checkpoint(train_net, \"mindspore_lenet.ckpt\")\n",
+ " break"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "从上述打印信息可以看出`mindspore_lenet.ckpt`的权重参数,包括了前向传播过程中LeNet网络中各隐藏层中的权重参数、学习率、优化率以及反向传播中优化各权重层的优化器函数的权重。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### 保存指定的Cell\n",
+ "\n",
+ "使用方法:`CheckpointConfig`类的`saved_network`参数。\n",
+ "\n",
+ "应用场景:\n",
+ "\n",
+ "- 只保存推理网络模型的参数(不保存优化器的参数会使生成的CheckPoint文件大小减小一倍)。\n",
+ "\n",
+ "- 保存子网的参数,用于Fine-tune(模型微调)任务。\n",
+ "\n",
+ "在回调函数中使用方法`CheckpointConfig`,并指定保存模型的Cell为`network`即前向传播的LeNet网络。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "epoch: 1 step: 625, loss is 0.116291314\n",
+ "epoch: 1 step: 1250, loss is 0.09527888\n",
+ "epoch: 1 step: 1875, loss is 0.23090823\n"
+ ]
+ }
+ ],
+ "source": [
+ "import os\n",
+ "from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor\n",
+ "\n",
+ "ds_train = create_dataset(train_path)\n",
+ "epoch_size = 1\n",
+ "model = Model(train_net)\n",
+ "config_ck = CheckpointConfig(saved_network=network)\n",
+ "ckpoint = ModelCheckpoint(prefix=\"lenet\", config=config_ck)\n",
+ "model.train(epoch_size, ds_train, callbacks=[ckpoint, LossMonitor(625)])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "模型经过训练后,保存出模型文件`lenet-1_1875.ckpt`。接下来对比指定保存的模型cell和原始模型在大小和具体权重有何区别。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "with_opt size: 482 kB\n",
+ "{'conv1.weight': Parameter (name=conv1.weight), 'conv2.weight': Parameter (name=conv2.weight), 'fc1.weight': Parameter (name=fc1.weight), 'fc1.bias': Parameter (name=fc1.bias), 'fc2.weight': Parameter (name=fc2.weight), 'fc2.bias': Parameter (name=fc2.bias), 'fc3.weight': Parameter (name=fc3.weight), 'fc3.bias': Parameter (name=fc3.bias), 'learning_rate': Parameter (name=learning_rate), 'momentum': Parameter (name=momentum), 'moments.conv1.weight': Parameter (name=moments.conv1.weight), 'moments.conv2.weight': Parameter (name=moments.conv2.weight), 'moments.fc1.weight': Parameter (name=moments.fc1.weight), 'moments.fc1.bias': Parameter (name=moments.fc1.bias), 'moments.fc2.weight': Parameter (name=moments.fc2.weight), 'moments.fc2.bias': Parameter (name=moments.fc2.bias), 'moments.fc3.weight': Parameter (name=moments.fc3.weight), 'moments.fc3.bias': Parameter (name=moments.fc3.bias)}\n",
+ "\n",
+ "=========after train===========\n",
+ "\n",
+ "without_opt size: 241 kB\n",
+ "{'conv1.weight': Parameter (name=conv1.weight), 'conv2.weight': Parameter (name=conv2.weight), 'fc1.weight': Parameter (name=fc1.weight), 'fc1.bias': Parameter (name=fc1.bias), 'fc2.weight': Parameter (name=fc2.weight), 'fc2.bias': Parameter (name=fc2.bias), 'fc3.weight': Parameter (name=fc3.weight), 'fc3.bias': Parameter (name=fc3.bias)}\n"
+ ]
+ }
+ ],
+ "source": [
+ "model_with_opt = os.path.getsize(\"./checkpoint_lenet-1_1875.ckpt\") // 1024\n",
+ "params_without_change = load_checkpoint(\"./checkpoint_lenet-1_1875.ckpt\")\n",
+ "print(\"with_opt size:\", model_with_opt, \"kB\")\n",
+ "print(params_without_change)\n",
+ "\n",
+ "print(\"\\n=========after train===========\\n\")\n",
+ "model_without_opt = os.path.getsize(\"./lenet-1_1875.ckpt\") // 1024\n",
+ "params_with_change = load_checkpoint(\"./lenet-1_1875.ckpt\")\n",
+ "print(\"without_opt size:\", model_without_opt, \"kB\")\n",
+ "print(params_with_change)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "训练后,保存出来的模型`lenet-1_1875.ckpt`,模型权重文件大小为241kB,跟原始完整模型大小482kB相比,整体减少了将近一半;\n",
+ "\n",
+ "具体对比模型中的参数,可以看出`lenet-1_1875.ckpt`中参数相比`checkpoint_lenet-1_1875.ckpt`减少了学习率、优化率和反向优化等相关的权重参数,只保留了前向传播网络LeNet的权重参数。符合预期效果。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### 异步保存CheckPoint\n",
+ "\n",
+ "使用方法:`CheckpointConfig`类的`async_save`参数。\n",
+ "\n",
+ "应用场景:训练的模型参数量较大,可以边训练边保存,节省保存CheckPoint文件时的写入时间。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "config_ck = CheckpointConfig(async_save=True)\n",
+ "ckpoint = ModelCheckpoint(prefix=\"lenet\", config=config_ck)\n",
+ "model.train(epoch_size, ds_train, callbacks=ckpoint)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### 保存自定义参数字典\n",
+ "\n",
+ "使用方法:构造一个`obj_dict`传入`save_checkpoint`方法。\n",
+ "\n",
+ "使用场景:\n",
+ "\n",
+ "- 训练过程中需要额外保存参数(`lr`、`epoch_size`等)为CheckPoint文件。\n",
+ "\n",
+ "- 修改CheckPoint里面的参数值后重新保存。\n",
+ "\n",
+ "- 把PyTorch、TensorFlow的CheckPoint文件转化为MindSpore的CheckPoint文件。\n",
+ "\n",
+ "根据具体场景分为两种情况:\n",
+ "\n",
+ "1. 已有CheckPoint文件,修改内容后重新保存。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "==========param_list===========\n",
+ "\n",
+ "[{'name': 'conv1.weight', 'data': Parameter (name=conv1.weight)}, {'name': 'conv2.weight', 'data': Parameter (name=conv2.weight)}, {'name': 'fc1.weight', 'data': Parameter (name=fc1.weight)}, {'name': 'fc1.bias', 'data': Parameter (name=fc1.bias)}, {'name': 'fc2.weight', 'data': Parameter (name=fc2.weight)}, {'name': 'fc2.bias', 'data': Parameter (name=fc2.bias)}, {'name': 'fc3.weight', 'data': Parameter (name=fc3.weight)}, {'name': 'fc3.bias', 'data': Parameter (name=fc3.bias)}]\n",
+ "\n",
+ "==========after delete param_list[2]===========\n",
+ "\n",
+ "[{'name': 'conv1.weight', 'data': Parameter (name=conv1.weight)}, {'name': 'conv2.weight', 'data': Parameter (name=conv2.weight)}, {'name': 'fc1.bias', 'data': Parameter (name=fc1.bias)}, {'name': 'fc2.weight', 'data': Parameter (name=fc2.weight)}, {'name': 'fc2.bias', 'data': Parameter (name=fc2.bias)}, {'name': 'fc3.weight', 'data': Parameter (name=fc3.weight)}, {'name': 'fc3.bias', 'data': Parameter (name=fc3.bias)}]\n",
+ "\n",
+ "==========after add element===========\n",
+ "\n",
+ "[{'name': 'conv1.weight', 'data': Parameter (name=conv1.weight)}, {'name': 'conv2.weight', 'data': Parameter (name=conv2.weight)}, {'name': 'fc1.bias', 'data': Parameter (name=fc1.bias)}, {'name': 'fc2.weight', 'data': Parameter (name=fc2.weight)}, {'name': 'fc2.bias', 'data': Parameter (name=fc2.bias)}, {'name': 'fc3.weight', 'data': Parameter (name=fc3.weight)}, {'name': 'fc3.bias', 'data': Parameter (name=fc3.bias)}, {'name': 'epoch_size', 'data': Tensor(shape=[], dtype=Int64, value= 10)}]\n",
+ "\n",
+ "==========after modify element===========\n",
+ "\n",
+ "[{'name': 'conv1.weight', 'data': Parameter (name=conv1.weight)}, {'name': 'conv2.weight', 'data': Parameter (name=conv2.weight)}, {'name': 'fc1.bias', 'data': Parameter (name=fc1.bias)}, {'name': 'fc2.weight', 'data': Tensor(shape=[], dtype=Int64, value= 66)}, {'name': 'fc2.bias', 'data': Parameter (name=fc2.bias)}, {'name': 'fc3.weight', 'data': Parameter (name=fc3.weight)}, {'name': 'fc3.bias', 'data': Parameter (name=fc3.bias)}, {'name': 'epoch_size', 'data': Tensor(shape=[], dtype=Int64, value= 10)}]\n"
+ ]
+ }
+ ],
+ "source": [
+ "params = load_checkpoint(\"./lenet-1_1875.ckpt\")\n",
+ "\n",
+ "# eg: param_list = [{\"name\": param_name, \"data\": param_data},...]\n",
+ "param_list = [{\"name\": k, \"data\":v} for k,v in params.items()]\n",
+ "print(\"==========param_list===========\\n\")\n",
+ "print(param_list)\n",
+ "\n",
+ "# del element\n",
+ "del param_list[2]\n",
+ "print(\"\\n==========after delete param_list[2]===========\\n\")\n",
+ "print(param_list)\n",
+ "\n",
+ "\n",
+ "# add element \"epoch_size\"\n",
+ "param = {\"name\": \"epoch_size\"}\n",
+ "param[\"data\"] = Tensor(10)\n",
+ "param_list.append(param)\n",
+ "print(\"\\n==========after add element===========\\n\")\n",
+ "print(param_list)\n",
+ "\n",
+ "# modify element\n",
+ "param_list[3][\"data\"] = Tensor(66)\n",
+ "# save a new checkpoint file\n",
+ "print(\"\\n==========after modify element===========\\n\")\n",
+ "print(param_list)\n",
+ "\n",
+ "save_checkpoint(param_list, 'modify.ckpt')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "将加载的模型文件转换成list类型后,可以对模型参数进行删除,添加,修改等操作,并使用`save_checkpoint`手动保存,完成对模型权重的内容修改操作。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "2. 自定义参数列表保存成CheckPoint文件。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[{'name': 'epoch_size', 'data': Tensor(shape=[], dtype=Int64, value= 10)}, {'name': 'learning_rate', 'data': Tensor(shape=[], dtype=Float64, value= 0.01)}]\n"
+ ]
+ }
+ ],
+ "source": [
+ "param_list = []\n",
+ "# save epoch_size\n",
+ "param = {\"name\": \"epoch_size\"}\n",
+ "param[\"data\"] = Tensor(10)\n",
+ "param_list.append(param)\n",
+ "\n",
+ "# save learning rate\n",
+ "param = {\"name\": \"learning_rate\"}\n",
+ "param[\"data\"] = Tensor(0.01)\n",
+ "param_list.append(param)\n",
+ "# save a new checkpoint file\n",
+ "print(param_list)\n",
+ "\n",
+ "save_checkpoint(param_list, 'hyperparameters.ckpt')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 加载\n",
+ "\n",
+ "#### 严格匹配参数名\n",
+ "\n",
+ "CheckPoint文件中的权重参数到`net`中的时候,会优先匹配`net`和CheckPoint中name相同的parameter。匹配完成后,发现net中存在没有加载的parameter,会匹配net中后缀名称与ckpt相同的parameter。\n",
+ "\n",
+ "例如:会把CheckPoint中名为`conv.0.weight`的参数值加载到net中名为`net.conv.0.weight`的parameter中。\n",
+ "\n",
+ "如果想取消这种模糊匹配,只采取严格匹配机制,可以通过方法`load_param_into_net`中的`strict_load`参数控制,默认为False,表示采取模糊匹配机制。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "==========strict load mode===========\n",
+ "{'conv1.weight': Parameter (name=conv1.weight), 'conv2.weight': Parameter (name=conv2.weight), 'fc1.weight': Parameter (name=fc1.weight), 'fc1.bias': Parameter (name=fc1.bias), 'fc2.weight': Parameter (name=fc2.weight), 'fc2.bias': Parameter (name=fc2.bias), 'fc3.weight': Parameter (name=fc3.weight), 'fc3.bias': Parameter (name=fc3.bias)}\n"
+ ]
+ }
+ ],
+ "source": [
+ "net = LeNet5()\n",
+ "params = load_checkpoint(\"lenet-1_1875.ckpt\")\n",
+ "load_param_into_net(net, params, strict_load=True)\n",
+ "print(\"==========strict load mode===========\")\n",
+ "print(params)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### 过滤指定前缀\n",
+ "\n",
+ "使用方法:`load_checkpoint`的`filter_prefix`参数。\n",
+ "\n",
+ "使用场景:加载CheckPoint时,想要过滤某些包含特定前缀的parameter。\n",
+ "\n",
+ "- 加载CheckPoint时,不加载优化器中的`parameter(eg:filter_prefix=’moments’)`。\n",
+ "\n",
+ "- 不加载卷积层的`parameter(eg:filter_prefix=’conv1’)`。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "=============net params=============\n",
+ "{'conv1.weight': Parameter (name=conv1.weight), 'conv2.weight': Parameter (name=conv2.weight), 'fc1.weight': Parameter (name=fc1.weight), 'fc1.bias': Parameter (name=fc1.bias), 'fc2.weight': Parameter (name=fc2.weight), 'fc2.bias': Parameter (name=fc2.bias), 'fc3.weight': Parameter (name=fc3.weight), 'fc3.bias': Parameter (name=fc3.bias), 'learning_rate': Parameter (name=learning_rate), 'momentum': Parameter (name=momentum), 'moments.conv1.weight': Parameter (name=moments.conv1.weight), 'moments.conv2.weight': Parameter (name=moments.conv2.weight), 'moments.fc1.weight': Parameter (name=moments.fc1.weight), 'moments.fc1.bias': Parameter (name=moments.fc1.bias), 'moments.fc2.weight': Parameter (name=moments.fc2.weight), 'moments.fc2.bias': Parameter (name=moments.fc2.bias), 'moments.fc3.weight': Parameter (name=moments.fc3.weight), 'moments.fc3.bias': Parameter (name=moments.fc3.bias)}\n",
+ "\n",
+ "=============after filter_prefix moments=============\n",
+ "{'conv1.weight': Parameter (name=conv1.weight), 'conv2.weight': Parameter (name=conv2.weight), 'fc1.weight': Parameter (name=fc1.weight), 'fc1.bias': Parameter (name=fc1.bias), 'fc2.weight': Parameter (name=fc2.weight), 'fc2.bias': Parameter (name=fc2.bias), 'fc3.weight': Parameter (name=fc3.weight), 'fc3.bias': Parameter (name=fc3.bias), 'learning_rate': Parameter (name=learning_rate), 'momentum': Parameter (name=momentum)}\n"
+ ]
+ }
+ ],
+ "source": [
+ "net = LeNet5()\n",
+ "print(\"=============net params=============\")\n",
+ "params = load_checkpoint(\"checkpoint_lenet-1_1875.ckpt\")\n",
+ "load_param_into_net(net, params)\n",
+ "print(params)\n",
+ "\n",
+ "net = LeNet5()\n",
+ "print(\"\\n=============after filter_prefix moments=============\")\n",
+ "params = load_checkpoint(\"checkpoint_lenet-1_1875.ckpt\", filter_prefix='moments')\n",
+ "load_param_into_net(net, params)\n",
+ "print(params)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "使用过滤前缀的机制,可以将不想载入的参数(本例为优化器权重参数)过滤掉,进行Fine-tune时,可以选用其他的优化器进行优化。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "> 以上为使用MindSpore checkpoint功能的进阶用法,上述所有用法均可共同使用。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 转化其他框架CheckPoint为MindSpore的格式\n",
+ "\n",
+ "把其他框架的CheckPoint文件转化成MindSpore格式。\n",
+ "\n",
+ "一般情况下,CheckPoint文件中保存的就是参数名和参数值,调用相应框架的读取接口后,获取到参数名和数值后,按照MindSpore格式,构建出对象,就可以直接调用MindSpore接口保存成MindSpore格式的CheckPoint文件了。\n",
+ "\n",
+ "其中主要的工作量为对比不同框架间的parameter名称,做到两个框架的网络中所有parameter name一一对应(可以使用一个map进行映射),下面代码的逻辑转化parameter格式,不包括对应parameter name。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import torch\n",
+ "from mindspore import Tensor, save_checkpoint\n",
+ "\n",
+ "def pytorch2mindspore(default_file = 'torch_resnet.pth'):\n",
+ " # read pth file\n",
+ " par_dict = torch.load(default_file)['state_dict']\n",
+ " params_list = []\n",
+ " for name in par_dict:\n",
+ " param_dict = {}\n",
+ " parameter = par_dict[name]\n",
+ " param_dict['name'] = name\n",
+ " param_dict['data'] = Tensor(parameter.numpy())\n",
+ " params_list.append(param_dict)\n",
+ " save_checkpoint(params_list, 'ms_resnet.ckpt')"
+ ]
+ }
+ ],
+ "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/docs/programming_guide/source_zh_cn/advanced_usage_of_checkpoint.md b/docs/programming_guide/source_zh_cn/advanced_usage_of_checkpoint.md
deleted file mode 100644
index eb1120b332a399cb69579482133b82c61164c3cb..0000000000000000000000000000000000000000
--- a/docs/programming_guide/source_zh_cn/advanced_usage_of_checkpoint.md
+++ /dev/null
@@ -1,172 +0,0 @@
-# 保存、加载与转化模型
-
-- [基础用法](#基础用法)
-- [高级用法](#高级用法)
- - [保存](#保存)
- - [手动保存ckpt](#手动保存ckpt)
- - [保存指定cell](#保存指定cell)
- - [异步保存ckpt](#异步保存ckpt)
- - [保存自定义参数字典](#保存自定义参数字典)
- - [加载](#加载)
- - [严格加载参数名](#严格加载参数名)
- - [过滤指定前缀](#过滤指定前缀)
- - [转化其他框架ckpt为ms格式](#转化其他框架ckpt为ms格式)
-
-
-
-## 基础用法
-
-此文为MindSpore checkpoint的进阶用法,用于涉及使用checkpoint的复杂场景。
-
-基础用法可参考:[保存加载参数](https://www.mindspore.cn/tutorial/training/zh-CN/master/use/save_model.html#checkpoint)。
-
-## 高级用法
-
-### 保存
-
-#### 手动保存ckpt
-
-使用方法:`save_checkpoint`
-
-应用场景:
-1、保存网络的初始值
-2、手动保存指定网络
-
-```python
-save_checkpoint(save_obj, ckpt_file_name)
-net = LeNet()
-save_checkpoint(net, 'resnet.ckpt')
-```
-
-#### 保存指定cell
-
-使用方法:`ModelCheckpoin` 类的 `saved_network` 参数
-
-应用场景:
-
-- 只保存推理网的参数(不保存优化器的参数会使生成的ckpt文件大小减小一倍)
-- 保存子网的参数,用于fune-tine任务
-
-```python
-net = LeNet5()
-loss = SoftmaxCrossEntropyWithLogits()
-opt = nn.Momentum()
-model = Model(net, loss, opt)
-config_ck = CheckpointConfig()
-ckpoint = ModelCheckpoint(prefix="lenet";, config=config_ck, saved_network=net)
-model.train(epoch_size, dataset, callbacks=ckpoint)
-```
-
-#### 异步保存ckpt
-
-使用方法:`ModelCheckpoint` 类的 `async_save` 参数
-
-应用场景:训练的模型参数量较大,可以边训练边保存,节省保存ckpt时写文件的时间
-
-```python
-ckpoint = ModelCheckpoint(prefix="lenet", config=config_ck, async_save=True)
-model.train(epoch_size, dataset, callbacks=ckpoint)
-```
-
-#### 保存自定义参数字典
-
-使用方法:构造一个`obj_dict` 传入 `save_checkpoint` 方法
-
-使用场景:
-
-- 训练过程中需要额外保存参数(lr、epoch_size等)为checkpoint文件
-- 修改checkpoint里面的参数值后重新保存
-- 把PyTorch、TensorFlow的checkpoint文件转化为MindSpore的checkpoint文件
-
-根据具体场景分为两种情况:
-
-1、已有checkpoint文件,修改内容后重新保存
-
-```python
-param_list = load_checkpoint("resnet.ckpt")
-# eg: param_list = [{"name": param_name, "data": param_data},...]
-# del element
-del param_list[2]
-# add element "epoch_size"
-param = {"name": "epoch_size"}
-param["data"] = Tensor(10)
-param_list.append(param)
-# modify element
-param_list[3]["data"] = Tensor(66)
-# save a new checkpoint file
-save_checkpoint(param_list, 'modify.ckpt')
-```
-
-2、自定义参数列表保存成checkpoint文件
-
-```python
-param_list = []
-# save epoch_size
-param = {"name": "epoch_size"}
-param["data"] = Tensor(10)
-param_list.append(param)
-# save learning rate
-param = {"name": "learning_rate"}
-param["data"] = Tensor(0.01)
-param_list.append(param)
-# save a new checkpoint file
-save_checkpoint(param_list, 'hyperparameters.ckpt')
-```
-
-### 加载
-
-#### 严格匹配参数名
-
-背景:ckpt文件中的权重参数到net中的时候,会优先匹配net和ckpt中name相同的parameter。匹配完成后,发现net中存在没有加载的parameter,会匹配net中后缀名称与ckpt相同的parameter。(原因是ms的parameter命名机制)
-
-例如:会把ckpt中名为:`conv.0.weight`的参数值加载到net中名为`net.conv.0.weight`的parameter中。
-
-如果想取消这种模糊匹配,只采取严格匹配机制,可以通过方法`load_param_into_net`中的`strict_load`参数控制,默认为False,表示采取模糊匹配机制。
-
-```python
-net = Lenet()
-param_list = load_checkpoint("resnet.ckpt")
-load_param_into_net(net, param_list, strict_load=True):
-```
-
-#### 过滤指定前缀
-
-使用方法:`load_checkpoint`的`filter_prefix` 参数
-
-使用场景:加载ckpt时,想要过滤某些包含特定前缀的parameter
-
-- 加载ckpt时,不加载优化器中的parameter(eg:filter_prefix='opt')
-- 不加载卷积层的parameter(eg:filter_prefix='conv')
-
-```python
-net = Lenet()
-param_list = load_checkpoint("resnet.ckpt")
-load_param_into_net(net, param_list, filter_prefix='opt'):
-```
-
-> 以上为使用MindSpore checkpoint功能的进阶用法,上述所有用法均可共同使用。
-
-### 转化其他框架ckpt为ms格式
-
-把其他框架的checkpoint文件转化成MindSpore格式
-
-思路:不管是什么框架,checkpoint文件中保存的就是参数名和参数值,调用相应框架的读取接口后,获取到参数名和数值后,按照MindSpore格式,构建出对象,就可以直接调用ms接口保存成ms格式的checkpoint文件了。
-
-其中主要的工作量为对比不同框架间的parameter名称,做到两个框架的网络中所有parameter name一一对应(可以使用一个map进行映射),下面代码的逻辑转化parameter格式,不包括对应parameter name。
-
-```python
-import torch
-from mindspore import Tensor save_checkpoint
-
-def pytorch2mindspore('torch_resnet.pth'):
- # read pth file
- par_dict = torch.load('torch_resnet.pth')['state_dict']
- params_list = []
- for name in par_dict:
- param_dict = {}
- parameter = par_dict[name]
- param_dict['name'] = name
- param_dict['data'] = Tensor(parameter.numpy())
- params_list.append(param_dict)
- save_checkpoint(params_list, 'ms_resnet.ckpt')
-```
diff --git a/docs/programming_guide/source_zh_cn/api_structure.ipynb b/docs/programming_guide/source_zh_cn/api_structure.ipynb
index 45f8bb223ac2f888f3314b172b519909db82b24c..09134e4f0ab722d5e3e59f457e6e53ec5379c940 100644
--- a/docs/programming_guide/source_zh_cn/api_structure.ipynb
+++ b/docs/programming_guide/source_zh_cn/api_structure.ipynb
@@ -6,7 +6,7 @@
"source": [
"# MindSpore API概述\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/api_structure.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_api_overview.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9hcGlfb3ZlcnZpZXcuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/api_structure.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_api_overview.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9hcGlfb3ZlcnZpZXcuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -22,7 +22,7 @@
"source": [
"MindSpore是一个全场景深度学习框架,旨在实现易开发、高效执行、全场景覆盖三大目标,其中易开发表现为API友好、调试难度低,高效执行包括计算效率、数据预处理效率和分布式训练效率,全场景则指框架同时支持云、边缘以及端侧场景。\n",
"\n",
- "MindSpore总体架构分为前端表示层(Mind Expression,ME)、计算图引擎(Graph Engine,GE)和后端运行时三个部分。ME提供了用户级应用软件编程接口(Application Programming Interface,API),用于科学计算以及构建和训练神经网络,并将用户的Python代码转换为数据流图。GE是算子和硬件资源的管理器,负责控制从ME接收的数据流图的执行。后端运行时包括云、边、端上不同环境中的高效运行环境,例如CPU、GPU、Ascend AI处理器、Android/iOS等。更多总体架构的相关内容请参见[总体架构](https://www.mindspore.cn/doc/note/zh-CN/master/design/mindspore/architecture.html)。"
+ "MindSpore总体架构分为前端表示层(Mind Expression,ME)、计算图引擎(Graph Engine,GE)和后端运行时三个部分。ME提供了用户级应用软件编程接口(Application Programming Interface,API),用于科学计算以及构建和训练神经网络,并将用户的Python代码转换为数据流图。GE是算子和硬件资源的管理器,负责控制从ME接收的数据流图的执行。后端运行时包括云、边、端上不同环境中的高效运行环境,例如CPU、GPU、Ascend AI处理器、Android/iOS等。更多总体架构的相关内容请参见[总体架构](https://www.mindspore.cn/doc/note/zh-CN/r1.2/design/mindspore/architecture.html)。"
]
},
{
@@ -38,7 +38,7 @@
"source": [
"MindSpore源于全产业的最佳实践,向数据科学家和算法工程师提供了统一的模型训练、推理和导出等接口,支持端、边、云等不同场景下的灵活部署,推动深度学习和科学计算等领域繁荣发展。\n",
"\n",
- "MindSpore目前提供了Python编程范式,用户使用Python原生控制逻辑即可构建复杂的神经网络模型,AI编程变得简单,具体示例请参见[实现一个图片分类应用](https://www.mindspore.cn/tutorial/training/zh-CN/master/quick_start/quick_start.html)。\n",
+ "MindSpore目前提供了Python编程范式,用户使用Python原生控制逻辑即可构建复杂的神经网络模型,AI编程变得简单,具体示例请参见[实现一个图片分类应用](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/quick_start/quick_start.html)。\n",
"\n",
"目前主流的深度学习框架的执行模式有两种,分别为静态图模式和动态图模式。静态图模式拥有较高的训练性能,但难以调试。动态图模式相较于静态图模式虽然易于调试,但难以高效执行。MindSpore提供了动态图和静态图统一的编码方式,大大增加了静态图和动态图的可兼容性,用户无需开发多套代码,仅变更一行代码便可切换动态图/静态图模式,例如设置`context.set_context(mode=context.PYNATIVE_MODE)`切换成动态图模式,设置`context.set_context(mode=context.GRAPH_MODE)`即可切换成静态图模式,用户可拥有更轻松的开发调试及性能体验。\n",
"\n",
@@ -92,11 +92,11 @@
"\n",
"此外,SCT能够将Python代码转换为MindSpore函数中间表达(Intermediate Representation,IR),该函数中间表达构造出能够在不同设备解析和执行的计算图,并且在执行该计算图前,应用了多种软硬件协同优化技术,端、边、云等不同场景下的性能和效率得到针对性的提升。\n",
"\n",
- "如何提高数据处理能力以匹配人工智能芯片的算力,是保证人工智能芯片发挥极致性能的关键。MindSpore为用户提供了多种数据处理算子,通过自动数据加速技术实现了高性能的流水线,包括数据加载、数据论证、数据转换等,支持CV/NLP/GNN等全场景的数据处理能力。MindRecord是MindSpore的自研数据格式,具有读写高效、易于分布式处理等优点,用户可将非标准的数据集和常用的数据集转换为MindRecord格式,从而获得更好的性能体验,转换详情请参见[MindSpore数据格式转换](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/dataset_conversion.html)。MindSpore支持加载常用的数据集和多种数据存储格式下的数据集,例如通过`dataset=dataset.Cifar10Dataset(\"Cifar10Data/\")`即可完成CIFAR-10数据集的加载,其中`Cifar10Data/`为数据集本地所在目录,用户也可通过`GeneratorDataset`自定义数据集的加载方式。数据增强是一种基于(有限)数据生成新数据的方法,能够减少网络模型过拟合的现象,从而提高模型的泛化能力。MindSpore除了支持用户自定义数据增强外,还提供了自动数据增强方式,使得数据增强更加灵活,详情请见[自动数据增强](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/auto_augmentation.html)。\n",
+ "如何提高数据处理能力以匹配人工智能芯片的算力,是保证人工智能芯片发挥极致性能的关键。MindSpore为用户提供了多种数据处理算子,通过自动数据加速技术实现了高性能的流水线,包括数据加载、数据论证、数据转换等,支持CV/NLP/GNN等全场景的数据处理能力。MindRecord是MindSpore的自研数据格式,具有读写高效、易于分布式处理等优点,用户可将非标准的数据集和常用的数据集转换为MindRecord格式,从而获得更好的性能体验,转换详情请参见[MindSpore数据格式转换](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/dataset_conversion.html)。MindSpore支持加载常用的数据集和多种数据存储格式下的数据集,例如通过`dataset=dataset.Cifar10Dataset(\"Cifar10Data/\")`即可完成CIFAR-10数据集的加载,其中`Cifar10Data/`为数据集本地所在目录,用户也可通过`GeneratorDataset`自定义数据集的加载方式。数据增强是一种基于(有限)数据生成新数据的方法,能够减少网络模型过拟合的现象,从而提高模型的泛化能力。MindSpore除了支持用户自定义数据增强外,还提供了自动数据增强方式,使得数据增强更加灵活,详情请见[自动数据增强](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/auto_augmentation.html)。\n",
"\n",
- "深度学习神经网络模型通常含有较多的隐藏层进行特征提取,但特征提取随机化、调试过程不可视限制了深度学习技术的可信和调优。MindSpore支持可视化调试调优(MindInsight),提供训练看板、溯源、性能分析和调试器等功能,帮助用户发现模型训练过程中出现的偏差,轻松进行模型调试和性能调优。例如用户可在初始化网络前,通过`profiler=Profiler()`初始化`Profiler`对象,自动收集训练过程中的算子耗时等信息并记录到文件中,在训练结束后调用`profiler.analyse()`停止收集并生成性能分析结果,以可视化形式供用户查看分析,从而更高效地调试网络性能,更多调试调优相关内容请见[训练过程可视化](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/visualization_tutorials.html)。\n",
+ "深度学习神经网络模型通常含有较多的隐藏层进行特征提取,但特征提取随机化、调试过程不可视限制了深度学习技术的可信和调优。MindSpore支持可视化调试调优(MindInsight),提供训练看板、溯源、性能分析和调试器等功能,帮助用户发现模型训练过程中出现的偏差,轻松进行模型调试和性能调优。例如用户可在初始化网络前,通过`profiler=Profiler()`初始化`Profiler`对象,自动收集训练过程中的算子耗时等信息并记录到文件中,在训练结束后调用`profiler.analyse()`停止收集并生成性能分析结果,以可视化形式供用户查看分析,从而更高效地调试网络性能,更多调试调优相关内容请见[训练过程可视化](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/visualization_tutorials.html)。\n",
"\n",
- "随着神经网络模型和数据集的规模不断增加,分布式并行训练成为了神经网络训练的常见做法,但分布式并行训练的策略选择和编写十分复杂,这严重制约着深度学习模型的训练效率,阻碍深度学习的发展。MindSpore统一了单机和分布式训练的编码方式,开发者无需编写复杂的分布式策略,在单机代码中添加少量代码即可实现分布式训练,例如设置`context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL)`便可自动建立代价模型,为用户选择一种较优的并行模式,提高神经网络训练效率,大大降低了AI开发门槛,使用户能够快速实现模型思路,更多内容请见[分布式并行训练](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/distributed_training_tutorials.html)。"
+ "随着神经网络模型和数据集的规模不断增加,分布式并行训练成为了神经网络训练的常见做法,但分布式并行训练的策略选择和编写十分复杂,这严重制约着深度学习模型的训练效率,阻碍深度学习的发展。MindSpore统一了单机和分布式训练的编码方式,开发者无需编写复杂的分布式策略,在单机代码中添加少量代码即可实现分布式训练,例如设置`context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL)`便可自动建立代价模型,为用户选择一种较优的并行模式,提高神经网络训练效率,大大降低了AI开发门槛,使用户能够快速实现模型思路,更多内容请见[分布式并行训练](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/distributed_training_tutorials.html)。"
]
},
{
@@ -112,7 +112,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- ""
+ ""
]
},
{
diff --git a/docs/programming_guide/source_zh_cn/augmentation.ipynb b/docs/programming_guide/source_zh_cn/augmentation.ipynb
index 18408fa19f8b96e9d27c823e8adb6fb90f8fc583..6eb6cec99121fa3990ba0bab692c1c8806ee7ee6 100644
--- a/docs/programming_guide/source_zh_cn/augmentation.ipynb
+++ b/docs/programming_guide/source_zh_cn/augmentation.ipynb
@@ -6,7 +6,7 @@
"source": [
"# 数据增强\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/augmentation.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_augmentation.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9hdWdtZW50YXRpb24uaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/augmentation.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_augmentation.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9hdWdtZW50YXRpb24uaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -24,7 +24,7 @@
"| c_transforms | 基于C++的OpenCV实现 | 具有较高的性能。 |\n",
"| py_transforms | 基于Python的PIL实现 | 该模块提供了多种图像增强功能,并提供了PIL Image和NumPy数组之间的传输方法。|\n",
"\n",
- "MindSpore目前支持多种常用的数据增强算子,如下表所示,更多数据增强算子参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.dataset.vision.html)。\n",
+ "MindSpore目前支持多种常用的数据增强算子,如下表所示,更多数据增强算子参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.dataset.vision.html)。\n",
"\n",
"| 模块 | 算子 | 说明 |\n",
"| :---- | :---- | :---- |\n",
@@ -73,10 +73,11 @@
"metadata": {},
"outputs": [
{
- "output_type": "stream",
"name": "stdout",
+ "output_type": "stream",
"text": [
- "./datasets/cifar10\n",
+ "./datasets/cifar-10-batches-bin\n",
+ "├── readme.html\n",
"├── test\n",
"│ └── test_batch.bin\n",
"└── train\n",
@@ -87,14 +88,18 @@
" ├── data_batch_4.bin\n",
" └── data_batch_5.bin\n",
"\n",
- "2 directories, 7 files\n"
+ "2 directories, 8 files\n"
]
}
],
"source": [
- "!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/cifar10.zip\n",
- "!unzip -o cifar10.zip -d ./datasets\n",
- "!tree ./datasets/cifar10"
+ "!wget -N https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/cifar-10-binary.tar.gz\n",
+ "!mkdir -p datasets\n",
+ "!tar -xzf cifar-10-binary.tar.gz -C datasets\n",
+ "!mkdir -p datasets/cifar-10-batches-bin/train datasets/cifar-10-batches-bin/test\n",
+ "!mv -f datasets/cifar-10-batches-bin/test_batch.bin datasets/cifar-10-batches-bin/test\n",
+ "!mv -f datasets/cifar-10-batches-bin/data_batch*.bin datasets/cifar-10-batches-bin/batches.meta.txt datasets/cifar-10-batches-bin/train\n",
+ "!tree ./datasets/cifar-10-batches-bin"
]
},
{
@@ -129,7 +134,7 @@
"ds.config.set_seed(5)\n",
"ds.config.set_num_parallel_workers(1)\n",
"\n",
- "DATA_DIR = \"./datasets/cifar10/train/\"\n",
+ "DATA_DIR = \"./datasets/cifar-10-batches-bin/train/\"\n",
"\n",
"sampler = ds.SequentialSampler(num_samples=3)\n",
"dataset1 = ds.Cifar10Dataset(DATA_DIR, sampler=sampler)\n",
@@ -210,7 +215,7 @@
"ds.config.set_seed(6)\n",
"ds.config.set_num_parallel_workers(1)\n",
"\n",
- "DATA_DIR = \"./datasets/cifar10/train/\"\n",
+ "DATA_DIR = \"./datasets/cifar-10-batches-bin/train/\"\n",
"\n",
"sampler = ds.RandomSampler(num_samples=4)\n",
"dataset1 = ds.Cifar10Dataset(DATA_DIR, sampler=sampler)\n",
@@ -266,10 +271,10 @@
"metadata": {},
"outputs": [
{
- "output_type": "stream",
"name": "stdout",
+ "output_type": "stream",
"text": [
- "./datasets/MNIST_Data/\n",
+ "./datasets/MNIST_Data\n",
"├── test\n",
"│ ├── t10k-images-idx3-ubyte\n",
"│ └── t10k-labels-idx1-ubyte\n",
@@ -282,9 +287,12 @@
}
],
"source": [
- "!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/MNIST_Data.zip\n",
- "!unzip -o MNIST_Data.zip -d ./datasets\n",
- "!tree ./datasets/MNIST_Data/"
+ "!mkdir -p ./datasets/MNIST_Data/train ./datasets/MNIST_Data/test\n",
+ "!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-labels-idx1-ubyte \n",
+ "!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-images-idx3-ubyte\n",
+ "!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-labels-idx1-ubyte\n",
+ "!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-images-idx3-ubyte\n",
+ "!tree ./datasets/MNIST_Data"
]
},
{
@@ -391,7 +399,7 @@
"\n",
"ds.config.set_seed(8)\n",
"\n",
- "DATA_DIR = \"./datasets/cifar10/train/\"\n",
+ "DATA_DIR = \"./datasets/cifar-10-batches-bin/train/\"\n",
"\n",
"dataset1 = ds.Cifar10Dataset(DATA_DIR, num_samples=4, shuffle=True)\n",
"\n",
@@ -473,7 +481,7 @@
"\n",
"ds.config.set_seed(8)\n",
"\n",
- "DATA_DIR = \"./datasets/cifar10/train/\"\n",
+ "DATA_DIR = \"./datasets/cifar-10-batches-bin/train/\"\n",
"\n",
"dataset1 = ds.Cifar10Dataset(DATA_DIR, num_samples=5, shuffle=True)\n",
"\n",
@@ -598,11 +606,11 @@
"source": [
"MindSpore目前可以支持Eager模式的数据增强算子包括:\n",
"\n",
- "- [mindspore.dataset.vision.c_transforms](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.dataset.vision.html#mindspore-dataset-vision-c-transforms)\n",
+ "- [mindspore.dataset.vision.c_transforms](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.dataset.vision.html#mindspore-dataset-vision-c-transforms)\n",
"\n",
- "- [mindspore.dataset.vision.py_transforms](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.dataset.vision.html#mindspore-dataset-vision-py-transforms)\n",
+ "- [mindspore.dataset.vision.py_transforms](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.dataset.vision.html#mindspore-dataset-vision-py-transforms)\n",
"\n",
- "- [mindspore.dataset.text.transforms](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.dataset.text.html#mindspore-dataset-text-transforms)"
+ "- [mindspore.dataset.text.transforms](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.dataset.text.html#mindspore-dataset-text-transforms)"
],
"cell_type": "markdown",
"metadata": {}
@@ -617,7 +625,7 @@
"\n",
"(注:Eager模式混用`c_transforms`与`py_transforms`不受运行方式差异影响)\n",
"\n",
- "\n",
+ "\n",
"\n",
"混用会引发C++与Python切换的成本,建议不要过度混用两个模块的算子,但是适量混用是可以接受的。\n",
"\n",
@@ -625,21 +633,21 @@
"\n",
"- 单独使用`py_transform`或`c_transform`\n",
"\n",
- " \n",
+ " \n",
"\n",
"- 先使用`py_transform`,再使用`c_transform`\n",
"\n",
- " \n",
+ " \n",
"\n",
"- 先使用`c_transform`,再使用`py_transform`\n",
"\n",
- " \n",
+ " \n",
"\n",
"**不推荐的使用方式:**\n",
"\n",
"- 在两种transform之间频繁切换\n",
"\n",
- " \n",
+ " \n",
"\n",
"## 参考文献\n",
"\n",
diff --git a/docs/programming_guide/source_zh_cn/auto_augmentation.ipynb b/docs/programming_guide/source_zh_cn/auto_augmentation.ipynb
index dee684238689fd5afc408ba9ffff6805292e083f..59d934d7a87a1096ac2c8d3338d3382013f949e4 100644
--- a/docs/programming_guide/source_zh_cn/auto_augmentation.ipynb
+++ b/docs/programming_guide/source_zh_cn/auto_augmentation.ipynb
@@ -6,7 +6,7 @@
"source": [
"# 自动数据增强\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/auto_augmentation.ipynb)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/auto_augmentation.ipynb)"
]
},
{
@@ -28,7 +28,7 @@
"\n",
"MindSpore提供了一系列基于概率的自动数据增强API,用户可以对各种数据增强操作进行随机选择与组合,使数据增强更加灵活。\n",
"\n",
- "关于API的详细说明,可以参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.dataset.transforms.html)。\n",
+ "关于API的详细说明,可以参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.dataset.transforms.html)。\n",
"\n",
"### RandomApply\n",
"\n",
diff --git a/docs/programming_guide/source_zh_cn/auto_parallel.md b/docs/programming_guide/source_zh_cn/auto_parallel.md
index e1941e6144e3fe96bf25ea888625257d45605161..e43f58ea301952e4b77b53342b195d847b412d38 100644
--- a/docs/programming_guide/source_zh_cn/auto_parallel.md
+++ b/docs/programming_guide/source_zh_cn/auto_parallel.md
@@ -34,7 +34,7 @@
-
+
## 概述
@@ -104,7 +104,7 @@ context.get_auto_parallel_context("gradients_mean")
其中`auto_parallel`和`data_parallel`在MindSpore教程中有完整样例:
-。
+。
代码样例如下:
@@ -245,7 +245,7 @@ context.get_auto_parallel_context("pipeline_stage")
#### grad_accumulation_step
-`grad_accumulation_step`指梯度累积步数。具体用法请参考[指导教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/apply_gradient_accumulation.html)
+`grad_accumulation_step`指梯度累积步数。具体用法请参考[指导教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/apply_gradient_accumulation.html)
代码样例如下:
@@ -387,7 +387,7 @@ x = Parameter(Tensor(np.ones([2, 2])), layerwise_parallel=True)
具体用例请参考MindSpore分布式并行训练教程:
-。
+。
## 自动并行
@@ -395,4 +395,4 @@ x = Parameter(Tensor(np.ones([2, 2])), layerwise_parallel=True)
具体用例请参考MindSpore分布式并行训练教程:
-。
+。
diff --git a/docs/programming_guide/source_zh_cn/cache.md b/docs/programming_guide/source_zh_cn/cache.md
index d3fe5847854cd4b66d2260d42dabcd970390ff1e..185c7e6de69d2e51862848c5fab9119a56ad06be 100644
--- a/docs/programming_guide/source_zh_cn/cache.md
+++ b/docs/programming_guide/source_zh_cn/cache.md
@@ -11,7 +11,7 @@
-
+
## 概述
@@ -168,7 +168,7 @@
需要注意的是,两个例子均需要按照步骤4中的方法分别创建一个缓存实例,并在数据集加载或map算子中将所创建的`test_cache`作为`cache`参数分别传入。
- 下面两个样例中使用到CIFAR-10数据集。运行样例前,需参照[数据集加载](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/dataset_loading.html#cifar-10-100)中的方法下载并存放CIFAR-10数据集。
+ 下面两个样例中使用到CIFAR-10数据集。运行样例前,需参照[数据集加载](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/dataset_loading.html#cifar-10-100)中的方法下载并存放CIFAR-10数据集。
- 缓存原始数据集加载的数据。
@@ -327,11 +327,11 @@
done
```
- > 直接获取完整样例代码:[cache.sh](https://gitee.com/mindspore/docs/blob/master/tutorials/tutorial_code/cache/cache.sh)
+ > 直接获取完整样例代码:[cache.sh](https://gitee.com/mindspore/docs/blob/r1.2/tutorials/tutorial_code/cache/cache.sh)
4. 创建并应用缓存实例。
- 下面样例中使用到CIFAR-10数据集。运行样例前,需参照[数据集加载](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/dataset_loading.html#cifar-10-100)中的方法下载并存放CIFAR-10数据集。目录结构如下:
+ 下面样例中使用到CIFAR-10数据集。运行样例前,需参照[数据集加载](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/dataset_loading.html#cifar-10-100)中的方法下载并存放CIFAR-10数据集。目录结构如下:
```text
├─cache.sh
@@ -370,7 +370,7 @@
print("Got {} samples on device {}".format(num_iter, args_opt.device))
```
- > 直接获取完整样例代码:[my_training_script.py](https://gitee.com/mindspore/docs/blob/master/tutorials/tutorial_code/cache/my_training_script.py)
+ > 直接获取完整样例代码:[my_training_script.py](https://gitee.com/mindspore/docs/blob/r1.2/tutorials/tutorial_code/cache/my_training_script.py)
5. 运行训练脚本。
diff --git a/docs/programming_guide/source_zh_cn/callback.md b/docs/programming_guide/source_zh_cn/callback.md
index 15d1e9a391e00a18c0111030f2fd17457b39f4e4..9f0bef88190451bf02aa11d1216c0e002780613e 100644
--- a/docs/programming_guide/source_zh_cn/callback.md
+++ b/docs/programming_guide/source_zh_cn/callback.md
@@ -9,7 +9,7 @@
-
+
## 概述
@@ -23,19 +23,19 @@ Callback回调函数在MindSpore中被实现为一个类,Callback机制类似
与模型训练过程相结合,保存训练后的模型和网络参数,方便进行再推理或再训练。`ModelCheckpoint`一般与`CheckpointConfig`配合使用,`CheckpointConfig`是一个参数配置类,可自定义配置checkpoint的保存策略。
- 详细内容,请参考[Checkpoint官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/use/save_model.html)。
+ 详细内容,请参考[Checkpoint官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/use/save_model.html)。
- SummaryCollector
帮助收集一些常见信息,如loss、learning rate、计算图、参数权重等,方便用户将训练过程可视化和查看信息,并且可以允许summary操作从summary文件中收集数据。
- 详细内容,请参考[Summary官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/summary_record.html)。
+ 详细内容,请参考[Summary官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/summary_record.html)。
- LossMonitor
监控训练过程中的loss变化情况,当loss为NAN或INF时,提前终止训练。可以在日志中输出loss,方便用户查看。
- 详细内容,请参考[LossMonitor官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#mindsporecallback)。
+ 详细内容,请参考[LossMonitor官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/custom_debugging_info.html#mindsporecallback)。
- TimeMonitor
@@ -51,6 +51,6 @@ MindSpore不但有功能强大的内置回调函数,还可以支持用户自
2. 实现保存训练过程中精度最高的checkpoint文件,用户可以自定义在每一轮迭代后都保存当前精度最高的模型。
-详细内容,请参考[自定义Callback官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#id3)。
+详细内容,请参考[自定义Callback官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/custom_debugging_info.html#id3)。
根据教程,用户可以很容易实现具有其他功能的自定义回调函数,如实现在每一轮训练结束后都输出相应的详细训练信息,包括训练进度、训练轮次、训练名称、loss值等;如实现在loss或模型精度达到一定值后停止训练,用户可以设定loss或模型精度的阈值,当loss或模型精度达到该阈值后就提前终止训练等。
diff --git a/docs/programming_guide/source_zh_cn/cell.ipynb b/docs/programming_guide/source_zh_cn/cell.ipynb
index 6227e18e1e7ce4df8b5c8bf2635f0ce17d01a28a..3254279d3aea9b2e06b8f3dfaaa9d717d8dcd49c 100644
--- a/docs/programming_guide/source_zh_cn/cell.ipynb
+++ b/docs/programming_guide/source_zh_cn/cell.ipynb
@@ -6,7 +6,7 @@
"source": [
"# Cell构建及其子类\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/cell.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_cell.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9jZWxsLmlweW5i&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/cell.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_cell.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9jZWxsLmlweW5i&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -76,7 +76,7 @@
"\n",
"`parameters_dict`方法识别出网络结构中所有的参数,返回一个以key为参数名,value为参数值的`OrderedDict`。\n",
"\n",
- "`Cell`类中返回参数的方法还有许多,例如`get_parameters`、`trainable_params`等,具体使用方法可以参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/nn/mindspore.nn.Cell.html)。\n",
+ "`Cell`类中返回参数的方法还有许多,例如`get_parameters`、`trainable_params`等,具体使用方法可以参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/nn/mindspore.nn.Cell.html)。\n",
"\n",
"代码样例如下:"
]
@@ -494,7 +494,7 @@
"source": [
"## 优化算法\n",
"\n",
- "`mindspore.nn.optim`是MindSpore框架中实现各种优化算法的模块,详细说明参见[优化算法](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/optim.html)。"
+ "`mindspore.nn.optim`是MindSpore框架中实现各种优化算法的模块,详细说明参见[优化算法](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/optim.html)。"
]
},
{
diff --git a/docs/programming_guide/source_zh_cn/conf.py b/docs/programming_guide/source_zh_cn/conf.py
index 646636b16664cdd632f063f85b36361dc625e50d..adbf28ca537437b7cb6165286313131ba60879e0 100644
--- a/docs/programming_guide/source_zh_cn/conf.py
+++ b/docs/programming_guide/source_zh_cn/conf.py
@@ -73,11 +73,6 @@ html_search_options = {'dict': '../../resource/jieba.txt'}
html_static_path = ['_static']
-def setup(app):
- app.add_stylesheet('css/bootstrap.min.css')
- app.add_stylesheet('css/training.css')
- app.add_javascript('js/training.js')
-
# Remove extra outputs for nbsphinx extension.
nbsphinx_source_re = re.compile(r"(app\.connect\('html-collect-pages', html_collect_pages\))")
nbsphinx_math_re = re.compile(r"(\S.*$)")
diff --git a/docs/programming_guide/source_zh_cn/context.ipynb b/docs/programming_guide/source_zh_cn/context.ipynb
index 8fc730eaf0256b73e7ced332d45d6450cd292035..8a6e49d723f5015cd2c5153ebee532646954a66c 100644
--- a/docs/programming_guide/source_zh_cn/context.ipynb
+++ b/docs/programming_guide/source_zh_cn/context.ipynb
@@ -6,7 +6,7 @@
"source": [
"# 运行管理\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/context.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_context.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9jb250ZXh0LmlweW5i&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/context.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_context.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9jb250ZXh0LmlweW5i&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -147,9 +147,9 @@
"source": [
"## 分布式管理\n",
"\n",
- "context中有专门用于配置并行训练参数的接口:context.set_auto_parallel_context,该接口必须在初始化网络之前调用。\n",
+ "context中有专门用于配置并行训练参数的接口:`context.set_auto_parallel_context`,该接口必须在初始化网络之前调用。\n",
"\n",
- "> 分布式并行训练详细介绍可以查看[分布式并行训练](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/distributed_training_tutorials.html)。"
+ "> 分布式并行训练详细介绍可以查看[分布式并行训练](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/distributed_training_tutorials.html)。"
]
},
{
@@ -164,31 +164,31 @@
"\n",
"系统支持在训练过程中采集profiling数据,然后通过profiling工具进行性能分析。当前支持采集的profiling数据包括:\n",
"\n",
- "- `enable_profiling`:是否开启profiling功能。设置为True,表示开启profiling功能,从enable_options读取profiling的采集选项;设置为False,表示关闭profiling功能,仅采集training_trace。\n",
+ "- `enable_profiling`:是否开启profiling功能。设置为True,表示开启profiling功能,从`enable_options`读取profiling的采集选项;设置为False,表示关闭profiling功能,仅采集`training_trace`。\n",
"\n",
"- `profiling_options`:profiling采集选项,取值如下,支持采集多项数据。 \n",
- " - result_path: Profiling采集结果文件保存路径。该参数指定的目录需要在启动训练的环境上(容器或Host侧)提前创建且确保安装时配置的运行用户具有读写权限,支持配置绝对路径或相对路径(相对执行命令时的当前路径)。 \n",
- " - training_trace:采集迭代轨迹数据,即训练任务及AI软件栈的软件信息,实现对训练任务的性能分析,重点关注数据增强、前后向计算、梯度聚合更新等相关数据,取值on/off。 \n",
- " - task_trace:采集任务轨迹数据,即昇腾910处理器HWTS/AICore的硬件信息,分析任务开始、结束等信息,取值on/off。 \n",
- " - aicpu_trace: 采集aicpu数据增强的profiling数据。取值on/off。 \n",
- " - fp_point: training_trace为on时需要配置。指定训练网络迭代轨迹正向算子的开始位置,用于记录前向算子开始时间戳。配置值为指定的正向第一个算子名字。当该值为空时,系统自动获取正向第一个算子名字。 \n",
- " - bp_point: training_trace为on时需要配置。指定训练网络迭代轨迹反向算子的结束位置,用于记录反向算子结束时间戳。配置值为指定的反向最后一个算子名字。当该值为空时,系统自动获取反向最后一个算子名字。 \n",
- " - ai_core_metrics: 取值如下:\n",
+ " - `result_path`:Profiling采集结果文件保存路径。该参数指定的目录需要在启动训练的环境上(容器或Host侧)提前创建且确保安装时配置的运行用户具有读写权限,支持配置绝对路径或相对路径(相对执行命令时的当前路径)。 \n",
+ " - `training_trace`:采集迭代轨迹数据,即训练任务及AI软件栈的软件信息,实现对训练任务的性能分析,重点关注数据增强、前后向计算、梯度聚合更新等相关数据,取值on/off。 \n",
+ " - `task_trace`:采集任务轨迹数据,即昇腾910处理器HWTS/AICore的硬件信息,分析任务开始、结束等信息,取值on/off。 \n",
+ " - `aicpu_trace`:采集aicpu数据增强的profiling数据。取值on/off。 \n",
+ " - `fp_point`:`training_trace`为on时需要配置。指定训练网络迭代轨迹正向算子的开始位置,用于记录前向算子开始时间戳。配置值为指定的正向第一个算子名字。当该值为空时,系统自动获取正向第一个算子名字。 \n",
+ " - `bp_point`:`training_trace`为on时需要配置。指定训练网络迭代轨迹反向算子的结束位置,用于记录反向算子结束时间戳。配置值为指定的反向最后一个算子名字。当该值为空时,系统自动获取反向最后一个算子名字。 \n",
+ " - `ai_core_metrics`取值如下:\n",
"\n",
- " - ArithmeticUtilization: 各种计算类指标占比统计。\n",
+ " - ArithmeticUtilization:各种计算类指标占比统计。\n",
"\n",
- " - PipeUtilization: 计算单元和搬运单元耗时占比,该项为默认值。\n",
+ " - PipeUtilization:计算单元和搬运单元耗时占比,该项为默认值。\n",
"\n",
- " - Memory: 外部内存读写类指令占比。\n",
+ " - Memory:外部内存读写类指令占比。\n",
"\n",
- " - MemoryL0: 内部内存读写类指令占比。\n",
+ " - MemoryL0:内部内存读写类指令占比。\n",
"\n",
- " - ResourceConflictRatio: 流水线队列类指令占比。\n",
+ " - ResourceConflictRatio:流水线队列类指令占比。\n",
"\n",
"代码样例如下:\n",
"\n",
"```python\n",
- "ffrom mindspore import context\n",
+ "from mindspore import context\n",
"context.set_context(enable_profiling=True, profiling_options= '{\"result_path\":\"/home/data/output\",\"training_trace\":\"on\"}')\n",
"\n",
"```"
@@ -200,19 +200,25 @@
"source": [
"### 保存MindIR\n",
"\n",
- "通过context.set_context(save_graphs=True)来保存各个编译阶段的中间代码。\n",
+ "通过`context.set_context(save_graphs=True)`来保存各个编译阶段的中间代码。\n",
"\n",
"被保存的中间代码有两种格式:一个是后缀名为`.ir`的文本格式,一个是后缀名为`.dot`的图形化格式。\n",
"\n",
"当网络规模较大时建议使用更高效的文本格式来查看,当网络规模不大时,建议使用更直观的图形化格式来查看。\n",
"\n",
"代码样例如下:\n",
+ "\n",
"```python\n",
"from mindspore import context\n",
"context.set_context(save_graphs=True)\n",
- "```\n",
- "\n",
- "> MindIR详细介绍可以查看[MindSpore IR(MindIR)](https://www.mindspore.cn/doc/note/zh-CN/master/design/mindspore/mindir.html)。"
+ "```"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "> MindIR详细介绍可以查看[MindSpore IR(MindIR)](https://www.mindspore.cn/doc/note/zh-CN/r1.2/design/mindspore/mindir.html)。"
]
},
{
@@ -223,17 +229,23 @@
"\n",
"默认情况下,MindSpore的自研print算子可以将用户输入的Tensor或字符串信息打印出来,支持多字符串输入,多Tensor输入和字符串与Tensor的混合输入,输入参数以逗号隔开。\n",
"\n",
- "> Print打印功能可以查看[Print算子功能介绍](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_debugging_info.html#print)。\n",
+ "> Print打印功能可以查看[Print算子功能介绍](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/custom_debugging_info.html#print)。\n",
"\n",
"- `print_file_path`:可以将print算子数据保存到文件,同时关闭屏幕打印功能。如果保存的文件已经存在,则会给文件添加时间戳后缀。数据保存到文件可以解决数据量较大时屏幕打印数据丢失的问题。\n",
"\n",
"代码样例如下:\n",
+ "\n",
"```python\n",
"from mindspore import context\n",
"context.set_context(print_file_path=\"print.pb\")\n",
- "```\n",
- "\n",
- "> context接口详细介绍可以查看[mindspore.context](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.context.html)。"
+ "```"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "> context接口详细介绍可以查看[mindspore.context](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.context.html)。"
]
}
],
@@ -253,7 +265,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.7.6"
+ "version": "3.7.5"
}
},
"nbformat": 4,
diff --git a/docs/programming_guide/source_zh_cn/customized.rst b/docs/programming_guide/source_zh_cn/customized.rst
index 129b147956d9fc0e702dc68cc1e0add0f7e6d2d0..1cd69a4b33bbe47ab7e369a72c145105eb589296 100644
--- a/docs/programming_guide/source_zh_cn/customized.rst
+++ b/docs/programming_guide/source_zh_cn/customized.rst
@@ -4,6 +4,6 @@
.. toctree::
:maxdepth: 1
- 自定义算子(Ascend)
- 自定义算子(GPU)
- 自定义算子(CPU)
+ 自定义算子(Ascend)
+ 自定义算子(GPU)
+ 自定义算子(CPU)
diff --git a/docs/programming_guide/source_zh_cn/data_pipeline.rst b/docs/programming_guide/source_zh_cn/data_pipeline.rst
index 8bc54aba6fa12da23b0ba648e92e37a314a3dda4..1c173eee2b5bd9c4a266a374150016709e9d3916 100644
--- a/docs/programming_guide/source_zh_cn/data_pipeline.rst
+++ b/docs/programming_guide/source_zh_cn/data_pipeline.rst
@@ -11,4 +11,4 @@
tokenizer
dataset_conversion
auto_augmentation
- cache
\ No newline at end of file
+ cache
diff --git a/docs/programming_guide/source_zh_cn/dataset_conversion.ipynb b/docs/programming_guide/source_zh_cn/dataset_conversion.ipynb
index 18efa450d14ab08fc0f1fc07d38371bd8bf1745c..6b2156ef487b4477d9282a405b7cbe401b1a5f4b 100644
--- a/docs/programming_guide/source_zh_cn/dataset_conversion.ipynb
+++ b/docs/programming_guide/source_zh_cn/dataset_conversion.ipynb
@@ -6,7 +6,7 @@
"source": [
"# MindSpore数据格式转换\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/dataset_conversion.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_dataset_conversion.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9kYXRhc2V0X2NvbnZlcnNpb24uaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/dataset_conversion.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_dataset_conversion.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9kYXRhc2V0X2NvbnZlcnNpb24uaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -38,8 +38,7 @@
"ExecuteTime": {
"end_time": "2021-02-22T10:33:34.444561Z",
"start_time": "2021-02-22T10:33:34.441434Z"
- },
- "scrolled": true
+ }
},
"outputs": [],
"source": [
@@ -319,7 +318,7 @@
"| TFRecord | TFRecordToMR |\n",
"| CSV File | CsvToMR |\n",
"\n",
- "更多数据集转换的详细说明可参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.mindrecord.html)。"
+ "更多数据集转换的详细说明可参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.mindrecord.html)。"
]
},
{
@@ -342,7 +341,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "./datasets/cifar-10-batches-py/\n",
+ "./datasets/cifar-10-batches-py\n",
"├── batches.meta\n",
"├── data_batch_1\n",
"├── data_batch_2\n",
@@ -357,9 +356,10 @@
}
],
"source": [
- "!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/cifar10-py.zip\n",
- "!unzip -o cifar10-py.zip -d ./datasets\n",
- "!tree ./datasets/cifar-10-batches-py/"
+ "!wget -N https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/cifar-10-python.tar.gz\n",
+ "!mkdir -p datasets\n",
+ "!tar -xzf cifar-10-python.tar.gz -C datasets\n",
+ "!tree ./datasets/cifar-10-batches-py"
]
},
{
@@ -840,9 +840,9 @@
],
"metadata": {
"kernelspec": {
- "display_name": "MindSpore-1.0.1",
+ "display_name": "MindSpore-1.1.1",
"language": "python",
- "name": "mindspore-1.0.1"
+ "name": "mindspore-1.1.1"
},
"language_info": {
"codemirror_mode": {
diff --git a/docs/programming_guide/source_zh_cn/dataset_loading.ipynb b/docs/programming_guide/source_zh_cn/dataset_loading.ipynb
index 86f1f350b49466bf5a547156ea2885682e07dd33..daec29ccbdf09d02f7c09b3ebea8df37b1433a33 100644
--- a/docs/programming_guide/source_zh_cn/dataset_loading.ipynb
+++ b/docs/programming_guide/source_zh_cn/dataset_loading.ipynb
@@ -6,7 +6,7 @@
"source": [
"# 数据集加载\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/dataset_loading.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_dataset_loading.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9kYXRhc2V0X2xvYWRpbmcuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/dataset_loading.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_dataset_loading.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9kYXRhc2V0X2xvYWRpbmcuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -45,7 +45,7 @@
"| GeneratorDataset | 用户自定义的数据集读取、处理的方式。 |\n",
"| NumpySlicesDataset | 用户自定义的由NumPy构建数据集的方式。 |\n",
"\n",
- "> 更多详细的数据集加载接口说明,参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.dataset.html)。"
+ "> 更多详细的数据集加载接口说明,参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.dataset.html)。"
]
},
{
@@ -70,7 +70,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "./datasets/cifar10/\n",
+ "./datasets/cifar-10-batches-bin\n",
+ "├── readme.html\n",
"├── test\n",
"│ └── test_batch.bin\n",
"└── train\n",
@@ -81,14 +82,18 @@
" ├── data_batch_4.bin\n",
" └── data_batch_5.bin\n",
"\n",
- "2 directories, 7 files\n"
+ "2 directories, 8 files\n"
]
}
],
"source": [
- "!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/cifar10.zip\n",
- "!unzip -o ./cifar10.zip -d ./datasets/\n",
- "!tree ./datasets/cifar10/"
+ "!wget -N https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/cifar-10-binary.tar.gz\n",
+ "!mkdir -p datasets\n",
+ "!tar -xzf cifar-10-binary.tar.gz -C datasets\n",
+ "!mkdir -p datasets/cifar-10-batches-bin/train datasets/cifar-10-batches-bin/test\n",
+ "!mv -f datasets/cifar-10-batches-bin/test_batch.bin datasets/cifar-10-batches-bin/test\n",
+ "!mv -f datasets/cifar-10-batches-bin/data_batch*.bin datasets/cifar-10-batches-bin/batches.meta.txt datasets/cifar-10-batches-bin/train\n",
+ "!tree ./datasets/cifar-10-batches-bin"
]
},
{
@@ -120,7 +125,7 @@
"source": [
"import mindspore.dataset as ds\n",
"\n",
- "DATA_DIR = \"./datasets/cifar10/train/\"\n",
+ "DATA_DIR = \"./datasets/cifar-10-batches-bin/train/\"\n",
"\n",
"sampler = ds.SequentialSampler(num_samples=5)\n",
"dataset = ds.Cifar10Dataset(DATA_DIR, sampler=sampler)\n",
@@ -250,7 +255,7 @@
"\n",
"MindRecord是MindSpore定义的一种数据格式,使用MindRecord能够获得更好的性能提升。\n",
"\n",
- "> 阅读[数据格式转换](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/dataset_conversion.html)章节,了解如何将数据集转化为MindSpore数据格式。\n",
+ "> 阅读[数据格式转换](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/dataset_conversion.html)章节,了解如何将数据集转化为MindSpore数据格式。\n",
"\n",
"执行本例之前需下载对应的测试数据`test_mindrecord.zip`并解压到指定位置,执行如下命令:"
]
@@ -976,4 +981,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
-}
\ No newline at end of file
+}
diff --git a/docs/programming_guide/source_zh_cn/dtype.ipynb b/docs/programming_guide/source_zh_cn/dtype.ipynb
index f46818c5e357310e62920bf8158ad9a5e09ce1c7..ea3e4bfbfeadcaa25e6a964b55805ad4d48501bd 100644
--- a/docs/programming_guide/source_zh_cn/dtype.ipynb
+++ b/docs/programming_guide/source_zh_cn/dtype.ipynb
@@ -6,7 +6,7 @@
"source": [
"# dtype\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/dtype.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_dtype.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9kdHlwZS5pcHluYg==&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/dtype.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_dtype.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9kdHlwZS5pcHluYg==&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -19,7 +19,7 @@
"\n",
"在MindSpore的运算处理流程中,Python中的`int`数会被转换为定义的`int64`类型,`float`数会被转换为定义的`float32`类型。\n",
"\n",
- "详细的类型支持情况请参考:。\n",
+ "详细的类型支持情况请参考:。\n",
"\n",
"以下代码,打印MindSpore的数据类型int32。"
]
diff --git a/docs/programming_guide/source_zh_cn/infer.md b/docs/programming_guide/source_zh_cn/infer.md
index 6c200c9c47fe0177d59cd8b0d7ed8f6109957f1c..cb23f47cda0df17c2c33362d1b8d89c9724a7809 100644
--- a/docs/programming_guide/source_zh_cn/infer.md
+++ b/docs/programming_guide/source_zh_cn/infer.md
@@ -6,20 +6,20 @@
-
+
基于MindSpore训练后的模型,支持在Ascend 910 AI处理器、Ascend 310 AI处理器、GPU、CPU、端侧等多种不同的平台上执行推理。使用方法可参考如下教程:
-- [在Ascend 910 AI处理器上执行推理](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference_ascend_910.html)
-- [在Ascend 310 AI处理器上执行推理](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference_ascend_310.html)
-- [在GPU上执行推理](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference_gpu.html)
-- [在CPU上执行推理](https://www.mindspore.cn/tutorial/inference/zh-CN/master/multi_platform_inference_cpu.html)
-- [在端侧执行推理](https://www.mindspore.cn/tutorial/lite/zh-CN/master/quick_start/quick_start.html)
+- [在Ascend 910 AI处理器上执行推理](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference_ascend_910.html)
+- [在Ascend 310 AI处理器上执行推理](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference_ascend_310.html)
+- [在GPU上执行推理](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference_gpu.html)
+- [在CPU上执行推理](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/multi_platform_inference_cpu.html)
+- [在端侧执行推理](https://www.mindspore.cn/tutorial/lite/zh-CN/r1.2/quick_start/quick_start.html)
同时,MindSpore提供了一个轻量级、高性能的服务模块,称为MindSpore Serving,可帮助MindSpore开发者在生产环境中高效部署在线推理服务。使用方法可参考如下教程:
-- [部署推理服务](https://www.mindspore.cn/tutorial/inference/zh-CN/master/serving_example.html)
-- [基于MindSpore Serving部署分布式推理服务](https://www.mindspore.cn/tutorial/inference/zh-CN/master/serving_distributed_example.html)
-- [基于gRPC接口访问MindSpore Serving服务](https://www.mindspore.cn/tutorial/inference/zh-CN/master/serving_grpc.html)
-- [基于RESTful接口访问MindSpore Serving服务](https://www.mindspore.cn/tutorial/inference/zh-CN/master/serving_restful.html)
-- [通过配置模型提供Servable](https://www.mindspore.cn/tutorial/inference/zh-CN/master/serving_model.html)
+- [部署推理服务](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/serving_example.html)
+- [基于MindSpore Serving部署分布式推理服务](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/serving_distributed_example.html)
+- [基于gRPC接口访问MindSpore Serving服务](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/serving_grpc.html)
+- [基于RESTful接口访问MindSpore Serving服务](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/serving_restful.html)
+- [通过配置模型提供Servable](https://www.mindspore.cn/tutorial/inference/zh-CN/r1.2/serving_model.html)
diff --git a/docs/programming_guide/source_zh_cn/network_component.ipynb b/docs/programming_guide/source_zh_cn/network_component.ipynb
index b37b2ddc926f18396896e7e3e7a1e8e76ec225cf..11cadca5a9621a0cabf85c3d05c1b6e90e2bc443 100644
--- a/docs/programming_guide/source_zh_cn/network_component.ipynb
+++ b/docs/programming_guide/source_zh_cn/network_component.ipynb
@@ -6,7 +6,7 @@
"source": [
"# 常用网络组件\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/network_component.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_network_component.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9uZXR3b3JrX2NvbXBvbmVudC5pcHluYg==&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/network_component.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_network_component.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9uZXR3b3JrX2NvbXBvbmVudC5pcHluYg==&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -28,7 +28,7 @@
"source": [
"## GradOperation\n",
"\n",
- "GradOperation组件用于生成输入函数的梯度,利用`get_all`、`get_by_list`和`sens_param`参数控制梯度的计算方式,细节内容详见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/ops/mindspore.ops.GradOperation.html)。\n",
+ "GradOperation组件用于生成输入函数的梯度,利用`get_all`、`get_by_list`和`sens_param`参数控制梯度的计算方式,细节内容详见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/ops/mindspore.ops.GradOperation.html)。\n",
"\n",
"GradOperation的使用实例如下:"
]
diff --git a/docs/programming_guide/source_zh_cn/network_list.rst b/docs/programming_guide/source_zh_cn/network_list.rst
index 0086283c5f999b6131593dd0be63ce852df01927..0ef5c8b057dc3c685a273f928d92b555117ee5ef 100644
--- a/docs/programming_guide/source_zh_cn/network_list.rst
+++ b/docs/programming_guide/source_zh_cn/network_list.rst
@@ -4,4 +4,4 @@
.. toctree::
:maxdepth: 1
- MindSpore网络支持
\ No newline at end of file
+ MindSpore网络支持
\ No newline at end of file
diff --git a/docs/programming_guide/source_zh_cn/numpy.md b/docs/programming_guide/source_zh_cn/numpy.md
index 51b2193677f1bc2b222bc83f4647e22f74f5fd36..27113dc358dbfcef2be0360eefe96ce91517925d 100644
--- a/docs/programming_guide/source_zh_cn/numpy.md
+++ b/docs/programming_guide/source_zh_cn/numpy.md
@@ -27,7 +27,7 @@
-
+
## 概述
@@ -35,7 +35,7 @@ MindSpore NumPy工具包提供了一系列类NumPy接口。用户可以使用类
## 算子介绍
-MindSpore Numpy具有四大功能模块:张量生成、张量操作、逻辑运算和其他常用数学运算。算子的具体相关信息可以参考[NumPy接口列表](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.numpy.html)。
+MindSpore Numpy具有四大功能模块:张量生成、张量操作、逻辑运算和其他常用数学运算。算子的具体相关信息可以参考[NumPy接口列表](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.numpy.html)。
### 张量生成
@@ -366,7 +366,7 @@ 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?highlight=ms_function#mindspore.ms_function)。
+> 目前静态图不支持在命令行模式中运行,并且有部分语法限制。`ms_function`的更多信息可参考[API: ms_function](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.html#mindspore.ms_function)。
### GradOperation使用示例
@@ -390,7 +390,7 @@ 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)。
+ 更多细节可参考[API: GradOperation](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/ops/mindspore.ops.GradOperation.html)。
### mindspore.context使用示例
@@ -416,7 +416,7 @@ context.set_context(device_target="Ascend")
...
```
- 更多细节可参考[API: mindspore.context](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.context.html)。
+ 更多细节可参考[API: mindspore.context](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.context.html)。
### mindspore.numpy使用示例
@@ -461,4 +461,4 @@ print(net(x, w1, b1, w2, b2, w3, b3))
[2816. 2816. 2816. 2816.]]
```
-更多构建网络的细节可以参考[MindSpore训练指导](https://www.mindspore.cn/tutorial/training/zh-CN/master/index.html)。
+更多构建网络的细节可以参考[MindSpore训练指导](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/index.html)。
diff --git a/docs/programming_guide/source_zh_cn/operator_list.rst b/docs/programming_guide/source_zh_cn/operator_list.rst
index 4f2daa0ae22d8ec80280754c66d95ce938a36450..dd53cbfa2fb9feb7122314e00bdf628b3d4a57cf 100644
--- a/docs/programming_guide/source_zh_cn/operator_list.rst
+++ b/docs/programming_guide/source_zh_cn/operator_list.rst
@@ -4,6 +4,6 @@
.. toctree::
:maxdepth: 1
- MindSpore算子支持
- MindSpore隐式类型转换的算子支持
- MindSpore分布式算子支持
\ No newline at end of file
+ MindSpore算子支持
+ MindSpore隐式类型转换的算子支持
+ MindSpore分布式算子支持
\ No newline at end of file
diff --git a/docs/programming_guide/source_zh_cn/operators.ipynb b/docs/programming_guide/source_zh_cn/operators.ipynb
index d0fe36ca0533d7749a8d2df36fbc15d115695f56..4ff69909516d0a7c9e31d97139a24efe985a6087 100644
--- a/docs/programming_guide/source_zh_cn/operators.ipynb
+++ b/docs/programming_guide/source_zh_cn/operators.ipynb
@@ -6,7 +6,7 @@
"source": [
"# 算子\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/operators.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_operators.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9vcGVyYXRvcnMuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/operators.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_operators.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9vcGVyYXRvcnMuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -29,7 +29,7 @@
"\n",
"### mindspore.ops.operations\n",
"\n",
- "operations提供了所有的Primitive算子接口,是开放给用户的最低阶算子接口。算子支持情况可查询[算子支持列表](https://www.mindspore.cn/doc/note/zh-CN/master/operator_list.html)。\n",
+ "operations提供了所有的Primitive算子接口,是开放给用户的最低阶算子接口。算子支持情况可查询[算子支持列表](https://www.mindspore.cn/doc/note/zh-CN/r1.2/operator_list.html)。\n",
"\n",
"Primitive算子也称为算子原语,它直接封装了底层的Ascend、GPU、AICPU、CPU等多种算子的具体实现,为用户提供基础算子能力。\n",
"\n",
@@ -70,7 +70,7 @@
"source": [
"### mindspore.ops.functional\n",
"\n",
- "为了简化没有属性的算子的调用流程,MindSpore提供了一些算子的functional版本。入参要求参考原算子的输入输出要求。算子支持情况可以查询[算子支持列表](https://www.mindspore.cn/doc/note/zh-CN/master/operator_list_ms.html#mindspore-ops-functional)。\n",
+ "为了简化没有属性的算子的调用流程,MindSpore提供了一些算子的functional版本。入参要求参考原算子的输入输出要求。算子支持情况可以查询[算子支持列表](https://www.mindspore.cn/doc/note/zh-CN/r1.2/operator_list_ms.html#mindspore-ops-functional)。\n",
"\n",
"例如`P.Pow`算子,我们提供了functional版本的`F.tensor_pow`算子。\n",
"\n",
@@ -189,7 +189,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "此外,高阶函数`GradOperation`提供了根据输入的函数,求这个函数对应的梯度函数的方式,详细可以参阅[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/ops/mindspore.ops.GradOperation.html)。\n",
+ "此外,高阶函数`GradOperation`提供了根据输入的函数,求这个函数对应的梯度函数的方式,详细可以参阅[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/ops/mindspore.ops.GradOperation.html)。\n",
"\n",
"### operations/functional/composite三类算子合并用法\n",
"\n",
@@ -231,7 +231,7 @@
"source": [
"## 算子功能\n",
"\n",
- "算子按功能可分为张量操作、网络操作、数组操作、图像操作、编码操作、调试操作和量化操作七个功能模块。所有的算子在Ascend AI处理器、GPU和CPU的支持情况,参见[算子支持列表](https://www.mindspore.cn/doc/note/zh-CN/master/operator_list.html)。\n",
+ "算子按功能可分为张量操作、网络操作、数组操作、图像操作、编码操作、调试操作和量化操作七个功能模块。所有的算子在Ascend AI处理器、GPU和CPU的支持情况,参见[算子支持列表](https://www.mindspore.cn/doc/note/zh-CN/r1.2/operator_list.html)。\n",
"\n",
"### 张量操作\n",
"\n",
@@ -891,8 +891,13 @@
"[ 1.12317121e+00 8.98950577e-01 4.22795087e-01]\n",
"[ 5.13781667e-01 5.12095273e-01 -3.68211865e-01]\n",
"[-7.04941899e-02 -1.09924078e+00 6.89047515e-01]]]]\n",
- "```\n",
- "\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
"> 以上代码运行于MindSpore的Ascend版本。"
]
},
@@ -1069,9 +1074,9 @@
],
"metadata": {
"kernelspec": {
- "display_name": "MindSpore-1.1.1",
+ "display_name": "Python 3",
"language": "python",
- "name": "mindspore-1.1.1"
+ "name": "python3"
},
"language_info": {
"codemirror_mode": {
@@ -1083,7 +1088,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.7.5"
+ "version": "3.7.6"
}
},
"nbformat": 4,
diff --git a/docs/programming_guide/source_zh_cn/optim.ipynb b/docs/programming_guide/source_zh_cn/optim.ipynb
new file mode 100644
index 0000000000000000000000000000000000000000..830af1bcab290efd119db2d0debac1f8e72f3064
--- /dev/null
+++ b/docs/programming_guide/source_zh_cn/optim.ipynb
@@ -0,0 +1,271 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 优化算法\n",
+ "\n",
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/optim.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_optim.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9vcHRpbS5pcHluYg==&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 概述\n",
+ "\n",
+ "`mindspore.nn.optim`是MindSpore框架中实现各种优化算法的模块,包含常用的优化器、学习率等,并且接口具备足够的通用性,可以将以后更新、更复杂的方法集成到模块里。\n",
+ "\n",
+ "`mindspore.nn.optim`为模型提供常用的优化器,如`SGD`、`ADAM`、`Momentum`。优化器用于计算和更新梯度,模型优化算法的选择直接关系到最终模型的性能,如果有时候效果不好,未必是特征或者模型设计的问题,很有可能是优化算法的问题;同时还有`mindspore.nn`提供的学习率的模块,学习率分为`dynamic_lr`和`learning_rate_schedule`,都是动态学习率,但是实现方式不同,学习率是监督学习以及深度学习中最为重要的参数,其决定着目标函数是否能收敛到局部最小值以及何时能收敛到最小值。合适的学习率能够使目标函数在合适的的时间内收敛到局部最小值。\n",
+ "\n",
+ "> 本文档适用于CPU、GPU和Ascend环境。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 学习率\n",
+ "\n",
+ "### dynamic_lr\n",
+ "\n",
+ "`mindspore.nn.dynamic_lr`模块有以下几个类:\n",
+ "\n",
+ "- `piecewise_constant_lr`类:基于得到分段不变的学习速率。\n",
+ "\n",
+ "- `exponential_decay_lr`类:基于指数衰减函数计算学习率。\n",
+ "\n",
+ "- `natural_exp_decay_lr`类:基于自然指数衰减函数计算学习率。\n",
+ "\n",
+ "- `inverse_decay_lr`类:基于反时间衰减函数计算学习速率。\n",
+ "\n",
+ "- `cosine_decay_lr`类:基于余弦衰减函数计算学习率。\n",
+ "\n",
+ "- `polynomial_decay_lr`类:基于多项式衰减函数计算学习率。\n",
+ "\n",
+ "- `warmup_lr`类:提高学习率。\n",
+ "\n",
+ "它们是属于`dynamic_lr`的不同实现方式。\n",
+ "\n",
+ "例如`piecewise_constant_lr`类代码样例如下:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[0.1, 0.1, 0.05, 0.05, 0.05, 0.01, 0.01, 0.01, 0.01, 0.01]\n"
+ ]
+ }
+ ],
+ "source": [
+ "from mindspore.nn.dynamic_lr import piecewise_constant_lr\n",
+ "\n",
+ "def test_dynamic_lr():\n",
+ " milestone = [2, 5, 10]\n",
+ " learning_rates = [0.1, 0.05, 0.01]\n",
+ " lr = piecewise_constant_lr(milestone, learning_rates)\n",
+ " print(lr)\n",
+ "\n",
+ "if __name__ == '__main__':\n",
+ " test_dynamic_lr()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### learning_rate_schedule\n",
+ "\n",
+ "`mindspore.nn.learning_rate_schedule`模块下有以下几个类:`ExponentialDecayLR`类、`NaturalExpDecayLR`类、`InverseDecayLR`类、`CosineDecayLR`类、`PolynomialDecayLR`类和`WarmUpLR`类。它们都属于`learning_rate_schedule`,只是实现方式不同,各自含义如下:\n",
+ "\n",
+ "- `ExponentialDecayLR`类:基于指数衰减函数计算学习率。\n",
+ "\n",
+ "- `NaturalExpDecayLR`类:基于自然指数衰减函数计算学习率。\n",
+ "\n",
+ "- `InverseDecayLR`类:基于反时间衰减函数计算学习速率。\n",
+ "\n",
+ "- `CosineDecayLR`类:基于余弦衰减函数计算学习率。\n",
+ "\n",
+ "- `PolynomialDecayLR`类:基于多项式衰减函数计算学习率。\n",
+ "\n",
+ "- `WarmUpLR`类:提高学习率。\n",
+ "\n",
+ "它们是属于`learning_rate_schedule`的不同实现方式。\n",
+ "\n",
+ "例如`ExponentialDecayLR`类代码样例如下:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "0.094868325\n"
+ ]
+ }
+ ],
+ "source": [
+ "from mindspore.common import dtype as mstype\n",
+ "from mindspore import Tensor\n",
+ "from mindspore.nn.learning_rate_schedule import ExponentialDecayLR\n",
+ "\n",
+ "def test_learning_rate_schedule():\n",
+ " learning_rate = 0.1 # learning_rate(float) - The initial value of learning rate.\n",
+ " decay_rate = 0.9 # decay_rate(float) - The decay rate.\n",
+ " decay_steps = 4 # decay_steps(int) - A value used to calculate decayed learning rate.\n",
+ " global_step = Tensor(2, mstype.int32)\n",
+ " exponential_decay_lr = ExponentialDecayLR(learning_rate, decay_rate, decay_steps)\n",
+ " res = exponential_decay_lr(global_step)\n",
+ " print(res)\n",
+ "\n",
+ "\n",
+ "if __name__ == '__main__':\n",
+ " test_learning_rate_schedule()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Optimzer\n",
+ "\n",
+ "### 如何使用\n",
+ "\n",
+ "为了使用`mindspore.nn.optim`,我们需要构建一个`Optimizer`对象。这个对象能够保持当前参数状态并基于计算得到的梯度进行参数更新。\n",
+ "\n",
+ "- 构建\n",
+ "\n",
+ "为了构建一个`Optimizer`,我们需要给它一个包含可需要优化的参数(必须是Variable对象)的iterable。然后,你可以设置Optimizer的参数选项,比如学习率,权重衰减等等。\n",
+ "\n",
+ "代码样例如下:\n",
+ "\n",
+ "```python\n",
+ "from mindspore import nn\n",
+ "\n",
+ "optim = nn.SGD(group_params, learning_rate=0.1, weight_decay=0.0)\n",
+ "optim = nn.Adam(params=net.trainable_params())\n",
+ "\n",
+ "optim = nn.Adam(group_params, learning_rate=0.1, weight_decay=0.0)\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "- 为每一个参数单独设置选项\n",
+ "\n",
+ "优化器也支持为每个参数单独设置选项。若想这么做,不要直接传入变量Variable,而是传入一个字典的iterable。每一个字典都分别定义了一组参数,并且包含一个key键,这个key键对应相应的参数value值。其他的key键应该是优化器所接受的其他参数,并且会被用于对这组参数的优化。\n",
+ "\n",
+ "我们仍然能够传递选项作为关键字参数,在未重写这些选项的组中,它们会被用作默认值。当你只想改动一个参数组的选项,但其他参数组的选项不变时,这是非常有用的。\n",
+ "\n",
+ "例如,当我们想制定每一层的学习率时,以`SGD`为例:\n",
+ "\n",
+ "```python\n",
+ "from mindspore import nn\n",
+ "\n",
+ "optim = nn.SGD([{'params': conv_params, 'weight_decay': 0.01},\n",
+ " {'params': no_conv_params, 'lr': 0.01},\n",
+ " {'order_params': net.trainable_params()}],\n",
+ " learning_rate=0.1, weight_decay=0.0)\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "这段示例意味着当参数是`conv_params`时候,权重衰减使用的是0.01,学习率使用的是0.1;而参数是`no_conv_params`时候,权重衰减使用的是0.0,学习率使用的是0.01。这个学习率`learning_rate=0.1`会被用于所有分组里没有设置学习率的参数,权重衰减`weight_decay`也是如此。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 内置优化器\n",
+ "\n",
+ "深度学习优化算法大概常用的有`SGD`、`Adam`、`Ftrl`、`lazyadam`、`Momentum`、`RMSprop`、`Lars`、`Proximal_ada_grad`和`lamb`这几种。\n",
+ "\n",
+ "在`mindspore.nn.optim`模块中,他们都有对应的类实现。例如:\n",
+ "\n",
+ "- `SGD`,默认参数为纯SGD,设置`momentum`参数不为0,考虑了一阶动量,设置`nesterov`为True后变成`NAG`,即`Nesterov Accelerated Gradient`,在计算梯度时计算的是向前走一步所在位置的梯度。\n",
+ "\n",
+ "- `RMSprop`,考虑了二阶动量,对于不同的参数有不同的学习率,即自适应学习率,对`Adagrad`进行了优化,通过指数平滑只考虑一定窗口内的二阶动量。\n",
+ "\n",
+ "- `Adam`,同时考虑了一阶动量和二阶动量,可以看成`RMSprop`上进一步考虑了一阶动量。\n",
+ "\n",
+ "例如`SGD`的代码样例如下:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from mindspore import nn, Model, Tensor\n",
+ "import mindspore.ops as ops\n",
+ "import numpy as np\n",
+ "from mindspore import dtype as mstype\n",
+ "from mindspore import Parameter\n",
+ "\n",
+ "class Net(nn.Cell):\n",
+ " def __init__(self):\n",
+ " super(Net, self).__init__()\n",
+ " self.matmul = ops.MatMul()\n",
+ " self.conv = nn.Conv2d(1, 6, 5, pad_mode=\"valid\")\n",
+ " self.z = Parameter(Tensor(np.array([1.0], np.float32)), name='z')\n",
+ " \n",
+ " def construct(self, x, y):\n",
+ " x = x * self.z\n",
+ " out = self.matmul(x, y)\n",
+ " return out\n",
+ "\n",
+ "net = Net()\n",
+ "optim = nn.SGD(params=net.trainable_params())\n",
+ "\n",
+ "conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))\n",
+ "no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))\n",
+ "group_params = [{'params': conv_params, 'weight_decay': 0.01},\n",
+ " {'params': no_conv_params, 'lr': 0.01},\n",
+ " {'order_params': net.trainable_params()}]\n",
+ "optim = nn.SGD(group_params, learning_rate=0.1, weight_decay=0.0)\n",
+ "\n",
+ "loss = nn.SoftmaxCrossEntropyWithLogits()\n",
+ "model = Model(net, loss_fn=loss, optimizer=optim)"
+ ]
+ }
+ ],
+ "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": 4
+}
diff --git a/docs/programming_guide/source_zh_cn/optim.md b/docs/programming_guide/source_zh_cn/optim.md
deleted file mode 100644
index 40023f29d9ac06420895d6a34b1a4a83a564cf32..0000000000000000000000000000000000000000
--- a/docs/programming_guide/source_zh_cn/optim.md
+++ /dev/null
@@ -1,194 +0,0 @@
-# 优化算法
-
-
-
-- [优化算法](#优化算法)
- - [概述](#概述)
- - [学习率](#学习率)
- - [dynamic_lr](#dynamic_lr)
- - [learning_rate_schedule](#learning_rate_schedule)
- - [Optimzer](#optimzer)
- - [如何使用](#如何使用)
- - [内置优化器](#内置优化器)
-
-
-
-
-
-
-
-
-
-## 概述
-
-`mindspore.nn.optim`是MindSpore框架中实现各种优化算法的模块,包含常用的优化器、学习率等,并且接口具备足够的通用性,可以将以后更新、更复杂的方法集成到模块里。
-
-`mindspore.nn.optim`为模型提供常用的优化器,如`SGD`、`ADAM`、`Momentum`。优化器用于计算和更新梯度,模型优化算法的选择直接关系到最终模型的性能,如果有时候效果不好,未必是特征或者模型设计的问题,很有可能是优化算法的问题;同时还有`mindspore.nn`提供的学习率的模块,学习率分为`dynamic_lr`和`learning_rate_schedule`,都是动态学习率,但是实现方式不同,学习率是监督学习以及深度学习中最为重要的参数,其决定着目标函数是否能收敛到局部最小值以及何时能收敛到最小值。合适的学习率能够使目标函数在合适的时间内收敛到局部最小值。
-
-> 本文档中的所有示例,支持CPU,GPU,Ascend环境。
-
-## 学习率
-
-### dynamic_lr
-
-`mindspore.nn.dynamic_lr`模块有以下几个类:
-
-- `piecewise_constant_lr`类:基于得到分段不变的学习速率。
-- `exponential_decay_lr`类:基于指数衰减函数计算学习率。
-- `natural_exp_decay_lr`类:基于自然指数衰减函数计算学习率。
-- `inverse_decay_lr`类:基于反时间衰减函数计算学习速率。
-- `cosine_decay_lr`类:基于余弦衰减函数计算学习率。
-- `polynomial_decay_lr`类:基于多项式衰减函数计算学习率。
-- `warmup_lr`类:提高学习率。
-
-它们是属于`dynamic_lr`的不同实现方式。
-
-例如`piecewise_constant_lr`类代码样例如下:
-
-```python
-from mindspore.nn.dynamic_lr import piecewise_constant_lr
-
-def test_dynamic_lr():
- milestone = [2, 5, 10]
- learning_rates = [0.1, 0.05, 0.01]
- lr = piecewise_constant_lr(milestone, learning_rates)
- print(lr)
-
-
-if __name__ == '__main__':
- test_dynamic_lr()
-```
-
-返回结果如下:
-
-```text
-[0.1, 0.1, 0.05, 0.05, 0.05, 0.01, 0.01, 0.01, 0.01, 0.01]
-```
-
-### learning_rate_schedule
-
-`mindspore.nn.learning_rate_schedule`模块下有以下几个类:`ExponentialDecayLR`类、`NaturalExpDecayLR`类、`InverseDecayLR`类、`CosineDecayLR`类、`PolynomialDecayLR`类和`WarmUpLR`类。它们都属于`learning_rate_schedule`,只是实现方式不同,各自含义如下:
-
-- `ExponentialDecayLR`类:基于指数衰减函数计算学习率。
-- `NaturalExpDecayLR`类:基于自然指数衰减函数计算学习率。
-- `InverseDecayLR`类:基于反时间衰减函数计算学习速率。
-- `CosineDecayLR`类:基于余弦衰减函数计算学习率。
-- `PolynomialDecayLR`类:基于多项式衰减函数计算学习率。
-- `WarmUpLR`类:提高学习率。
-
-它们是属于`learning_rate_schedule`的不同实现方式。
-
-例如ExponentialDecayLR类代码样例如下:
-
-```python
-from mindspore import dtype as mstype
-from mindspore import Tensor
-from mindspore.nn.learning_rate_schedule import ExponentialDecayLR
-
-def test_learning_rate_schedule():
- learning_rate = 0.1 # learning_rate(float) - The initial value of learning rate.
- decay_rate = 0.9 # decay_rate(float) - The decay rate.
- decay_steps = 4 # decay_steps(int) - A value used to calculate decayed learning rate.
- global_step = Tensor(2, mstype.int32)
- exponential_decay_lr = ExponentialDecayLR(learning_rate, decay_rate, decay_steps)
- res = exponential_decay_lr(global_step)
- print(res)
-
-
-if __name__ == '__main__':
- test_learning_rate_schedule()
-```
-
-返回结果如下:
-
-```text
-0.094868325
-```
-
-## Optimzer
-
-### 如何使用
-
-为了使用`mindspore.nn.optim`,我们需要构建一个`Optimizer`对象。这个对象能够保持当前参数状态并基于计算得到的梯度进行参数更新。
-
-- 构建
-
-为了构建一个`Optimizer`,我们需要给它一个包含可需要优化的参数(必须是Variable对象)的iterable。然后,你可以设置Optimizer的参数选项,比如学习率,权重衰减等等。
-
-代码样例如下:
-
-```python
-from mindspore import nn
-
-optim = nn.SGD(group_params, learning_rate=0.1, weight_decay=0.0)
-optim = nn.Adam(params=net.trainable_params())
-
-optim = nn.Adam(group_params, learning_rate=0.1, weight_decay=0.0)
-
-```
-
-- 为每一个参数单独设置选项
-
-优化器也支持为每个参数单独设置选项。若想这么做,不要直接传入变量Variable,而是传入一个字典的iterable。每一个字典都分别定义了一组参数,并且包含一个key键,这个key键对应相应的参数value值。其他的key键应该是优化器所接受的其他参数,并且会被用于对这组参数的优化。
-
-我们仍然能够传递选项作为关键字参数,在未重写这些选项的组中,它们会被用作默认值。当你只想改动一个参数组的选项,但其他参数组的选项不变时,这是非常有用的。
-例如,当我们想制定每一层的学习率时,以`SGD`为例:
-
-```python
-from mindspore import nn
-
-optim = nn.SGD([{'params': conv_params, 'weight_decay': 0.01},
- {'params': no_conv_params, 'lr': 0.01},
- {'order_params': net.trainable_params()}],
- learning_rate=0.1, weight_decay=0.0)
-
-```
-
-这段示例意味着当参数是conv_params时候,权重衰减使用的是0.01,学习率使用的是0.1;而参数是no_conv_params时候,权重衰减使用的是0.0,学习率使用的是0.01。这个学习率learning_rate=0.1会被用于所有分组里没有设置学习率的参数,权重衰减weight_deca也是如此。
-
-### 内置优化器
-
-深度学习优化算法大概常用的有`SGD`、`Adam`、`Ftrl`、`lazyadam`、`Momentum`、`RMSprop`、`Lars`、`Proximal_ada_grad`和`lamb`这几种。
-在`mindspore.nn.optim`模块中,他们都有对应的类实现。例如:
-
-- `SGD`,默认参数为纯SGD,设置`momentum`参数不为0,考虑了一阶动量,设置`nesterov`为True后变成`NAG`,即`Nesterov Accelerated Gradient`,在计算梯度时计算的是向前走一步所在位置的梯度。
-
-- `RMSprop`,考虑了二阶动量,对于不同的参数有不同的学习率,即自适应学习率,对`Adagrad`进行了优化,通过指数平滑只考虑一定窗口内的二阶动量。
-
-- `Adam`,同时考虑了一阶动量和二阶动量,可以看成`RMSprop`上进一步考虑了一阶动量。
-
-例如`SGD`的代码样例如下:
-
-```python
-from mindspore import nn, Tensor, Model
-import mindspore.ops as ops
-import numpy as np
-from mindspore import dtype as mstype
-from mindspore import Parameter
-
-class Net(nn.Cell):
- def __init__(self):
- super(Net, self).__init__()
- self.matmul = ops.MatMul()
- self.conv = nn.Conv2d(1, 6, 5, pad_mode='valid')
- self.z = Parameter(Tensor(np.array([1.0], np.float32)))
-
- def construct(self, x, y):
- x = x * self.z
- out = self.matmul(x, y)
- return out
-
-net = Net()
-optim = nn.SGD(params=net.trainable_params())
-
-conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
-no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
-group_params = [{'params': conv_params, 'weight_decay': 0.01},
- {'params': no_conv_params, 'lr': 0.01},
- {'order_params': net.trainable_params()}]
-optim = nn.SGD(group_params, learning_rate=0.1, weight_decay=0.0)
-
-loss = nn.SoftmaxCrossEntropyWithLogits()
-model = Model(net, loss_fn=loss, optimizer=optim)
-
-```
diff --git a/docs/programming_guide/source_zh_cn/parameter.ipynb b/docs/programming_guide/source_zh_cn/parameter.ipynb
index 293a34e4ad852e0ac6a16c1d05800a2485e5cac4..da3fac279a2c2bba9ccbe9b48a50da3cc82e70f1 100644
--- a/docs/programming_guide/source_zh_cn/parameter.ipynb
+++ b/docs/programming_guide/source_zh_cn/parameter.ipynb
@@ -6,7 +6,7 @@
"source": [
"# Parameter\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/parameter.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_parameter.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9wYXJhbWV0ZXIuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/parameter.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_parameter.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9wYXJhbWV0ZXIuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -40,7 +40,7 @@
"\n",
"当`layerwise_parallel`(混合并行)配置为`True`时,参数广播和参数梯度聚合时会过滤掉该参数。\n",
"\n",
- "有关分布式并行的相关配置,可以参考文档:https://www.mindspore.cn/doc/programming_guide/zh-CN/master/auto_parallel.html 。\n",
+ "有关分布式并行的相关配置,可以参考文档:https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/auto_parallel.html 。\n",
"\n",
"下例通过三种不同的数据类型构造了`Parameter`,三个`Parameter`都需要更新,都不采用layerwise并行。如下:"
]
@@ -156,7 +156,7 @@
"\n",
"- `set_data`:设置`Parameter`保存的数据,支持传入`Tensor`、`Initializer`、`int`和`float`进行设置, 将方法的入参`slice_shape`设置为True时,可改变`Parameter`的shape,反之,设置的数据shape必须与`Parameter`原来的shape保持一致。\n",
"\n",
- "- `set_param_ps`:控制训练参数是否通过[Parameter Server](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/apply_parameter_server_training.html)进行训练。\n",
+ "- `set_param_ps`:控制训练参数是否通过[Parameter Server](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/apply_parameter_server_training.html)进行训练。\n",
"\n",
"- `clone`:克隆`Parameter`,克隆完成后可以给新Parameter指定新的名字。\n",
"\n",
diff --git a/docs/programming_guide/source_zh_cn/performance_optimization.md b/docs/programming_guide/source_zh_cn/performance_optimization.md
index 06f4ba13cb2d7ab534b473581d15370dc276d41c..f58906a54c2d15ed409df3cec9eeba0a0c7651ac 100644
--- a/docs/programming_guide/source_zh_cn/performance_optimization.md
+++ b/docs/programming_guide/source_zh_cn/performance_optimization.md
@@ -6,14 +6,14 @@
-
+
MindSpore提供了多种性能优化方法,用户可根据实际情况,利用它们来提升训练和推理的性能。
| 优化阶段 | 优化方法 | 支持情况 |
| --- | --- | --- |
-| 训练 | [分布式并行训练](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/distributed_training_tutorials.html) | Ascend、GPU |
-| | [混合精度](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/enable_mixed_precision.html) | Ascend、GPU |
-| | [图算融合](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/enable_graph_kernel_fusion.html) | Ascend、GPU |
-| | [梯度累积](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/apply_gradient_accumulation.html) | GPU |
-| 推理 | [训练后量化](https://www.mindspore.cn/tutorial/lite/zh-CN/master/use/post_training_quantization.html) | Lite |
+| 训练 | [分布式并行训练](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/distributed_training_tutorials.html) | Ascend、GPU |
+| | [混合精度](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/enable_mixed_precision.html) | Ascend、GPU |
+| | [图算融合](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/enable_graph_kernel_fusion.html) | Ascend、GPU |
+| | [梯度累积](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/apply_gradient_accumulation.html) | GPU |
+| 推理 | [训练后量化](https://www.mindspore.cn/tutorial/lite/zh-CN/r1.2/use/post_training_quantization.html) | Lite |
diff --git a/docs/programming_guide/source_zh_cn/pipeline.ipynb b/docs/programming_guide/source_zh_cn/pipeline.ipynb
index 965a70b7d896db7c9ffac6baf991ca8825b708db..6da15a106765a921bb8bb3bc2390a9eafbee0991 100644
--- a/docs/programming_guide/source_zh_cn/pipeline.ipynb
+++ b/docs/programming_guide/source_zh_cn/pipeline.ipynb
@@ -6,7 +6,7 @@
"source": [
"# 数据处理\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/pipeline.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_pipeline.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9waXBlbGluZS5pcHluYg==&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/pipeline.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_pipeline.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9waXBlbGluZS5pcHluYg==&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -19,7 +19,7 @@
"\n",
"MindSpore的各个数据集类都为用户提供了多种数据处理算子,用户可以构建数据处理pipeline定义需要使用的数据处理操作,数据即可在训练过程中像水一样源源不断地经过数据处理pipeline流向训练系统。\n",
"\n",
- "MindSpore目前支持的部分常用数据处理算子如下表所示,更多数据处理操作参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.dataset.html)。"
+ "MindSpore目前支持的部分常用数据处理算子如下表所示,更多数据处理操作参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.dataset.html)。"
]
},
{
@@ -48,7 +48,7 @@
"\n",
"> 设定的`buffer_size`越大,混洗程度越大,但时间、计算资源消耗也会更大。\n",
"\n",
- "\n",
+ "\n",
"\n",
"下面的样例先构建了一个随机数据集,然后对其进行混洗操作,最后展示了混洗后的数据结果。"
]
@@ -95,9 +95,9 @@
"\n",
"将指定的函数或算子作用于数据集的指定列数据,实现数据映射操作。用户可以自定义映射函数,也可以直接使用`c_transforms`或`py_transforms`中的算子针对图像、文本数据进行数据增强。\n",
"\n",
- "> 更多数据增强的使用说明,参见编程指南中[数据增强](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/augmentation.html)章节。\n",
+ "> 更多数据增强的使用说明,参见编程指南中[数据增强](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/augmentation.html)章节。\n",
"\n",
- "\n",
+ "\n",
"\n",
"下面的样例先构建了一个随机数据集,然后定义了数据翻倍的映射函数并将其作用于数据集,最后对比展示了映射前后的数据结果。"
]
@@ -157,7 +157,7 @@
"\n",
"将数据集分批,分别输入到训练系统中进行训练,可以减少训练轮次,达到加速训练过程的目的。\n",
"\n",
- "\n",
+ "\n",
"\n",
"下面的样例先构建了一个随机数据集,然后分别展示了保留多余数据与否的数据集分批结果,其中批大小为2。"
]
@@ -222,7 +222,7 @@
"\n",
"> `repeat`和`batch`操作的顺序会影响训练batch的数量,建议将`repeat`置于`batch`之后。\n",
"\n",
- "\n",
+ "\n",
"\n",
"下面的样例先构建了一个随机数据集,然后将其重复2次,最后展示了重复后的数据结果。"
]
@@ -276,7 +276,7 @@
"> \n",
"> 如果两个数据集的行数不同,合并后的行数将和较小行数保持一致。\n",
"\n",
- " \n",
+ " \n",
"\n",
"下面的样例先构建了两个不同样本数的随机数据集,然后将其进行列拼接,最后展示了拼接后的数据结果。"
]
@@ -328,7 +328,7 @@
"\n",
"> 输入数据集中的列名,列数据类型和列数据的排列应相同。\n",
"\n",
- "\n",
+ "\n",
"\n",
"下面的样例先构建了两个随机数据集,然后将其进行行拼接,最后展示了拼接后的数据结果。值得一提的是,使用`+`运算符也能达到同样的效果。"
]
diff --git a/docs/programming_guide/source_zh_cn/probability.ipynb b/docs/programming_guide/source_zh_cn/probability.ipynb
new file mode 100644
index 0000000000000000000000000000000000000000..551ee5dbe184c185c5b12fa98f52fb6c589c1650
--- /dev/null
+++ b/docs/programming_guide/source_zh_cn/probability.ipynb
@@ -0,0 +1,1621 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 深度概率编程库\n",
+ "\n",
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/probability.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_probability.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbWFzdGVyL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9wcm9iYWJpbGl0eS5pcHluYg==&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "MindSpore深度概率编程的目标是将深度学习和贝叶斯学习结合,包括概率分布、概率分布映射、深度概率网络、概率推断算法、贝叶斯层、贝叶斯转换和贝叶斯工具箱,面向不同的开发者。对于专业的贝叶斯学习用户,提供概率采样、推理算法和模型构建库;另一方面,为不熟悉贝叶斯深度学习的用户提供了高级的API,从而不用更改深度学习编程逻辑,即可利用贝叶斯模型。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 概率分布\n",
+ "\n",
+ "概率分布(`mindspore.nn.probability.distribution`)是概率编程的基础。`Distribution`类提供多样的概率统计接口,例如概率密度函数`pdf`、累积密度函数`cdf`、散度计算`kl_loss`、抽样`sample`等。现有的概率分布实例包括高斯分布,伯努利分布,指数型分布,几何分布和均匀分布。\n",
+ "\n",
+ "### 概率分布类\n",
+ "\n",
+ "- `Distribution`:所有概率分布的基类。\n",
+ "\n",
+ "- `Bernoulli`:伯努利分布。参数为试验成功的概率。\n",
+ "\n",
+ "- `Exponential`:指数型分布。参数为率参数。\n",
+ "\n",
+ "- `Geometric`:几何分布。参数为一次伯努利试验成功的概率。\n",
+ "\n",
+ "- `Normal`:正态(高斯)分布。参数为均值和标准差。\n",
+ "\n",
+ "- `Uniform`:均匀分布。参数为数轴上的最小值和最大值。\n",
+ "\n",
+ "- `Categorical`:类别分布。每种类别出现的概率。\n",
+ "\n",
+ "- `LogNormal`:对数正态分布。参数为位置参数和规模参数。\n",
+ "\n",
+ "- `Gumbel`: 耿贝尔极值分布。参数为位置参数和规模参数。\n",
+ "\n",
+ "- `Logistic`:逻辑斯谛分布。参数为位置参数和规模参数。\n",
+ "\n",
+ "- `Cauchy`:柯西分布。参数为位置参数和规模参数。\n",
+ "\n",
+ "#### Distribution基类\n",
+ "\n",
+ "`Distribution`是所有概率分布的基类。\n",
+ "\n",
+ "接口介绍:`Distribution`类支持的函数包括`prob`、`log_prob`、`cdf`、`log_cdf`、`survival_function`、`log_survival`、`mean`、`sd`、`var`、`entropy`、`kl_loss`、`cross_entropy`和`sample`。分布不同,所需传入的参数也不同。只有在派生类中才能使用,由派生类的函数实现决定参数。\n",
+ "\n",
+ "- `prob`:概率密度函数(PDF)/ 概率质量函数(PMF)。\n",
+ "\n",
+ "- `log_prob`:对数似然函数。\n",
+ "\n",
+ "- `cdf`:累积分布函数(CDF)。\n",
+ "\n",
+ "- `log_cdf`:对数累积分布函数。\n",
+ "\n",
+ "- `survival_function`:生存函数。\n",
+ "\n",
+ "- `log_survival`:对数生存函数。\n",
+ "\n",
+ "- `mean`:均值。\n",
+ "\n",
+ "- `sd`:标准差。\n",
+ "\n",
+ "- `var`:方差。\n",
+ "\n",
+ "- `entropy`:熵。\n",
+ "\n",
+ "- `kl_loss`:Kullback-Leibler 散度。\n",
+ "\n",
+ "- `cross_entropy`:两个概率分布的交叉熵。\n",
+ "\n",
+ "- `sample`:概率分布的随机抽样。\n",
+ "\n",
+ "- `get_dist_args`:概率分布在网络中使用的参数。\n",
+ "\n",
+ "- `get_dist_type`:概率分布的类型。\n",
+ "\n",
+ "#### 伯努利分布(Bernoulli)\n",
+ "\n",
+ "伯努利分布,继承自`Distribution`类。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `Bernoulli.probs`:返回伯努利试验成功的概率,类型为`Tensor`。\n",
+ "\n",
+ "`Distribution`基类调用`Bernoulli`中私有接口以实现基类中的公有接口。`Bernoulli`支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`var`,`sd`:可选择传入 试验成功的概率`probs1`。\n",
+ "\n",
+ "- `entropy`:可选择传入 试验成功的概率`probs1`。\n",
+ "\n",
+ "- `cross_entropy`,`kl_loss`:必须传入`dist`和`probs1_b`。`dist`为另一分布的类型,目前只支持此处为“Bernoulli”。`probs1_b`为分布`b`的试验成功概率。可选择传入分布`a`的参数`probs1_a`。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。可选择传入试验成功的概率`probs`。\n",
+ "\n",
+ "- `sample`:可选择传入样本形状`shape`和试验成功的概率`probs1`。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入试验成功的概率`probs`。返回值为`(probs,)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Bernoulli”。\n",
+ "\n",
+ "#### 指数分布(Exponential)\n",
+ "\n",
+ "指数分布,继承自`Distribution`类。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `Exponential.rate`:返回分布的率参数,类型为`Tensor`。\n",
+ "\n",
+ "`Distribution`基类调用`Exponential`私有接口以实现基类中的公有接口。`Exponential`支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`var`,`sd`:可选择传入率参数`rate`。\n",
+ "\n",
+ "- `entropy`:可选择传入率参数`rate`。\n",
+ "\n",
+ "- `cross_entropy`,`kl_loss`:必须传入`dist`和`rate_b`。`dist`为另一分布的类型的名称, 目前只支持此处为“Exponential”。`rate_b`为分布`b`的率参数。可选择传入分布`a`的参数`rate_a`。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。可选择传入率参数`rate`。\n",
+ "\n",
+ "- `sample`:可选择传入样本形状`shape`和率参数`rate`。返回值为`(rate,)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入率参数`rate`。返回值为`(rate,)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Exponential”。\n",
+ "\n",
+ "#### 几何分布(Geometric)\n",
+ "\n",
+ "几何分布,继承自`Distribution`类。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `Geometric.probs`:返回伯努利试验成功的概率,类型为`Tensor`。\n",
+ "\n",
+ "`Distribution`基类调用`Geometric`中私有接口以实现基类中的公有接口。`Geometric`支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`var`,`sd`:可选择传入试验成功的概率`probs1`。\n",
+ "\n",
+ "- `entropy`:可选择传入 试验成功的概率`probs1`。\n",
+ "\n",
+ "- `cross_entropy`,`kl_loss`:必须传入`dist`和`probs1_b`。`dist`为另一分布的类型的名称,目前只支持此处为“Geometric”。`probs1_b`为分布`b`的试验成功概率。可选择传入分布`a`的参数`probs1_a`。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。可选择传入试验成功的概率`probs1`。\n",
+ "\n",
+ "- `sample`:可选择传入样本形状`shape`和试验成功的概率`probs1`。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入试验成功的概率`probs1`。返回值为`(probs1,)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Geometric”。\n",
+ "\n",
+ "#### 正态分布(Normal)\n",
+ "\n",
+ "正态(高斯)分布,继承自`Distribution`类。\n",
+ "\n",
+ "`Distribution`基类调用`Normal`中私有接口以实现基类中的公有接口。`Normal`支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`var`,`sd`:可选择传入分布的参数均值`mean`和标准差`sd`。\n",
+ "\n",
+ "\n",
+ "- `entropy`:可选择传入分布的参数均值`mean`和标准差`sd`。\n",
+ "\n",
+ "- `cross_entropy`,`kl_loss`:必须传入`dist`,`mean_b`和`sd_b`。`dist`为另一分布的类型的名称,目前只支持此处为“Normal”。\n",
+ "`mean_b`和`sd_b`为分布`b`的均值和标准差。可选择传入分布的参数`a`均值`mean_a`和标准差`sd_a`。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。可选择分布的参数包括均值`mean_a`和标准差`sd_a`。\n",
+ "\n",
+ "- `sample`:可选择传入样本形状`shape`和分布的参数包括均值`mean_a`和标准差`sd_a`。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入分布的参数均值`mean`和标准差`sd`。返回值为`(mean, sd)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Normal”。\n",
+ "\n",
+ "#### 均匀分布(Uniform)\n",
+ "\n",
+ "均匀分布,继承自`Distribution`类。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `Uniform.low`:返回分布的最小值,类型为`Tensor`。\n",
+ "\n",
+ "- `Uniform.high`:返回分布的最大值,类型为`Tensor`。\n",
+ "\n",
+ "`Distribution`基类调用`Uniform`以实现基类中的公有接口。`Uniform`支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`var`,`sd`:可选择传入分布的参数最大值`high`和最小值`low`。\n",
+ "\n",
+ "- `entropy`:可选择传入分布的参数最大值`high`和最小值`low`。\n",
+ "\n",
+ "- `cross_entropy`,`kl_loss`:必须传入`dist`,`high_b`和`low_b`。`dist`为另一分布的类型的名称,目前只支持此处为“Uniform”。`high_b`和`low_b`为分布`b`的参数。可选择传入分布`a`的参数即最大值`high_a`和最小值`low_a`。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。可选择传入分布的参数最大值`high`和最小值`low`。\n",
+ "\n",
+ "- `sample`:可选择传入`shape`和分布的参数即最大值`high`和最小值`low`。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入分布的参数最大值`high`和最小值`low`。返回值为`(low, high)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Uniform”。\n",
+ "\n",
+ "#### 多类别分布(Categorical)\n",
+ "\n",
+ "多类别分布,继承自`Distribution`类。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `Categorical.probs`:返回各种类别的概率,类型为`Tensor`。\n",
+ "\n",
+ "`Distribution`基类调用`Categorical`以实现基类中的公有接口。`Categorical`支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`var`,`sd`:可选择传入分布的参数类别概率`probs`。\n",
+ "\n",
+ "- `entropy`:可选择传入分布的参数类别概率`probs`。\n",
+ "\n",
+ "- `cross_entropy`,`kl_loss`:必须传入`dist`,`probs_b`。`dist`为另一分布的类型的名称,目前只支持此处为“Categorical”。`probs_b`为分布`b`的参数。可选择传入分布`a`的参数即`probs_a`。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。可选择传入分布的参数类别概率`probs`。\n",
+ "\n",
+ "- `sample`:可选择传入`shape`和类别概率`probs`。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入分布的参数类别概率`probs`。返回值为`(probs,)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Categorical”。\n",
+ "\n",
+ "#### 对数正态分布(LogNormal)\n",
+ "\n",
+ "对数正态分布,继承自`TransformedDistribution`类,由`Exp`Bijector 和`Normal`Distribution 构成。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `LogNormal.loc`:返回分布的位置参数,类型为`Tensor`。\n",
+ "\n",
+ "- `LogNormal.scale`:返回分布的规模参数,类型为`Tensor`。\n",
+ "\n",
+ "`Distribution`基类调用`LogNormal`及`TransformedDistribution`中私有接口以实现基类中的公有接口。`LogNormal`支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`var`,`sd`:可选择传入分布的位置参数`loc`和规模参数`scale`。\n",
+ "\n",
+ "- `entropy`:可选择传入分布的位置参数`loc`和规模参数`scale`。\n",
+ "\n",
+ "- `cross_entropy`,`kl_loss`:必须传入`dist`,`loc_b`和`scale_b`。`dist`为另一分布的类型的名称,目前只支持此处为“LogNormal”。`loc_b`和`scale_b`为分布`b`的均值和标准差。可选择传入分布的参数`a`均值`loc_a`和标准差`sclae_a`。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。可选择分布的参数包括均值`loc_a`和标准差`scale_a`。`Distribution`基类调用`TransformedDistribution`私有接口。\n",
+ "\n",
+ "- `sample`:可选择传入样本形状`shape`和分布的参数包括均值`loc_a`和标准差`scale_a`。`Distribution`基类调用`TransformedDistribution`私有接口。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入分布的位置参数`loc`和规模参数`scale`。返回值为`(loc, scale)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“LogNormal”。\n",
+ "\n",
+ "#### 柯西分布(Cauchy)\n",
+ "\n",
+ "柯西分布,继承自`Distribution`类。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `Cauchy.loc`:返回分布的位置参数,类型为`Tensor`。\n",
+ "\n",
+ "- `Cauchy.scale`:返回分布的规模参数,类型为`Tensor`。\n",
+ "\n",
+ "`Distribution`基类调用`Cauchy`中私有接口以实现基类中的公有接口。`Cauchy`支持的公有接口为:\n",
+ "\n",
+ "- `entropy`:可选择传入分布的位置参数`loc`和规模参数`scale`。\n",
+ "\n",
+ "- `cross_entropy`,`kl_loss`:必须传入`dist`,`loc_b`和`scale_b`。`dist`为另一分布的类型的名称,目前只支持此处为“Cauchy”。`loc_b`和`scale_b`为分布`b`的位置参数和规模参数。可选择传入分布的参数`a`位置`loc_a`和规模`scale_a`。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。可选择传入分布的位置参数`loc`和规模参数`scale`。\n",
+ "\n",
+ "- `sample`:可选择传入样本形状`shape`和分布的参数包括分布的位置参数`loc`和规模参数`scale`。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入分布的位置参数`loc`和规模参数`scale`。返回值为`(loc, scale)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Cauchy”。\n",
+ "\n",
+ "#### 耿贝尔极值分布(Gumbel)\n",
+ "\n",
+ "耿贝尔极值分布,继承自`TransformedDistribution`类,由`GumbelCDF`Bijector和`Uniform`Distribution 构成。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `Gumbel.loc`:返回分布的位置参数,类型为`Tensor`。\n",
+ "\n",
+ "- `Gumbel.scale`:返回分布的规模参数,类型为`Tensor`。\n",
+ "\n",
+ "`Distribution`基类调用`Gumbel`中私有接口以实现基类中的公有接口。`Gumbel`支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`var`,`sd`:无参数 。\n",
+ "\n",
+ "- `entropy`:无参数 。\n",
+ "\n",
+ "- `cross_entropy`,`kl_loss`:必须传入`dist`,`loc_b`和`scale_b`。`dist`为另一分布的类型的名称,目前只支持此处为“Gumbel”。`loc_b`和`scale_b`为分布`b`的位置参数和规模参数。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。\n",
+ "\n",
+ "- `sample`:可选择传入样本形状`shape`。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入分布的位置参数`loc`和规模参数`scale`。返回值为`(loc, scale)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Gumbel”。\n",
+ "\n",
+ "#### 逻辑斯谛分布(Logistic)\n",
+ "\n",
+ "逻辑斯谛分布,继承自`Distribution`类。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `Logistic.loc`:返回分布的位置参数,类型为`Tensor`。\n",
+ "\n",
+ "- `Logistic.scale`:返回分布的规模参数,类型为`Tensor`。\n",
+ "\n",
+ "`Distribution`基类调用`logistic`中私有接口以实现基类中的公有接口。`Logistic`支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`var`,`sd`:可选择传入分布的位置参数`loc`和规模参数`scale`。\n",
+ "\n",
+ "- `entropy`:可选择传入分布的位置参数`loc`和规模参数`scale`。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。可选择传入分布的位置参数`loc`和规模参数`scale`。\n",
+ "\n",
+ "- `sample`:可选择传入样本形状`shape`和分布的参数包括分布的位置参数`loc`和规模参数`scale`。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入分布的位置参数`loc`和规模参数`scale`。返回值为`(loc, scale)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Logistic”。\n",
+ "\n",
+ "#### 泊松分布\n",
+ "\n",
+ "泊松分布,继承自`Distribution`类。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `Poisson.rate`:返回分布的率参数,类型为Tensor。\n",
+ "\n",
+ "- `Distribution` 基类调用`Poisson`中私有接口以实现基类中的公有接口。`Poisson`支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`var`,`sd`:可选择传入分布的率参数 rate 。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。可选择传入分布的率参数`rate`。\n",
+ "\n",
+ "- `sample`:可选择传入样本形状shape 和分布的率参数 rate 。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入分布的率参数`rate`。返回值为`(rate,)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Poisson”。\n",
+ "\n",
+ "#### 伽马分布(Gamma)\n",
+ "\n",
+ "伽马分布,继承自 `Distribution` 类。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `Gamma.concentration`:返回分布的参数 `concentration` ,类型为`Tensor`。\n",
+ "\n",
+ "- `Gamma.rate`:返回分布的参数 `rate` ,类型为`Tensor`。\n",
+ "\n",
+ "`Distribution` 基类调用 `Gamma` 中私有接口以实现基类中的公有接口。`Gamma` 支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`sd`,`var`:可选择传入分布的参数`concentration`和参数`rate` 。\n",
+ "\n",
+ "- `entropy`:可选择传入分布的参数`concentration`和参数`rate`。\n",
+ "\n",
+ "- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入`value`。可选择传入分布的参数`concentration`和参数`rate`。\n",
+ "\n",
+ "- `cross_entropy`,`kl_loss`:必须传入`dist`,`concentration_b`和`rate_b`。`dist`为另一分布的类型的名称,目前只支持此处为“Gamma”。 `concentration_b`和`rate_b`为分布`b`的参数。可选择传入分布`a`的参数即`concentration_a`和`rate_a`。\n",
+ "\n",
+ "- `sample`:可选择传入样本形状`shape`和分布的参数包括分布的参数`concentration`和参数`rate`。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入分布的参数`concentration`和参数`rate`。返回值为`(concentration, rate)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Gamma”。\n",
+ "\n",
+ "#### 贝塔分布(Beta)\n",
+ "\n",
+ "贝塔分布,继承自 `Distribution` 类。\n",
+ "\n",
+ "属性:\n",
+ "\n",
+ "- `Beta.concentration1`:返回分布的参数 `concentration1` ,类型为`Tensor`。\n",
+ "\n",
+ "- `Beta.concentration0`:返回分布的参数 `concentration0` ,类型为`Tensor`。\n",
+ "\n",
+ "`Distribution` 基类调用 `Beta` 中私有接口以实现基类中的公有接口。`Beta` 支持的公有接口为:\n",
+ "\n",
+ "- `mean`,`mode`,`sd`,`var`:可选择传入分布的参数`concentration1`和参数`concentration0`。\n",
+ "\n",
+ "- `entropy`:可选择传入分布的参数`concentration1`和参数`concentration0`。\n",
+ "\n",
+ "- `prob`,`log_prob`:必须传入`value`。可选择传入分布的参数`concentration1`和参数`concentration0`。\n",
+ "\n",
+ "- `cross_entropy`,`kl_loss`:必须传入`dist`,`concentration1_b`和`concentration1_b`。`dist`为另一分布的类型的名称,目前只支持此处为“Beta”。`concentration1_b`和`concentration1_b`为分布`b`的参数。可选择传入分布`a`的参数即`concentration1_a`和`concentration0_a`。\n",
+ "\n",
+ "- `sample`:可选择传入样本形状`shape`和分布的参数包括分布的位置参数`loc`和规模参数`scale`。\n",
+ "\n",
+ "- `get_dist_args`:可选择传入分布的参数`concentration1`和参数`concentration0`。返回值为`(concentration1, concentration0)`,类型为tuple。\n",
+ "\n",
+ "- `get_dist_type`:返回“Beta”。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 概率分布类在PyNative模式下的应用\n",
+ "\n",
+ "`Distribution`子类可在PyNative模式下使用。\n",
+ "\n",
+ "以`Normal`为例, 创建一个均值为0.0、标准差为1.0的正态分布,然后计算相关函数。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "mean: 0.0\n",
+ "var: 1.0\n",
+ "entropy: 1.4189385\n",
+ "prob: [0.35206532 0.3989423 0.35206532]\n",
+ "cdf: [0.30853754 0.5 0.69146246]\n",
+ "kl: 0.44314718\n",
+ "dist_arg: (Tensor(shape=[], dtype=Float32, value= 0), Tensor(shape=[], dtype=Float32, value= 1))\n"
+ ]
+ }
+ ],
+ "source": [
+ "from mindspore import Tensor\n",
+ "from mindspore import dtype as mstype\n",
+ "import mindspore.context as context\n",
+ "import mindspore.nn.probability.distribution as msd\n",
+ "\n",
+ "context.set_context(mode=context.PYNATIVE_MODE, device_target=\"GPU\")\n",
+ "\n",
+ "my_normal = msd.Normal(0.0, 1.0, dtype=mstype.float32)\n",
+ "\n",
+ "mean = my_normal.mean()\n",
+ "var = my_normal.var()\n",
+ "entropy = my_normal.entropy()\n",
+ "\n",
+ "value = Tensor([-0.5, 0.0, 0.5], dtype=mstype.float32)\n",
+ "prob = my_normal.prob(value)\n",
+ "cdf = my_normal.cdf(value)\n",
+ "\n",
+ "mean_b = Tensor(1.0, dtype=mstype.float32)\n",
+ "sd_b = Tensor(2.0, dtype=mstype.float32)\n",
+ "kl = my_normal.kl_loss('Normal', mean_b, sd_b)\n",
+ "\n",
+ "# get the distribution args as a tuple\n",
+ "dist_arg = my_normal.get_dist_args()\n",
+ "\n",
+ "print(\"mean: \", mean)\n",
+ "print(\"var: \", var)\n",
+ "print(\"entropy: \", entropy)\n",
+ "print(\"prob: \", prob)\n",
+ "print(\"cdf: \", cdf)\n",
+ "print(\"kl: \", kl)\n",
+ "print(\"dist_arg: \", dist_arg)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 概率分布类在图模式下的应用\n",
+ "\n",
+ "在图模式下,`Distribution`子类可用在网络中。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "pdf: [0.35206532 0.3989423 0.35206532]\n",
+ "kl: 0.5\n"
+ ]
+ }
+ ],
+ "source": [
+ "import mindspore.nn as nn\n",
+ "from mindspore import Tensor\n",
+ "from mindspore import dtype as mstype\n",
+ "import mindspore.context as context\n",
+ "import mindspore.nn.probability.distribution as msd\n",
+ "context.set_context(mode=context.GRAPH_MODE)\n",
+ "\n",
+ "class Net(nn.Cell):\n",
+ " def __init__(self):\n",
+ " super(Net, self).__init__()\n",
+ " self.normal = msd.Normal(0.0, 1.0, dtype=mstype.float32)\n",
+ "\n",
+ " def construct(self, value, mean, sd):\n",
+ " pdf = self.normal.prob(value)\n",
+ " kl = self.normal.kl_loss(\"Normal\", mean, sd)\n",
+ " return pdf, kl\n",
+ "\n",
+ "net = Net()\n",
+ "value = Tensor([-0.5, 0.0, 0.5], dtype=mstype.float32)\n",
+ "mean = Tensor(1.0, dtype=mstype.float32)\n",
+ "sd = Tensor(1.0, dtype=mstype.float32)\n",
+ "pdf, kl = net(value, mean, sd)\n",
+ "print(\"pdf: \", pdf)\n",
+ "print(\"kl: \", kl)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### TransformedDistribution类接口设计\n",
+ "\n",
+ "`TransformedDistribution`继承自`Distribution`,是可通过映射f(x)变化得到的数学分布的基类。其接口包括:\n",
+ "\n",
+ "1. 属性\n",
+ "\n",
+ " - `bijector`:返回分布的变换方法。\n",
+ " \n",
+ " - `distribution`:返回原始分布。\n",
+ " \n",
+ " - `is_linear_transformation`:返回线性变换标志。\n",
+ "\n",
+ "2. 接口函数(以下接口函数的参数与构造函数中`distribution`的对应接口的参数相同)。\n",
+ "\n",
+ " - `cdf`:累积分布函数(CDF)。\n",
+ " \n",
+ " - `log_cdf`:对数累积分布函数。\n",
+ " \n",
+ " - `survival_function`:生存函数。\n",
+ " \n",
+ " - `log_survival`:对数生存函数。\n",
+ " \n",
+ " - `prob`:概率密度函数(PDF)/ 概率质量函数(PMF)。\n",
+ " \n",
+ " - `log_prob`:对数似然函数。\n",
+ " \n",
+ " - `sample`:随机取样。\n",
+ " \n",
+ " - `mean`:无参数。只有当`Bijector.is_constant_jacobian=true`时可调用。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### PyNative模式下调用TransformedDistribution实例\n",
+ "\n",
+ "`TransformedDistribution`子类可在PyNative模式下使用。\n",
+ "\n",
+ "这里构造一个`TransformedDistribution`实例,使用`Normal`分布作为需要变换的分布类,使用`Exp`作为映射变换,可以生成`LogNormal`分布。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "TransformedDistribution<\n",
+ " (_bijector): Exp\n",
+ " (_distribution): Normal\n",
+ " >\n",
+ "underlying distribution:\n",
+ " Normal\n",
+ "bijector:\n",
+ " Exp\n",
+ "cdf:\n",
+ " [0.7558914 0.9462397 0.9893489]\n",
+ "sample:\n",
+ " (3, 2)\n"
+ ]
+ }
+ ],
+ "source": [
+ "import numpy as np\n",
+ "import mindspore.nn as nn\n",
+ "import mindspore.nn.probability.bijector as msb\n",
+ "import mindspore.nn.probability.distribution as msd\n",
+ "from mindspore import Tensor, dtype, context\n",
+ "\n",
+ "context.set_context(mode=context.PYNATIVE_MODE)\n",
+ "\n",
+ "normal = msd.Normal(0.0, 1.0, dtype=dtype.float32)\n",
+ "exp = msb.Exp()\n",
+ "LogNormal = msd.TransformedDistribution(exp, normal, seed=0, name=\"LogNormal\")\n",
+ "\n",
+ "# compute cumulative distribution function\n",
+ "x = np.array([2.0, 5.0, 10.0], dtype=np.float32)\n",
+ "tx = Tensor(x, dtype=dtype.float32)\n",
+ "cdf = LogNormal.cdf(tx)\n",
+ "\n",
+ "# generate samples from the distribution\n",
+ "shape = ((3, 2))\n",
+ "sample = LogNormal.sample(shape)\n",
+ "\n",
+ "# get information of the distribution\n",
+ "print(LogNormal)\n",
+ "# get information of the underlying distribution and the bijector separately\n",
+ "print(\"underlying distribution:\\n\", LogNormal.distribution)\n",
+ "print(\"bijector:\\n\", LogNormal.bijector)\n",
+ "# get the computation results\n",
+ "print(\"cdf:\\n\", cdf)\n",
+ "print(\"sample:\\n\", sample.shape)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "当构造`TransformedDistribution`映射变换的`is_constant_jacobian = true`时(如`ScalarAffine`),构造的`TransformedDistribution`实例可以使用直接使用`mean`接口计算均值,例如:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "2.0\n"
+ ]
+ }
+ ],
+ "source": [
+ "normal = msd.Normal(0.0, 1.0, dtype=dtype.float32)\n",
+ "scalaraffine = msb.ScalarAffine(1.0, 2.0)\n",
+ "trans_dist = msd.TransformedDistribution(scalaraffine, normal, seed=0)\n",
+ "mean = trans_dist.mean()\n",
+ "print(mean)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 图模式下调用TransformedDistribution实例\n",
+ "\n",
+ "在图模式下,`TransformedDistribution`类可用在网络中。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "cdf: [0.7558914 0.86403143 0.9171715 0.9462397 ]\n",
+ "sample: (2, 3)\n"
+ ]
+ }
+ ],
+ "source": [
+ "import numpy as np\n",
+ "import mindspore.nn as nn\n",
+ "from mindspore import Tensor, dtype\n",
+ "import mindspore.context as context\n",
+ "import mindspore.nn.probability.bijector as msb\n",
+ "import mindspore.nn.probability.distribution as msd\n",
+ "context.set_context(mode=context.GRAPH_MODE)\n",
+ "\n",
+ "class Net(nn.Cell):\n",
+ " def __init__(self, shape, dtype=dtype.float32, seed=0, name='transformed_distribution'):\n",
+ " super(Net, self).__init__()\n",
+ " # create TransformedDistribution distribution\n",
+ " self.exp = msb.Exp()\n",
+ " self.normal = msd.Normal(0.0, 1.0, dtype=dtype)\n",
+ " self.lognormal = msd.TransformedDistribution(self.exp, self.normal, seed=seed, name=name)\n",
+ " self.shape = shape\n",
+ "\n",
+ " def construct(self, value):\n",
+ " cdf = self.lognormal.cdf(value)\n",
+ " sample = self.lognormal.sample(self.shape)\n",
+ " return cdf, sample\n",
+ "\n",
+ "shape = (2, 3)\n",
+ "net = Net(shape=shape, name=\"LogNormal\")\n",
+ "x = np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32)\n",
+ "tx = Tensor(x, dtype=dtype.float32)\n",
+ "cdf, sample = net(tx)\n",
+ "print(\"cdf: \", cdf)\n",
+ "print(\"sample: \", sample.shape)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 概率分布映射\n",
+ "\n",
+ "Bijector(`mindspore.nn.probability.bijector`)是概率编程的基本组成部分。Bijector描述了一种随机变量的变换方法,可以通过一个已有的随机变量X和一个映射函数f生成一个新的随机变量$Y = f(x)$。\n",
+ "\n",
+ "`Bijector`提供了映射相关的四种变换方法。它可以当做算子直接使用,也可以作用在某个随机变量`Distribution`类实例上生成新的随机变量的`Distribution`类实例。\n",
+ "\n",
+ "### Bijector类接口设计\n",
+ "\n",
+ "#### Bijector基类\n",
+ "\n",
+ "`Bijector`类是所有概率分布映射的基类。其接口包括:\n",
+ "\n",
+ "1. 属性\n",
+ "\n",
+ " - `name`:返回`name`的值。\n",
+ " \n",
+ " - `is_dtype`:返回`dtype`的值。\n",
+ " \n",
+ " - `parameter`:返回`parameter`的值。\n",
+ " \n",
+ " - `is_constant_jacobian`:返回`is_constant_jacobian`的值。\n",
+ " \n",
+ " - `is_injective`:返回`is_injective`的值。\n",
+ "\n",
+ "2. 映射函数\n",
+ "\n",
+ " - `forward`:正向映射,创建派生类后由派生类的`_forward`决定参数。\n",
+ " \n",
+ " - `inverse`:反向映射,创建派生类后由派生类的`_inverse`决定参数。\n",
+ " \n",
+ " - `forward_log_jacobian`:正向映射的导数的对数,创建派生类后由派生类的`_forward_log_jacobian`决定参数。\n",
+ " \n",
+ " - `inverse_log_jacobian`:反向映射的导数的对数,创建派生类后由派生类的`_inverse_log_jacobian`决定参数。\n",
+ "\n",
+ "`Bijector`作为函数调用:输入是一个`Distribution`类:生成一个`TransformedDistribution` *(不可在图内调用)*。\n",
+ "\n",
+ "#### 幂函数变换映射(PowerTransform)\n",
+ "\n",
+ "`PowerTransform`做如下变量替换:$Y = g(X) = {(1 + X \\times power)}^{1 / power}$。其接口包括:\n",
+ "\n",
+ "1. 属性\n",
+ "\n",
+ " - `power`:返回`power`的值,类型为`Tensor`。\n",
+ "\n",
+ "2. 映射函数\n",
+ " \n",
+ " - `forward`:正向映射,输入为`Tensor`。\n",
+ " \n",
+ " - `inverse`:反向映射,输入为`Tensor`。\n",
+ " \n",
+ " - `forward_log_jacobian`:正向映射的导数的对数,输入为`Tensor`。\n",
+ " \n",
+ " - `inverse_log_jacobian`:反向映射的导数的对数,输入为`Tensor`。\n",
+ "\n",
+ "#### 指数变换映射(Exp)\n",
+ "\n",
+ "`Exp`做如下变量替换:$Y = g(X)= exp(X)$。其接口包括:\n",
+ "\n",
+ "映射函数\n",
+ "\n",
+ "- `forward`:正向映射,输入为`Tensor`。\n",
+ "\n",
+ "- `inverse`:反向映射,输入为`Tensor`。\n",
+ "\n",
+ "- `forward_log_jacobian`:正向映射的导数的对数,输入为`Tensor`。\n",
+ "\n",
+ "- `inverse_log_jacobian`:反向映射的导数的对数,输入为`Tensor`。\n",
+ "\n",
+ "#### 标量仿射变换映射(ScalarAffine)\n",
+ "\n",
+ "`ScalarAffine`做如下变量替换:$Y = g(X) = scale\\times X + shift$。其接口包括:\n",
+ "\n",
+ "1. 属性\n",
+ "\n",
+ " - `scale`:返回`scale`的值,类型为`Tensor`。\n",
+ " \n",
+ " - `shift`:返回`shift`的值,类型为`Tensor`。\n",
+ "\n",
+ "2. 映射函数\n",
+ " \n",
+ " - `forward`:正向映射,输入为`Tensor`。\n",
+ " \n",
+ " - `inverse`:反向映射,输入为`Tensor`。\n",
+ " \n",
+ " - `forward_log_jacobian`:正向映射的导数的对数,输入为`Tensor`。\n",
+ " \n",
+ " - `inverse_log_jacobian`:反向映射的导数的对数,输入为`Tensor`。\n",
+ "\n",
+ "#### Softplus变换映射(Softplus)\n",
+ "\n",
+ "`Softplus`做如下变量替换:$Y = g(X) = \\frac{log(1 + e ^ {sharpness \\times X}\\ \\ \\ \\ \\ \\ )} {sharpness}$。其接口包括:\n",
+ "\n",
+ "1. 属性\n",
+ " \n",
+ " - `sharpness`:返回`sharpness`的值,类型为`Tensor`。\n",
+ "\n",
+ "2. 映射函数\n",
+ " \n",
+ " - `forward`:正向映射,输入为`Tensor`。\n",
+ " \n",
+ " - `inverse`:反向映射,输入为`Tensor`。\n",
+ " \n",
+ " - `forward_log_jacobian`:正向映射的导数的对数,输入为`Tensor`。\n",
+ " \n",
+ " - `inverse_log_jacobian`:反向映射的导数的对数,输入为`Tensor`。\n",
+ "\n",
+ "#### 耿贝尔累计密度函数映射(GumbelCDF)\n",
+ "\n",
+ "`GumbelCDF`做如下变量替换:$Y = g(X) = \\exp(-\\exp(-\\frac{X - loc}{scale}))$。其接口包括:\n",
+ "\n",
+ "1. 属性\n",
+ " \n",
+ " - `loc`:返回`loc`的值,类型为`Tensor`。\n",
+ " \n",
+ " - `scale`:返回`scale`的值,类型为`Tensor`。\n",
+ "\n",
+ "2. 映射函数\n",
+ " \n",
+ " - `forward`:正向映射,输入为`Tensor`。\n",
+ " \n",
+ " - `inverse`:反向映射,输入为`Tensor`。\n",
+ " \n",
+ " - `forward_log_jacobian`:正向映射的导数的对数,输入为`Tensor`。\n",
+ " \n",
+ " - `inverse_log_jacobian`:反向映射的导数的对数,输入为`Tensor`。\n",
+ "\n",
+ "#### 逆映射(Invert)\n",
+ "\n",
+ "`Invert`对一个映射做逆变换,其接口包括:\n",
+ "\n",
+ "1. 属性\n",
+ " \n",
+ " - `bijector`:返回初始化时使用的`Bijector`,类型为`Bijector`。\n",
+ "\n",
+ "2. 映射函数\n",
+ " \n",
+ " - `forward`:正向映射,输入为`Tensor`。\n",
+ " \n",
+ " - `inverse`:反向映射,输入为`Tensor`。\n",
+ " \n",
+ " - `forward_log_jacobian`:正向映射的导数的对数,输入为`Tensor`。\n",
+ " \n",
+ " - `inverse_log_jacobian`:反向映射的导数的对数,输入为`Tensor`。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### PyNative模式下调用Bijector实例\n",
+ "\n",
+ "在执行之前,我们需要导入需要的库文件包。双射类最主要的库是`mindspore.nn.probability.bijector`,导入后我们使用`msb`作为库的缩写并进行调用。\n",
+ "\n",
+ "下面我们以`PowerTransform`为例。创建一个指数为2的`PowerTransform`对象。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "PowerTransform\n",
+ "forward: [2.236068 2.6457515 3. 3.3166249]\n",
+ "inverse: [ 1.5 4. 7.5 12.000001]\n",
+ "forward_log_jacobian: [-0.804719 -0.9729551 -1.0986123 -1.1989477]\n",
+ "inverse_log_jacobian: [0.6931472 1.0986123 1.3862944 1.609438 ]\n"
+ ]
+ }
+ ],
+ "source": [
+ "import numpy as np\n",
+ "import mindspore.nn as nn\n",
+ "import mindspore.nn.probability.bijector as msb\n",
+ "import mindspore.context as context\n",
+ "from mindspore import Tensor, dtype\n",
+ "\n",
+ "context.set_context(mode=context.PYNATIVE_MODE)\n",
+ "\n",
+ "powertransform = msb.PowerTransform(power=2.)\n",
+ "\n",
+ "x = np.array([2.0, 3.0, 4.0, 5.0], dtype=np.float32)\n",
+ "tx = Tensor(x, dtype=dtype.float32)\n",
+ "forward = powertransform.forward(tx)\n",
+ "inverse = powertransform.inverse(tx)\n",
+ "forward_log_jaco = powertransform.forward_log_jacobian(tx)\n",
+ "inverse_log_jaco = powertransform.inverse_log_jacobian(tx)\n",
+ "\n",
+ "print(powertransform)\n",
+ "print(\"forward: \", forward)\n",
+ "print(\"inverse: \", inverse)\n",
+ "print(\"forward_log_jacobian: \", forward_log_jaco)\n",
+ "print(\"inverse_log_jacobian: \", inverse_log_jaco)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 图模式下调用Bijector实例\n",
+ "\n",
+ "在图模式下,`Bijector`子类可用在网络中。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "forward: [2.236068 2.6457515 3. 3.3166249]\n",
+ "inverse: [ 1.5 4. 7.5 12.000001]\n",
+ "forward_log_jacobian: [-0.804719 -0.9729551 -1.0986123 -1.1989477]\n",
+ "inverse_log_jacobian: [0.6931472 1.0986123 1.3862944 1.609438 ]\n"
+ ]
+ }
+ ],
+ "source": [
+ "import numpy as np\n",
+ "import mindspore.nn as nn\n",
+ "from mindspore import Tensor\n",
+ "from mindspore import dtype as mstype\n",
+ "import mindspore.context as context\n",
+ "import mindspore.nn.probability.bijector as msb\n",
+ "context.set_context(mode=context.GRAPH_MODE)\n",
+ "\n",
+ "class Net(nn.Cell):\n",
+ " def __init__(self):\n",
+ " super(Net, self).__init__()\n",
+ " # create a PowerTransform bijector\n",
+ " self.powertransform = msb.PowerTransform(power=2.)\n",
+ "\n",
+ " def construct(self, value):\n",
+ " forward = self.powertransform.forward(value)\n",
+ " inverse = self.powertransform.inverse(value)\n",
+ " forward_log_jaco = self.powertransform.forward_log_jacobian(value)\n",
+ " inverse_log_jaco = self.powertransform.inverse_log_jacobian(value)\n",
+ " return forward, inverse, forward_log_jaco, inverse_log_jaco\n",
+ "\n",
+ "net = Net()\n",
+ "x = np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32)\n",
+ "tx = Tensor(x, dtype=mstype.float32)\n",
+ "forward, inverse, forward_log_jaco, inverse_log_jaco = net(tx)\n",
+ "print(\"forward: \", forward)\n",
+ "print(\"inverse: \", inverse)\n",
+ "print(\"forward_log_jacobian: \", forward_log_jaco)\n",
+ "print(\"inverse_log_jacobian: \", inverse_log_jaco)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 深度概率网络\n",
+ "\n",
+ "使用MindSpore深度概率编程库(`mindspore.nn.probability.dpn`)来构造变分自编码器(VAE)进行推理尤为简单。我们只需要自定义编码器和解码器(DNN模型),调用VAE或CVAE接口形成其派生网络,然后调用ELBO接口进行优化,最后使用SVI接口进行变分推理。这样做的好处是,不熟悉变分推理的用户可以像构建DNN模型一样来构建概率模型,而熟悉的用户可以调用这些接口来构建更为复杂的概率模型。VAE的接口在`mindspore.nn.probability.dpn`下面,dpn代表的是Deep probabilistic network,这里提供了一些基本的深度概率网络的接口,例如VAE。\n",
+ "\n",
+ "### VAE\n",
+ "\n",
+ "首先,我们需要先自定义encoder和decoder,调用`mindspore.nn.probability.dpn.VAE`接口来构建VAE网络,我们除了传入encoder和decoder之外,还需要传入encoder输出变量的维度hidden size,以及VAE网络存储潜在变量的维度latent size,一般latent size会小于hidden size。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import mindspore.nn as nn\n",
+ "import mindspore.ops as ops\n",
+ "from mindspore.nn.probability.dpn import VAE\n",
+ "\n",
+ "IMAGE_SHAPE = (-1, 1, 32, 32)\n",
+ "\n",
+ "class Encoder(nn.Cell):\n",
+ " def __init__(self):\n",
+ " super(Encoder, self).__init__()\n",
+ " self.fc1 = nn.Dense(1024, 800)\n",
+ " self.fc2 = nn.Dense(800, 400)\n",
+ " self.relu = nn.ReLU()\n",
+ " self.flatten = nn.Flatten()\n",
+ "\n",
+ " def construct(self, x):\n",
+ " x = self.flatten(x)\n",
+ " x = self.fc1(x)\n",
+ " x = self.relu(x)\n",
+ " x = self.fc2(x)\n",
+ " x = self.relu(x)\n",
+ " return x\n",
+ "\n",
+ "\n",
+ "class Decoder(nn.Cell):\n",
+ " def __init__(self):\n",
+ " super(Decoder, self).__init__()\n",
+ " self.fc1 = nn.Dense(400, 1024)\n",
+ " self.sigmoid = nn.Sigmoid()\n",
+ " self.reshape = ops.Reshape()\n",
+ "\n",
+ " def construct(self, z):\n",
+ " z = self.fc1(z)\n",
+ " z = self.reshape(z, IMAGE_SHAPE)\n",
+ " z = self.sigmoid(z)\n",
+ " return z\n",
+ "\n",
+ "\n",
+ "encoder = Encoder()\n",
+ "decoder = Decoder()\n",
+ "vae = VAE(encoder, decoder, hidden_size=400, latent_size=20)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### ConditionalVAE\n",
+ "\n",
+ "类似地,ConditionalVAE与VAE的使用方法比较相近,不同的是,ConditionalVAE利用了数据集的标签信息,属于有监督学习算法,其生成效果一般会比VAE好。\n",
+ "\n",
+ "首先,先自定义encoder和decoder,并调用`mindspore.nn.probability.dpn.ConditionalVAE`接口来构建ConditionalVAE网络,这里的encoder和VAE的不同,因为需要传入数据集的标签信息;decoder和上述的一样。ConditionalVAE接口的传入则还需要传入数据集的标签类别个数,其余和VAE接口一样。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import mindspore.nn as nn\n",
+ "import mindspore.ops as ops\n",
+ "from mindspore.nn.probability.dpn import ConditionalVAE\n",
+ "\n",
+ "IMAGE_SHAPE = (-1, 1, 32, 32)\n",
+ "\n",
+ "class Encoder(nn.Cell):\n",
+ " def __init__(self, num_classes):\n",
+ " super(Encoder, self).__init__()\n",
+ " self.fc1 = nn.Dense(1024 + num_classes, 400)\n",
+ " self.relu = nn.ReLU()\n",
+ " self.flatten = nn.Flatten()\n",
+ " self.concat = ops.Concat(axis=1)\n",
+ " self.one_hot = nn.OneHot(depth=num_classes)\n",
+ "\n",
+ " def construct(self, x, y):\n",
+ " x = self.flatten(x)\n",
+ " y = self.one_hot(y)\n",
+ " input_x = self.concat((x, y))\n",
+ " input_x = self.fc1(input_x)\n",
+ " input_x = self.relu(input_x)\n",
+ " return input_x\n",
+ "\n",
+ "class Decoder(nn.Cell):\n",
+ " def __init__(self):\n",
+ " super(Decoder, self).__init__()\n",
+ " self.fc1 = nn.Dense(400, 1024)\n",
+ " self.sigmoid = nn.Sigmoid()\n",
+ " self.reshape = ops.Reshape()\n",
+ "\n",
+ " def construct(self, z):\n",
+ " z = self.fc1(z)\n",
+ " z = self.reshape(z, IMAGE_SHAPE)\n",
+ " z = self.sigmoid(z)\n",
+ " return z\n",
+ "\n",
+ "encoder = Encoder(num_classes=10)\n",
+ "decoder = Decoder()\n",
+ "cvae = ConditionalVAE(encoder, decoder, hidden_size=400, latent_size=20, num_classes=10)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "加载数据集,我们可以使用Mnist数据集,具体的数据加载和预处理过程可以参考这里[实现一个图片分类应用](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/quick_start/quick_start.html),这里会用到`create_dataset`函数创建数据迭代器。\n",
+ "\n",
+ "直接执行下面代码,会自动进行训练集的下载与解压。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import os\n",
+ "os.system(\"wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/MNIST_Data.zip\")\n",
+ "os.system(\"unzip -o MNIST_Data.zip -d ./datasets\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "加载数据集,并进行数据增强操作。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import mindspore.dataset as ds\n",
+ "from mindspore.dataset.vision import Inter\n",
+ "from mindspore import dtype as mstype\n",
+ "import mindspore.dataset.vision.c_transforms as CV\n",
+ "import mindspore.dataset.transforms.c_transforms as C\n",
+ "\n",
+ "def create_dataset(data_path, batch_size=32, repeat_size=1,\n",
+ " num_parallel_workers=1):\n",
+ " \"\"\" create dataset for train or test\n",
+ " Args:\n",
+ " data_path: Data path\n",
+ " batch_size: The number of data records in each group\n",
+ " repeat_size: The number of replicated data records\n",
+ " num_parallel_workers: The number of parallel workers\n",
+ " \"\"\"\n",
+ " # define dataset\n",
+ " mnist_ds = ds.MnistDataset(data_path)\n",
+ "\n",
+ " # define operation parameters\n",
+ " resize_height, resize_width = 32, 32\n",
+ " rescale = 1.0 / 255.0\n",
+ " shift = 0.0\n",
+ "\n",
+ " # define map operations\n",
+ " resize_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR) # Resize images to (32, 32)\n",
+ " rescale_op = CV.Rescale(rescale, shift) # rescale images\n",
+ " hwc2chw_op = CV.HWC2CHW() # change shape from (height, width, channel) to (channel, height, width) to fit network.\n",
+ " type_cast_op = C.TypeCast(mstype.int32) # change data type of label to int32 to fit network\n",
+ "\n",
+ " # apply map operations on images\n",
+ " mnist_ds = mnist_ds.map(operations=type_cast_op, input_columns=\"label\", num_parallel_workers=num_parallel_workers)\n",
+ " mnist_ds = mnist_ds.map(operations=resize_op, input_columns=\"image\", num_parallel_workers=num_parallel_workers)\n",
+ " mnist_ds = mnist_ds.map(operations=rescale_op, input_columns=\"image\", num_parallel_workers=num_parallel_workers)\n",
+ " mnist_ds = mnist_ds.map(operations=hwc2chw_op, input_columns=\"image\", num_parallel_workers=num_parallel_workers)\n",
+ "\n",
+ " # apply DatasetOps\n",
+ " buffer_size = 10000\n",
+ " mnist_ds = mnist_ds.shuffle(buffer_size=buffer_size) # 10000 as in LeNet train script\n",
+ " mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True)\n",
+ " mnist_ds = mnist_ds.repeat(repeat_size)\n",
+ "\n",
+ " return mnist_ds\n",
+ "\n",
+ "image_path = \"./datasets/MNIST_Data/train\"\n",
+ "ds_train = create_dataset(image_path, 128, 1)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "接下来,需要用到infer接口进行VAE网络的变分推断。\n",
+ "\n",
+ "## 概率推断算法\n",
+ "\n",
+ "调用ELBO接口(`mindspore.nn.probability.infer.ELBO`)来定义VAE网络的损失函数,调用`WithLossCell`封装VAE网络和损失函数,并定义优化器,之后传入SVI接口(`mindspore.nn.probability.infer.SVI`)。SVI的`run`函数可理解为VAE网络的训练,可以指定训练的`epochs`,返回结果为训练好的网络;`get_train_loss`函数可以返回训练好后模型的loss。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "46.33266196291671\n"
+ ]
+ }
+ ],
+ "source": [
+ "from mindspore.nn.probability.infer import ELBO, SVI\n",
+ "\n",
+ "net_loss = ELBO(latent_prior='Normal', output_prior='Normal')\n",
+ "net_with_loss = nn.WithLossCell(vae, net_loss)\n",
+ "optimizer = nn.Adam(params=vae.trainable_params(), learning_rate=0.001)\n",
+ "\n",
+ "vi = SVI(net_with_loss=net_with_loss, optimizer=optimizer)\n",
+ "vae = vi.run(train_dataset=ds_train, epochs=10)\n",
+ "trained_loss = vi.get_train_loss()\n",
+ "print(trained_loss)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "最后,得到训练好的VAE网络后,我们可以使用`vae.generate_sample`生成新样本,需要传入待生成样本的个数,及生成样本的shape,shape需要保持和原数据集中的样本shape一样;当然,我们也可以使用`vae.reconstruct_sample`重构原来数据集中的样本,来测试VAE网络的重建能力。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The shape of the generated sample is (64, 1, 32, 32)\n"
+ ]
+ }
+ ],
+ "source": [
+ "generated_sample = vae.generate_sample(64, IMAGE_SHAPE)\n",
+ "for sample in ds_train.create_dict_iterator():\n",
+ " sample_x = Tensor(sample['image'], dtype=mstype.float32)\n",
+ " reconstructed_sample = vae.reconstruct_sample(sample_x)\n",
+ "print('The shape of the generated sample is ', generated_sample.shape)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "ConditionalVAE训练过程和VAE的过程类似,但需要注意的是使用训练好的ConditionalVAE网络生成新样本和重建新样本时,需要输入标签信息,例如下面生成的新样本就是64个0-7的数字。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The shape of the generated sample is (64, 1, 32, 32)\n"
+ ]
+ }
+ ],
+ "source": [
+ "sample_label = Tensor([i for i in range(0, 8)]*8, dtype=mstype.int32)\n",
+ "generated_sample = cvae.generate_sample(sample_label, 64, IMAGE_SHAPE)\n",
+ "for sample in ds_train.create_dict_iterator():\n",
+ " sample_x = Tensor(sample['image'], dtype=mstype.float32)\n",
+ " sample_y = Tensor(sample['label'], dtype=mstype.int32)\n",
+ " reconstructed_sample = cvae.reconstruct_sample(sample_x, sample_y)\n",
+ "print('The shape of the generated sample is ', generated_sample.shape)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "如果希望新生成的样本更好,更清晰,用户可以自己定义更复杂的encoder和decoder,这里的示例只用了两层全连接层,仅供示例的指导。\n",
+ "\n",
+ "## 贝叶斯层\n",
+ "\n",
+ "下面的范例使用MindSpore的`nn.probability.bnn_layers`中的API实现BNN图片分类模型。MindSpore的`nn.probability.bnn_layers`中的API包括`NormalPrior`,`NormalPosterior`,`ConvReparam`,`DenseReparam`,`DenseLocalReparam`和`WithBNNLossCell`。BNN与DNN的最大区别在于,BNN层的weight和bias不再是确定的值,而是服从一个分布。其中,`NormalPrior`,`NormalPosterior`分别用来生成服从正态分布的先验分布和后验分布;`ConvReparam`和`DenseReparam`分别是使用reparameteration方法实现的贝叶斯卷积层和全连接层;`DenseLocalReparam`是使用Local Reparameterization方法实现的贝叶斯全连接层;`WithBNNLossCell`是用来封装BNN和损失函数的。\n",
+ "\n",
+ "如何使用`nn.probability.bnn_layers`中的API构建贝叶斯神经网络并实现图片分类,可以参考教程[使用贝叶斯网络](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/apply_deep_probability_programming.html#id3)。\n",
+ "\n",
+ "## 贝叶斯转换\n",
+ "\n",
+ "对于不熟悉贝叶斯模型的研究人员,MDP提供了贝叶斯转换接口(`mindspore.nn.probability.transform`),支持DNN (Deep Neural Network)模型一键转换成BNN (Bayesian Neural Network)模型。\n",
+ "\n",
+ "其中的模型转换API`TransformToBNN`的`__init__`函数定义如下:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "class TransformToBNN:\n",
+ " def __init__(self, trainable_dnn, dnn_factor=1, bnn_factor=1):\n",
+ " net_with_loss = trainable_dnn.network\n",
+ " self.optimizer = trainable_dnn.optimizer\n",
+ " self.backbone = net_with_loss.backbone_network\n",
+ " self.loss_fn = getattr(net_with_loss, \"_loss_fn\")\n",
+ " self.dnn_factor = dnn_factor\n",
+ " self.bnn_factor = bnn_factor\n",
+ " self.bnn_loss_file = None"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "参数`trainable_bnn`是经过`TrainOneStepCell`包装的可训练DNN模型,`dnn_factor`和`bnn_factor`分别为由损失函数计算得到的网络整体损失的系数和每个贝叶斯层的KL散度的系数。\n",
+ "API`TransformToBNN`主要实现了两个功能:\n",
+ "\n",
+ "- 功能一:转换整个模型\n",
+ "\n",
+ " `transform_to_bnn_model`方法可以将整个DNN模型转换为BNN模型。其定义如下:\n",
+ "\n",
+ " ```python\n",
+ " def transform_to_bnn_model(self,\n",
+ " get_dense_args=lambda dp: {\"in_channels\": dp.in_channels, \"has_bias\": dp.has_bias,\n",
+ " \"out_channels\": dp.out_channels, \"activation\": dp.activation},\n",
+ " get_conv_args=lambda dp: {\"in_channels\": dp.in_channels, \"out_channels\": dp.out_channels,\n",
+ " \"pad_mode\": dp.pad_mode, \"kernel_size\": dp.kernel_size,\n",
+ " \"stride\": dp.stride, \"has_bias\": dp.has_bias,\n",
+ " \"padding\": dp.padding, \"dilation\": dp.dilation,\n",
+ " \"group\": dp.group},\n",
+ " add_dense_args=None,\n",
+ " add_conv_args=None):\n",
+ " r\"\"\"\n",
+ " Transform the whole DNN model to BNN model, and wrap BNN model by TrainOneStepCell.\n",
+ "\n",
+ " Args:\n",
+ " get_dense_args (function): The arguments gotten from the DNN full connection layer. Default: lambda dp:\n",
+ " {\"in_channels\": dp.in_channels, \"out_channels\": dp.out_channels, \"has_bias\": dp.has_bias}.\n",
+ " get_conv_args (function): The arguments gotten from the DNN convolutional layer. Default: lambda dp:\n",
+ " {\"in_channels\": dp.in_channels, \"out_channels\": dp.out_channels, \"pad_mode\": dp.pad_mode,\n",
+ " \"kernel_size\": dp.kernel_size, \"stride\": dp.stride, \"has_bias\": dp.has_bias}.\n",
+ " add_dense_args (dict): The new arguments added to BNN full connection layer. Default: {}.\n",
+ " add_conv_args (dict): The new arguments added to BNN convolutional layer. Default: {}.\n",
+ "\n",
+ " Returns:\n",
+ " Cell, a trainable BNN model wrapped by TrainOneStepCell.\n",
+ " \"\"\"\n",
+ "\n",
+ " ```\n",
+ "\n",
+ " 参数`get_dense_args`指定从DNN模型的全连接层中获取哪些参数,默认值是DNN模型的全连接层和BNN的全连接层所共有的参数,参数具体的含义可以参考[API说明文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/nn/mindspore.nn.Dense.html);`get_conv_args`指定从DNN模型的卷积层中获取哪些参数,默认值是DNN模型的卷积层和BNN的卷积层所共有的参数,参数具体的含义可以参考[API说明文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/nn/mindspore.nn.Conv2d.html);参数`add_dense_args`和`add_conv_args`分别指定了要为BNN层指定哪些新的参数值。需要注意的是,`add_dense_args`中的参数不能与`get_dense_args`重复,`add_conv_args`和`get_conv_args`也是如此。\n",
+ "\n",
+ "- 功能二:转换指定类型的层\n",
+ "\n",
+ " `transform_to_bnn_layer`方法可以将DNN模型中指定类型的层(`nn.Dense`或者`nn.Conv2d`)转换为对应的贝叶斯层。其定义如下:\n",
+ "\n",
+ " ```python\n",
+ " def transform_to_bnn_layer(self, dnn_layer, bnn_layer, get_args=None, add_args=None):\n",
+ " r\"\"\"\n",
+ " Transform a specific type of layers in DNN model to corresponding BNN layer.\n",
+ "\n",
+ " Args:\n",
+ " dnn_layer_type (Cell): The type of DNN layer to be transformed to BNN layer. The optional values are\n",
+ " nn.Dense, nn.Conv2d.\n",
+ " bnn_layer_type (Cell): The type of BNN layer to be transformed to. The optional values are\n",
+ " DenseReparameterization, ConvReparameterization.\n",
+ " get_args (dict): The arguments gotten from the DNN layer. Default: None.\n",
+ " add_args (dict): The new arguments added to BNN layer. Default: None.\n",
+ "\n",
+ " Returns:\n",
+ " Cell, a trainable model wrapped by TrainOneStepCell, whose sprcific type of layer is transformed to the corresponding bayesian layer.\n",
+ " \"\"\"\n",
+ " ```\n",
+ "\n",
+ " 参数`dnn_layer`指定将哪个类型的DNN层转换成BNN层,`bnn_layer`指定DNN层将转换成哪个类型的BNN层,`get_args`和`add_args`分别指定从DNN层中获取哪些参数和要为BNN层的哪些参数重新赋值。\n",
+ "\n",
+ "如何在MindSpore中使用API`TransformToBNN`可以参考教程[DNN一键转换成BNN](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/apply_deep_probability_programming.html#dnnbnn)。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 贝叶斯工具箱\n",
+ "\n",
+ "### 不确定性评估\n",
+ "\n",
+ "贝叶斯神经网络的优势之一就是可以获取不确定性,MDP在上层提供了不确定性估计的工具箱(`mindspore.nn.probability.toolbox`),用户可以很方便地使用该工具箱计算不确定性。不确定性意味着深度学习模型对预测结果的不确定程度。目前,大多数深度学习算法只能给出高置信度的预测结果,而不能判断预测结果的确定性,不确定性主要有两种类型:偶然不确定性和认知不确定性。\n",
+ "\n",
+ "- 偶然不确定性(Aleatoric Uncertainty):描述数据中的内在噪声,即无法避免的误差,这个现象不能通过增加采样数据来削弱。\n",
+ "\n",
+ "- 认知不确定性(Epistemic Uncertainty):模型自身对输入数据的估计可能因为训练不佳、训练数据不够等原因而不准确,可以通过增加训练数据等方式来缓解。\n",
+ "\n",
+ "不确定性评估工具箱的接口如下:\n",
+ "\n",
+ "- `model`:待评估不确定性的已训练好的模型。\n",
+ "\n",
+ "- `train_dataset`:用于训练的数据集,迭代器类型。\n",
+ "\n",
+ "- `task_type`:模型的类型,字符串,输入“regression”或者“classification”。\n",
+ "\n",
+ "- `num_classes`:如果是分类模型,需要指定类别的标签数量。\n",
+ "\n",
+ "- `epochs`:用于训练不确定模型的迭代数。\n",
+ "\n",
+ "- `epi_uncer_model_path`:用于存储或加载计算认知不确定性的模型的路径。\n",
+ "\n",
+ "- `ale_uncer_model_path`:用于存储或加载计算偶然不确定性的模型的路径。\n",
+ "\n",
+ "- `save_model`:布尔类型,是否需要存储模型。\n",
+ "\n",
+ "在使用前,需要先训练好模型,以LeNet5为例,使用方式如下:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "epoch: 1 step: 1, loss is 0.11922359\n",
+ "epoch: 1 step: 2, loss is 0.21615174\n",
+ "epoch: 1 step: 3, loss is 0.18693243\n",
+ "epoch: 1 step: 4, loss is 0.14668123\n",
+ "epoch: 1 step: 5, loss is 0.32135463\n",
+ "epoch: 1 step: 6, loss is 0.086044185\n",
+ "... ...\n",
+ "epoch: 1 step: 1872, loss is 0.07358544\n",
+ "epoch: 1 step: 1873, loss is 0.006983331\n",
+ "epoch: 1 step: 1874, loss is 0.122501254\n",
+ "epoch: 1 step: 1875, loss is 0.02729987\n",
+ "The shape of epistemic uncertainty is (32, 10)\n",
+ "The shape of aleatoric uncertainty is (32,)\n"
+ ]
+ }
+ ],
+ "source": [
+ "import os\n",
+ "import mindspore.nn as nn\n",
+ "from mindspore import Tensor\n",
+ "from mindspore.common.initializer import Normal\n",
+ "from mindspore.nn.probability.toolbox.uncertainty_evaluation import UncertaintyEvaluation\n",
+ "from mindspore import load_checkpoint, load_param_into_net\n",
+ "\n",
+ "class LeNet5(nn.Cell):\n",
+ " \"\"\"Lenet network structure.\"\"\"\n",
+ " # define the operator required\n",
+ " def __init__(self, num_class=10, num_channel=1):\n",
+ " super(LeNet5, self).__init__()\n",
+ " self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')\n",
+ " self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')\n",
+ " self.fc1 = nn.Dense(16*5*5, 120, weight_init=Normal(0.02))\n",
+ " self.fc2 = nn.Dense(120, 84, weight_init=Normal(0.02))\n",
+ " self.fc3 = nn.Dense(84, num_class, weight_init=Normal(0.02))\n",
+ " self.relu = nn.ReLU()\n",
+ " self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)\n",
+ " self.flatten = nn.Flatten()\n",
+ "\n",
+ " # use the preceding operators to construct networks\n",
+ " def construct(self, x):\n",
+ " x = self.max_pool2d(self.relu(self.conv1(x)))\n",
+ " x = self.max_pool2d(self.relu(self.conv2(x)))\n",
+ " x = self.flatten(x)\n",
+ " x = self.relu(self.fc1(x))\n",
+ " x = self.relu(self.fc2(x))\n",
+ " x = self.fc3(x)\n",
+ " return x\n",
+ "\n",
+ "if __name__ == '__main__':\n",
+ " # get trained model checkpoint_lenet-1_1875.ckpt\n",
+ " os.system(\"wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/checkpoint_lenet-1_1875.zip\")\n",
+ " os.system(\"unzip -o checkpoint_lenet-1_1875.zip\")\n",
+ " \n",
+ " network = LeNet5()\n",
+ " param_dict = load_checkpoint('checkpoint_lenet-1_1875.ckpt')\n",
+ " load_param_into_net(network, param_dict)\n",
+ " # get train and eval dataset\n",
+ " ds_train = create_dataset('./datasets/MNIST_Data/train')\n",
+ " ds_eval = create_dataset('./datasets/MNIST_Data/test')\n",
+ " evaluation = UncertaintyEvaluation(model=network,\n",
+ " train_dataset=ds_train,\n",
+ " task_type='classification',\n",
+ " num_classes=10,\n",
+ " epochs=1,\n",
+ " epi_uncer_model_path=None,\n",
+ " ale_uncer_model_path=None,\n",
+ " save_model=False)\n",
+ " for eval_data in ds_eval.create_dict_iterator():\n",
+ " eval_data = Tensor(eval_data['image'], mstype.float32)\n",
+ " epistemic_uncertainty = evaluation.eval_epistemic_uncertainty(eval_data)\n",
+ " aleatoric_uncertainty = evaluation.eval_aleatoric_uncertainty(eval_data)\n",
+ " print('The shape of epistemic uncertainty is ', epistemic_uncertainty.shape)\n",
+ " print('The shape of aleatoric uncertainty is ', aleatoric_uncertainty.shape)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "`eval_epistemic_uncertainty`计算的是认知不确定性,也叫模型不确定性,对于每一个样本的每个预测标签都会有一个不确定值;\n",
+ "\n",
+ "`eval_aleatoric_uncertainty`计算的是偶然不确定性,也叫数据不确定性,对于每一个样本都会有一个不确定值。\n",
+ "\n",
+ "uncertainty的值大于等于0,越大表示不确定性越高。"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 异常检测\n",
+ "\n",
+ "异常检测(Anomaly Detection)可以找到与“主要数据分布不同”的异常值,比如在数据预处理中找出异常点,有助于提升模型的拟合能力。\n",
+ "\n",
+ "MDP在上层基于变分自编码器(VAE)提供了异常检测的工具箱(`VAEAnomalyDetection`),与VAE的使用类似,我们只需要自定义编码器和解码器(DNN模型),初始化相关参数,便可以使用该工具箱检测异常点。\n",
+ "\n",
+ "基于VAE的异常检测工具箱的接口如下:\n",
+ "\n",
+ "- `encoder`:编码器(Cell类型)。\n",
+ "\n",
+ "- `decoder`:解码器(Cell类型)。\n",
+ "\n",
+ "- `hidden_size`:编码器输出张量的大小。\n",
+ "\n",
+ "- `latent_size`:隐空间的大小。\n",
+ "\n",
+ "构建VAE模型的编码器`EncoderVAE`和解码器`DecoderVAE`,设置`hidden_size`和`latent_size`,进行类的初始化,之后传入数据集可以进行异常点的检测。"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "30.0681 False\n",
+ "33.119724 False\n",
+ "34.285515 False\n",
+ "34.41275 False\n",
+ "35.29742 False\n",
+ "... ...\n",
+ "33.438026 False\n",
+ "34.576874 False\n",
+ "33.033386 False\n",
+ "35.304153 False\n",
+ "35.387157 False\n"
+ ]
+ }
+ ],
+ "source": [
+ "import mindspore.nn as nn\n",
+ "import mindspore.ops as ops\n",
+ "from mindspore.nn.probability.toolbox import VAEAnomalyDetection\n",
+ "\n",
+ "class EncoderVAE(nn.Cell):\n",
+ " def __init__(self):\n",
+ " super(EncoderVAE, self).__init__()\n",
+ " self.fc1 = nn.Dense(1024, 800)\n",
+ " self.fc2 = nn.Dense(800, 400)\n",
+ " self.relu = nn.ReLU()\n",
+ " self.flatten = nn.Flatten()\n",
+ "\n",
+ " def construct(self, x):\n",
+ " x = self.flatten(x)\n",
+ " x = self.fc1(x)\n",
+ " x = self.relu(x)\n",
+ " x = self.fc2(x)\n",
+ " x = self.relu(x)\n",
+ " return x\n",
+ "\n",
+ "\n",
+ "class DecoderVAE(nn.Cell):\n",
+ " def __init__(self):\n",
+ " super(DecoderVAE, self).__init__()\n",
+ " self.fc1 = nn.Dense(400, 1024)\n",
+ " self.sigmoid = nn.Sigmoid()\n",
+ " self.reshape = ops.Reshape()\n",
+ "\n",
+ " def construct(self, z):\n",
+ " z = self.fc1(z)\n",
+ " z = self.reshape(z, IMAGE_SHAPE)\n",
+ " z = self.sigmoid(z)\n",
+ " return z\n",
+ "\n",
+ "if __name__ == '__main__':\n",
+ "\n",
+ " encodervae = EncoderVAE()\n",
+ " decodervae = DecoderVAE()\n",
+ " ood = VAEAnomalyDetection(encoder=encodervae, decoder=decodervae,\n",
+ " hidden_size=400, latent_size=20)\n",
+ " ds_train = create_dataset('./datasets/MNIST_Data/train')\n",
+ " ds_eval = create_dataset('./datasets/MNIST_Data/test')\n",
+ " ood.train(ds_train, epochs=5)\n",
+ " for sample in ds_train.create_dict_iterator(output_numpy=True, num_epochs=1):\n",
+ " sample_x = Tensor(sample['image'], dtype=mstype.float32)\n",
+ " score = ood.predict_outlier_score(sample_x)\n",
+ " outlier = ood.predict_outlier(sample_x)\n",
+ " print(score, outlier)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "`score`输出的是样本的异常分数;`outlier`是布尔类型,True代表是异常点,False代表不是异常点。"
+ ]
+ }
+ ],
+ "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/docs/programming_guide/source_zh_cn/probability.md b/docs/programming_guide/source_zh_cn/probability.md
deleted file mode 100644
index a693fc869e8db75a772c054f5f7b3e6c1efccce5..0000000000000000000000000000000000000000
--- a/docs/programming_guide/source_zh_cn/probability.md
+++ /dev/null
@@ -1,1096 +0,0 @@
-# 深度概率编程库
-
-
-
-- [深度概率编程库](#深度概率编程库)
- - [概率分布](#概率分布)
- - [概率分布类](#概率分布类)
- - [Distribution基类](#distribution基类)
- - [伯努利分布(Bernoulli)](#伯努利分布bernoulli)
- - [指数分布(Exponential)](#指数分布exponential)
- - [几何分布(Geometric)](#几何分布geometric)
- - [正态分布(Normal)](#正态分布normal)
- - [均匀分布(Uniform)](#均匀分布uniform)
- - [多类别分布(Categorical)](#多类别分布categorical)
- - [柯西分布(Cauchy)](#柯西分布cauchy)
- - [对数正态分布分布(LogNormal)](#对数正态分布lognormal)
- - [耿贝尔极值分布(Gumbel)](#耿贝尔极值分布gumbel)
- - [逻辑斯谛分布(Logistic)](#逻辑斯谛分布logistic)
- - [泊松分布(Poisson)](#对数正态分布lognormal)
- - [伽马分布(Gamma)](#伽马分布gamma)
- - [贝塔分布(Beta)](#贝塔分布beta)
- - [概率分布类在PyNative模式下的应用](#概率分布类在pynative模式下的应用)
- - [概率分布类在图模式下的应用](#概率分布类在图模式下的应用)
- - [TransformedDistribution类接口设计](#transformeddistribution类接口设计)
- - [PyNative模式下调用TransformedDistribution实例](#pynative模式下调用transformeddistribution实例)
- - [图模式下调用TransformedDistribution实例](#图模式下调用transformeddistribution实例)
- - [概率分布映射](#概率分布映射)
- - [Bijector类接口设计](#bijector类接口设计)
- - [Bijector基类](#bijector基类)
- - [幂函数变换映射(PowerTransform)](#幂函数变换映射powertransform)
- - [指数变换映射(Exp)](#指数变换映射exp)
- - [标量仿射变换映射(ScalarAffine)](#标量仿射变换映射scalaraffine)
- - [Softplus变换映射(Softplus)](#softplus变换映射softplus)
- - [耿贝尔累计密度函数映射(GumbelCDF)](#耿贝尔累计密度函数映射gumbelcdf)
- - [逆映射(Invert)](#逆映射Invert)
- - [PyNative模式下调用Bijector实例](#pynative模式下调用bijector实例)
- - [图模式下调用Bijector实例](#图模式下调用bijector实例)
- - [深度概率网络](#深度概率网络)
- - [VAE](#vae)
- - [ConditionalVAE](#conditionalvae)
- - [概率推断算法](#概率推断算法)
- - [贝叶斯层](#贝叶斯层)
- - [贝叶斯转换](#贝叶斯转换)
- - [贝叶斯工具箱](#贝叶斯工具箱)
- - [不确定性评估](#不确定性评估)
- - [异常检测](#异常检测)
-
-
-
-
-
-MindSpore深度概率编程的目标是将深度学习和贝叶斯学习结合,包括概率分布、概率分布映射、深度概率网络、概率推断算法、贝叶斯层、贝叶斯转换和贝叶斯工具箱,面向不同的开发者。对于专业的贝叶斯学习用户,提供概率采样、推理算法和模型构建库;另一方面,为不熟悉贝叶斯深度学习的用户提供了高级的API,从而不用更改深度学习编程逻辑,即可利用贝叶斯模型。
-
-## 概率分布
-
-概率分布(`mindspore.nn.probability.distribution`)是概率编程的基础。`Distribution` 类提供多样的概率统计接口,例如概率密度函数 *pdf* 、累积密度函数 *cdf* 、散度计算 *kl_loss* 、抽样 *sample* 等。现有的概率分布实例包括高斯分布,伯努利分布,指数型分布,几何分布和均匀分布。
-
-### 概率分布类
-
-- `Distribution`:所有概率分布的基类。
-
-- `Bernoulli`:伯努利分布。参数为试验成功的概率。
-
-- `Exponential`:指数型分布。参数为率参数。
-
-- `Geometric`:几何分布。参数为一次伯努利试验成功的概率。
-
-- `Normal`:正态(高斯)分布。参数为均值和标准差。
-
-- `Uniform`:均匀分布。参数为数轴上的最小值和最大值。
-
-- `Categorical`:类别分布。每种类别出现的概率。
-
-- `LogNormal`:对数正态分布。参数为位置参数和规模参数。
-
-- `Gumbel`: 耿贝尔极值分布。参数为位置参数和规模参数。
-
-- `Logistic`:逻辑斯谛分布。参数为位置参数和规模参数。
-
-- `Cauchy`:柯西分布。参数为位置参数和规模参数。
-
-#### Distribution基类
-
-`Distribution` 是所有概率分布的基类。
-
-接口介绍:`Distribution` 类支持的函数包括 `prob`、`log_prob`、`cdf`、`log_cdf`、`survival_function`、`log_survival`、`mean`、`sd`、`var`、`entropy`、`kl_loss`、`cross_entropy` 和 `sample` 。分布不同,所需传入的参数也不同。只有在派生类中才能使用,由派生类的函数实现决定参数。
-
-- `prob` :概率密度函数(PDF)/ 概率质量函数(PMF)。
-- `log_prob` :对数似然函数。
-- `cdf` :累积分布函数(CDF)。
-- `log_cdf` :对数累积分布函数。
-- `survival_function` :生存函数。
-- `log_survival` :对数生存函数。
-- `mean` :均值。
-- `sd` :标准差。
-- `var` :方差。
-- `entropy` :熵。
-- `kl_loss` :Kullback-Leibler 散度。
-- `cross_entropy` :两个概率分布的交叉熵。
-- `sample` :概率分布的随机抽样。
-- `get_dist_args` :概率分布在网络中使用的参数。
-- `get_dist_type` :概率分布的类型。
-
-#### 伯努利分布(Bernoulli)
-
-伯努利分布,继承自 `Distribution` 类。
-
-属性:
-
-- `Bernoulli.probs`:返回伯努利试验成功的概率,类型为`Tensor`。
-
-`Distribution` 基类调用 `Bernoulli` 中私有接口以实现基类中的公有接口。`Bernoulli` 支持的公有接口为:
-
-- `mean`,`mode`,`var`,`sd`:可选择传入 试验成功的概率 *probs1* 。
-- `entropy`:可选择传入 试验成功的概率 *probs1* 。
-- `cross_entropy`,`kl_loss`:必须传入 *dist* 和 *probs1_b* 。*dist* 为另一分布的类型,目前只支持此处为 *‘Bernoulli’* 。 *probs1_b* 为分布 *b* 的试验成功概率。可选择传入分布 *a* 的参数 *probs1_a* 。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。可选择传入试验成功的概率 *probs* 。
-- `sample`:可选择传入样本形状 *shape* 和试验成功的概率 *probs1* 。
-- `get_dist_args` :可选择传入试验成功的概率 *probs*。返回值为`(probs,)`,类型为tuple。
-- `get_dist_type` :返回 *‘Bernoulli’* 。
-
-#### 指数分布(Exponential)
-
-指数分布,继承自 `Distribution` 类。
-
-属性:
-
-- `Exponential.rate`:返回分布的率参数,类型为`Tensor`。
-
-`Distribution` 基类调用 `Exponential` 私有接口以实现基类中的公有接口。`Exponential` 支持的公有接口为:
-
-- `mean`,`mode`,`var`,`sd`:可选择传入率参数 *rate* 。
-- `entropy`:可选择传入率参数 *rate* 。
-- `cross_entropy`,`kl_loss`:必须传入 *dist* 和 *rate_b* 。 *dist* 为另一分布的类型的名称, 目前只支持此处为 *‘Exponential’* 。*rate_b* 为分布 *b* 的率参数。可选择传入分布 *a* 的参数 *rate_a* 。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。可选择传入率参数 *rate* 。
-- `sample`:可选择传入样本形状 *shape* 和率参数 *rate* 。
-- `get_dist_args` :可选择传入率参数 *rate* 。返回值为`(rate,)`,类型为tuple。
-- `get_dist_type` :返回 *‘Exponential’* 。
-
-#### 几何分布(Geometric)
-
-几何分布,继承自 `Distribution` 类。
-
-属性:
-
-- `Geometric.probs`:返回伯努利试验成功的概率,类型为`Tensor`。
-
-`Distribution` 基类调用 `Geometric` 中私有接口以实现基类中的公有接口。`Geometric` 支持的公有接口为:
-
-- `mean`,`mode`,`var`,`sd`:可选择传入试验成功的概率 *probs1* 。
-- `entropy`:可选择传入 试验成功的概率 *probs1* 。
-- `cross_entropy`,`kl_loss`:必须传入 *dist* 和 *probs1_b* 。*dist* 为另一分布的类型的名称,目前只支持此处为 *‘Geometric’* 。 *probs1_b* 为分布 *b* 的试验成功概率。可选择传入分布 *a* 的参数 *probs1_a* 。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。可选择传入试验成功的概率 *probs1* 。
-- `sample`:可选择传入样本形状 *shape* 和试验成功的概率 *probs1* 。
-- `get_dist_args` :可选择传入试验成功的概率 *probs1* 。返回值为`(probs1,)`,类型为tuple。
-- `get_dist_type` :返回 *‘Geometric’* 。
-
-#### 正态分布(Normal)
-
-正态(高斯)分布,继承自 `Distribution` 类。
-
-`Distribution` 基类调用 `Normal` 中私有接口以实现基类中的公有接口。`Normal` 支持的公有接口为:
-
-- `mean`,`mode`,`var`,`sd`:可选择传入分布的参数均值 *mean* 和标准差 *sd* 。
-- `entropy`:可选择传入分布的参数均值 *mean* 和标准差 *sd* 。
-- `cross_entropy`,`kl_loss`:必须传入 *dist* ,*mean_b* 和 *sd_b* 。*dist* 为另一分布的类型的名称,目前只支持此处为 *‘Normal’* 。*mean_b* 和 *sd_b* 为分布 *b* 的均值和标准差。可选择传入分布的参数 *a* 均值 *mean_a* 和标准差 *sd_a* 。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。可选择分布的参数包括均值 *mean_a* 和标准差 *sd_a* 。
-- `sample`:可选择传入样本形状 *shape* 和分布的参数包括均值 *mean_a* 和标准差 *sd_a* 。
-- `get_dist_args` :可选择传入分布的参数均值 *mean* 和标准差 *sd* 。返回值为`(mean, sd)`,类型为tuple。
-- `get_dist_type` :返回 *‘Normal’* 。
-
-#### 均匀分布(Uniform)
-
-均匀分布,继承自 `Distribution` 类。
-
-属性:
-
-- `Uniform.low`:返回分布的最小值,类型为`Tensor`。
-- `Uniform.high`:返回分布的最大值,类型为`Tensor`。
-
-`Distribution` 基类调用 `Uniform` 以实现基类中的公有接口。`Uniform` 支持的公有接口为:
-
-- `mean`,`mode`,`var`,`sd`:可选择传入分布的参数最大值 *high* 和最小值 *low* 。
-- `entropy`:可选择传入分布的参数最大值 *high* 和最小值 *low* 。
-- `cross_entropy`,`kl_loss`:必须传入 *dist* ,*high_b* 和 *low_b* 。*dist* 为另一分布的类型的名称,目前只支持此处为 *‘Uniform’* 。 *high_b* 和 *low_b* 为分布 *b* 的参数。可选择传入分布 *a* 的参数即最大值 *high_a* 和最小值 *low_a* 。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。可选择传入分布的参数最大值 *high* 和最小值 *low* 。
-- `sample`:可选择传入 *shape* 和分布的参数即最大值 *high* 和最小值 *low* 。
-- `get_dist_args` :可选择传入分布的参数最大值 *high* 和最小值 *low* 。返回值为`(low, high)`,类型为tuple。
-- `get_dist_type` :返回 *‘Uniform’* 。
-
-#### 多类别分布(Categorical)
-
-多类别分布,继承自 `Distribution` 类。
-
-属性:
-
-- `Categorical.probs`:返回各种类别的概率,类型为`Tensor`。
-
-`Distribution` 基类调用 `Categorical` 以实现基类中的公有接口。`Categorical` 支持的公有接口为:
-
-- `mean`,`mode`,`var`,`sd`:可选择传入分布的参数类别概率 *probs*。
-- `entropy`:可选择传入分布的参数类别概率 *probs* 。
-- `cross_entropy`,`kl_loss`:必须传入 *dist* ,*probs_b* 。*dist* 为另一分布的类型的名称,目前只支持此处为 *‘Categorical’* 。 *probs_b* 为分布 *b* 的参数。可选择传入分布 *a* 的参数即 *probs_a* 。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。可选择传入分布的参数类别概率 *probs* 。
-- `sample`:可选择传入 *shape* 和类别概率 *probs* 。
-- `get_dist_args` :可选择传入分布的参数类别概率 *probs* 。返回值为`(probs,)`,类型为tuple。
-- `get_dist_type` :返回 *‘Categorical’* 。
-
-#### 对数正态分布(LogNormal)
-
-对数正态分布,继承自 `TransformedDistribution` 类,由 `Exp` Bijector 和 `Normal` Distribution 构成。
-
-属性:
-
-- `LogNormal.loc`:返回分布的位置参数,类型为`Tensor`。
-- `LogNormal.scale`:返回分布的规模参数,类型为`Tensor`。
-
-`Distribution` 基类调用 `LogNormal`及 `TransformedDistribution` 中私有接口以实现基类中的公有接口。`LogNormal` 支持的公有接口为:
-
-- `mean`,`mode`,`var`,`sd`:可选择传入分布的位置参数*loc*和规模参数*scale* 。
-- `entropy`:可选择传入分布的位置参数 *loc* 和规模参数 *scale* 。
-- `cross_entropy`,`kl_loss`:必须传入 *dist* ,*loc_b* 和 *scale_b* 。*dist* 为另一分布的类型的名称,目前只支持此处为 *‘LogNormal’* 。*loc_b* 和 *scale_b* 为分布 *b* 的均值和标准差。可选择传入分布的参数 *a* 均值 *loc_a* 和标准差 *sclae_a* 。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。可选择分布的参数包括均值 *loc_a* 和标准差 *scale_a* 。`Distribution` 基类调用 `TransformedDistribution`私有接口。
-- `sample`:可选择传入样本形状 *shape* 和分布的参数包括均值 *loc_a* 和标准差 *scale_a* 。`Distribution` 基类调用 `TransformedDistribution`私有接口。
-- `get_dist_args` :可选择传入分布的位置参数 *loc* 和规模参数*scale* 。返回值为`(loc, scale)`,类型为tuple。
-- `get_dist_type` :返回 *‘LogNormal’* 。
-
-#### 柯西分布(Cauchy)
-
-柯西分布,继承自 `Distribution` 类。
-
-属性:
-
-- `Cauchy.loc`:返回分布的位置参数,类型为`Tensor`。
-- `Cauchy.scale`:返回分布的规模参数,类型为`Tensor`。
-
-`Distribution` 基类调用 `Cauchy` 中私有接口以实现基类中的公有接口。`Cauchy` 支持的公有接口为:
-
-- `entropy`:可选择传入分布的位置参数*loc*和规模参数*scale*。
-- `cross_entropy`,`kl_loss`:必须传入 *dist* ,*loc_b* 和 *scale_b* 。*dist* 为另一分布的类型的名称,目前只支持此处为 *‘Cauchy’* 。*loc_b* 和 *scale_b* 为分布 *b* 的位置参数和规模参数。可选择传入分布的参数 *a* 位置 *loc_a* 和规模 *scale_a* 。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。可选择传入分布的位置参数 *loc* 和规模参数 *scale* 。
-- `sample`:可选择传入样本形状 *shape* 和分布的参数包括分布的位置参数 *loc* 和规模参数 *scale* 。
-- `get_dist_args` :可选择传入分布的位置参数 *loc* 和规模参数 *scale* 。返回值为`(loc, scale)`,类型为tuple。
-- `get_dist_type` :返回 *‘Cauchy’* 。
-
-#### 耿贝尔极值分布(Gumbel)
-
-耿贝尔极值分布,继承自 `TransformedDistribution` 类,由 `GumbelCDF` Bijector和 `Uniform` Distribution 构成。
-
-属性:
-
-- `Gumbel.loc`:返回分布的位置参数,类型为`Tensor`。
-- `Gumbel.scale`:返回分布的规模参数,类型为`Tensor`。
-
-`Distribution` 基类调用 `Gumbel` 中私有接口以实现基类中的公有接口。`Gumbel` 支持的公有接口为:
-
-- `mean`,`mode`,`var`,`sd`:无参数 。
-- `entropy`:无参数 。
-- `cross_entropy`,`kl_loss`:必须传入 *dist* ,*loc_b* 和 *scale_b* 。*dist* 为另一分布的类型的名称,目前只支持此处为 *‘Gumbel’* 。*loc_b* 和 *scale_b* 为分布 *b* 的位置参数和规模参数。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。
-- `sample`:可选择传入样本形状 *shape* 。
-- `get_dist_args` :可选择传入分布的位置参数 *loc* 和规模参数 *scale* 。返回值为`(loc, scale)`,类型为tuple。
-- `get_dist_type` :返回 *‘Gumbel’* 。
-
-#### 逻辑斯谛分布(Logistic)
-
-逻辑斯谛分布,继承自 `Distribution` 类。
-
-属性:
-
-- `Logistic.loc`:返回分布的位置参数,类型为`Tensor`。
-- `Logistic.scale`:返回分布的规模参数,类型为`Tensor`。
-
-`Distribution` 基类调用 `Logistic` 中私有接口以实现基类中的公有接口。`Logistic` 支持的公有接口为:
-
-- `mean`,`mode`,`var`,`sd`:可选择传入分布的位置参数 *loc* 和规模参数 *scale* 。
-- `entropy`:可选择传入分布的位置参数 *loc* 和规模参数 *scale* 。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。可选择传入分布的位置参数 *loc* 和规模参数 *scale* 。
-- `sample`:可选择传入样本形状 *shape* 和分布的参数包括分布的位置参数 *loc* 和规模参数 *scale* 。
-- `get_dist_args` :可选择传入分布的位置参数 *loc* 和规模参数 *scale* 。返回值为`(loc, scale)`,类型为tuple。
-- `get_dist_type` :返回 *‘Logistic’* 。
-
-#### 泊松分布(Poisson)
-
-泊松分布,继承自 `Distribution` 类。
-
-属性:
-
-- `Poisson.rate`:返回分布的率参数,类型为`Tensor`。
-
-`Distribution` 基类调用 `Poisson` 中私有接口以实现基类中的公有接口。`Poisson` 支持的公有接口为:
-
-- `mean`,`mode`,`var`,`sd`:可选择传入分布的率参数 *rate* 。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。可选择传入分布的率参数 *rate* 。
-- `sample`:可选择传入样本形状 *shape* 和分布的率参数 *rate* 。
-- `get_dist_args` :可选择传入分布的率参数 *rate* 。返回值为`(rate,)`,类型为tuple。
-- `get_dist_type` :返回 *‘Poisson’* 。
-
-#### 伽马分布(Gamma)
-
-伽马分布,继承自 `Distribution` 类。
-
-属性:
-
-- `Gamma.concentration`:返回分布的参数 `concentration` ,类型为`Tensor`。
-- `Gamma.rate`:返回分布的参数 `rate` ,类型为`Tensor`。
-
-`Distribution` 基类调用 `Gamma` 中私有接口以实现基类中的公有接口。`Gamma` 支持的公有接口为:
-
-- `mean`,`mode`,`sd`,`var`:可选择传入分布的参数 *concentration* 和参数 *rate* 。
-- `entropy`:可选择传入分布的参数 *concentration* 和参数 *rate* 。
-- `prob`,`log_prob`,`cdf`,`log_cdf`,`survival_function`,`log_survival`:必须传入 *value* 。可选择传入分布的参数 *concentration* 和参数 *rate* 。
-- `cross_entropy`,`kl_loss`:必须传入 *dist* ,*concentration_b* 和 *rate_b* 。*dist* 为另一分布的类型的名称,目前只支持此处为 *‘Gamma’* 。*concentration_b* 和 *rate_b* 为分布 *b* 的参数。可选择传入分布 *a* 的参数即 *concentration_a* 和 *rate_a* 。
-- `sample`:可选择传入样本形状 *shape* 和分布的参数包括分布的参数 *concentration* 和参数 *rate* 。
-- `get_dist_args` :可选择传入分布的参数 *concentration* 和参数 *rate* 。返回值为`(concentration, rate)`,类型为tuple。
-- `get_dist_type` :返回 *‘Gamma’* 。
-
-#### 贝塔分布(Beta)
-
-贝塔分布,继承自 `Distribution` 类。
-
-属性:
-
-- `Beta.concentration1`:返回分布的参数 `concentration1` ,类型为`Tensor`。
-- `Beta.concentration0`:返回分布的参数 `concentration0` ,类型为`Tensor`。
-
-`Distribution` 基类调用 `Beta` 中私有接口以实现基类中的公有接口。`Beta` 支持的公有接口为:
-
-- `mean`,`mode`,`sd`,`var`:可选择传入分布的参数 *concentration1* 和参数 *concentration0* 。
-- `entropy`:可选择传入分布的参数 *concentration1* 和参数 *concentration0* 。
-- `prob`,`log_prob`:必须传入 *value* 。可选择传入分布的参数 *concentration1* 和参数 *concentration0* 。
-- `cross_entropy`,`kl_loss`:必须传入 *dist* ,*concentration1_b* 和 *concentration1_b* 。*dist* 为另一分布的类型的名称,目前只支持此处为 *‘Beta’* 。*concentration1_b* 和 *concentration1_b* 为分布 *b* 的参数。可选择传入分布 *a* 的参数即 *concentration1_a* 和 *concentration0_a* 。
-- `sample`:可选择传入样本形状 *shape* 和分布的参数包括分布的位置参数 *loc* 和规模参数 *scale* 。
-- `get_dist_args` :可选择传入分布的参数 *concentration1* 和参数 *concentration0* 。返回值为`(concentration1, concentration0)`,类型为tuple。
-- `get_dist_type` :返回 *‘Beta’* 。
-
-### 概率分布类在PyNative模式下的应用
-
-`Distribution` 子类可在 **PyNative** 模式下使用。
-
-以 `Normal` 为例, 创建一个均值为0.0、标准差为1.0的正态分布,然后计算相关函数。
-
-```python
-from mindspore import Tensor
-from mindspore import dtype as mstype
-import mindspore.context as context
-import mindspore.nn.probability.distribution as msd
-context.set_context(mode=context.PYNATIVE_MODE)
-
-my_normal = msd.Normal(0.0, 1.0, dtype=mstype.float32)
-
-mean = my_normal.mean()
-var = my_normal.var()
-entropy = my_normal.entropy()
-
-value = Tensor([-0.5, 0.0, 0.5], dtype=mstype.float32)
-prob = my_normal.prob(value)
-cdf = my_normal.cdf(value)
-
-mean_b = Tensor(1.0, dtype=mstype.float32)
-sd_b = Tensor(2.0, dtype=mstype.float32)
-kl = my_normal.kl_loss('Normal', mean_b, sd_b)
-
-# get the distribution args as a tuple
-dist_arg = my_normal.get_dist_args()
-
-print("mean: ", mean)
-print("var: ", var)
-print("entropy: ", entropy)
-print("prob: ", prob)
-print("cdf: ", cdf)
-print("kl: ", kl)
-print("dist_arg: ", dist_arg)
-```
-
-输出为:
-
-```text
-mean: 0.0
-var: 1.0
-entropy: 1.4189385
-prob: [0.35206532 0.3989423 0.35206532]
-cdf: [0.30853754 0.5 0.69146246]
-kl: 0.44314718
-dist_arg: (Tensor(shape=[], dtype=Float32, value= 0), Tensor(shape=[], dtype=Float32, value= 1))
-```
-
-### 概率分布类在图模式下的应用
-
-在图模式下,`Distribution` 子类可用在网络中。
-
-```python
-import mindspore.nn as nn
-from mindspore import Tensor
-from mindspore import dtype as mstype
-import mindspore.context as context
-import mindspore.nn.probability.distribution as msd
-context.set_context(mode=context.GRAPH_MODE)
-
-class Net(nn.Cell):
- def __init__(self):
- super(Net, self).__init__()
- self.normal = msd.Normal(0.0, 1.0, dtype=mstype.float32)
-
- def construct(self, value, mean, sd):
- pdf = self.normal.prob(value)
- kl = self.normal.kl_loss("Normal", mean, sd)
- return pdf, kl
-
-net = Net()
-value = Tensor([-0.5, 0.0, 0.5], dtype=mstype.float32)
-mean = Tensor(1.0, dtype=mstype.float32)
-sd = Tensor(1.0, dtype=mstype.float32)
-pdf, kl = net(value, mean, sd)
-print("pdf: ", pdf)
-print("kl: ", kl)
-```
-
-输出为:
-
-```text
-pdf: [0.35206532 0.3989423 0.35206532]
-kl: 0.5
-```
-
-### TransformedDistribution类接口设计
-
-`TransformedDistribution` 继承自 `Distribution` ,是可通过映射f(x)变化得到的数学分布的基类。其接口包括:
-
-1. 属性
-
- - `bijector`:返回分布的变换方法。
- - `distribution`:返回原始分布。
- - `is_linear_transformation`:返回线性变换标志。
-
-2. 接口函数(以下接口函数的参数与构造函数中 `distribution` 的对应接口的参数相同)。
-
- - `cdf`:累积分布函数(CDF)。
- - `log_cdf`:对数累积分布函数。
- - `survival_function`:生存函数。
- - `log_survival`:对数生存函数。
- - `prob`:概率密度函数(PDF)/ 概率质量函数(PMF)。
- - `log_prob`:对数似然函数。
- - `sample`:随机取样。
- - `mean`:无参数。只有当 `Bijector.is_constant_jacobian=true` 时可调用。
-
-### PyNative模式下调用TransformedDistribution实例
-
-`TransformedDistribution` 子类可在 **PyNative** 模式下使用。
-
-这里构造一个 `TransformedDistribution` 实例,使用 `Normal` 分布作为需要变换的分布类,使用 `Exp` 作为映射变换,可以生成 `LogNormal` 分布。
-
-```python
-import numpy as np
-import mindspore.nn as nn
-import mindspore.nn.probability.bijector as msb
-import mindspore.nn.probability.distribution as msd
-import mindspore.context as context
-from mindspore import Tensor, dtype
-
-context.set_context(mode=context.PYNATIVE_MODE)
-
-normal = msd.Normal(0.0, 1.0, dtype=dtype.float32)
-exp = msb.Exp()
-LogNormal = msd.TransformedDistribution(exp, normal, seed=0, name="LogNormal")
-
-# compute cumulative distribution function
-x = np.array([2.0, 5.0, 10.0], dtype=np.float32)
-tx = Tensor(x, dtype=dtype.float32)
-cdf = LogNormal.cdf(tx)
-
-# generate samples from the distribution
-shape = (3, 2)
-sample = LogNormal.sample(shape)
-
-# get information of the distribution
-print(LogNormal)
-# get information of the underlying distribution and the bijector separately
-print("underlying distribution:\n", LogNormal.distribution)
-print("bijector:\n", LogNormal.bijector)
-# get the computation results
-print("cdf:\n", cdf)
-print("sample shape:\n", sample.shape)
-```
-
-输出为:
-
-```text
-TransformedDistribution<
- (_bijector): Exp
- (_distribution): Normal
- >
-underlying distribution:
- Normal
-bijector:
- Exp
-cdf:
- [0.7558914 0.9462397 0.9893489]
-sample shape:
-(3, 2)
-```
-
-当构造 `TransformedDistribution` 映射变换的 `is_constant_jacobian = true` 时(如 `ScalarAffine`),构造的 `TransformedDistribution` 实例可以使用直接使用 `mean` 接口计算均值,例如:
-
-```python
-normal = msd.Normal(0.0, 1.0, dtype=dtype.float32)
-scalaraffine = msb.ScalarAffine(1.0, 2.0)
-trans_dist = msd.TransformedDistribution(scalaraffine, normal, seed=0)
-mean = trans_dist.mean()
-print(mean)
-```
-
-输出为:
-
-```text
-2.0
-```
-
-### 图模式下调用TransformedDistribution实例
-
-在图模式下,`TransformedDistribution` 类可用在网络中。
-
-```python
-import numpy as np
-import mindspore.nn as nn
-from mindspore import Tensor, dtype
-import mindspore.context as context
-import mindspore.nn.probability.bijector as msb
-import mindspore.nn.probability.distribution as msd
-context.set_context(mode=context.GRAPH_MODE)
-
-class Net(nn.Cell):
- def __init__(self, shape, dtype=dtype.float32, seed=0, name='transformed_distribution'):
- super(Net, self).__init__()
- # create TransformedDistribution distribution
- self.exp = msb.Exp()
- self.normal = msd.Normal(0.0, 1.0, dtype=dtype)
- self.lognormal = msd.TransformedDistribution(self.exp, self.normal, seed=seed, name=name)
- self.shape = shape
-
- def construct(self, value):
- cdf = self.lognormal.cdf(value)
- sample = self.lognormal.sample(self.shape)
- return cdf, sample
-
-shape = (2, 3)
-net = Net(shape=shape, name="LogNormal")
-x = np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32)
-tx = Tensor(x, dtype=dtype.float32)
-cdf, sample = net(tx)
-print("cdf: ", cdf)
-print("sample shape: ", sample.shape)
-```
-
-输出为:
-
-```text
-cdf: [0.7558914 0.86403143 0.9171715 0.9462397 ]
-sample shape: (2, 3)
-```
-
-## 概率分布映射
-
-Bijector(`mindspore.nn.probability.bijector`)是概率编程的基本组成部分。Bijector描述了一种随机变量的变换方法,可以通过一个已有的随机变量X和一个映射函数f生成一个新的随机变量$Y = f(x)$。
-`Bijector` 提供了映射相关的四种变换方法。它可以当做算子直接使用,也可以作用在某个随机变量 `Distribution` 类实例上生成新的随机变量的 `Distribution` 类实例。
-
-### Bijector类接口设计
-
-#### Bijector基类
-
-`Bijector` 类是所有概率分布映射的基类。其接口包括:
-
-1. 属性
- - `name`:返回 `name` 的值。
- - `dtype`:返回 `dtype` 的值。
- - `parameters`:返回 `parameter` 的值。
- - `is_constant_jacobian`:返回 `is_constant_jacobian` 的值。
- - `is_injective`:返回 `is_injective` 的值。
-
-2. 映射函数
- - `forward`:正向映射,创建派生类后由派生类的 `_forward` 决定参数。
- - `inverse`:反向映射,创建派生类后由派生类的 `_inverse` 决定参数。
- - `forward_log_jacobian`:正向映射的导数的对数,创建派生类后由派生类的 `_forward_log_jacobian` 决定参数。
- - `inverse_log_jacobian`:反向映射的导数的对数,创建派生类后由派生类的 `_inverse_log_jacobian` 决定参数。
-
-`Bijector` 作为函数调用:输入是一个 `Distribution` 类:生成一个 `TransformedDistribution` **(不可在图内调用)**。
-
-#### 幂函数变换映射(PowerTransform)
-
-`PowerTransform` 做如下变量替换:`Y = g(X) = {(1 + X * power)}^{1 / power}`。其接口包括:
-
-1. 属性
- - `power`:返回 `power` 的值,类型为`Tensor`。
-
-2. 映射函数
- - `forward`:正向映射,输入为 `Tensor` 。
- - `inverse`:反向映射,输入为 `Tensor` 。
- - `forward_log_jacobian`:正向映射的导数的对数,输入为 `Tensor` 。
- - `inverse_log_jacobian`:反向映射的导数的对数,输入为 `Tensor` 。
-
-#### 指数变换映射(Exp)
-
-`Exp` 做如下变量替换:`Y = g(X)= exp(X)`。其接口包括:
-
-映射函数
-
-- `forward`:正向映射,输入为 `Tensor` 。
-- `inverse`:反向映射,输入为 `Tensor` 。
-- `forward_log_jacobian`:正向映射的导数的对数,输入为 `Tensor` 。
-- `inverse_log_jacobian`:反向映射的导数的对数,输入为 `Tensor` 。
-
-#### 标量仿射变换映射(ScalarAffine)
-
-`ScalarAffine` 做如下变量替换:`Y = g(X) = scale * X + shift`。其接口包括:
-
-1. 属性
- - `scale`:返回`scale`的值,类型为`Tensor`。
- - `shift`:返回`shift`的值,类型为`Tensor`。
-
-2. 映射函数
- - `forward`:正向映射,输入为 `Tensor` 。
- - `inverse`:反向映射,输入为 `Tensor` 。
- - `forward_log_jacobian`:正向映射的导数的对数,输入为 `Tensor` 。
- - `inverse_log_jacobian`:反向映射的导数的对数,输入为 `Tensor` 。
-
-#### Softplus变换映射(Softplus)
-
-`Softplus` 做如下变量替换:`Y = g(X) = log(1 + e ^ {sharpness * X}) / sharpness`。其接口包括:
-
-1. 属性
- - `sharpness`:返回 `sharpness` 的值,类型为`Tensor`。
-
-2. 映射函数
- - `forward`:正向映射,输入为 `Tensor` 。
- - `inverse`:反向映射,输入为 `Tensor` 。
- - `forward_log_jacobian`:正向映射的导数的对数,输入为 `Tensor` 。
- - `inverse_log_jacobian`:反向映射的导数的对数,输入为 `Tensor` 。
-
-#### 耿贝尔累计密度函数映射(GumbelCDF)
-
-`GumbelCDF` 做如下变量替换:$Y = g(X) = \exp(-\exp(-\frac{X - loc}{scale}))$。其接口包括:
-
-1. 属性
- - `loc`:返回`loc`的值,类型为`Tensor`。
- - `scale`:返回`scale`的值,类型为`Tensor`。
-
-2. 映射函数
- - `forward`:正向映射,输入为 `Tensor` 。
- - `inverse`:反向映射,输入为 `Tensor` 。
- - `forward_log_jacobian`:正向映射的导数的对数,输入为 `Tensor` 。
- - `inverse_log_jacobian`:反向映射的导数的对数,输入为 `Tensor` 。
-
-#### 逆映射(Invert)
-
-`Invert` 对一个映射做逆变换,其接口包括:
-
-1. 属性
- - `bijector`:返回初始化时使用的*Bijector*,类型为`Bijector`。
-
-2. 映射函数
- - `forward`:正向映射,输入为 `Tensor` 。
- - `inverse`:反向映射,输入为 `Tensor` 。
- - `forward_log_jacobian`:正向映射的导数的对数,输入为 `Tensor` 。
- - `inverse_log_jacobian`:反向映射的导数的对数,输入为 `Tensor` 。
-
-### PyNative模式下调用Bijector实例
-
-在执行之前,我们需要导入需要的库文件包。双射类最主要的库是 `mindspore.nn.probability.bijector`,导入后我们使用 `msb` 作为库的缩写并进行调用。
-
-下面我们以 `PowerTransform` 为例。创建一个指数为2的 `PowerTransform` 对象。
-
-```python
-import numpy as np
-import mindspore.nn as nn
-import mindspore.nn.probability.bijector as msb
-import mindspore.context as context
-from mindspore import Tensor, dtype
-
-context.set_context(mode=context.PYNATIVE_MODE)
-
-powertransform = msb.PowerTransform(power=2.)
-
-x = np.array([2.0, 3.0, 4.0, 5.0], dtype=np.float32)
-tx = Tensor(x, dtype=dtype.float32)
-forward = powertransform.forward(tx)
-inverse = powertransform.inverse(tx)
-forward_log_jaco = powertransform.forward_log_jacobian(tx)
-inverse_log_jaco = powertransform.inverse_log_jacobian(tx)
-
-print(powertransform)
-print("forward: ", forward)
-print("inverse: ", inverse)
-print("forward_log_jacobian: ", forward_log_jaco)
-print("inverse_log_jacobian: ", inverse_log_jaco)
-```
-
-输出:
-
-```text
-PowerTransform
-forward: [2.236068 2.6457515 3. 3.3166249]
-inverse: [ 1.5 4. 7.5 12.000001]
-forward_log_jacobian: [-0.804719 -0.9729551 -1.0986123 -1.1989477]
-inverse_log_jacobian: [0.6931472 1.0986123 1.3862944 1.609438 ]
-```
-
-### 图模式下调用Bijector实例
-
-在图模式下,`Bijector` 子类可用在网络中。
-
-```python
-import numpy as np
-import mindspore.nn as nn
-from mindspore import Tensor
-from mindspore import dtype as mstype
-import mindspore.context as context
-import mindspore.nn.probability.bijector as msb
-context.set_context(mode=context.GRAPH_MODE)
-
-class Net(nn.Cell):
- def __init__(self):
- super(Net, self).__init__()
- # create a PowerTransform bijector
- self.powertransform = msb.PowerTransform(power=2.)
-
- def construct(self, value):
- forward = self.powertransform.forward(value)
- inverse = self.powertransform.inverse(value)
- forward_log_jaco = self.powertransform.forward_log_jacobian(value)
- inverse_log_jaco = self.powertransform.inverse_log_jacobian(value)
- return forward, inverse, forward_log_jaco, inverse_log_jaco
-
-net = Net()
-x = np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32)
-tx = Tensor(x, dtype=mstype.float32)
-forward, inverse, forward_log_jaco, inverse_log_jaco = net(tx)
-print("forward: ", forward)
-print("inverse: ", inverse)
-print("forward_log_jacobian: ", forward_log_jaco)
-print("inverse_log_jacobian: ", inverse_log_jaco)
-```
-
-输出为:
-
-```text
-forward: [2.236068 2.6457515 3. 3.3166249]
-inverse: [ 1.5 4. 7.5 12.000001]
-forward_log_jacobian: [-0.804719 -0.9729551 -1.0986123 -1.1989477]
-inverse_log_jacobian: [0.6931472 1.0986123 1.3862944 1.609438 ]
-```
-
-## 深度概率网络
-
-使用MindSpore深度概率编程库(`mindspore.nn.probability.dpn`)来构造变分自编码器(VAE)进行推理尤为简单。我们只需要自定义编码器和解码器(DNN模型),调用VAE或CVAE接口形成其派生网络,然后调用ELBO接口进行优化,最后使用SVI接口进行变分推理。这样做的好处是,不熟悉变分推理的用户可以像构建DNN模型一样来构建概率模型,而熟悉的用户可以调用这些接口来构建更为复杂的概率模型。VAE的接口在`mindspore.nn.probability.dpn`下面,dpn代表的是Deep probabilistic network,这里提供了一些基本的深度概率网络的接口,例如VAE。
-
-### VAE
-
-首先,我们需要先自定义encoder和decoder,调用`mindspore.nn.probability.dpn.VAE`接口来构建VAE网络,我们除了传入encoder和decoder之外,还需要传入encoder输出变量的维度hidden size,以及VAE网络存储潜在变量的维度latent size,一般latent size会小于hidden size。
-
-```python
-import mindspore.nn as nn
-import mindspore.ops as ops
-from mindspore.nn.probability.dpn import VAE
-
-IMAGE_SHAPE = (-1, 1, 32, 32)
-
-
-class Encoder(nn.Cell):
- def __init__(self):
- super(Encoder, self).__init__()
- self.fc1 = nn.Dense(1024, 800)
- self.fc2 = nn.Dense(800, 400)
- self.relu = nn.ReLU()
- self.flatten = nn.Flatten()
-
- def construct(self, x):
- x = self.flatten(x)
- x = self.fc1(x)
- x = self.relu(x)
- x = self.fc2(x)
- x = self.relu(x)
- return x
-
-
-class Decoder(nn.Cell):
- def __init__(self):
- super(Decoder, self).__init__()
- self.fc1 = nn.Dense(400, 1024)
- self.sigmoid = nn.Sigmoid()
- self.reshape = ops.Reshape()
-
- def construct(self, z):
- z = self.fc1(z)
- z = self.reshape(z, IMAGE_SHAPE)
- z = self.sigmoid(z)
- return z
-
-
-encoder = Encoder()
-decoder = Decoder()
-vae = VAE(encoder, decoder, hidden_size=400, latent_size=20)
-```
-
-### ConditionalVAE
-
-类似地,ConditionalVAE与VAE的使用方法比较相近,不同的是,ConditionalVAE利用了数据集的标签信息,属于有监督学习算法,其生成效果一般会比VAE好。
-
-首先,先自定义encoder和decoder,并调用`mindspore.nn.probability.dpn.ConditionalVAE`接口来构建ConditionalVAE网络,这里的encoder和VAE的不同,因为需要传入数据集的标签信息;decoder和上述的一样。ConditionalVAE接口的传入则还需要传入数据集的标签类别个数,其余和VAE接口一样。
-
-```python
-import mindspore.nn as nn
-import mindspore.ops as ops
-from mindspore.nn.probability.dpn import ConditionalVAE
-
-IMAGE_SHAPE = (-1, 1, 32, 32)
-
-
-class Encoder(nn.Cell):
- def __init__(self, num_classes):
- super(Encoder, self).__init__()
- self.fc1 = nn.Dense(1024 + num_classes, 400)
- self.relu = nn.ReLU()
- self.flatten = nn.Flatten()
- self.concat = ops.Concat(axis=1)
- self.one_hot = nn.OneHot(depth=num_classes)
-
- def construct(self, x, y):
- x = self.flatten(x)
- y = self.one_hot(y)
- input_x = self.concat((x, y))
- input_x = self.fc1(input_x)
- input_x = self.relu(input_x)
- return input_x
-
-
-class Decoder(nn.Cell):
- def __init__(self):
- super(Decoder, self).__init__()
- self.fc1 = nn.Dense(400, 1024)
- self.sigmoid = nn.Sigmoid()
- self.reshape = ops.Reshape()
-
- def construct(self, z):
- z = self.fc1(z)
- z = self.reshape(z, IMAGE_SHAPE)
- z = self.sigmoid(z)
- return z
-
-
-encoder = Encoder(num_classes=10)
-decoder = Decoder()
-cvae = ConditionalVAE(encoder, decoder, hidden_size=400, latent_size=20, num_classes=10)
-```
-
-加载数据集,我们可以使用Mnist数据集,具体的数据加载和预处理过程可以参考这里[实现一个图片分类应用](https://www.mindspore.cn/tutorial/training/zh-CN/master/quick_start/quick_start.html),这里会用到create_dataset函数创建数据迭代器。
-
-```python
-ds_train = create_dataset(image_path, 128, 1)
-```
-
-接下来,需要用到infer接口进行VAE网络的变分推断。
-
-## 概率推断算法
-
-调用ELBO接口(`mindspore.nn.probability.infer.ELBO`)来定义VAE网络的损失函数,调用`WithLossCell`封装VAE网络和损失函数,并定义优化器,之后传入SVI接口(`mindspore.nn.probability.infer.SVI`)。SVI的`run`函数可理解为VAE网络的训练,可以指定训练的`epochs`,返回结果为训练好的网络;`get_train_loss`函数可以返回训练好后模型的loss。
-
-```python
-from mindspore.nn.probability.infer import ELBO, SVI
-
-net_loss = ELBO(latent_prior='Normal', output_prior='Normal')
-net_with_loss = nn.WithLossCell(vae, net_loss)
-optimizer = nn.Adam(params=vae.trainable_params(), learning_rate=0.001)
-
-vi = SVI(net_with_loss=net_with_loss, optimizer=optimizer)
-vae = vi.run(train_dataset=ds_train, epochs=10)
-trained_loss = vi.get_train_loss()
-```
-
-最后,得到训练好的VAE网络后,我们可以使用`vae.generate_sample`生成新样本,需要传入待生成样本的个数,及生成样本的shape,shape需要保持和原数据集中的样本shape一样;当然,我们也可以使用`vae.reconstruct_sample`重构原来数据集中的样本,来测试VAE网络的重建能力。
-
-```python
-generated_sample = vae.generate_sample(64, IMAGE_SHAPE)
-for sample in ds_train.create_dict_iterator():
- sample_x = Tensor(sample['image'], dtype=mstype.float32)
- reconstructed_sample = vae.reconstruct_sample(sample_x)
-print('The shape of the generated sample is ', generated_sample.shape)
-```
-
-我们可以看一下新生成样本的shape:
-
-```text
-The shape of the generated sample is (64, 1, 32, 32)
-```
-
-ConditionalVAE训练过程和VAE的过程类似,但需要注意的是使用训练好的ConditionalVAE网络生成新样本和重建新样本时,需要输入标签信息,例如下面生成的新样本就是64个0-7的数字。
-
-```python
-sample_label = Tensor([i for i in range(0, 8)] * 8, dtype=mstype.int32)
-generated_sample = cvae.generate_sample(sample_label, 64, IMAGE_SHAPE)
-for sample in ds_train.create_dict_iterator():
- sample_x = Tensor(sample['image'], dtype=mstype.float32)
- sample_y = Tensor(sample['label'], dtype=mstype.int32)
- reconstructed_sample = cvae.reconstruct_sample(sample_x, sample_y)
-print('The shape of the generated sample is ', generated_sample.shape)
-```
-
-查看一下新生成的样本的shape:
-
-```text
-The shape of the generated sample is (64, 1, 32, 32)
-```
-
-如果希望新生成的样本更好,更清晰,用户可以自己定义更复杂的encoder和decoder,这里的示例只用了两层全连接层,仅供示例的指导。
-
-## 贝叶斯层
-
-下面的范例使用MindSpore的`nn.probability.bnn_layers`中的API实现BNN图片分类模型。MindSpore的`nn.probability.bnn_layers`中的API包括`NormalPrior`,`NormalPosterior`,`ConvReparam`,`DenseReparam`,`DenseLocalReparam`和`WithBNNLossCell`。BNN与DNN的最大区别在于,BNN层的weight和bias不再是确定的值,而是服从一个分布。其中,`NormalPrior`,`NormalPosterior`分别用来生成服从正态分布的先验分布和后验分布;`ConvReparam`和`DenseReparam`分别是使用reparameterization方法实现的贝叶斯卷积层和全连接层;`DenseLocalReparam`是使用Local Reparameterization方法实现的贝叶斯全连接层;`WithBNNLossCell`是用来封装BNN和损失函数的。
-
-如何使用`nn.probability.bnn_layers`中的API构建贝叶斯神经网络并实现图片分类,可以参考教程[使用贝叶斯网络](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/apply_deep_probability_programming.html#id3)。
-
-## 贝叶斯转换
-
-对于不熟悉贝叶斯模型的研究人员,MDP提供了贝叶斯转换接口(`mindspore.nn.probability.transform`),支持DNN (Deep Neural Network)模型一键转换成BNN (Bayesian Neural Network)模型。
-
-其中的模型转换API`TransformToBNN`的`__init__`函数定义如下:
-
-```python
-class TransformToBNN:
- def __init__(self, trainable_dnn, dnn_factor=1, bnn_factor=1):
- net_with_loss = trainable_dnn.network
- self.optimizer = trainable_dnn.optimizer
- self.backbone = net_with_loss.backbone_network
- self.loss_fn = getattr(net_with_loss, "_loss_fn")
- self.dnn_factor = dnn_factor
- self.bnn_factor = bnn_factor
- self.bnn_loss_file = None
-```
-
-参数`trainable_bnn`是经过`TrainOneStepCell`包装的可训练DNN模型,`dnn_factor`和`bnn_factor`分别为由损失函数计算得到的网络整体损失的系数和每个贝叶斯层的KL散度的系数。
-API`TransformToBNN`主要实现了两个功能:
-
-- 功能一:转换整个模型
-
- `transform_to_bnn_model`方法可以将整个DNN模型转换为BNN模型。其定义如下:
-
- ```python
- def transform_to_bnn_model(self,
- get_dense_args=lambda dp: {"in_channels": dp.in_channels, "has_bias": dp.has_bias,
- "out_channels": dp.out_channels, "activation": dp.activation},
- get_conv_args=lambda dp: {"in_channels": dp.in_channels, "out_channels": dp.out_channels,
- "pad_mode": dp.pad_mode, "kernel_size": dp.kernel_size,
- "stride": dp.stride, "has_bias": dp.has_bias,
- "padding": dp.padding, "dilation": dp.dilation,
- "group": dp.group},
- add_dense_args=None,
- add_conv_args=None):
- r"""
- Transform the whole DNN model to BNN model, and wrap BNN model by TrainOneStepCell.
-
- Args:
- get_dense_args (function): The arguments gotten from the DNN full connection layer. Default: lambda dp:
- {"in_channels": dp.in_channels, "out_channels": dp.out_channels, "has_bias": dp.has_bias}.
- get_conv_args (function): The arguments gotten from the DNN convolutional layer. Default: lambda dp:
- {"in_channels": dp.in_channels, "out_channels": dp.out_channels, "pad_mode": dp.pad_mode,
- "kernel_size": dp.kernel_size, "stride": dp.stride, "has_bias": dp.has_bias}.
- add_dense_args (dict): The new arguments added to BNN full connection layer. Default: {}.
- add_conv_args (dict): The new arguments added to BNN convolutional layer. Default: {}.
-
- Returns:
- Cell, a trainable BNN model wrapped by TrainOneStepCell.
- """
-
- ```
-
- 参数`get_dense_args`指定从DNN模型的全连接层中获取哪些参数,默认值是DNN模型的全连接层和BNN的全连接层所共有的参数,参数具体的含义可以参考[API说明文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/nn/mindspore.nn.Dense.html);`get_conv_args`指定从DNN模型的卷积层中获取哪些参数,默认值是DNN模型的卷积层和BNN的卷积层所共有的参数,参数具体的含义可以参考[API说明文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/nn/mindspore.nn.Conv2d.html);参数`add_dense_args`和`add_conv_args`分别指定了要为BNN层指定哪些新的参数值。需要注意的是,`add_dense_args`中的参数不能与`get_dense_args`重复,`add_conv_args`和`get_conv_args`也是如此。
-
-- 功能二:转换指定类型的层
-
- `transform_to_bnn_layer`方法可以将DNN模型中指定类型的层(`nn.Dense`或者`nn.Conv2d`)转换为对应的贝叶斯层。其定义如下:
-
- ```python
- def transform_to_bnn_layer(self, dnn_layer, bnn_layer, get_args=None, add_args=None):
- r"""
- Transform a specific type of layers in DNN model to corresponding BNN layer.
-
- Args:
- dnn_layer_type (Cell): The type of DNN layer to be transformed to BNN layer. The optional values are
- nn.Dense, nn.Conv2d.
- bnn_layer_type (Cell): The type of BNN layer to be transformed to. The optional values are
- DenseReparameterization, ConvReparameterization.
- get_args (dict): The arguments gotten from the DNN layer. Default: None.
- add_args (dict): The new arguments added to BNN layer. Default: None.
-
- Returns:
- Cell, a trainable model wrapped by TrainOneStepCell, whose sprcific type of layer is transformed to the corresponding bayesian layer.
- """
- ```
-
- 参数`dnn_layer`指定将哪个类型的DNN层转换成BNN层,`bnn_layer`指定DNN层将转换成哪个类型的BNN层,`get_args`和`add_args`分别指定从DNN层中获取哪些参数和要为BNN层的哪些参数重新赋值。
-
-如何在MindSpore中使用API`TransformToBNN`可以参考教程[DNN一键转换成BNN](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/apply_deep_probability_programming.html#dnnbnn)
-
-## 贝叶斯工具箱
-
-### 不确定性评估
-
-贝叶斯神经网络的优势之一就是可以获取不确定性,MDP在上层提供了不确定性估计的工具箱(`mindspore.nn.probability.toolbox`),用户可以很方便地使用该工具箱计算不确定性。不确定性意味着深度学习模型对预测结果的不确定程度。目前,大多数深度学习算法只能给出高置信度的预测结果,而不能判断预测结果的确定性,不确定性主要有两种类型:偶然不确定性和认知不确定性。
-
-- 偶然不确定性(Aleatoric Uncertainty):描述数据中的内在噪声,即无法避免的误差,这个现象不能通过增加采样数据来削弱。
-- 认知不确定性(Epistemic Uncertainty):模型自身对输入数据的估计可能因为训练不佳、训练数据不够等原因而不准确,可以通过增加训练数据等方式来缓解。
-
-不确定性评估工具箱的接口如下:
-
-- `model`:待评估不确定性的已训练好的模型。
-- `train_dataset`:用于训练的数据集,迭代器类型。
-- `task_type`:模型的类型,字符串,输入“regression”或者“classification”。
-- `num_classes`:如果是分类模型,需要指定类别的标签数量。
-- `epochs`:用于训练不确定模型的迭代数。
-- `epi_uncer_model_path`:用于存储或加载计算认知不确定性的模型的路径。
-- `ale_uncer_model_path`:用于存储或加载计算偶然不确定性的模型的路径。
-- `save_model`:布尔类型,是否需要存储模型。
-
-在使用前,需要先训练好模型,以LeNet5为例,使用方式如下:
-
-```python
-from mindspore.nn.probability.toolbox.uncertainty_evaluation import UncertaintyEvaluation
-from mindspore import load_checkpoint, load_param_into_net
-
-if __name__ == '__main__':
- # get trained model
- network = LeNet5()
- param_dict = load_checkpoint('checkpoint_lenet.ckpt')
- load_param_into_net(network, param_dict)
- # get train and eval dataset
- ds_train = create_dataset('workspace/mnist/train')
- ds_eval = create_dataset('workspace/mnist/test')
- evaluation = UncertaintyEvaluation(model=network,
- train_dataset=ds_train,
- task_type='classification',
- num_classes=10,
- epochs=1,
- epi_uncer_model_path=None,
- ale_uncer_model_path=None,
- save_model=False)
- for eval_data in ds_eval.create_dict_iterator():
- eval_data = Tensor(eval_data['image'], mstype.float32)
- epistemic_uncertainty = evaluation.eval_epistemic_uncertainty(eval_data)
- aleatoric_uncertainty = evaluation.eval_aleatoric_uncertainty(eval_data)
- print('The shape of epistemic uncertainty is ', epistemic_uncertainty.shape)
- print('The shape of aleatoric uncertainty is ', aleatoric_uncertainty.shape)
-```
-
-`eval_epistemic_uncertainty`计算的是认知不确定性,也叫模型不确定性,对于每一个样本的每个预测标签都会有一个不确定值;`eval_aleatoric_uncertainty`计算的是偶然不确定性,也叫数据不确定性,对于每一个样本都会有一个不确定值。
-所以输出为:
-
-```text
-The shape of epistemic uncertainty is (32, 10)
-The shape of aleatoric uncertainty is (32,)
-```
-
-uncertainty的值大于等于0,越大表示不确定性越高。
-
-### 异常检测
-
-异常检测(Anomaly Detection)可以找到与“主要数据分布不同”的异常值,比如在数据预处理中找出异常点,有助于提升模型的拟合能力。
-
-MDP在上层基于变分自编码器(VAE)提供了异常检测的工具箱(`VAEAnomalyDetection`),与VAE的使用类似,我们只需要自定义编码器和解码器(DNN模型),初始化相关参数,便可以使用该工具箱检测异常点。
-
-基于VAE的异常检测工具箱的接口如下:
-
-- `encoder`:编码器(Cell类型)
-- `decoder`:解码器(Cell类型)
-- `hidden_size`:编码器输出张量的大小
-- `latent_size`:隐空间的大小
-
-编码器和解码器可使用以上的Encoder和Decoder,设置hidden_size和latent_size,进行类的初始化,之后传入数据集可以进行异常点的检测。
-
-```python
-from mindspore.nn.probability.toolbox.vae_anomaly_detection import VAEAnomalyDetection
-
-if __name__ == '__main__':
- encoder = Encoder()
- decoder = Decoder()
- ood = VAEAnomalyDetection(encoder=encoder, decoder=decoder,
- hidden_size=400, latent_size=20)
- ds_train = create_dataset('workspace/mnist/train')
- ds_eval = create_dataset('workspace/mnist/test')
- model = ood.train(ds_train)
- for sample in ds_eval.create_dict_iterator(output_numpy=True, num_epochs=1):
- sample_x = Tensor(sample['image'], dtype=mstype.float32)
- score = ood.predict_outlier_score(sample_x)
- outlier = ood.predict_outlier(sample_x)
- print(score, outlier)
-```
-
-`score`输出的是样本的异常分数;`outlier`是布尔类型,True代表是异常点,False代表不是异常点。
diff --git a/docs/programming_guide/source_zh_cn/run.ipynb b/docs/programming_guide/source_zh_cn/run.ipynb
index 7402c96f5cd346157e02498981c020d5561c51e1..510cc266bb618f596afa06a23a336bad9966450a 100644
--- a/docs/programming_guide/source_zh_cn/run.ipynb
+++ b/docs/programming_guide/source_zh_cn/run.ipynb
@@ -6,7 +6,7 @@
"source": [
"# 运行方式\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/run.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_run.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9ydW4uaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/run.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_run.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9ydW4uaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -133,11 +133,11 @@
"source": [
"## 执行网络模型\n",
"\n",
- "MindSpore的[Model接口](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.html#mindspore.Model)是用于训练和验证的高级接口。可以将有训练或推理功能的layers组合成一个对象,通过调用`train`、`eval`、`predict`接口可以分别实现训练、推理和预测功能。\n",
+ "MindSpore的[Model接口](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.html#mindspore.Model)是用于训练和验证的高级接口。可以将有训练或推理功能的layers组合成一个对象,通过调用`train`、`eval`、`predict`接口可以分别实现训练、推理和预测功能。\n",
"\n",
"用户可以根据实际需要传入网络、损失函数和优化器等初始化Model接口,还可以通过配置`amp_level`实现混合精度,配置`metrics`实现模型评估。\n",
"\n",
- "> 执行网络模型会在执行目录下生成`kernel_meta`目录,并在执行过程中保存网络编译生成的算子缓存文件到此目录,包括`.o`,`.info`和`.json`文件。若用户再次执行相同的网络模型,或者仅有部分变化,MindSpore会自动调用`kernel_meta`目录下可复用的算子缓存文件,显著减少网络编译时间,提升执行性能。详细内容请参考[算子增量编译](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/kernel_incremental_compilation.html)。\n",
+ "> 执行网络模型会在执行目录下生成`kernel_meta`目录,并在执行过程中保存网络编译生成的算子缓存文件到此目录,包括`.o`,`.info`和`.json`文件。若用户再次执行相同的网络模型,或者仅有部分变化,MindSpore会自动调用`kernel_meta`目录下可复用的算子缓存文件,显著减少网络编译时间,提升执行性能。详细内容请参考[算子增量编译](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/incremental_operator_build.html)。\n",
"\n",
"在执行网络之前,先将所需要的数据集下载并解压缩到指定位置:"
]
@@ -151,7 +151,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "./datasets/MNIST_Data/\n",
+ "./datasets/MNIST_Data\n",
"├── test\n",
"│ ├── t10k-images-idx3-ubyte\n",
"│ └── t10k-labels-idx1-ubyte\n",
@@ -164,9 +164,12 @@
}
],
"source": [
- "!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/MNIST_Data.zip\n",
- "!unzip -o MNIST_Data.zip -d ./datasets\n",
- "!tree ./datasets/MNIST_Data/"
+ "!mkdir -p ./datasets/MNIST_Data/train ./datasets/MNIST_Data/test\n",
+ "!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-labels-idx1-ubyte \n",
+ "!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-images-idx3-ubyte\n",
+ "!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-labels-idx1-ubyte\n",
+ "!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-images-idx3-ubyte\n",
+ "!tree ./datasets/MNIST_Data"
]
},
{
@@ -308,11 +311,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "> 示例中用到的MNIST数据集的获取方法,可以参照[实现一个图片分类应用](https://www.mindspore.cn/tutorial/training/zh-CN/master/quick_start/quick_start.html)的下载数据集部分,下同。\n",
+ "> 示例中用到的MNIST数据集的获取方法,可以参照[实现一个图片分类应用](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/quick_start/quick_start.html)的下载数据集部分,下同。\n",
">\n",
- "> 使用PyNative模式调试, 请参考[使用PyNative模式调试](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/debug_in_pynative_mode.html), 包括单算子、普通函数和网络训练模型的执行。\n",
+ "> 使用PyNative模式调试, 请参考[使用PyNative模式调试](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/debug_in_pynative_mode.html), 包括单算子、普通函数和网络训练模型的执行。\n",
">\n",
- "> 使用自由控制循环的迭代次数、遍历数据集等,可以参照官网编程指南《[训练](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/train.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%AE%AD%E7%BB%83%E5%BE%AA%E7%8E%AF)》的自定义循环训练部分。\n"
+ "> 使用自由控制循环的迭代次数、遍历数据集等,可以参照官网编程指南《[训练](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/train.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%AE%AD%E7%BB%83%E5%BE%AA%E7%8E%AF)》的自定义循环训练部分。\n"
]
},
{
@@ -473,7 +476,7 @@
"\n",
"- `load_param_into_net`:通过该接口把参数加载到网络中。\n",
"\n",
- "> `checkpoint_lenet-1_1875.ckpt`文件的保存方法,可以参考[实现一个图片分类应用](https://www.mindspore.cn/tutorial/training/zh-CN/master/quick_start/quick_start.html)的训练网络部分。"
+ "> `checkpoint_lenet-1_1875.ckpt`文件的保存方法,可以参考[实现一个图片分类应用](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/quick_start/quick_start.html)的训练网络部分。"
]
}
],
diff --git a/docs/programming_guide/source_zh_cn/sampler.ipynb b/docs/programming_guide/source_zh_cn/sampler.ipynb
index 756295600d8a9b386fb0dea0ab76ee573a6f0e88..828a151694aefb3918dd9c7419c3e7ebbcc6704f 100644
--- a/docs/programming_guide/source_zh_cn/sampler.ipynb
+++ b/docs/programming_guide/source_zh_cn/sampler.ipynb
@@ -6,7 +6,7 @@
"source": [
"# 采样器\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/sampler.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_sampler.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV90b2tlbml6ZXIuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/sampler.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_sampler.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV90b2tlbml6ZXIuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -17,7 +17,7 @@
"\n",
"MindSpore提供了多种用途的采样器(Sampler),帮助用户对数据集进行不同形式的采样,以满足训练需求,能够解决诸如数据集过大或样本类别分布不均等问题。只需在加载数据集时传入采样器对象,即可实现数据的采样。\n",
"\n",
- "MindSpore目前提供的部分采样器类别如下表所示。此外,用户也可以根据需要实现自定义的采样器类。更多采样器的使用方法参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.dataset.html)。"
+ "MindSpore目前提供的部分采样器类别如下表所示。此外,用户也可以根据需要实现自定义的采样器类。更多采样器的使用方法参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.dataset.html)。"
]
},
{
@@ -53,7 +53,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "./datasets/cifar10\n",
+ "./datasets/cifar-10-batches-bin\n",
+ "├── readme.html\n",
"├── test\n",
"│ └── test_batch.bin\n",
"└── train\n",
@@ -64,14 +65,18 @@
" ├── data_batch_4.bin\n",
" └── data_batch_5.bin\n",
"\n",
- "2 directories, 7 files\n"
+ "2 directories, 8 files\n"
]
}
],
"source": [
- "!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/cifar10.zip\n",
- "!unzip -o cifar10.zip -d ./datasets\n",
- "!tree ./datasets/cifar10"
+ "!wget -N https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/cifar-10-binary.tar.gz\n",
+ "!mkdir -p datasets\n",
+ "!tar -xzf cifar-10-binary.tar.gz -C datasets\n",
+ "!mkdir -p datasets/cifar-10-batches-bin/train datasets/cifar-10-batches-bin/test\n",
+ "!mv -f datasets/cifar-10-batches-bin/test_batch.bin datasets/cifar-10-batches-bin/test\n",
+ "!mv -f datasets/cifar-10-batches-bin/data_batch*.bin datasets/cifar-10-batches-bin/batches.meta.txt datasets/cifar-10-batches-bin/train\n",
+ "!tree ./datasets/cifar-10-batches-bin"
]
},
{
@@ -114,7 +119,7 @@
"\n",
"ds.config.set_seed(0)\n",
"\n",
- "DATA_DIR = \"./datasets/cifar10/train/\"\n",
+ "DATA_DIR = \"./datasets/cifar-10-batches-bin/train/\"\n",
"\n",
"print(\"------ Without Replacement ------\")\n",
"\n",
@@ -167,7 +172,7 @@
"\n",
"ds.config.set_seed(1)\n",
"\n",
- "DATA_DIR = \"./datasets/cifar10/train/\"\n",
+ "DATA_DIR = \"./datasets/cifar-10-batches-bin/train/\"\n",
"\n",
"weights = [1, 1, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"sampler = ds.WeightedRandomSampler(weights, num_samples=6)\n",
@@ -208,7 +213,7 @@
"\n",
"ds.config.set_seed(2)\n",
"\n",
- "DATA_DIR = \"./datasets/cifar10/train/\"\n",
+ "DATA_DIR = \"./datasets/cifar-10-batches-bin/train/\"\n",
"\n",
"indices = [0, 1, 2, 3, 4, 5]\n",
"sampler = ds.SubsetRandomSampler(indices, num_samples=3)\n",
@@ -266,7 +271,7 @@
"\n",
"ds.config.set_seed(3)\n",
"\n",
- "DATA_DIR = \"./datasets/cifar10/train/\"\n",
+ "DATA_DIR = \"./datasets/cifar-10-batches-bin/train/\"\n",
"\n",
"sampler = ds.PKSampler(num_val=2, class_column='label', num_samples=20)\n",
"dataset = ds.Cifar10Dataset(DATA_DIR, sampler=sampler)\n",
@@ -350,7 +355,7 @@
" for i in range(0, 10, 2):\n",
" yield i\n",
"\n",
- "DATA_DIR = \"./datasets/cifar10/train/\"\n",
+ "DATA_DIR = \"./datasets/cifar-10-batches-bin/train/\"\n",
"\n",
"dataset = ds.Cifar10Dataset(DATA_DIR, sampler=MySampler())\n",
"\n",
diff --git a/docs/programming_guide/source_zh_cn/security_and_privacy.md b/docs/programming_guide/source_zh_cn/security_and_privacy.md
index 61a63ee5e241c6cce1aca97230551245062a5564..2d3ebc1544f2d7f66dd7c87578e8c5f6a94cb75c 100644
--- a/docs/programming_guide/source_zh_cn/security_and_privacy.md
+++ b/docs/programming_guide/source_zh_cn/security_and_privacy.md
@@ -17,7 +17,7 @@
-
+
## 概述
@@ -37,7 +37,7 @@
`Detector`基类定义了对抗样本检测的使用接口,其子类实现了各种具体的检测算法,增强模型的对抗鲁棒性。
-详细内容,请参考[对抗鲁棒性官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/improve_model_security_nad.html)。
+详细内容,请参考[对抗鲁棒性官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/improve_model_security_nad.html)。
## 模型安全测试
@@ -45,7 +45,7 @@
`Fuzzer`类基于神经元覆盖率增益控制fuzzing流程,采用自然扰动和对抗样本生成方法作为变异策略,激活更多的神经元,从而探索不同类型的模型输出结果、错误行为,指导用户增强模型鲁棒性。
-详细内容,请参考[模型安全测试官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/test_model_security_fuzzing.html)。
+详细内容,请参考[模型安全测试官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/test_model_security_fuzzing.html)。
## 差分隐私训练
@@ -53,7 +53,7 @@
`DPModel`继承了`mindspore.Model`,提供了差分隐私训练的入口函数。
-详细内容,请参考[差分隐私官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/protect_user_privacy_with_differential_privacy.html)。
+详细内容,请参考[差分隐私官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/protect_user_privacy_with_differential_privacy.html)。
## 抑制隐私训练
@@ -61,7 +61,7 @@
`SuppressModel`继承了`mindspore.Model`,提供了抑制隐私训练的入口函数。
-详细内容,请参考[抑制隐私官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/protect_user_privacy_with_suppress_privacy.html)。
+详细内容,请参考[抑制隐私官网教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/protect_user_privacy_with_suppress_privacy.html)。
## 隐私泄露风险评估
@@ -69,4 +69,4 @@
`MembershipInference`类提供了一种模型逆向分析方法,能够基于模型对样本的预测信息,推测某个样本是否在模型的训练集中,以此评估模型的隐私泄露风险。
-详细内容,请参考[隐私泄露风险评估官方教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/test_model_security_membership_inference.html)。
+详细内容,请参考[隐私泄露风险评估官方教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/test_model_security_membership_inference.html)。
diff --git a/docs/programming_guide/source_zh_cn/syntax_list.rst b/docs/programming_guide/source_zh_cn/syntax_list.rst
index ee6c9218ca1be9856d50de5d3d40b71e0f8f57df..019704343bbc03fe4b95af0734f7e46cff5af55e 100644
--- a/docs/programming_guide/source_zh_cn/syntax_list.rst
+++ b/docs/programming_guide/source_zh_cn/syntax_list.rst
@@ -4,4 +4,4 @@
.. toctree::
:maxdepth: 1
- 静态图语法支持
\ No newline at end of file
+ 静态图语法支持
\ No newline at end of file
diff --git a/docs/programming_guide/source_zh_cn/tensor.ipynb b/docs/programming_guide/source_zh_cn/tensor.ipynb
index 1fa9a7c378d35958b4fbc6377d109650f6b80db1..500091ad6f957c1793f0d2f37b1273844ab33076 100644
--- a/docs/programming_guide/source_zh_cn/tensor.ipynb
+++ b/docs/programming_guide/source_zh_cn/tensor.ipynb
@@ -6,7 +6,7 @@
"source": [
"# Tensor\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/tensor.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_tensor.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV90ZW5zb3IuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/tensor.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_tensor.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV90ZW5zb3IuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -15,7 +15,7 @@
"source": [
"## 概述\n",
"\n",
- "张量(Tensor)是MindSpore网络运算中的基本数据结构。张量中的数据类型可参考[dtype](https://www.mindspore.cn/doc/programming_guide/zh-CN/master/dtype.html)。\n",
+ "张量(Tensor)是MindSpore网络运算中的基本数据结构。张量中的数据类型可参考[dtype](https://www.mindspore.cn/doc/programming_guide/zh-CN/r1.2/dtype.html)。\n",
"\n",
"不同维度的张量分别表示不同的数据,0维张量表示标量,1维张量表示向量,2维张量表示矩阵,3维张量可以表示彩色图像的RGB三通道等等。\n",
"\n",
diff --git a/docs/programming_guide/source_zh_cn/tokenizer.ipynb b/docs/programming_guide/source_zh_cn/tokenizer.ipynb
index 5f729b63245213f162a5f7d2f09ea72890d83e7a..00490a9d329ac48b707474f0b00e983cda10ee85 100644
--- a/docs/programming_guide/source_zh_cn/tokenizer.ipynb
+++ b/docs/programming_guide/source_zh_cn/tokenizer.ipynb
@@ -6,7 +6,7 @@
"source": [
"# 分词器\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/tokenizer.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_tokenizer.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV90b2tlbml6ZXIuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/tokenizer.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_tokenizer.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV90b2tlbml6ZXIuaXB5bmI=&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -33,7 +33,7 @@
"| WhitespaceTokenizer | 根据空格符对标量文本数据进行分词。 |\n",
"| WordpieceTokenizer | 根据单词集对标量文本数据进行分词。 |\n",
"\n",
- "更多分词器的详细说明,可以参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/master/mindspore/mindspore.dataset.text.html)。"
+ "更多分词器的详细说明,可以参见[API文档](https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/mindspore.dataset.text.html)。"
]
},
{
@@ -264,7 +264,7 @@
"for data in dataset.create_dict_iterator(output_numpy=True):\n",
" print(text.to_str(data['text']))\n",
"\n",
- "# file from MindSpore repository https://gitee.com/mindspore/mindspore/blob/master/tests/ut/data/dataset/test_sentencepiece/botchan.txt\n",
+ "# file from MindSpore repository https://gitee.com/mindspore/mindspore/blob/r1.2/tests/ut/data/dataset/test_sentencepiece/botchan.txt\n",
"vocab_file = \"./datasets/tokenizer/botchan.txt\"\n",
"vocab = text.SentencePieceVocab.from_file([vocab_file], 5000, 0.9995, SentencePieceModel.UNIGRAM, {})\n",
"tokenizer_op = text.SentencePieceTokenizer(vocab, out_type=SPieceTokenizerOutType.STRING)\n",
diff --git a/docs/programming_guide/source_zh_cn/train.ipynb b/docs/programming_guide/source_zh_cn/train.ipynb
index 216f984350e902f51a63d1b59b878052f03dce1a..65a5746358830276dbb320092516859c6c584da4 100644
--- a/docs/programming_guide/source_zh_cn/train.ipynb
+++ b/docs/programming_guide/source_zh_cn/train.ipynb
@@ -6,7 +6,7 @@
"source": [
"# 训练\n",
"\n",
- "[](https://gitee.com/mindspore/docs/blob/master/docs/programming_guide/source_zh_cn/train.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_train.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV90cmFpbi5pcHluYg==&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
+ "[](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/train.ipynb) [](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/programming_guide/mindspore_train.ipynb) [](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV90cmFpbi5pcHluYg==&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)"
]
},
{
@@ -23,13 +23,13 @@
"\n",
"在自定义训练网络前,需要先了解下MindSpore的网络支持、Python源码构造网络约束和算子支持情况。\n",
"\n",
- "- 网络支持:当前MindSpore已经支持多种网络,按类型分为计算机视觉、自然语言处理、推荐和图神经网络,可以通过[网络支持](https://www.mindspore.cn/doc/note/zh-CN/master/network_list.html)查看具体支持的网络情况。如果现有网络无法满足用户需求,用户可以根据实际需要定义自己的网络。\n",
+ "- 网络支持:当前MindSpore已经支持多种网络,按类型分为计算机视觉、自然语言处理、推荐和图神经网络,可以通过[网络支持](https://www.mindspore.cn/doc/note/zh-CN/r1.2/network_list.html)查看具体支持的网络情况。如果现有网络无法满足用户需求,用户可以根据实际需要定义自己的网络。\n",
"\n",
- "- Python源码构造网络约束:MindSpore暂不支持将任意Python源码转换成计算图,所以对于用户源码支持的写法有所限制,主要包括语法约束和网络定义约束两方面。详细情况可以查看[静态图语法支持](https://www.mindspore.cn/doc/note/zh-CN/master/static_graph_syntax_support.html)了解。随着MindSpore的演进,这些约束可能会发生变化。\n",
+ "- Python源码构造网络约束:MindSpore暂不支持将任意Python源码转换成计算图,所以对于用户源码支持的写法有所限制,主要包括语法约束和网络定义约束两方面。详细情况可以查看[静态图语法支持](https://www.mindspore.cn/doc/note/zh-CN/r1.2/static_graph_syntax_support.html)了解。随着MindSpore的演进,这些约束可能会发生变化。\n",
"\n",
- "- 算子支持:顾名思义,网络的基础是算子,所以用户自定义训练网络前要对MindSpore当前支持的算子有所了解,可以通过查看[算子支持](https://www.mindspore.cn/doc/note/zh-CN/master/operator_list.html)了解不同的后端(Ascend、GPU和CPU)的算子实现情况。\n",
+ "- 算子支持:顾名思义,网络的基础是算子,所以用户自定义训练网络前要对MindSpore当前支持的算子有所了解,可以通过查看[算子支持](https://www.mindspore.cn/doc/note/zh-CN/r1.2/operator_list.html)了解不同的后端(Ascend、GPU和CPU)的算子实现情况。\n",
"\n",
- "> 当开发网络遇到内置算子不足以满足需求时,用户也可以参考[自定义算子](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/custom_operator_ascend.html),方便快捷地扩展昇腾AI处理器的自定义算子。\n",
+ "> 当开发网络遇到内置算子不足以满足需求时,用户也可以参考[自定义算子](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/custom_operator_ascend.html),方便快捷地扩展昇腾AI处理器的自定义算子。\n",
"\n",
"代码样例如下:"
]
@@ -109,7 +109,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "./datasets/MNIST_Data/\n",
+ "./datasets/MNIST_Data\n",
"├── test\n",
"│ ├── t10k-images-idx3-ubyte\n",
"│ └── t10k-labels-idx1-ubyte\n",
@@ -122,9 +122,12 @@
}
],
"source": [
- "!wget -N https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/datasets/MNIST_Data.zip\n",
- "!unzip -o MNIST_Data.zip -d ./datasets\n",
- "!tree ./datasets/MNIST_Data/"
+ "!mkdir -p ./datasets/MNIST_Data/train ./datasets/MNIST_Data/test\n",
+ "!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-labels-idx1-ubyte \n",
+ "!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-images-idx3-ubyte\n",
+ "!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-labels-idx1-ubyte\n",
+ "!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-images-idx3-ubyte\n",
+ "!tree ./datasets/MNIST_Data"
]
},
{
@@ -326,9 +329,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "> 示例中用到的MNIST数据集的获取方法,可以参照[实现一个图片分类应用](https://www.mindspore.cn/tutorial/training/zh-CN/master/quick_start/quick_start.html)的下载数据集部分,下同。\n",
+ "> 示例中用到的MNIST数据集的获取方法,可以参照[实现一个图片分类应用](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/quick_start/quick_start.html)的下载数据集部分,下同。\n",
">\n",
- "> 典型的使用场景是梯度累积,详细查看[梯度累积](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/apply_gradient_accumulation.html)。"
+ "> 典型的使用场景是梯度累积,详细查看[梯度累积](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/apply_gradient_accumulation.html)。"
]
},
{
@@ -337,7 +340,7 @@
"source": [
"## 边训练边推理\n",
"\n",
- "对于某些数据量较大、训练时间较长的复杂网络,为了能掌握训练的不同阶段模型精度的指标变化情况,可以通过边训练边推理的方式跟踪精度的变化情况。具体可以参考[同步训练和验证模型](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/evaluate_the_model_during_training.html)。"
+ "对于某些数据量较大、训练时间较长的复杂网络,为了能掌握训练的不同阶段模型精度的指标变化情况,可以通过边训练边推理的方式跟踪精度的变化情况。具体可以参考[同步训练和验证模型](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/evaluate_the_model_during_training.html)。"
]
},
{
diff --git a/install/mindspore_ascend310_install_pip.md b/install/mindspore_ascend310_install_pip.md
index e001ba982f3ff5bd879f8d881412662e8346d087..0eca1085e6bcf54892d2db83a1edbbf1a157a588 100644
--- a/install/mindspore_ascend310_install_pip.md
+++ b/install/mindspore_ascend310_install_pip.md
@@ -17,14 +17,14 @@
## 确认系统环境信息
-- 确认安装Ubuntu 18.04/CentOS 8.2/EulerOS 2.8是64位操作系统。
-- 确认安装正确[GCC 版本](http://ftp.gnu.org/gnu/gcc/),Ubuntu 18.04/EulerOS 2.8用户,GCC>=7.3.0;CentOS 8.2用户 GCC>=8.3.1。
+- 确认安装Ubuntu 18.04/CentOS 7.6/EulerOS 2.8是64位操作系统。
+- 确认安装[GCC 7.3.0版本](http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz)。
- 确认安装[gmp 6.1.2版本](https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz)。
- 确认安装[CMake 3.18.3及以上版本](https://cmake.org/download/)。
- 安装完成后将CMake所在路径添加到系统环境变量。
- 确认安装Python 3.7.5版本。
- 如果未安装或者已安装其他版本的Python,可从[官网](https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz)或者[华为云](https://mirrors.huaweicloud.com/python/3.7.5/Python-3.7.5.tgz)下载Python 3.7.5版本 64位,进行安装。
-- 确认安装Ascend 310 AI处理器软件配套包([Atlas Intelligent Edge Solution V100R020C20](https://support.huawei.com/enterprise/zh/ascend-computing/atlas-intelligent-edge-solution-pid-251167903/software/251687140))。
+- 确认安装Ascend 310 AI处理器软件配套包([Atlas Intelligent Edge Solution V100R001C77]())。
- 确认当前用户有权限访问Ascend 310 AI处理器配套软件包的安装路径`/usr/local/Ascend`,若无权限,需要root用户将当前用户添加到`/usr/local/Ascend`所在的用户组,具体配置请详见配套软件包的说明文档。
- 需要安装配套GCC 7.3版本的Ascend 310 AI处理器软件配套包。
- 安装Ascend 310 AI处理器配套软件包提供的whl包,whl包随配套软件包发布,升级配套软件包之后需要重新安装。
diff --git a/install/mindspore_ascend310_install_pip_en.md b/install/mindspore_ascend310_install_pip_en.md
index 5cceedb0811c4c54fc42a0e191ef78fb6f14341b..61cde98812cf811eba3503f5bdf7d35092d7ffac 100644
--- a/install/mindspore_ascend310_install_pip_en.md
+++ b/install/mindspore_ascend310_install_pip_en.md
@@ -17,14 +17,14 @@ The following describes how to quickly install MindSpore by pip on Linux in the
## Checking System Environment Information
-- Ensure that the 64-bit Ubuntu 18.04, CentOS 8.2, or EulerOS 2.8 is installed.
-- Ensure that right version [GCC](http://ftp.gnu.org/gnu/gcc/) is installed, for Ubuntu 18.04, EulerOS 2.8 users, GCC>=7.3.0; for CentOS 8.2 users, GCC>=8.3.1 .
+- Ensure that the 64-bit Ubuntu 18.04, CentOS 7.6, or EulerOS 2.8 is installed.
+- Ensure that right version [GCC 7.3.0](http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz) is installed.
- Ensure that [GMP 6.1.2](https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz) is installed.
- Ensure that [CMake 3.18.3 or later](https://cmake.org/download/) is installed.
- After installation, add the path of CMake to the system environment variables.
- Ensure that Python 3.7.5 is installed.
- If Python 3.7.5 (64-bit) is not installed, download it from the [Python official website](https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz) or [HUAWEI CLOUD](https://mirrors.huaweicloud.com/python/3.7.5/Python-3.7.5.tgz) and install it.
-- Ensure that the Ascend 310 AI Processor software packages ([Atlas Intelligent Edge Solution V100R020C20](https://support.huawei.com/enterprise/zh/ascend-computing/atlas-intelligent-edge-solution-pid-251167903/software/251687140)) are installed.
+- Ensure that the Ascend 310 AI Processor software packages ([Atlas Intelligent Edge Solution V100R001C77]()) are installed.
- Ensure that you have permissions to access the installation path `/usr/local/Ascend` of the Ascend 310 AI Processor software package. If not, ask the user root to add you to a user group to which `/usr/local/Ascend` belongs. For details about the configuration, see the description document in the software package.
- Ensure that the Ascend 310 AI Processor software package that matches GCC 7.3 is installed.
- Install the .whl package provided with the Ascend 310 AI Processor software package. The .whl package is released with the software package. After the software package is upgraded, you need to reinstall the .whl package.
diff --git a/install/mindspore_ascend310_install_source.md b/install/mindspore_ascend310_install_source.md
index 7018f39376dc6ffb7ce9ea5347e582f4541061ca..66a8168eae2cd8ae81ebe94e708f99acf9701eea 100644
--- a/install/mindspore_ascend310_install_source.md
+++ b/install/mindspore_ascend310_install_source.md
@@ -19,8 +19,8 @@
## 确认系统环境信息
-- 确认安装Ubuntu 18.04/CentOS 8.2/EulerOS 2.8是64位操作系统。
-- 确认安装正确[GCC 版本](http://ftp.gnu.org/gnu/gcc/),Ubuntu 18.04/EulerOS 2.8用户,GCC>=7.3.0;CentOS 8.2用户 GCC>=8.3.1。
+- 确认安装Ubuntu 18.04/CentOS 7.6/EulerOS 2.8是64位操作系统。
+- 确认安装[GCC 7.3.0版本](http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz)。
- 确认安装[gmp 6.1.2版本](https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz)。
- 确认安装[Python 3.7.5版本](https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz)。
- 确认安装[OpenSSL 1.1.1及以上版本](https://github.com/openssl/openssl.git)。
@@ -30,7 +30,7 @@
- 确认安装[patch 2.5及以上版本](http://ftp.gnu.org/gnu/patch/)。
- 安装完成后将patch所在路径添加到系统环境变量中。
- 确认安装[wheel 0.32.0及以上版本](https://pypi.org/project/wheel/)。
-- 确认安装Ascend 310 AI处理器软件配套包([Atlas Intelligent Edge Solution V100R020C20](https://support.huawei.com/enterprise/zh/ascend-computing/atlas-intelligent-edge-solution-pid-251167903/software/251687140))。
+- 确认安装Ascend 310 AI处理器软件配套包([Atlas Intelligent Edge Solution V100R001C77]())。
- 确认当前用户有权限访问Ascend 310 AI处理器配套软件包的安装路径`/usr/local/Ascend`,若无权限,需要root用户将当前用户添加到`/usr/local/Ascend`所在的用户组,具体配置请详见配套软件包的说明文档。
- 需要安装配套GCC 7.3版本的Ascend 310 AI处理器软件配套包。
- 安装Ascend 310 AI处理器配套软件包提供的whl包,whl包随配套软件包发布,升级配套软件包之后需要重新安装。
diff --git a/install/mindspore_ascend310_install_source_en.md b/install/mindspore_ascend310_install_source_en.md
index 7ad075d332d77545c78be65e28437f86cfa8b43a..52b3f67c12e2cf2391b1cb717607f3f53ba93973 100644
--- a/install/mindspore_ascend310_install_source_en.md
+++ b/install/mindspore_ascend310_install_source_en.md
@@ -19,8 +19,8 @@ The following describes how to quickly install MindSpore by compiling the source
## Checking System Environment Information
-- Ensure that the 64-bit Ubuntu 18.04, CentOS 8.2, or EulerOS 2.8 is installed.
-- Ensure that right version [GCC](http://ftp.gnu.org/gnu/gcc/) is installed, for Ubuntu 18.04, EulerOS 2.8 users, GCC>=7.3.0; for CentOS 8.2 users, GCC>=8.3.1 .
+- Ensure that the 64-bit Ubuntu 18.04, CentOS 7.6, or EulerOS 2.8 is installed.
+- Ensure that right version [GCC 7.3.0](http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz) is installed.
- Ensure that [GMP 6.1.2](https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz) is installed.
- Ensure that [Python 3.7.5](https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz) is installed.
- Ensure that [OpenSSL 1.1.1 or later](https://github.com/openssl/openssl.git) is installed.
@@ -30,7 +30,7 @@ The following describes how to quickly install MindSpore by compiling the source
- Ensure that [patch 2.5 or later](http://ftp.gnu.org/gnu/patch/) is installed.
- After installation, add the patch path to the system environment variables.
- Ensure that [wheel 0.32.0 or later](https://pypi.org/project/wheel/) is installed.
-- Ensure that the Ascend 310 AI Processor software packages ([Atlas Intelligent Edge Solution V100R020C20](https://support.huawei.com/enterprise/zh/ascend-computing/atlas-intelligent-edge-solution-pid-251167903/software/251687140)) are installed.
+- Ensure that the Ascend 310 AI Processor software packages ([Atlas Intelligent Edge Solution V100R001C77]()) are installed.
- Ensure that you have permissions to access the installation path `/usr/local/Ascend` of the Ascend 310 AI Processor software package. If not, ask the user root to add you to a user group to which `/usr/local/Ascend` belongs. For details about the configuration, see the description document in the software package.
- Ensure that the Ascend 310 AI Processor software package that matches GCC 7.3 is installed.
- Install the .whl package provided with the Ascend 310 AI Processor software package. The .whl package is released with the software package. After the software package is upgraded, you need to reinstall the .whl package.
diff --git a/install/mindspore_ascend_install_conda.md b/install/mindspore_ascend_install_conda.md
index b80a7bbd96236f8a89bbbe65ee992c2e5d199d38..2abbadc0ca36f8fbe6a767019d8942c41fbcd272 100644
--- a/install/mindspore_ascend_install_conda.md
+++ b/install/mindspore_ascend_install_conda.md
@@ -24,8 +24,8 @@
## 确认系统环境信息
-- 确认安装Ubuntu 18.04/CentOS 8.2/EulerOS 2.8是64位操作系统。
-- 确认安装Ascend 910 AI处理器软件配套包([Atlas Data Center Solution V100R020C20](https://support.huawei.com/enterprise/zh/ascend-computing/atlas-data-center-solution-pid-251167910/software/251826872))。
+- 确认安装Ubuntu 18.04/CentOS 7.6/EulerOS 2.8是64位操作系统。
+- 确认安装Ascend 910 AI处理器软件配套包([Atlas Data Center Solution V100R001C77]())。
- 确认当前用户有权限访问Ascend 910 AI处理器配套软件包的安装路径`/usr/local/Ascend`,若无权限,需要root用户将当前用户添加到`/usr/local/Ascend`所在的用户组,具体配置请详见配套软件包的说明文档。
- 安装Ascend 910 AI处理器配套软件包提供的whl包,whl包随配套软件包发布,参考如下命令完成安装。
diff --git a/install/mindspore_ascend_install_docker.md b/install/mindspore_ascend_install_docker.md
index e25efa4e64c6cd4a5f0ff20c1278e28adbdb5cca..4b56adc5fa1d2b893e1379315309f81e59eef2f5 100644
--- a/install/mindspore_ascend_install_docker.md
+++ b/install/mindspore_ascend_install_docker.md
@@ -29,9 +29,9 @@ MindSpore的Ascend 910镜像托管在[Ascend Hub](https://ascend.huawei.com/asce
## 确认系统环境信息
-- 确认安装Ubuntu 18.04/CentOS 8.2是64位操作系统。
+- 确认安装Ubuntu 18.04/CentOS 7.6是64位操作系统。
- 确认安装[Docker 18.03或更高版本](https://docs.docker.com/get-docker/)。
-- 确认安装Ascend 910 AI处理器软件配套包([Atlas Data Center Solution V100R020C20](https://support.huawei.com/enterprise/zh/ascend-computing/atlas-data-center-solution-pid-251167910/software/251826872))。
+- 确认安装Ascend 910 AI处理器软件配套包([Atlas Data Center Solution V100R001C77]())。
- 确认当前用户有权限访问Ascend 910 AI处理器配套软件包的安装路径`/usr/local/Ascend`,若无权限,需要root用户将当前用户添加到`/usr/local/Ascend`所在的用户组,具体配置请详见配套软件包的说明文档。
- 在完成安装基础驱动与配套软件包的基础上,确认安装CANN软件包中的toolbox实用工具包,即Ascend-cann-toolbox-{version}.run,该工具包提供了Ascend NPU容器化支持的Ascend Docker runtime工具。
diff --git a/install/mindspore_ascend_install_docker_en.md b/install/mindspore_ascend_install_docker_en.md
index 9f2f06e18a9717fd8fef65b54eaf893e53d84d20..0d1efdb24e80b3420d12018c33ae67540968a999 100644
--- a/install/mindspore_ascend_install_docker_en.md
+++ b/install/mindspore_ascend_install_docker_en.md
@@ -29,9 +29,9 @@ The current support for containerized build options is as follows:
## System Environment Information Confirmation
-- Confirm that Ubuntu 18.04/CentOS 8.2 is installed with the 64-bit operating system.
+- Confirm that Ubuntu 18.04/CentOS 7.6 is installed with the 64-bit operating system.
- Confirm that [Docker 18.03 or later](https://docs.docker.com/get-docker/) is installed.
-- Confirm that the Ascend 910 AI processor software package ([Atlas Data Center Solution V100R020C20](https://support.huawei.com/enterprise/zh/ascend-computing/atlas-data-center-solution-pid-251167910/software/251826872)) are installed.
+- Confirm that the Ascend 910 AI processor software package ([Atlas Data Center Solution V100R001C77]()) are installed.
- Confirm that the current user has the right to access the installation path `/usr/local/Ascend`of Ascend 910 AI processor software package. If not, the root user needs to add the current user to the user group where `/usr/local/Ascend` is located. For the specific configuration, please refer to the software package instruction document.
- After installing basic driver and corresponding software packages, confirm that the toolbox utility package in the CANN software package is installed, namely Ascend-cann-toolbox-{version}.run. The toolbox provides Ascend Docker runtime tools supported by Ascend NPU containerization.
diff --git a/install/mindspore_ascend_install_pip.md b/install/mindspore_ascend_install_pip.md
index 0fa8de569fe901f702f5fc0baa645c1a9c7cd73a..c916fbc3ba40ad71380ae68c432b4d17f0171d0c 100644
--- a/install/mindspore_ascend_install_pip.md
+++ b/install/mindspore_ascend_install_pip.md
@@ -21,12 +21,12 @@
## 确认系统环境信息
-- 确认安装Ubuntu 18.04/CentOS 8.2/EulerOS 2.8是64位操作系统。
-- 确认安装正确[GCC 版本](http://ftp.gnu.org/gnu/gcc/),Ubuntu 18.04/EulerOS 2.8用户,GCC>=7.3.0;CentOS 8.2用户 GCC>=8.3.1。
+- 确认安装Ubuntu 18.04/CentOS 7.6/EulerOS 2.8是64位操作系统。
+- 确认安装[GCC 7.3.0版本](http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz)。
- 确认安装[gmp 6.1.2版本](https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz)。
- 确认安装Python 3.7.5版本。
- 如果未安装或者已安装其他版本的Python,可从[官网](https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz)或者[华为云](https://mirrors.huaweicloud.com/python/3.7.5/Python-3.7.5.tgz)下载Python 3.7.5版本 64位,进行安装。
-- 确认安装Ascend 910 AI处理器软件配套包([Atlas Data Center Solution V100R020C20](https://support.huawei.com/enterprise/zh/ascend-computing/atlas-data-center-solution-pid-251167910/software/251826872))。
+- 确认安装Ascend 910 AI处理器软件配套包([Atlas Data Center Solution V100R001C77]())。
- 确认当前用户有权限访问Ascend 910 AI处理器配套软件包的安装路径`/usr/local/Ascend`,若无权限,需要root用户将当前用户添加到`/usr/local/Ascend`所在的用户组,具体配置请详见配套软件包的说明文档。
- 安装Ascend 910 AI处理器配套软件包提供的whl包,whl包随配套软件包发布,参考如下命令完成安装。
diff --git a/install/mindspore_ascend_install_pip_en.md b/install/mindspore_ascend_install_pip_en.md
index d135ec1082a03a234e0f9eec92a2366a4fc809d9..0e1ec22352835a759cd141bdfafef144fea4139e 100644
--- a/install/mindspore_ascend_install_pip_en.md
+++ b/install/mindspore_ascend_install_pip_en.md
@@ -21,12 +21,12 @@ This document describes how to quickly install MindSpore in a Linux system with
## System Environment Information Confirmation
-- Confirm that Ubuntu 18.04/CentOS 8.2/EulerOS 2.8 is installed with the 64-bit operating system.
-- Ensure that right version [GCC](http://ftp.gnu.org/gnu/gcc/) is installed, for Ubuntu 18.04, EulerOS 2.8 users, GCC>=7.3.0; for CentOS 8.2 users, GCC>=8.3.1 .
+- Confirm that Ubuntu 18.04/CentOS 7.6/EulerOS 2.8 is installed with the 64-bit operating system.
+- Ensure that right version [GCC 7.3.0](http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz) is installed.
- Confirm that [gmp 6.1.2](https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz) is installed.
- Confirm that Python 3.7.5 is installed.
- If you didn't install Python or you have installed other versions, please download the Python 3.7.5 64-bit from [Python](https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz) or [Huaweicloud](https://mirrors.huaweicloud.com/python/3.7.5/Python-3.7.5.tgz) to install.
-- Confirm that the Ascend 910 AI processor software package ([Atlas Data Center Solution V100R020C20](https://support.huawei.com/enterprise/zh/ascend-computing/atlas-data-center-solution-pid-251167910/software/251826872)) are installed.
+- Confirm that the Ascend 910 AI processor software package ([Atlas Data Center Solution V100R001C77]()) are installed.
- Confirm that the current user has the right to access the installation path `/usr/local/Ascend`of Ascend 910 AI processor software package, If not, the root user needs to add the current user to the user group where `/usr/local/Ascend` is located. For the specific configuration, please refer to the software package instruction document.
- Install the .whl package provided in Ascend 910 AI processor software package. The .whl package is released with the software package. After software package is upgraded, reinstall the .whl package.
diff --git a/install/mindspore_ascend_install_source.md b/install/mindspore_ascend_install_source.md
index 13f756ac8ec7b89c28ea83cfba1fc0378e3b9867..22c4af5cbc20d11fae8f7f8f00919e82c044841e 100644
--- a/install/mindspore_ascend_install_source.md
+++ b/install/mindspore_ascend_install_source.md
@@ -23,8 +23,8 @@
## 确认系统环境信息
-- 确认安装Ubuntu 18.04/CentOS 8.2/EulerOS 2.8是64位操作系统。
-- 确认安装正确[GCC 版本](http://ftp.gnu.org/gnu/gcc/),Ubuntu 18.04/EulerOS 2.8用户,GCC>=7.3.0;CentOS 8.2用户 GCC>=8.3.1。
+- 确认安装Ubuntu 18.04/CentOS 7.6/EulerOS 2.8是64位操作系统。
+- 确认安装[GCC 7.3.0版本](http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz)。
- 确认安装[gmp 6.1.2版本](https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz)。
- 确认安装[Python 3.7.5版本](https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz)。
- 确认安装[OpenSSL 1.1.1及以上版本](https://github.com/openssl/openssl.git)。
@@ -34,7 +34,7 @@
- 确认安装[patch 2.5及以上版本](http://ftp.gnu.org/gnu/patch/)。
- 安装完成后将patch所在路径添加到系统环境变量中。
- 确认安装[wheel 0.32.0及以上版本](https://pypi.org/project/wheel/)。
-- 确认安装Ascend 910 AI处理器软件配套包([Atlas Data Center Solution V100R020C20](https://support.huawei.com/enterprise/zh/ascend-computing/atlas-data-center-solution-pid-251167910/software/251826872))。
+- 确认安装Ascend 910 AI处理器软件配套包([Atlas Data Center Solution V100R001C77]())。
- 确认当前用户有权限访问Ascend 910 AI处理器配套软件包的安装路径`/usr/local/Ascend`,若无权限,需要root用户将当前用户添加到`/usr/local/Ascend`所在的用户组,具体配置请详见配套软件包的说明文档。
- 安装Ascend 910 AI处理器配套软件包提供的whl包,whl包随配套软件包发布,参考如下命令完成安装。
diff --git a/install/mindspore_ascend_install_source_en.md b/install/mindspore_ascend_install_source_en.md
index cf23ec076fed04a7d7a942fa60ec773816a9076e..1247358db18f39abfe6d5f4859c35fa1f0c2442d 100644
--- a/install/mindspore_ascend_install_source_en.md
+++ b/install/mindspore_ascend_install_source_en.md
@@ -23,8 +23,8 @@ This document describes how to quickly install MindSpore in a Linux system with
## System Environment Information Confirmation
-- Confirm that Ubuntu 18.04/CentOS 8.2/EulerOS 2.8 is installed with the 64-bit operating system.
-- Ensure that right version [GCC](http://ftp.gnu.org/gnu/gcc/) is installed, for Ubuntu 18.04, EulerOS 2.8 users, GCC>=7.3.0; for CentOS 8.2 users, GCC>=8.3.1 .
+- Confirm that Ubuntu 18.04/CentOS 7.6/EulerOS 2.8 is installed with the 64-bit operating system.
+- Ensure that right version [GCC 7.3.0](http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz) is installed.
- Confirm that [gmp 6.1.2](https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz) is installed.
- Confirm that [Python 3.7.5](https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz) is installed.
- Confirm that [OpenSSL 1.1.1 or later](https://github.com/openssl/openssl.git) is installed.
@@ -34,7 +34,7 @@ This document describes how to quickly install MindSpore in a Linux system with
- Confirm that [patch 2.5 or later](http://ftp.gnu.org/gnu/patch/) is installed.
- Add the path where the executable file `patch` stores to the environment variable PATH.
- Confirm that [wheel 0.32.0 or later](https://pypi.org/project/wheel/) is installed.
-- Confirm that the Ascend 910 AI processor software package ([Atlas Data Center Solution V100R020C20](https://support.huawei.com/enterprise/zh/ascend-computing/atlas-data-center-solution-pid-251167910/software/251826872)) are installed.
+- Confirm that the Ascend 910 AI processor software package ([Atlas Data Center Solution V100R001C77]()) are installed.
- Confirm that the current user has the right to access the installation path `/usr/local/Ascend`of Ascend 910 AI processor software package, If not, the root user needs to add the current user to the user group where `/usr/local/Ascend` is located. For the specific configuration, please refer to the software package instruction document.
- Install the .whl package provided in Ascend 910 AI processor software package. The .whl package is released with the software package. After software package is upgraded, reinstall the .whl package.
diff --git a/install/mindspore_cpu_install_conda.md b/install/mindspore_cpu_install_conda.md
index 3abc0b48593f21065734786290aff139d54d3523..6a239342710739778c0396f3788ae030107acd8b 100644
--- a/install/mindspore_cpu_install_conda.md
+++ b/install/mindspore_cpu_install_conda.md
@@ -12,6 +12,7 @@
- [升级MindSpore版本](#升级mindspore版本)
- [安装MindArmour](#安装mindarmour)
- [安装MindSpore Hub](#安装mindspore-hub)
+ - [安装MindQuantum](#安装mindquantum)
@@ -89,3 +90,9 @@ pip install --upgrade mindspore
当您想要快速体验MindSpore预训练模型时,可以选装MindSpore Hub。
具体安装步骤参见[MindSpore Hub](https://gitee.com/mindspore/hub/blob/master/README_CN.md)。
+
+## 安装MindQuantum
+
+当您想要搭建并训练量子神经网络,可以选装MindQuantum。
+
+具体安装步骤参见[MindQuantum](https://gitee.com/mindspore/mindquantum/blob/master/README_CN.md)。
diff --git a/install/mindspore_cpu_install_pip.md b/install/mindspore_cpu_install_pip.md
index a7abd6c15f149bd5c0b22a05376901a0390b9f7a..886132431f55049551b8d46968715e7f1bc2bcf1 100644
--- a/install/mindspore_cpu_install_pip.md
+++ b/install/mindspore_cpu_install_pip.md
@@ -9,6 +9,7 @@
- [升级MindSpore版本](#升级mindspore版本)
- [安装MindArmour](#安装mindarmour)
- [安装MindSpore Hub](#安装mindspore-hub)
+ - [安装MindQuantum](#安装mindquantum)
@@ -63,3 +64,9 @@ pip install --upgrade mindspore
当您想要快速体验MindSpore预训练模型时,可以选装MindSpore Hub。
具体安装步骤参见[MindSpore Hub](https://gitee.com/mindspore/hub/blob/master/README_CN.md)。
+
+## 安装MindQuantum
+
+当您想要搭建并训练量子神经网络,可以选装MindQuantum。
+
+具体安装步骤参见[MindQuantum](https://gitee.com/mindspore/mindquantum/blob/master/README_CN.md)。
diff --git a/install/mindspore_cpu_install_pip_en.md b/install/mindspore_cpu_install_pip_en.md
index 7aa8c70d5c06c947c90b4e3bd38b3eefb31d8a11..230cc436dccecb91069e1ffc3372e08bc8ff2cbd 100644
--- a/install/mindspore_cpu_install_pip_en.md
+++ b/install/mindspore_cpu_install_pip_en.md
@@ -9,6 +9,7 @@
- [Version Update](#version-update)
- [Installing MindArmour](#installing-mindarmour)
- [Installing MindSpore Hub](#installing-mindspore-hub)
+ - [Installing MindQuantum](#installing-mindquantum)
@@ -63,3 +64,9 @@ For more details, please refer to [MindArmour](https://gitee.com/mindspore/minda
If you need to access and experience MindSpore pre-trained models quickly, you can install MindSpore Hub.
For more details, please refer to [MindSpore Hub](https://gitee.com/mindspore/hub/blob/master/README.md).
+
+## Installing MindQuantum
+
+If you need to build and train quantum neural network, you can install MindQuantum.
+
+For more details, please refer to [MindQuantum](https://gitee.com/mindspore/mindquantum/blob/master/README.md).
diff --git a/install/mindspore_cpu_install_source.md b/install/mindspore_cpu_install_source.md
index 7823e4f85d21b9f6db5090c3f731179b9d2b759e..48d44923e95dd918a2c858b383876ff6676df712 100644
--- a/install/mindspore_cpu_install_source.md
+++ b/install/mindspore_cpu_install_source.md
@@ -11,6 +11,7 @@
- [升级MindSpore版本](#升级mindspore版本)
- [安装MindArmour](#安装mindarmour)
- [安装MindSpore Hub](#安装mindspore-hub)
+ - [安装MindQuantum](#安装mindquantum)
@@ -113,3 +114,9 @@ python -c 'import mindspore;print(mindspore.__version__)'
当您想要快速体验MindSpore预训练模型时,可以选装MindSpore Hub。
具体安装步骤参见[MindSpore Hub](https://gitee.com/mindspore/hub/blob/master/README_CN.md)。
+
+## 安装MindQuantum
+
+当您想要搭建并训练量子神经网络,可以选装MindQuantum。
+
+具体安装步骤参见[MindQuantum](https://gitee.com/mindspore/mindquantum/blob/master/README_CN.md)。
diff --git a/install/mindspore_cpu_install_source_en.md b/install/mindspore_cpu_install_source_en.md
index 94280b766b71d9da2882ae15f9a7a8444e90fd2b..f4bd70cbd559a2130628ef77ba5b94ec8c913b20 100644
--- a/install/mindspore_cpu_install_source_en.md
+++ b/install/mindspore_cpu_install_source_en.md
@@ -11,6 +11,7 @@
- [Version Update](#version-update)
- [Installing MindArmour](#installing-mindarmour)
- [Installing MindSpore Hub](#installing-mindspore-hub)
+ - [Installing MindQuantum](#installing-mindquantum)
@@ -112,3 +113,9 @@ For more details, please refer to [MindArmour](https://gitee.com/mindspore/minda
If you need to access and experience MindSpore pre-trained models quickly, you can install MindSpore Hub.
For more details, please refer to [MindSpore Hub](https://gitee.com/mindspore/hub/blob/master/README.md).
+
+## Installing MindQuantum
+
+If you need to build and train quantum neural network, you can install MindQuantum.
+
+For more details, please refer to [MindQuantum](https://gitee.com/mindspore/mindquantum/blob/master/README.md).
diff --git a/resource/release/release_list_en.md b/resource/release/release_list_en.md
index c7214d9be29ec986335504ef51ecbe766dc30e0d..4dc8ce7f2439e9763ad49395d4b3c3ef8db22737 100644
--- a/resource/release/release_list_en.md
+++ b/resource/release/release_list_en.md
@@ -3,6 +3,10 @@
- [Release List](#release-list)
+ - [1.2.0](#120)
+ - [Releasenotes and API Updates](#releasenotes-and-api-updates)
+ - [Downloads](#downloads)
+ - [Related Documents](#related-documents)
- [1.1.1](#111)
- [Releasenotes and API Updates](#releasenotes-and-api-updates)
- [Downloads](#downloads)
@@ -54,6 +58,54 @@
+## 1.2.0
+
+### Releasenotes and API Updates
+
+
+
+### Downloads
+
+| Module Name | Hardware Platform | Operating System | Download Links | SHA-256 |
+| --- | --- | --- | --- | --- |
+| MindSpore | Ascend 910 | Ubuntu-x86 | | |
+| | | Ubuntu-aarch64 | | |
+| | | EulerOS-aarch64 | | |
+| | | CentOS-x86 | | |
+| | | CentOS-aarch64 | | |
+| | Ascend 310 | Ubuntu-x86 | | |
+| | | Ubuntu-aarch64 | | |
+| | | EulerOS-aarch64 | | |
+| | | CentOS-x86 | | |
+| | | CentOS-aarch64 | | |
+| | GPU CUDA 10.1 | Ubuntu-x86 | | |
+| | CPU | Ubuntu-x86 | | |
+| | | Ubuntu-aarch64 | | |
+| | | Windows-x64 | | |
+| MindInsight | Ascend 910 | Ubuntu-x86 | | |
+| | | Ubuntu-aarch64 | | |
+| | | EulerOS-aarch64 | | |
+| | | CentOS-x86 | | |
+| | | CentOS-aarch64 | | |
+| | GPU CUDA 10.1 | Ubuntu-x86 | | |
+| MindArmour | Ascend 910 | Ubuntu-x86
CentOS-x86 | | |
+| | | Ubuntu-aarch64
EulerOS-aarch64
CentOS-aarch64 | | |
+| | GPU CUDA 10.1
CPU | Ubuntu-x86 | | |
+| MindSpore
Hub | | any | | |
+| MindSpore
Serving | Ascend 910
Ascend310 | Ubuntu-x86 | | |
+| | | Ubuntu-aarch64 | | |
+| | | EulerOS-aarch64 | | |
+| | | CentOS-x86 | | |
+| | | CentOS-aarch64 | | |
+
+### Related Documents
+
+| Category | URL |
+| --- | --- |
+| Installation | |
+| Tutorials | Training
Inference
Mobile Phone&IoT |
+| Docs | Python API
C++ API
Java API
FAQ
Design&Specification |
+
## 1.1.1
### Releasenotes and API Updates
diff --git a/resource/release/release_list_zh_cn.md b/resource/release/release_list_zh_cn.md
index aa5b6b12f4602bb961eb99ceb1df4717897593d7..228f65e452a5dcf28aa30eb2c768fa24501d1f91 100644
--- a/resource/release/release_list_zh_cn.md
+++ b/resource/release/release_list_zh_cn.md
@@ -3,6 +3,10 @@
- [发布版本列表](#发布版本列表)
+ - [1.2.0](#120)
+ - [版本说明和接口变更](#版本说明和接口变更)
+ - [下载地址](#下载地址)
+ - [配套资料](#配套资料)
- [1.1.1](#111)
- [版本说明和接口变更](#版本说明和接口变更)
- [下载地址](#下载地址)
@@ -54,6 +58,54 @@