Esempio n. 1
0
    def mmr_train(self):

        if self.iperceptron == 0:
            mtra = self.mtrain

            KX=mmr_kernel(self,self.itrain,self.itrain,ioutput=0, \
                                  itraintest=0,itraindata=0,itensor=self.kmode)[0]
            KY=mmr_kernel(self,self.itrain,self.itrain,ioutput=1, \
                                  itraintest=0,itraindata=0)[0]

            cOptDual = base.cls_dual(None, None)
            self.dual = cOptDual
            self.csolver = mmr_solver_cls.cls_mmr_solver()
            self.dual.alpha=self.csolver.mmr_solver(KX,KY,self.penalty.c, \
                                                    self.penalty.d)
            ## estimate the bias for linear output kernel

            if self.YKernel.ifeature == 0:
                YTrain = self.YKernel.get_train_norm(self.itrain)
                ZW=np.dot(YTrain.T, \
                          (np.outer(self.dual.alpha,np.ones(mtra))*KX))
                xbias = np.median(YTrain - ZW.T, axis=0)
            else:
                KY = self.YKernel.Kcross
                ZW = np.dot(KY.T,
                            (np.outer(self.dual.alpha, np.ones(mtra)) * KX))
                xbias = np.zeros(mtra)

            self.dual.bias = xbias
        elif self.iperceptron == 1:
            cOptDual = base.cls_dual(None, None)
            XTrainNorm0 = self.XKernel[0].get_train_norm()
            YTrainNorm = self.YKernel.get_train_norm(self.itrain)
            cOptDual.W=mmr_perceptron_primal(XTrainNorm0,YTrainNorm, \
                                             self.perceptron.margin, \
                                             self.perceptron.stepsize, \
                                             self.perceptron.nrepeat)
        elif self.iperceptron == 2:
            cOptDual = base.cls_dual(None, None)
            XTrainNorm0 = self.XKernel[0].get_train_norm()
            YTrainNorm = self.YKernel.get_train_norm(self.itrain)
            cOptDual.alpha=mmr_perceptron_dual(XTrainNorm0,YTrainNorm, \
                                             self.XKernel[0].kernel_params.ipar1, \
                                             self.XKernel[0].kernel_params.ipar2, \
                                             self.XKernel[0].kernel_type, \
                                             self.perceptron.margin, \
                                             self.perceptron.stepsize, \
                                             self.perceptron.nrepeat)

        return (cOptDual)
Esempio n. 2
0
  def mmr_train(self):

    if self.iperceptron==0:
      mtra=self.mtrain

      KX=mmr_kernel(self,self.itrain,self.itrain,ioutput=0, \
                            itraintest=0,itraindata=0,itensor=self.kmode)[0]
      KY=mmr_kernel(self,self.itrain,self.itrain,ioutput=1, \
                            itraintest=0,itraindata=0)[0]

      cOptDual=base.cls_dual(None,None)
      self.dual=cOptDual
      self.csolver=mmr_solver_cls.cls_mmr_solver()
      self.dual.alpha=self.csolver.mmr_solver(KX,KY,self.penalty.c, \
                                              self.penalty.d)
    ## estimate the bias for linear output kernel

      if self.YKernel.ifeature==0:
        YTrain=self.YKernel.get_train_norm(self.itrain)
        ZW=np.dot(YTrain.T, \
                  (np.outer(self.dual.alpha,np.ones(mtra))*KX))
        xbias=np.median(YTrain-ZW.T,axis=0)
      else:
        KY=self.YKernel.Kcross
        ZW=np.dot(KY.T,(np.outer(self.dual.alpha,np.ones(mtra))*KX))
        xbias=np.zeros(mtra)

      self.dual.bias=xbias  
    elif self.iperceptron==1:
      cOptDual=base.cls_dual(None,None)
      XTrainNorm0=self.XKernel[0].get_train_norm()
      YTrainNorm=self.YKernel.get_train_norm(self.itrain)
      cOptDual.W=mmr_perceptron_primal(XTrainNorm0,YTrainNorm, \
                                       self.perceptron.margin, \
                                       self.perceptron.stepsize, \
                                       self.perceptron.nrepeat)      
    elif self.iperceptron==2:
      cOptDual=base.cls_dual(None,None)
      XTrainNorm0=self.XKernel[0].get_train_norm()
      YTrainNorm=self.YKernel.get_train_norm(self.itrain)
      cOptDual.alpha=mmr_perceptron_dual(XTrainNorm0,YTrainNorm, \
                                       self.XKernel[0].kernel_params.ipar1, \
                                       self.XKernel[0].kernel_params.ipar2, \
                                       self.XKernel[0].kernel_type, \
                                       self.perceptron.margin, \
                                       self.perceptron.stepsize, \
                                       self.perceptron.nrepeat)      

    return(cOptDual)
Esempio n. 3
0
    def edge_prekernel(self, X, params_spec):

        (m, n) = X.shape  ## xdataraw[0]
        dalpha = {}
        dxy = {}
        ialpha = 1
        ny = 2
        nitem = 3

        self.ddiag = np.zeros(m)
        csolver = mmr_solver_cls.cls_mmr_solver()

        ## read the edges
        for iview in range(m):
            xy = X[iview]
            nxy = len(xy) // nitem
            xy = xy[:nxy * nitem]
            xy = xy.reshape((nxy, nitem))
            ixy = np.where(xy[:, 0] > 0)[0]
            xy = xy[ixy]
            x = xy[:, :2]  ## edge position
            ya = xy[:, 2]  ## edge angle

            y = np.vstack((np.cos(ya), np.sin(ya))).T
            ## normalize the locations
            ilocal = self.norm.ilocal
            iscale = self.norm.iscale
            (xnorm, xdummy, opar) = mmr_normalization(ilocal, iscale, x, None,
                                                      0)
            ## xnorm=x
            self.dxy[iview] = [xnorm, y]
            if ialpha == 1:
                Ky = np.dot(y, y.T)
                xdata = [xnorm, None]
                (Kx, dx1, dx2) = kernel_eval_kernel(xdata, None, None,
                                                    params_spec)
                C = 1
                D = 0
                xalpha = csolver.mmr_solver(Kx, Ky, C, D, 1, 1, 1, 1)
                self.dalpha[iview] = xalpha
            else:
                xalpha = np.ones(len(y)) / len(y)
                self.dalpha[iview] = xalpha

            self.ddiag[iview] = np.dot(xalpha, np.dot(Kx * Ky, xalpha))

        return
Esempio n. 4
0
  def edge_prekernel(self,X,params_spec):

    (m,n)=X.shape   ## xdataraw[0]
    dalpha={}
    dxy={}
    ialpha=1
    ny=2
    nitem=3

    self.ddiag=np.zeros(m)
    csolver=mmr_solver_cls.cls_mmr_solver()

    ## read the edges
    for iview in range(m):
      xy=X[iview]
      nxy=len(xy)//nitem
      xy=xy[:nxy*nitem]
      xy=xy.reshape((nxy,nitem))
      ixy=np.where(xy[:,0]>0)[0]
      xy=xy[ixy]
      x=xy[:,:2]  ## edge position
      ya=xy[:,2]   ## edge angle

      y=np.vstack((np.cos(ya),np.sin(ya))).T
      ## normalize the locations
      ilocal=self.norm.ilocal
      iscale=self.norm.iscale
      (xnorm,xdummy,opar)=mmr_normalization(ilocal,iscale,x,None,0)
      ## xnorm=x
      self.dxy[iview]=[xnorm,y]
      if ialpha==1:
        Ky=np.dot(y,y.T)
        xdata=[xnorm,None]
        (Kx,dx1,dx2)=kernel_eval_kernel(xdata,None,None,params_spec)
        C=1
        D=0
        xalpha=csolver.mmr_solver(Kx,Ky,C,D,1,1,1,1)
        self.dalpha[iview]=xalpha
      else:
        xalpha=np.ones(len(y))/len(y)
        self.dalpha[iview]=xalpha
      
      self.ddiag[iview]=np.dot(xalpha,np.dot(Kx*Ky,xalpha))

    return
Esempio n. 5
0
def loadData(trainOutput, trainInput, dualParams, kernelParams):
    global cMMR

    params = mmr_setparams.cls_params()
    nview = 1
    params.ninputview = nview
    cMMR = mmr_mmr_cls.cls_mmr(params.ninputview)
    nfold = cMMR.nfold
    nrepeat = cMMR.nrepeat

    ## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    cdata_store=armar_load_data.cls_label_files(trainOutput,trainInput, \
                                                dualParams,kernelParams)
    cdata_store.load_mmr(cMMR)
    mdata = cMMR.mdata
    ## ############################################################
    xselector = np.ones(mdata)
    xselector[-1] = 0
    ifold = 0
    cMMR.split_train_test(xselector, ifold)
    cMMR.compute_kernels()
    ## cMMR.Y0=cMMR.YKernel.get_train(cMMR.itrain)   ## candidates
    cMMR.csolver = mmr_solver_cls.cls_mmr_solver()
Esempio n. 6
0
def loadData(trainOutput,trainInput,dualParams,kernelParams):
  global cMMR
  
  params=mmr_setparams.cls_params()
  nview=1
  params.ninputview=nview
  cMMR=mmr_mmr_cls.cls_mmr(params.ninputview)
  nfold=cMMR.nfold
  nrepeat=cMMR.nrepeat

  ## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  cdata_store=armar_load_data.cls_label_files(trainOutput,trainInput, \
                                              dualParams,kernelParams)  
  cdata_store.load_mmr(cMMR)
  mdata=cMMR.mdata
  ## ############################################################
  xselector=np.ones(mdata)
  xselector[-1]=0
  ifold=0
  cMMR.split_train_test(xselector,ifold)
  cMMR.compute_kernels()
  ## cMMR.Y0=cMMR.YKernel.get_train(cMMR.itrain)   ## candidates
  cMMR.csolver=mmr_solver_cls.cls_mmr_solver()