在caffe中定义新层时如何获得学习率或迭代时间 [英] how to get learning rate or iteration times when define new layer in caffe
问题描述
当迭代次数达到一定次数时,我想更改损耗层中的损耗计算方法.
为了实现这一点,我认为我需要获得当前的学习率或迭代次数,然后使用if
短语来选择是否更改损耗计算方法.
I want to change the loss calculation method in loss layer when the iteration times reach a certain number.
In order to realize it I think I need to get the current learning rate or iteration times, then I use if
phrase to choose changing loss calculation method or not.
推荐答案
您可以在 Caffe类中添加成员变量以保存当前的学习率或迭代时间,并在您所在的图层中进行访问想要.
You can add a member variable in Caffe class to save the current learning rate or iteration times and access it in the layer where you want.
例如,要获得您想要的当前迭代时间,您需要进行3个关键修改(为简化起见):
For example, to get the current iteration times where you want you need to make 3 key modifications(for simplification):
-
在 common.hpp 中:
class Caffe {
public:
static Caffe& Get();
...//Some other public members
//Returns the current iteration times
inline static int current_iter() { return Get().cur_iter_; }
//Sets the current iteration times
inline static void set_cur_iter(int iter) { Get().cur_iter_ = iter; }
protected:
//The variable to save the current itertion times
int cur_iter_;
...//Some other protected members
}
在 solver.cpp 中:
template <typename Dtype>
void Solver<Dtype>::Step(int iters) {
...
while (iter_ < stop_iter) {
Caffe::set_cur_iter(iter_ );
...//Left Operations
}
}
您要访问当前迭代时间的位置:
The place where you want to access the current iteration times:
template <typename Dtype>
void SomeLayer<Dtype>::some_func() {
int current_iter = Caffe::current_iter();
...//Operations you want
}
这篇关于在caffe中定义新层时如何获得学习率或迭代时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!