cnn 网络权值的初始化方式有很多种, caffe 里面实现了7种, 在include/caffe/filler.hpp。
caffe::Filler< Dtype > Class Template Reference
abstract
Fills a Blob with constant or randomly-generated data. More...
#include <filler.hpp>
Inheritance diagram for caffe::Filler< Dtype >:
Public Member Functions | |
Filler (const FillerParameter ¶m) | |
virtual void | Fill (Blob< Dtype > *blob)=0 |
Protected Attributes | |
FillerParameter | filler_param_ |
Detailed Description
template<typename Dtype>
class caffe::Filler< Dtype >
Fills a Blob with constant or randomly-generated data.
The documentation for this class was generated from the following file:
- include/caffe/filler.hpp
Filler<Dtype>* GetFiller(const FillerParameter& param) {
const std::string& type = param.type();
if (type == "constant") {
return new ConstantFiller<Dtype>(param);
} else if (type == "gaussian") {
return new GaussianFiller<Dtype>(param);
} else if (type == "positive_unitball") {
return new PositiveUnitballFiller<Dtype>(param);
} else if (type == "uniform") {
return new UniformFiller<Dtype>(param);
} else if (type == "xavier") {
return new XavierFiller<Dtype>(param);
} else if (type == "msra") {
return new MSRAFiller<Dtype>(param);
} else if (type == "bilinear") {
return new BilinearFiller<Dtype>(param);
} else {
CHECK(false) << "Unknown filler name: " << param.type();
}
return (Filler<Dtype>*)(NULL);
}
共有七种方法, 下面分别介绍一下:
(1) constant
template <typename Dtype>
class ConstantFiller : public Filler<Dtype> {
public:
explicit ConstantFiller(const FillerParameter& param)
: Filler<Dtype>(param) {}
virtual void Fill(Blob<Dtype>* blob) {
Dtype* data = blob->mutable_cpu_data();
const int count = blob->count();
// get prototxt filler parameters value
const Dtype value = this->filler_param_.value(); // 得到配置文件中 设置的值
CHECK(count);
for (int i = 0; i < count; ++i) {
data[i] = value; // 将配置文件中的值,直接赋值到 data中。 所以 是常量型,就是配置文件常量
}<pre name="code" class="cpp">
(2)
UniformFiller
CHECK_EQ(this->filler_param_.sparse(), -1) << "Sparsity not supported by this Filler."; }};
<pre name="code" class="cpp"><pre name="code" class="cpp">/// @brief Fills a Blob with uniformly distributed values @f$ x\sim U(a, b) @f$.
template <typename Dtype>
class UniformFiller : public Filler<Dtype> {
public:
explicit UniformFiller(const FillerParameter& param)
: Filler<Dtype>(param) {}
virtual void Fill(Blob<Dtype>* blob) {
CHECK(blob->count());
caffe_rng_uniform<Dtype>(blob->count(), Dtype(this->filler_param_.min()), //调用 caffe_rng_uniform ,这个在math_funtion 中实现
Dtype(this->filler_param_.max()), blob->mutable_cpu_data());
CHECK_EQ(this->filler_param_.sparse(), -1)
<< "Sparsity not supported by this Filler.";
}
};
CHECK_EQ(this->filler_param_.sparse(), -1) << "Sparsity not supported by this Filler."; }};