|
- import mindspore.nn as nn
- from mindspore.common.initializer import XavierUniform
- import mindspore.ops as ops
- from mindspore import Tensor
- from mindspore import dtype
- import math
- from mindspore.common.initializer import Initializer, _calculate_in_and_out, _assignment
- import numpy as np
-
-
- class GlorotUniform(Initializer):
- r"""
- Initialize the array with xavier uniform algorithm, and from a uniform distribution collect samples within
- U[-boundary, boundary] The boundary is defined as :
-
- where :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 (Array): The array to be assigned. Default: 1.
-
- Returns:
- Array, assigned array.
- """
- def __init__(self, gain=1):
- super(GlorotUniform, self).__init__(gain=gain)
- self.gain = gain
-
- def _initialize(self, arr):
- n_in, n_out = _calculate_in_and_out(arr)
-
- boundary = self.gain * math.sqrt(6.0 / (n_in + n_out))
- data = np.random.uniform(-boundary, boundary, arr.shape)
-
- _assignment(arr, data)
-
-
- class DenseRelu(nn.Cell):
- def __init__(self, in_chs, out_chs):
- super(DenseRelu, self).__init__()
- self.init = GlorotUniform()
- self.dense_relu = nn.SequentialCell(
- [nn.Dense(in_channels=in_chs, out_channels=out_chs, weight_init=self.init),
- nn.ReLU()])
-
- def construct(self, x):
- return self.dense_relu(x)
-
-
- class DenseSigmoid(nn.Cell):
- def __init__(self, in_chs, out_chs):
- super(DenseSigmoid, self).__init__()
- self.round = ops.Round()
- self.init = GlorotUniform()
- self.dense_sigmoid = nn.SequentialCell(
- [nn.Dense(in_channels=in_chs, out_channels=out_chs, weight_init=self.init),
- nn.Sigmoid()])
-
- def construct(self, x):
- # x = self.dense_sigmoid(x)
- return self.dense_sigmoid(x)
-
-
- # class AddNoise(nn.Cell):
- # def __init__(self, sigma):
- # super(AddNoise, self).__init__()
- # self.mean = Tensor(0.0, dtype.float32)
- # self.std = Tensor(sigma)
- # # self.noise = ops.normal(shape=(1, 1), stddev=self.std, mean=self.mean)
- #
- # def construct(self, x):
- # n = ops.normal(shape=x.shape, stddev=self.std, mean=self.mean)
- # return x + n
-
-
- # class Rounder(nn.Cell):
- #
- # def __init__(self):
- # super(Rounder, self).__init__()
- # self.round = ops.Round()
- #
- # def construct(self, x):
- # return self.round(x)
|