(Caffe)基本類(lèi)Solver、Caffe、Batch(二)

本文從CSDN上轉(zhuǎn)移過(guò)來(lái):
http://blog.csdn.net/mounty_fsc/article/details/51088173

1 Solver

1.1 簡(jiǎn)介

其對(duì)網(wǎng)絡(luò)進(jìn)行求解征峦,其作用有:

  1. 提供優(yōu)化日志支持、創(chuàng)建用于學(xué)習(xí)的訓(xùn)練網(wǎng)絡(luò)消请、創(chuàng)建用于評(píng)估的測(cè)試網(wǎng)絡(luò)
  2. 通過(guò)調(diào)用forward / backward迭代地優(yōu)化栏笆,更新權(quán)值
  3. 周期性地評(píng)估測(cè)試網(wǎng)絡(luò)
  4. 通過(guò)優(yōu)化了解model及solver的狀態(tài)

1.2 源代碼

/**
 * @brief An interface for classes that perform optimization on Net%s.
 *
 * Requires implementation of ApplyUpdate to compute a parameter update
 * given the current state of the Net parameters.
 */
template <typename Dtype>
class Solver {
 public:
  explicit Solver(const SolverParameter& param,
      const Solver* root_solver = NULL);
  explicit Solver(const string& param_file, const Solver* root_solver = NULL);
  void Init(const SolverParameter& param);
  void InitTrainNet();
  void InitTestNets();
 ...
  // The main entry of the solver function. In default, iter will be zero. Pass
  // in a non-zero iter number to resume training for a pre-trained net.
  virtual void Solve(const char* resume_file = NULL);
  inline void Solve(const string resume_file) { Solve(resume_file.c_str()); }
  void Step(int iters);
...

 protected:
  // Make and apply the update value for the current iteration.
  virtual void ApplyUpdate() = 0;
  ...

  SolverParameter param_;
  int iter_;
  int current_step_;
  shared_ptr<Net<Dtype> > net_;
  vector<shared_ptr<Net<Dtype> > > test_nets_;
  vector<Callback*> callbacks_;
  vector<Dtype> losses_;
  Dtype smoothed_loss_;

  // The root solver that holds root nets (actually containing shared layers)
  // in data parallelism
  const Solver* const root_solver_;
...
};

說(shuō)明:

  1. shared_ptr<Net<Dtype>> net_為訓(xùn)練網(wǎng)絡(luò)的指針,vector<shared_ptr<Net<Dtype>>> test_nets為測(cè)試網(wǎng)絡(luò)的指針組臊泰,可見(jiàn)測(cè)試網(wǎng)絡(luò)可以有多個(gè)

  2. 一般來(lái)說(shuō)訓(xùn)練網(wǎng)絡(luò)跟測(cè)試網(wǎng)絡(luò)在實(shí)現(xiàn)上會(huì)有區(qū)別蛉加,但是絕大部分網(wǎng)絡(luò)層是相同的。

  3. 不同的模型訓(xùn)練方法通過(guò)重載函數(shù)ComputeUpdateValue( )實(shí)現(xiàn)計(jì)算update參數(shù)的核心功能

  4. caffe.cpp中的train( )函數(shù)訓(xùn)練模型缸逃,在這里實(shí)例化一個(gè)Solver對(duì)象针饥,初始化后調(diào)用了Solver中的Solve( )方法。而這個(gè)Solve( )函數(shù)主要就是在迭代運(yùn)行下面這兩個(gè)函數(shù)需频。ComputeUpdateValue();
    net_->Update();

1.3 Solver的方法

  • Stochastic Gradient Descent (type: "SGD")
  • AdaDelta (type: "AdaDelta")
  • Adaptive Gradient (type: "AdaGrad")
  • Adam (type: "Adam")
  • Nesterov’s Accelerated Gradient (type: "Nesterov")
  • RMSprop (type: "RMSProp")

詳細(xì)參見(jiàn)引用1

2 Caffe類(lèi)

Caffe類(lèi)為一個(gè)包含常用的caffe成員的單例類(lèi)丁眼。如caffe使用的cuda庫(kù)cublas,curand的句柄等,以及生成Caffe中的隨機(jī)數(shù)等昭殉。


// common.hpp
// A singleton class to hold common caffe stuff, such as the handler that
// caffe is going to use for cublas, curand, etc.
class Caffe {
 public:
  ~Caffe();

  // Thread local context for Caffe. Moved to common.cpp instead of
  // including boost/thread.hpp to avoid a boost/NVCC issues (#1009, #1010)
  // on OSX. Also fails on Linux with CUDA 7.0.18.
  static Caffe& Get();

  enum Brew { CPU, GPU };
...

protected:
#ifndef CPU_ONLY
  cublasHandle_t cublas_handle_;
  curandGenerator_t curand_generator_;
#endif
  shared_ptr<RNG> random_generator_;

