Created
August 8, 2014 22:49
-
-
Save zhangce/f42661897c9518cf8432 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "common.h" | |
| #include "engine/dstruct.h" | |
| #include "engine/scheduler.h" | |
| //#include "engine/scheduler_strawman.h" | |
| //#include "engine/scheduler_hogwild.h" | |
| //#include "engine/scheduler_percore.h" | |
| // | |
| #include "app/glm.h" | |
| //#include "app/cnn.h" | |
| #include "engine/dense_dimmwitted.h" | |
| #include <xmmintrin.h> | |
| #include <immintrin.h> | |
| #include <avxintrin.h> | |
| class GLMModelExample{ | |
| public: | |
| double p[1024]; | |
| int n=1024; | |
| GLMModelExample(){} | |
| }; | |
| double f_lr_loss(const double* const p_row, int nfeat, GLMModelExample* const p_model){ | |
| const double * ex = &p_row[1]; | |
| double * model = p_model->p; | |
| double label = p_row[0]; | |
| double dot = 0.0; | |
| for(int i=1;i<nfeat;i++){ | |
| dot += ex[i-1] * model[i-1]; | |
| } | |
| return - label * dot + log(exp(dot) + 1.0); | |
| } | |
| double f_lr_sse(const double* const p_row, int nfeat, GLMModelExample* const p_model){ | |
| const double * ex = &p_row[1]; | |
| double * model = p_model->p; | |
| double label = p_row[0]; | |
| double dot = 0.0; | |
| double dot1 = 0.0, dot2 = 0.0, dot3 = 0.0, dot4 = 0.0; | |
| for(int i=0;i<nfeat-1;i+=4){ | |
| dot1 += ex[i] * model[i]; | |
| dot2 += ex[i+1] * model[i+1]; | |
| dot3 += ex[i+2] * model[i+2]; | |
| dot4 += ex[i+3] * model[i+3]; | |
| } | |
| dot = dot1 + dot2 + dot3 + dot4; | |
| const double d = exp(-dot); | |
| const double Z = 0.0001 * (-label + 1.0/(1.0+d)); | |
| for(int i=0;i<nfeat-1;i++){ | |
| model[i] -= ex[i] * Z; | |
| } | |
| return 1.0; | |
| } | |
| int main(int argc, char** argv){ | |
| long nexp = 100000; | |
| long nfeat = 1024; | |
| double ** examples = new double* [nexp]; | |
| for(long i=0;i<nexp;i++){ | |
| examples[i] = new double[nfeat + 1]; | |
| for(int j=1;j<nfeat+1;j++){ | |
| examples[i][j] = 1; | |
| } | |
| examples[i][0] = drand48() > 0.8 ? 0 : 1.0; | |
| } | |
| GLMModelExample model; | |
| for(int i=0;i<model.n;i++){ | |
| model.p[i] = 0.0; | |
| } | |
| DenseDimmWitted<double, GLMModelExample> | |
| dw(examples, nexp, nfeat+1, &model, DW_STRAWMAN, DW_SHARDING, DW_ROW); | |
| unsigned int f_handle = dw.register_row(f_lr_sse); | |
| unsigned int f_loss = dw.register_row(f_lr_loss); | |
| for(int i_epoch=0;i_epoch<1000;i_epoch++){ | |
| double loss = dw.exec(f_loss)/nexp; | |
| double sum = 0.0; | |
| for(int i=0;i<nfeat;i++){ | |
| sum += model.p[i]; | |
| } | |
| std::cout << sum << " loss=" << loss << std::endl; | |
| dw.exec(f_handle); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment