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
原创粉丝点击