From 4fa06dd9a29eb561a0f2e0b108a6858ae01f2b74 Mon Sep 17 00:00:00 2001 From: wanyiming Date: Thu, 24 Feb 2022 11:11:55 +0800 Subject: [PATCH] code_docs_init --- .../mindspore.common.initializer.rst | 80 +++++++++++++++++++ .../python/mindspore/common/initializer.py | 22 ++--- 2 files changed, 93 insertions(+), 9 deletions(-) diff --git a/docs/api/api_python/mindspore.common.initializer.rst b/docs/api/api_python/mindspore.common.initializer.rst index 9777d4b7c09..59ef53c8f03 100644 --- a/docs/api/api_python/mindspore.common.initializer.rst +++ b/docs/api/api_python/mindspore.common.initializer.rst @@ -132,3 +132,83 @@ mindspore.common.initializer .. automodule:: mindspore.common.initializer :members: + + +.. py:class:: mindspore.common.initializer.Identity(**kwargs) + + 生成一个2维的单位阵用于初始化Tensor。 + + **异常:** + + - **ValueError** - 被初始化的Tensor的维度不等于2。 + + +.. py:class:: mindspore.common.initializer.Sparse(sparsity, sigma=0.01) + + 生成一个2维的稀疏矩阵用于初始化Tensor。矩阵非0的位置的值服从正态分布N(0, 0.01)。 + + **参数:** + + **sparsity** (float) - 矩阵每列中元素被置0的比例。 + **sigma** (float) - 正态分布的标准差,默认值为0.01。 + + **异常:** + + - **ValueError** - 被初始化的Tensor的维度不等于2。 + + + +.. py:class:: mindspore.common.initializer.Dirac(group=1) + + 利用Dirac delta函数生成一个array用于初始化Tensor。这种初始化方式将会保留卷积层的输入。对于group + 卷积,通道的每个分组会被分别保留。 + + **参数:** + + **group** (int) - 卷积层中的分组,默认值为1。 + + **异常:** + + - **ValueError** - group不在[3, 4, 5]的范围内。 + - **ValueError** - 初始化的Tensor的第一个维度不能被group整除。 + + + +.. py:class:: mindspore.common.initializer.Orthogonal(gain=1.) + + 生成一个(半)正交矩阵用于初始化Tensor。被初始化的Tensor的维度至少为2。 + 如果维度大于2,多余的维度将会被展平。 + + **参数:** + + **gain** (float) - 可选的比例因子,默认值为1。 + + **异常:** + + - **ValueError** - 被初始化的Tensor的维度小于2。 + + +.. py:class:: mindspore.common.initializer.VarianceScaling(scale=1.0, mode="fan_in", distribution="truncated_normal") + + 生成一个随机的array用于初始化Tensor。 + 当distribution是"truncated_normal"或者"untruncated_normal"时,array中的值将服从均值为0,标准差 + 为 :math:`stddev = sqrt(scale/n)` 的截断或者非截断正太分布。如果mode是"fan_in", :math:`n` 是输入单元的数量; + 如果mode是"fan_out", :math:`n` 是输出单元的数量;如果mode是"fan_avg", :math:`n` 是输入输出单元数量的均值。 + 当distribution是"uniform"时,array中的值将服从均匀分布[`-sqrt(3*scale/n)`, `sqrt(3*scale/n)`]。 + + **参数:** + + **scale** (float) - 比例因子,默认值为1.0。 + **mode** (str) - 其值应为"fan_in","fan_out"或者"fan_avg",默认值为"fan_in"。 + **distribution** (str) - 用于采样的分布类型。它可以是"uniform","truncated_normal"或"untruncated_normal", + 默认值为"truncated_normal"。 + + + **异常:** + + - **ValueError** - scale小于等于0。 + - **ValueError** - mode不是"fan_in","fan_out"或者"fan_avg"。 + - **ValueError** - distribution不是"truncated_normal","untruncated_normal"或者"uniform"。 + + + diff --git a/mindspore/python/mindspore/common/initializer.py b/mindspore/python/mindspore/common/initializer.py index 1c7c1b875b6..5a0f585b3bb 100644 --- a/mindspore/python/mindspore/common/initializer.py +++ b/mindspore/python/mindspore/common/initializer.py @@ -379,7 +379,7 @@ class Constant(Initializer): @_register() class Identity(Initializer): """ - Initialize a 2 dimension identity matrix to fill the input tensor. + Generates a 2 dimension identity matrix array in order to initialize a tensor. Raises: ValueError: If the dimension of input tensor is not equal to 2. @@ -401,8 +401,8 @@ class Identity(Initializer): @_register() class Sparse(Initializer): """ - Initialize a 2 dimension sparse matrix to fill the input tensor. The non-zero positions will be filled with - the value sampled from the normal distribution :math:`{N}(0, 0.01)` + Generates a 2 dimension sparse matrix array in order to initialize a tensor. The non-zero positions + will be filled with the value sampled from the normal distribution :math:`{N}(0, 0.01)` Args: sparsity (float): The fraction of elements being set to zero in each column. @@ -436,8 +436,10 @@ class Sparse(Initializer): @_register() class Dirac(Initializer): - """Initialize input tensor with the Dirac delta function. It tries to preserves the identity of - input for convolution layers. For group convolution, each group of channels will be preserved respectively. + """ + Generates an array with the Dirac delta function in order to initialize a tensor. + It tries to preserves the identity of input for convolution layers. + For group convolution, each group of channels will be preserved respectively. Args: groups (int): The number of group in convolution layer. Default: 1. @@ -487,8 +489,9 @@ class Dirac(Initializer): @_register() class Orthogonal(Initializer): r""" - Initialize a (semi) orthogonal matrix to fill the input tensor. The dimension of input tensor must have at least 2 - dimensions. If the dimension is greater than 2, the trailing dimensions will be flattened. + Generates a (semi) orthogonal matrix array in order to initialize a tensor. + The dimension of input tensor must have at least 2 dimensions. + If the dimension is greater than 2, the trailing dimensions will be flattened. Args: gain (float): An optional scaling factor. Default: 1. @@ -532,7 +535,7 @@ class Orthogonal(Initializer): @_register() class VarianceScaling(Initializer): r""" - Randomly initialize an array with scaling to fill the input tensor. + Generates an random array with scaling in order to initialize a tensor. When distribution is truncated_normal or untruncated_normal, the value will be sampled from truncated or untruncated normal distribution with a mean of 0 and a scaled standard deviation :math:`stddev = sqrt(scale/n)`. :math:`n` will be the number of input units if mode is fan_in, the number of output units if mode is fan_out, @@ -543,7 +546,8 @@ class VarianceScaling(Initializer): Args: scale (float): The scaling factor. Default: 1.0. mode (str): Should be 'fan_in', 'fan_out' or 'fan_avg'. Default: 'fan_in'. - distribution(str): The type of distribution chose to sample values. Default: 'truncated_normal'. + distribution(str): The type of distribution chose to sample values. It should be + 'uniform', 'truncated_normal' or 'untruncated_normal'. Default: 'truncated_normal'. Raises: ValueError: If scale is not greater than 0.