|
- import math
- import numpy as np
- from mindspore.common.initializer import _assignment
- # from .seed import get_seed, _get_graph_seed
-
-
-
- class Initializer:
- """
- The base class of the initializer.
- Initialization of tensor basic attributes and model weight values.
-
- Args:
- kwargs (dict): Keyword arguments for Initializer.
-
- Returns:
- Array, an array after being initialized.
- """
- def __init__(self, **kwargs):
- self._kwargs = kwargs
- self._seed = None
-
- # @property
- # def seed(self):
- # if self._seed is None:
- # seed, seed2 = _get_graph_seed(get_seed(), "init")
- # else:
- # seed, seed2 = self._seed + 1, 0
- # return seed, seed2
-
- # @seed.setter
- # def seed(self, value):
- # self._seed = value
-
- # def _initialize(self, *kwargs):
- # raise NotImplementedError('Must be overridden!')
-
- # def __call__(self, arr):
- # return self._initialize(arr)
-
- class XavierInitializer_cqu(Initializer):
- """
- Initialize the array with xavier uniform algorithm, and from a uniform distribution collect samples within
- U[-boundary, boundary] The boundary is defined as:
-
- .. math::
- boundary = gain * \sqrt{\frac{6}{n_{in} + n_{out}}}
-
- - where :math:`n_{in}` is the number of input units in the weight tensor.
- - where :math:`n_{out}` is the number of output units in the weight tensor.
-
- Args:
- gain (float): An optional scaling factor. Default: 1.
-
- Returns:
- Array, assigned array.
- """
- def __init__(self, n_in,n_out,uniform = True):
- super(XavierInitializer_cqu, self).__init__(n_in=n_in,n_out=n_out,uniform=uniform)
- self.n_in = n_in
- self.n_out = n_out
- self.uniform = uniform
-
- def _initialize(self, arr):
- n_in = self.n_in
- n_out = self.n_out
- uniform = self.uniform
-
- if uniform:
- boundary = math.sqrt(6.0 / (n_in + n_out))
- else:
- boundary = math.sqrt(2.0 / (n_in + n_out))
- data = np.random.uniform(-boundary, boundary, arr.shape)
-
- _assignment(arr, data)
|