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如何根据主机名(域名)获取IP地址?

    在Java编程中,如何根据主机名(域名)获取IP地址? 以下示例显示了如何通过net.InetAddress类的InetAddress.getByName()方法将主机名更改为指定的IP地址. pac ...

  2. UIInterfaceOrientation over iOS6 (应用旋转屏幕)

      typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { UIInterfaceOrientationUnknown = UIDeviceOrien ...

  3. CentOS7 yum方式安装MariaDB 10.2.13-1

    注:以下步骤都是以root身份运行. 一.建立mariadb.repo 1,编辑新文件,命令:vim  /etc/yum.repos.d/mariadb.repo 2,输入如下内容,保存退出 [mar ...

  4. MTK 屏幕旋转90度

    http://blog.csdn.net/ouo555/article/details/44806837 1.屏幕显示顺时针旋转90度 lk 横屏logo,顺时针旋转90度显示修改bootable/b ...

  5. 查询Array中确定数值的对象&JS linq使用 = linq.js

    var x=new Array(); x.push({"a":3,"b":3},{"a":2,"b":2},{" ...

  6. linux后台执行命令:&和nohup

    当我们在终端或控制台工作时,可能不希望由于运行一个作业而占住了屏幕,因为可能还有更重要的事情要做,比如阅读电子邮件.对于密集访问磁盘的进程,我们更希望它能够在每天的非负荷高峰时间段运行(例如凌晨).为 ...

  7. [原]openstack-kilo--issue(九) heat stacks topology中图形无法正常显示

    本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. ======声明======= 欢迎转载:转载请注明出处 http://www. ...

  8. Oracle数据库入门——基础知识

    1.安装完成Oracle数据库后,使用sqlplus客户端登录数据库管理系统,只输入用户名,没有输入密码时,会提示口令为空,登录被拒绝. 请输入用户名:system 输入口令: ERROR:ORA-0 ...

  9. ssl---阿里云的public.crt和chain.crt的证书怎么弄

    由于项目需要,网站需要https服务,服务器是阿里云的,装的是宝塔的面板,下面是详细的配置ssl证书的方法: 如何在阿里云的后台申请ssl证书就不说了,下载下来的证书有三个:.key   chain. ...

  10. twisted 学习笔记一:事件循环

    from twisted.internet import reactor import time def printTime(): print "Current time is", ...