Stochastic Rounding Algorithm
来源:互联网 发布:爱知学院大学排名 编辑:程序博客网 时间:2024/06/03 08:32
Algorithm Implementation based on 《Deep Learning with Limited Numerical Precision》
ICML 2015 LILLE
C++ Version:
#include <iostream>#include <math.h>using namespace std;float StochasticRounding(float x, unsigned int WL, unsigned int FL);float Convert(float x, unsigned int WL, unsigned int FL);float random( void );template <class T>int getArrayLen(T& array){return (sizeof(array) / sizeof(array[0]));}int main(){/*float a = -1.183421;unsigned int WL = 8;unsigned int IL = 4;unsigned int FL = WL - IL;cout << Convert(a, WL, FL) << endl;*/float testArray[] = {0, 1.251, -1.251, 1.35, -1.35, 2.171, -2.171, 0.012, -0.012, 0.00012, -0.0012, 0.3275, -0.3275, 1.3, -1.3, -2, 2};unsigned int WL = 8;unsigned int IL = 4;unsigned int FL = WL - IL;srand((unsigned)time(NULL));for(int i = 0; i < getArrayLen(testArray); i++){ cout << Convert(testArray[i], WL, FL) << endl; }return 0;}float Convert(float x, unsigned int WL, unsigned int FL){unsigned int IL = WL - FL;float minValue = -1 * (1<<(IL-1));float maxValue = 1<<(IL-1); maxValue -= 1/float(1<<FL);if( x >= maxValue ){return maxValue;}else if( x <= minValue ){return minValue;}else{return StochasticRounding(x, WL, FL);}}float random( void ){return float(rand())/float(RAND_MAX + 1.0);}float Binornd(float p){return (random() >= p) ? 0 : 1;}float StochasticRounding(float x, unsigned int WL, unsigned int FL){float power = 1<<FL;float integer_data = floor(x * power);float float_data = integer_data / power;float p = (x - float_data) * power; return Binornd(p) * (1/power) + float_data;}Any bugs or room for performance improvement, please contact with shuanholmes@outlook.com .
Thanks. :)
0 0
- Stochastic Rounding Algorithm
- batch gradient algorithm and stochastic gradient algorithm
- Weird Rounding
- Weird Rounding
- Codeforces898A-Rounding
- A. Rounding
- stochastic regression
- stochastic pool
- stochastic matrix
- Algorithm
- Algorithm
- algorithm
- algorithm
- algorithm
- algorithm
- algorithm
- Algorithm
- Algorithm
- Scala学习笔记2 - 控制结构和函数
- 杭电oj----1035 Robot Motion
- java中queue的使用(转载)
- Problem G: 985的数字难题
- Hashtable与HashMap的区别
- Stochastic Rounding Algorithm
- Struts2中值栈的理解
- android studio 运行错误非法字符: \65279
- zzulioj1896 985的买饮料难题(数学)
- hdu 4862 Jump(最大费用最大流,巧妙构图)
- 自定义控件StepView
- Contest - 多校训练(985专场)
- thinkphp常用变量
- struts2配置文件详解