示例#1
0
文件: layer.cpp 项目: kiddos/reversi
mat OutputLayer::backprop(const mat label) {
  y = label;
  delta = costd(y, a, z);
  grad = pa.t() * delta;
  // regularization
  grad = grad + lambda * W;
  // compute next layer delta
  mat newdelta = delta * W.t();
  return newdelta;
}
示例#2
0
//*************************************************************************
void findbestcorr(double **newimage, int ncols, int *bounds, int n, double *origvec,int morigvec,double *transform, int ntransform,int iter) {
    double *tvec=NULL;
    double *t=transform+n*ntransform;
    int boundsvec[2];
    double score;
    double delta=INITDELTA;
    int i;
    int precibest=-1;
    double *newscore=(double *) malloc(2*ntransform*sizeof(double));
    int ibestscore;

    score=cost(newimage,newimage[n],bounds,bounds+2*n, ncols, n)-costd(transform,ncols,ntransform,n,iter);
    //mexPrintf("score original: %lf pour %d t:%lf costd:%lf\n",score,n,t[0],costd(transform,ncols,ntransform,n,iter));
    while(delta>=1) {
        //mexPrintf("Delta : %f \n",delta);
        ibestscore=-1;
        for(i=0;i<ntransform;i++) {
            if(precibest!=2*i+1) {
                t[i]+=delta;
                 //mexPrintf("before ==");
                transformvec(origvec,morigvec,tvec,boundsvec,t,ntransform);
                //mexPrintf(" ==after t1:%lf b1:%d b2:%d t:%lf\n",tvec[0],boundsvec[0],boundsvec[1],transform[i]);
                newscore[2*i]=cost(newimage,tvec,bounds,boundsvec, ncols, n)-costd(transform,ncols,ntransform,n,iter);
                if(newscore[2*i]>score) {
                    score=newscore[2*i];
                    ibestscore=2*i;
                }
                t[i]-=delta;
            }
            //mexPrintf("score : %f i:%d bi:%d :\n",newscore[2*i],i,ibestscore);           
            if(precibest!=2*i) {
                t[i]-=delta;
                //mexPrintf("before ==");
                transformvec(origvec,morigvec,tvec,boundsvec,t,ntransform);
                //mexPrintf(" ==after t1:%lf b1:%d b2:%d t:%lf\n",tvec[0],boundsvec[0],boundsvec[1],transform[i]);

                newscore[2*i+1]=cost(newimage,tvec,bounds,boundsvec, ncols, n)-costd(transform,ncols,ntransform,n,iter);  
                if(newscore[2*i+1]>score) {
                    score=newscore[2*i+1];
                    ibestscore=2*i+1;
                } 
                //mexPrintf("score : %f i:%d bi:%d :\n",newscore[2*i+1],i,ibestscore);           
                t[i]+=delta;
            }
        }
        if(ibestscore==-1)
            delta=0.5*delta;
        else{
            if(ibestscore%2==0)
                t[ibestscore/2]+=delta;
            else
                t[ibestscore/2]-=delta;        
        }
        precibest=ibestscore;
        
    }
    //mexPrintf("score final: %lf t:%lf costd:%lf\n",score,t[0],costd(transform,ncols,ntransform,n,iter));
    free(newscore);
    transformvec(origvec, morigvec, tvec, boundsvec, t, ntransform);

    
    free(newimage[n]);
    newimage[n]=tvec;
    bounds[2*n]=boundsvec[0];
    bounds[2*n+1]=boundsvec[1];
}