  Brew mode_;
  int solver_count_;
  bool root_solver_;

 private:
  // The private constructor to avoid duplicate instantiation.
  Caffe();
  DISABLE_COPY_AND_ASSIGN(Caffe);
};
//common.cpp

namespace caffe {

// Make sure each thread can have different values.
static boost::thread_specific_ptr<Caffe> thread_instance_;

Caffe& Caffe::Get() {
  if (!thread_instance_.get()) {
    thread_instance_.reset(new Caffe());
  }
  return *(thread_instance_.get());
}

...
Caffe::Caffe()
    : cublas_handle_(NULL), curand_generator_(NULL), random_generator_(),
    mode_(Caffe::CPU), solver_count_(1), root_solver_(true) {
  // Try to create a cublas handler, and report an error if failed (but we will
  // keep the program running as one might just want to run CPU code).
  if (cublasCreate(&cublas_handle_) != CUBLAS_STATUS_SUCCESS) {
    LOG(ERROR) << "Cannot create Cublas handle. Cublas won't be available.";
  }
  // Try to create a curand handler.
  if (curandCreateGenerator(&curand_generator_, CURAND_RNG_PSEUDO_DEFAULT)
      != CURAND_STATUS_SUCCESS ||
      curandSetPseudoRandomGeneratorSeed(curand_generator_, cluster_seedgen())
      != CURAND_STATUS_SUCCESS) {
    LOG(ERROR) << "Cannot create Curand generator. Curand won't be available.";
  }
}
...

}  // namespace caffe

說(shuō)明:

  1. Caffe類(lèi)為一個(gè)單例類(lèi)苞七,構(gòu)造方法私有
  2. 該單例由static boost::thread_specific_ptr<Caffe> thread_instance_維護(hù)藐守,確保多線(xiàn)程環(huán)境下,不同的線(xiàn)程有不同的Caffe單例版本
  3. 獲取該單例由Get()方法執(zhí)行蹂风,即Caffe::Get()方法返回thread_instance_維護(hù)的單例卢厂,
  4. thread_instance_的初值為NULL,若是第一次訪問(wèn)惠啄,則new Caffe()
  5. new Caffe()執(zhí)行構(gòu)造方法慎恒,其實(shí)只是創(chuàng)建了cublas,curand的句柄
  6. 單步調(diào)試可發(fā)現(xiàn)cublasCreate()創(chuàng)建cublas的句柄,生成了額外的兩個(gè)線(xiàn)程

3 Batch

template <typename Dtype>
class Batch {
 public:
  Blob<Dtype> data_, label_;
};

說(shuō)明:

  • Batch是對(duì)一個(gè)樣本的封裝撵渡,與Datum不同融柬,Datum是面向數(shù)據(jù)庫(kù)的,且一個(gè)Datum對(duì)應(yīng)一個(gè)樣本(圖像趋距、標(biāo)簽)丹鸿;而B(niǎo)atch是面向網(wǎng)絡(luò)的,一個(gè)Batch對(duì)應(yīng)一批樣本

[1].http://caffe.berkeleyvision.org/tutorial/solver.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末棚品,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子廊敌,更是在濱河造成了極大的恐慌铜跑,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件骡澈,死亡現(xiàn)場(chǎng)離奇詭異锅纺,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)肋殴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)囤锉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人护锤,你說(shuō)我怎么就攤上這事官地。” “怎么了烙懦?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵驱入,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我氯析,道長(zhǎng)亏较,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任掩缓,我火速辦了婚禮雪情,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘你辣。我一直安慰自己巡通,他們只是感情好尘执,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著扁达,像睡著了一般正卧。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上跪解,一...
    開(kāi)封第一講書(shū)人閱讀 51,190評(píng)論 1 299
  • 那天炉旷,我揣著相機(jī)與錄音,去河邊找鬼叉讥。 笑死窘行,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的图仓。 我是一名探鬼主播罐盔,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼救崔!你這毒婦竟也來(lái)了惶看?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤六孵,失蹤者是張志新(化名)和其女友劉穎纬黎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體劫窒,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡本今,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了主巍。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片冠息。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖孕索,靈堂內(nèi)的尸體忽然破棺而出逛艰,到底是詐尸還是另有隱情,我是刑警寧澤搞旭,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布瓮孙,位于F島的核電站,受9級(jí)特大地震影響选脊,放射性物質(zhì)發(fā)生泄漏杭抠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一恳啥、第九天 我趴在偏房一處隱蔽的房頂上張望偏灿。 院中可真熱鬧,春花似錦钝的、人聲如沸翁垂。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)沿猜。三九已至枚荣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間啼肩,已是汗流浹背橄妆。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留祈坠,地道東北人害碾。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像赦拘,于是被迫代替她去往敵國(guó)和親慌随。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容