SiaNet/Initializers/VarianceScaling.cs
/// <summary>
/// Generates a tensor with specified shape.
/// </summary>
/// <param name="shape">The shape of the tensor.</param>
/// <returns></returns>
Generates a tensor with specified shape.
Generates a tensor with specified shape .
public override Tensor Generate(params long[] shape)
        {
            Tensor tensor = null;
            var hwScale = 1.0f;
            if (shape.Length > 2)
            {
                for (int i = 2; i < shape.Length; ++i)
                    hwScale *= shape[i];
            }
            var @in = shape[1] * hwScale;
            var @out = shape[0] * hwScale;
            var factor = 1.0f;
            switch (Mode)
            {
                case "fan_avg":
                    factor = Scale / Math.Max(1, (@in + @out) / 2.0f);
                    break;
                case "fan_in":
                    factor = Scale / Math.Max(1, @in);
                    break;
                case "fan_out":
                    factor = Scale / Math.Max(1, @out);
                    break;
            }
            switch (Distribution)
            {
                case "uniform":
                    float limit = (float)Math.Sqrt(3f * factor);
                    tensor = K.RandomUniform(shape, -limit, limit, Seed);
                    break;
                case "normal":
                    float stddev = (float)Math.Sqrt(factor) / 0.87962566103423978f;
                    tensor = K.RandomNormal(shape, 0, stddev, Seed);
                    break;
            }
            return tensor;
        }
public override Tensor Generate ( params long [ ] shape ) { Tensor tensor = null ; var hwScale = 1.0f ; if ( shape . Length > 2 ) { for ( int i = 2 ; i < shape . Length ; ++ i ) hwScale *= shape [ i ] ; } var @in = shape [ 1 ] * hwScale ; var @out = shape [ 0 ] * hwScale ; var factor = 1.0f ; switch ( Mode ) { case " " : factor = Scale / Math . Max ( 1 , ( @in + @out ) / 2.0f ) ; break ; case " " : factor = Scale / Math . Max ( 1 , @in ) ; break ; case " " : factor = Scale / Math . Max ( 1 , @out ) ; break ; } switch ( Distribution ) { case " " : float limit = ( float ) Math . Sqrt ( 3f * factor ) ; tensor = K . RandomUniform ( shape , - limit , limit , Seed ) ; break ; case " " : float stddev = ( float ) Math . Sqrt ( factor ) / 0.87962566103423978f ; tensor = K . RandomNormal ( shape , 0 , stddev , Seed ) ; break ; } return tensor ; }
Generates a tensor with specified shape.
Generates a tensor with specified shape .
None
shape
The shape of the tensor.
The shape of the tensor .