C++ uniform_real_distribution及normal_distribution
std::uniform_real_distribution
std::uniform_int_distribution
用於從均勻分布中生成隨機的整數;與它相對,std::uniform_real_distribution
則是從均勻分布中生成隨機的浮點數。
其類別模板:
template <class RealType = double> class uniform_real_distribution;
我們可以透過指定其模板參數為double
或float
,以此來決定生成什麼精度的浮點數。
根據std::uniform_real_distribution::(constructor), std::uniform_real_distribution
的建構子的簽名如下:
std::uniform_real_distribution::(constructor)
/*(1)*/ explicit uniform_real_distribution ( result_type a = 0.0, result_type b = 1.0 );
/*(2)*/ explicit uniform_real_distribution ( const param_type& parm );
它的作用:
Construct uniform real distribution
Constructs a uniform_real_distribution object, adopting the distribution parameters specified either by a and b or by object parm.
即接受參數a
跟b
,分別表示取值範圍的下界(包含)跟上界(不包含),來生成一個uniform_real_distribution
物件。
而根據std::uniform_real_distribution::operator(), std::uniform_real_distribution::operator()
的簽名如下:
/*(1)*/ template<class URNG> result_type operator()(URNG& g);
/*(2)*/ template<class URNG> result_type operator()(URNG& g, const param_type& parm);
它的作用為:
Generate random number
Returns a new random number that follows the distribution's parameters associated to the object (version 1) or those specified by parm (version 2).
The generator object (g) supplies uniformly-distributed random integers through its operator() member function. The uniform_real_distribution object transforms the values obtained this way so that successive calls to this member function with the same arguments produce floating-point values that follow a uniform distribution within the appropriate range.
即從均勻分布中隨機選取一個浮點數回傳,選取範圍的上下界是之前設定好的。
在TensorRT/parsers/caffe/caffeWeightFactory/caffeWeightFactory.cpp
中定義的函數 allocateWeights(int64_t elems, std::uniform_real_distribution<float> distribution = std::uniform_real_distribution<float>(-0.01f, 0.01F))
:
//std::default_random_engine generator;
//nvinfer1::Weights allocateWeights(int64_t elems, std::uniform_real_distribution<float> distribution = std::uniform_real_distribution<float>(-0.01f, 0.01F));
Weights CaffeWeightFactory::allocateWeights(int64_t elems, std::uniform_real_distribution<float> distribution)
{
//...
((float*) data)[i] = distribution(generator);
//...
}
在TensorRT/parsers/caffe/caffeParser/opParsers/parseInnerProduct.cpp
的函數 parseInnerProduct
中是這麼對它調用的:
/**/weightFactory.allocateWeights(nbOutputs))/**/;
std::normal_distribution
根據std::normal_distribution::(constructor),std::normal_distribution
的建構子的簽名如下:
std::normal_distribution::(constructor)
/*(1)*/ explicit normal_distribution ( result_type mean = 0.0, result_type stddev = 1.0 );
/*(2)*/ explicit normal_distribution ( const param_type& parm );
它的作用:
Construct normal distribution
Constructs a normal_distribution object, adopting the distribution parameters specified either by mean and stddev or by object parm.
即接受參數mean
及stddev
來建構一個normal_distribution
物件。
而根據 std::normal_distribution::operator(), std::normal_distribution::operator()
的簽名如下:
std::normal_distribution::operator()
/*(1)*/ template<class URNG> result_type operator()(URNG& g);
/*(2)*/ template<class URNG> result_type operator()(URNG& g, const param_type& parm);
它的作用如下:
Generate random number
Returns a new random number that follows the distribution's parameters associated to the object (version 1) or those specified by parm (version 2).
The generator object (g) supplies uniformly-distributed random integers through its operator() member function. The normal_distribution object transforms the values obtained this way so that successive calls to this member function with the same arguments produce floating-point values that follow a Normal distribution with the appropriate parameters.
即std::normal_distribution::operator(URNG& g)
會從正態分布中隨機選取一個數回傳,其平均值及標準差是之前設定好的。
在TensorRT/parsers/caffe/caffeWeightFactory/caffeWeightFactory.cpp
中定義的函數 allocateWeights(int64_t elems, std::normal_distribution<float> distribution)
:
//std::default_random_engine generator;
//nvinfer1::Weights allocateWeights(int64_t elems, std::normal_distribution<float> distribution);
Weights CaffeWeightFactory::allocateWeights(int64_t elems, std::normal_distribution<float> distribution)
{
//...
((float*) data)[i] = distribution(generator);
//...
}
在TensorRT/parsers/caffe/caffeParser/opParsers/parseInnerProduct.cpp
的函數parseInnerProduct
中是這麼對它調用的:
/**/ weightFactory.allocateWeights(nbInputs * nbOutputs, std::normal_distribution<float>(0.0F, std_dev));
參考連結
std::uniform_real_distribution
std::uniform_real_distribution::(constructor)
std::uniform_real_distribution::operator()