https://stackoverflow.com/questions/38369565/how-to-get-learning-rate-or-iteration-times-when-define-new-layer-in-caffe

参考上述网址上的方法,需要修改

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
}
}

以及需要获取迭代次数的layer.cpp

  template <typename Dtype>
void SomeLayer<Dtype>::some_func() {
int current_iter = Caffe::current_iter();
...//Operations you want
}

再具体的实现过程中出现了获得的迭代次数都是0的问题,后来请教同事发现是因为多线程造成的。

需要修改定义int cur_iter_; 为static int cur_iter_;并且在common.cpp中进行声明:int Caffe::cur_iter_ = 0;

这样获取到的迭代次数就是正确的了。

因为layer是多线程的时候,int current_iter = Caffe::current_iter();这句话会重新初始化一个caffe实例,而不是与solver中共用一个caffe实例,但是将其定义为static类型之后,就是所有实例共享的了,

具体参见:http://blog.csdn.net/morewindows/article/details/6721430

在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用。所以在所有对象中都可以共享它。使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还可以节省内存。

静态成员的定义或声明要加个关键static。静态成员可以通过双冒号来使用即<类名>::<静态成员名>。

一。静态成员函数中不能调用非静态成员。

二。非静态成员函数中可以调用静态成员。因为静态成员属于类本身,在类的对象产生之前就已经存在了,所以在非静态成员函数中是可以调用静态成员的。

三。静态成员变量使用前必须先初始化(如int MyClass::m_nNumber = 0;),否则会在linker时出错。

在调试过程中发现使用to_string函数的时候会报错,'to_string' is not a member of 'std',查找资料之后,在CMakeLists.txt中添加如下一行即可:

set (CMAKE_CXX_STANDARD )

caffe中在某一层获得迭代次数的方法以及caffe编译时报错 error: 'to_string' is not a member of 'std'解决方法的更多相关文章

  1. Loadrunner在场景中添加多个负载机报错:Action.c(38): Error -26488: Could not obtain information about submitted解决方法

    Error -26488: Could not obtain information about submitted file "E:\.jpg": _stat32 rc=-1, ...

  2. Centos7.5中Nginx报错:nginx: [error] invalid PID number "" in "/run/nginx.pid" 解决方法

    服务器重启之后,执行 nginx -t 是OK的,然而在执行 nginx -s reload 的时候报错 nginx: [error] invalid PID number "" ...

  3. Android studio中的一次编译报错’Error:Execution failed for task ':app:transformClassesWithDexForDebug‘,困扰了两天

    先说下背景:随着各种第三方框架的使用,studio在编译打包成apk时,在dex如果发现有相同的jar包,不能创建dalvik虚拟机.一个apk,就是一个运行在linux上的一个虚拟机. 上图就是一直 ...

  4. 在ubuntu中我们使用sudo apt-get install 或者dpkg -i *.deb安装软件时,常常提示“有未能满足的依赖关系“,解决方法

    很早之前在ubuntu安装软件时遇到的问题,今天打开ubuntu看到了,总结如下: 在ubuntu中我们使用sudo apt-get install 或者dpkg -i *.deb安装软件常常提示“有 ...

  5. Dockerfile中npm中Error: could not get uid/gid问题的解决方法

    dockerfile 中  使用 npm 的时候报错:   解决办法:https://github.com/tootsuite/mastodon/issues/802              

  6. Qt - 错误总结 - 在自定义类头文件中添加Q_OBJECT 编译时报错(undefined reference to ‘vtable for xxThread)

    错误提示:在添加的QThread子类头文件添加Q_OBJECT时,编译程序,出现"undefined reference to 'vtable for xxThread'"错误提示 ...

  7. Sass for循环中编译%时报错解决方案

    sass功能强大,特别是支持for循环,节省大量开发时间,但是在开发时遇到一个问题,直接使用%时没有问题,当有变量时再加% 单位在编译时报错: 这样没有问题: @for $width from 0 t ...

  8. cocos2d-x 头文件中添加方法变量导致编译报错

    代码如下: HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__ #include " ...

  9. 解决:function in namespace ‘std’ does not name a type + allocator_/nullptr/dellocator_ was not declared + base operand of ‘->’ has non-pointer type ‘std::vector<cv::Mat>’ 错误编译时报错(caffe)

    解决方法,用到了c++11,g++命令需要加上-std=c++11选项 附:g++默认的c++标准 gcc-6.4.0 gcc-7.2.0 默认是 -std=gnu++14gcc-4.3.6 gcc- ...

随机推荐

  1. Java知多少(86)文本框和文本区的输入输出

    在GUI中,常用文本框和文本区实现数据的输入和输出.如果采用文本区输入,通常另设一个数据输入完成按钮.当数据输入结束时,点击这个按钮.事件处理程序利用getText()方法从文本区中读取字符串信息.对 ...

  2. python笔记2-数据类型:列表[List]常用操作

    序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,索引从0开始,依此类推. 序列都可以进行的操作:包括 索引,切片,加,乘,检查成员. 列表是最常用的Pyth ...

  3. 【转帖】解决远程连接MariaDB(mysql)很慢的方法

    在CentOS7上安装完成MariaDB之后,发现无论命令行还是程序中连接MariaDB的时候都很慢,大约要一二十秒,于是网上搜索了一番,发现下面的文章内容: 在进行 ping和route后发现网络通 ...

  4. 升级 Centos 6.5/6.7 的 php 版本

    Centos 6.5/6.7 的 php 预设是用 5.3.3 这个版本号 wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-releas ...

  5. System.getProperty()获取系统的配置信息

    原文地址:http://www.jsjtt.com/java/Javajichu/105.html 此处记录备用. 1. 通过System.getProperty()可以获取系统的配置信息,Syste ...

  6. eclipse去掉所有断点 恢复到默认窗口

    1.去掉所有断点 Window->Open Perspective->Debug默认是右上角的窗口中,切换到Breakpoints,如果里边有内容,那就是设置断点啦,点叉叉全部删掉就好了. ...

  7. [原]Jenkins(十三)---jenkins用户权限管理

    * 版权声明:本博客欢迎转发,但请保留原作者信息! http://www.cnblogs.com/horizonli/p/5337874.html 两种策略的比较

  8. java保存动态代理生成的类的class文件

    启动时加: -Dsun.misc.ProxyGenerator.saveGeneratedFiles=true

  9. ls --help 常用命令

    $ ls --help Usage: ls [OPTION]... [FILE]... List information about the FILEs (the current directory ...

  10. pycharm平台下的Django教程(转)

    本文面向:有python基础,刚接触web框架的初学者. 环境:windows7   python3.5.1  pycharm专业版  Django 1.10版 pip3 一.Django简介 百度百 ...