{ "cells": [ { "cell_type": "markdown", "source": [ "# Parameter\n", "\n", "`Ascend` `GPU` `CPU` `入门`\n", "\n", "[![在线运行](https://gitee.com/mindspore/docs/raw/r1.6/resource/_static/logo_modelarts.png)](https://authoring-modelarts-cnnorth4.huaweicloud.com/console/lab?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbWFzdGVyL3Byb2dyYW1taW5nX2d1aWRlL3poX2NuL21pbmRzcG9yZV9wYXJhbWV0ZXJfaW50cm9kdWN0aW9uLmlweW5i&imageid=65f636a0-56cf-49df-b941-7d2a07ba8c8c) [![下载Notebook](https://gitee.com/mindspore/docs/raw/r1.6/resource/_static/logo_notebook.png)](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/r1.6/programming_guide/zh_cn/mindspore_parameter_introduction.ipynb) [![下载样例代码](https://gitee.com/mindspore/docs/raw/r1.6/resource/_static/logo_download_code.png)](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/r1.6/programming_guide/zh_cn/mindspore_parameter_introduction.py) [![查看源文件](https://gitee.com/mindspore/docs/raw/r1.6/resource/_static/logo_source.png)](https://gitee.com/mindspore/docs/blob/r1.6/docs/mindspore/programming_guide/source_zh_cn/parameter_introduction.ipynb)" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "`Parameter`是变量张量,代表在训练网络时,需要被更新的参数,一般包括权重`weight`和偏置`bias`。\n", "\n", "例如,对于卷积算子`nn.Conv2D`, `weight`是`[out_channel, in_channel, kernel_size, kernel_size]`大小的张量, `bias`是`[out_channel]`大小的向量。而对于全连接操作`nn.Dense`,`weight`是`[out_channel, in_channel]`大小的张量, `bias`是`[out_channel]`大小的向量。\n", "\n", "```python\n", "import numpy as np\n", "from mindspore import Tensor\n", "from mindspore import nn\n", "\n", "conv_net = nn.Conv2d(in_channels=120, out_channels=240, kernel_size=4, has_bias=True, weight_init='normal')\n", "dense_net = nn.Dense(in_channels=3, out_channels=4, weight_init='normal', bias_init='zeros', has_bias=True)\n", "\n", "print(\"conv_net, weight shape:\", conv_net.weight.shape, \", bias shape:\", conv_net.bias.shape)\n", "print(\"dense_net, weight_shape:\", dense_net.weight.shape, \", bias shape:\", dense_net.bias.shape)\n", "```\n", "\n", "```python\n", "conv_net, weight shape: (240, 120, 4, 4) , bias shape: (240,)\n", "dense_net, weight_shape: (4, 3) , bias shape: (4,)\n", "```\n", "\n", "`nn`下的算子接口中一般集成了权重和偏置,用户可以指定初始化方式来初始化这些`Parameter`。\n", "\n", "在网络训练过程中,优化器根据反向传播计算出的梯度不断更新`Parameter`的值,最优的`Parameter`最终会被保存下来供推理使用。\n", "\n", "`Parameter`不支持稀疏矩阵,如[mindspore.RowTensor](https://www.mindspore.cn/docs/api/zh-CN/r1.6/api_python/mindspore/mindspore.RowTensor.html)、[mindspore.SparseTensor](https://www.mindspore.cn/docs/api/zh-CN/r1.6/api_python/mindspore/mindspore.SparseTensor.html)。其初始化和更新的详细介绍可参考[参数初始化](https://www.mindspore.cn/docs/programming_guide/zh-CN/r1.6/initializer.html)和[参数更新](https://www.mindspore.cn/docs/programming_guide/zh-CN/r1.6/parameter.html)。" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "\n" ], "metadata": {} } ], "metadata": { "kernelspec": { "display_name": "MindSpore", "language": "python", "name": "mindspore" }, "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.6.9" } }, "nbformat": 4, "nbformat_minor": 4 }