{ "cells": [ { "cell_type": "markdown", "source": [ "# Initializer初始化器\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=aHR0cHM6Ly9taW5kc3BvcmUtd2Vic2l0ZS5vYnMuY24tbm9ydGgtNC5teWh1YXdlaWNsb3VkLmNvbS9ub3RlYm9vay9tb2RlbGFydHMvcHJvZ3JhbW1pbmdfZ3VpZGUvbWluZHNwb3JlX2luaXRpYWxpemVyLmlweW5i&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_initializer.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_initializer.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/initializer.ipynb)" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## 概述\n", "\n", "Initializer类是MindSpore中用于进行初始化的基本数据结构,其子类包含了几种不同类型的数据分布(Zero,One,XavierUniform,HeUniform,HeNormal,Constant,Uniform,Normal,TruncatedNormal)。下面针对使用initializer对参数进行初始化的方法进行详细介绍。" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## 使用initializer方法对参数初始化\n", "\n", "使用initializer进行参数初始化时,支持传入的参数有`init`、`shape`、`dtype`:\n", "\n", "- `init`:支持传入`Tensor`、 `str`、 `Initializer的子类`。\n", "\n", "- `shape`:支持传入`list`、 `tuple`、 `int`。\n", "\n", "- `dtype`:支持传入`mindspore.dtype`。" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### init参数为Tensor" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "代码样例如下:" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "import numpy as np\n", "from mindspore import Tensor\n", "from mindspore import dtype as mstype\n", "from mindspore import set_seed\n", "from mindspore.common.initializer import initializer\n", "import mindspore.ops as ops\n", "\n", "set_seed(1)\n", "\n", "input_data = Tensor(np.ones([16, 3, 10, 32, 32]), dtype=mstype.float32)\n", "weight_init = Tensor(np.ones([32, 3, 4, 3, 3]), dtype=mstype.float32)\n", "weight = initializer(weight_init, shape=[32, 3, 4, 3, 3])\n", "conv3d = ops.Conv3D(out_channel=32, kernel_size=(4, 3, 3))\n", "output = conv3d(input_data, weight)\n", "print(output)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[[[108 108 108 ... 108 108 108]\n", " [108 108 108 ... 108 108 108]]\n", " [108 108 108 ... 108 108 108]]\n", " ...\n", " [108 108 108 ... 108 108 108]]\n", " [108 108 108 ... 108 108 108]]\n", " [108 108 108 ... 108 108 108]]]\n", " ...\n", " [[108 108 108 ... 108 108 108]]\n", " [108 108 108 ... 108 108 108]\n", " [108 108 108 ... 108 108 108]]\n", " ...\n", " [108 108 108 ... 108 108 108]]\n", " [108 108 108 ... 108 108 108]]\n", " [108 108 108 ... 108 108 108]]]]]\n" ] } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### init参数为str" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "代码样例如下:" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "import numpy as np\n", "from mindspore import Tensor\n", "from mindspore import dtype as mstype\n", "from mindspore import set_seed\n", "from mindspore.common.initializer import initializer\n", "import mindspore.ops as ops\n", "\n", "set_seed(1)\n", "\n", "input_data = Tensor(np.ones([16, 3, 10, 32, 32]), dtype=mstype.float32)\n", "weight = initializer('Normal', shape=[32, 3, 4, 3, 3], dtype=mstype.float32)\n", "conv3d = ops.Conv3D(out_channel=32, kernel_size=(4, 3, 3))\n", "output = conv3d(input_data, weight)\n", "print(output)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[[[0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " ...\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]]\n", " ...\n", " [[0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " ...\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]]]]\n" ] } ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### init参数为Initializer子类" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "代码样例如下:" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "import numpy as np\n", "from mindspore import Tensor\n", "from mindspore import dtype as mstype\n", "from mindspore import set_seed\n", "import mindspore.ops as ops\n", "from mindspore.common.initializer import Normal, initializer\n", "\n", "set_seed(1)\n", "\n", "input_data = Tensor(np.ones([16, 3, 10, 32, 32]), dtype=mstype.float32)\n", "weight = initializer(Normal(0.2), shape=[32, 3, 4, 3, 3], dtype=mstype.float32)\n", "conv3d = ops.Conv3D(out_channel=32, kernel_size=(4, 3, 3))\n", "output = conv3d(input_data, weight)\n", "print(output)" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[[[0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " ...\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]]\n", " ...\n", " [[0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " ...\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]\n", " [0 0 0 ... 0 0 0]]]]\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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }