Пример #1
0
    def mvm_train(self):
        """
    execute the trianing procedure
    Inputs:
    """

        ## print('Generate kernels')
        time0 = time.time()
        self.compute_kernels()
        if self.verbose == 1:
            print('Kernel computation:', time.time() - time0)
        ## print('Solve optimization problem')
        time0 = time.time()
        self.KX=mmr_kernel(self,self.itrain,self.itrain,ioutput=0, \
                                itraintest=0, itensor=self.kmode)[0]
        self.KY = mmr_kernel(self, self.itrain, self.itrain, ioutput=1)[0]
        if self.verbose == 1:
            print('Kernel merge computation:', time.time() - time0)
        ## self.solvertime=time.time()-time0

        ## t0=time.clock()
        cOptDual = base.cls_dual(None, None)
        self.dual = cOptDual
        time0 = time.time()

        cmvm_solver = mvm_solver_cls.cls_mvm_solver()
        self.dual.alpha = cmvm_solver.mvm_solver(self)
        self.solvertime = time.time() - time0

        return (cOptDual)
Пример #2
0
  def mvm_train(self):
    """
    execute the trianing procedure
    Inputs:
    """

    ## print('Generate kernels')
    time0=time.time()
    self.compute_kernels()
    if self.verbose==1:
      print('Kernel computation:',time.time()-time0)
    ## print('Solve optimization problem')
    time0=time.time()
    self.KX=mmr_kernel(self,self.itrain,self.itrain,ioutput=0, \
                            itraintest=0, itensor=self.kmode)[0]
    self.KY=mmr_kernel(self,self.itrain,self.itrain,ioutput=1)[0]
    if self.verbose==1:
      print('Kernel merge computation:',time.time()-time0)
    ## self.solvertime=time.time()-time0

    ## t0=time.clock()
    cOptDual=base.cls_dual(None,None)
    self.dual=cOptDual
    time0=time.time()

    cmvm_solver=mvm_solver_cls.cls_mvm_solver()
    self.dual.alpha=cmvm_solver.mvm_solver(self)
    self.solvertime=time.time()-time0

    return(cOptDual)
Пример #3
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)
Пример #4
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)
Пример #5
0
  def mmr_test(self,cOptDual,itraindata=1):

    if self.YKernel.kernel_params.kernel_type>0:
      self.itestmode=0
      
    if itraindata==0:
      itest=self.itrain
    else:
      itest=self.itest
    if self.iperceptron in (0,2):
      KXcross=mmr_kernel(self,self.itrain,itest,ioutput=0, \
                                 itraintest=1, itraindata=itraindata, \
                                 itensor=self.kmode)[0]
      KYcross=mmr_kernel(self,self.itrain,self.itrain,ioutput=1, \
                                 itraintest=1,itraindata=itraindata)[0]
      mtest=KXcross.shape[1]
      
      cPredict=base.cls_predict()

      if self.YKernel.ifeature==0:
        if self.YKernel.kernel_params.kernel_type==0:
          YTrain=self.YKernel.get_train_norm(self.itrain)
          Zw0=np.dot(YTrain.T,(np.outer(cOptDual.alpha, \
                                      np.ones(mtest))*KXcross))
          if self.csolver.ibias_estim==1:
            Zw0=Zw0+np.outer(cOptDual.bias,np.ones(mtest))

          xnorm=np.sqrt(np.sum(Zw0**2,axis=0))
          xnorm=xnorm+(xnorm==0)
          Zw=Zw0/np.outer(np.ones(Zw0.shape[0]),xnorm)
          Y0=self.YKernel.get_Y0_norm(self.itrain) 
          cPredict.ZTest=np.dot(Y0,Zw)
        else:
          Zw0=np.dot(KYcross.T, \
                     (np.outer(cOptDual.alpha,np.ones(mtest))*KXcross))
          cPredict.ZTest=Zw0
          if self.csolver.ibias_estim==1:
            cPredict.bias=cOptDual.bias
          else:
            cPredict.bias=0
          Zw=Zw0
        cPredict.Zw0=Zw0.T
        cPredict.Zw=Zw.T

        if self.itestmode in (0,2,3,4):
          Y0=self.YKernel.get_Y0(self.itrain)
          cPredict.zPremax=cPredict.ZTest.max(0)
          cPredict.iPredCat=cPredict.ZTest.argmax(0)
          cPredict.zPred=Y0[cPredict.iPredCat]
          if self.itestmode==2 and self.YKernel.kernel_params.kernel_type==0:
            cPredict.knnPredCat=np.argsort \
                              (-cPredict.ZTest,axis=0)[:self.testknn,:]
          elif self.itestmode==3:
            n=YTrain.shape[1]
            nsort=5
            for i in range(mtest):
              cPredict.zPred[i,:]=np.zeros(n)
              iones=np.argsort(-Zw[:,i])[:nsort]
              for j in range(nsort-1):
                if Zw[iones[j],i]<0.04 or \
                     Zw[iones[j],i]>10.0*Zw[iones[j+1],i]:
                  iones=iones[:(j+1)]
                  break
              cPredict.zPred[i,iones]=1
            
        elif self.itestmode==1:
          YTrain=self.YKernel.get_train(self.itrain)
          ndim=YTrain.shape[1]
          i2boro=np.argsort(-Zw0,axis=0)[0:self.testknn,:].T
          cPredict.zPred=np.zeros((mtest,ndim))
          for i in range(mtest):
            zsum=np.zeros(ndim)
            for j in range(self.testknn):
              zsum+=YTrain[i2boro[i,j]]
            cPredict.zPred[i,:]=1*(zsum>=self.testknn/2)

      elif self.YKernel.ifeature==1:
        Y0=self.YKernel.get_Y0(self.itrain)
        cPredict.ZTest=np.dot(KYcross.T,(np.outer(cOptDual.alpha, \
                                      np.ones(mtest))*KXcross))
        cPredict.zPremax=cPredict.ZTest.max(0)
        cPredict.iPredCat=cPredict.ZTest.argmax(0)
        cPredict.zPred=Y0[cPredict.iPredCat]
        if self.itestmode==2:
          cPredict.knnPredCat=np.argsort \
                               (-cPredict.ZTest,axis=0)[:self.testknn,:]
    elif self.iperceptron==1:
      if itraindata==0:
        X=self.XKernel[0].get_train_norm(self.itrain)
        for i in range(1,len(self.XKernel)):
          X=np.hstack((X,self.XKernel[i].get_train_norm(self.itrain)))
      else:
        X=self.XKernel[0].get_test_norm(itest)
        for i in range(1,len(self.XKernel)):
          X=np.hstack((X,self.XKernel[i].get_test_norm(itest)))
      cPredict=base.cls_predict()
      xmean=np.mean(X)
      mtest=X.shape[0]
      Zw=np.dot(cOptDual.W, \
                 np.hstack((X,np.ones((mtest,1))*xmean)).T)
      cPredict.ZTest=np.dot(YTrain,Zw)
      cPredict.zPremax=cPredict.ZTest.max(0)
      cPredict.iPredCat=cPredict.ZTest.argmax(0)
      cPredict.zPred=YTrain[cPredict.iPredCat]
      if self.itestmode==2:
        cPredict.knnPredCat=np.argsort(-cPredict.ZTest, \
                                       axis=0)[:self.testknn,:]
    
    return(cPredict)
Пример #6
0
    def mmr_test(self, cOptDual, itraindata=1):

        if self.YKernel.kernel_params.kernel_type > 0:
            self.itestmode = 0

        if itraindata == 0:
            itest = self.itrain
        else:
            itest = self.itest
        if self.iperceptron in (0, 2):
            KXcross=mmr_kernel(self,self.itrain,itest,ioutput=0, \
                                       itraintest=1, itraindata=itraindata, \
                                       itensor=self.kmode)[0]
            KYcross=mmr_kernel(self,self.itrain,self.itrain,ioutput=1, \
                                       itraintest=1,itraindata=itraindata)[0]
            mtest = KXcross.shape[1]

            cPredict = base.cls_predict()

            if self.YKernel.ifeature == 0:
                if self.YKernel.kernel_params.kernel_type == 0:
                    YTrain = self.YKernel.get_train_norm(self.itrain)
                    Zw0=np.dot(YTrain.T,(np.outer(cOptDual.alpha, \
                                                np.ones(mtest))*KXcross))
                    if self.csolver.ibias_estim == 1:
                        Zw0 = Zw0 + np.outer(cOptDual.bias, np.ones(mtest))

                    xnorm = np.sqrt(np.sum(Zw0**2, axis=0))
                    xnorm = xnorm + (xnorm == 0)
                    Zw = Zw0 / np.outer(np.ones(Zw0.shape[0]), xnorm)
                    Y0 = self.YKernel.get_Y0_norm(self.itrain)
                    cPredict.ZTest = np.dot(Y0, Zw)
                else:
                    Zw0=np.dot(KYcross.T, \
                               (np.outer(cOptDual.alpha,np.ones(mtest))*KXcross))
                    cPredict.ZTest = Zw0
                    if self.csolver.ibias_estim == 1:
                        cPredict.bias = cOptDual.bias
                    else:
                        cPredict.bias = 0
                    Zw = Zw0
                cPredict.Zw0 = Zw0.T
                cPredict.Zw = Zw.T

                if self.itestmode in (0, 2, 3, 4):
                    Y0 = self.YKernel.get_Y0(self.itrain)
                    cPredict.zPremax = cPredict.ZTest.max(0)
                    cPredict.iPredCat = cPredict.ZTest.argmax(0)
                    cPredict.zPred = Y0[cPredict.iPredCat]
                    if self.itestmode == 2 and self.YKernel.kernel_params.kernel_type == 0:
                        cPredict.knnPredCat=np.argsort \
                                          (-cPredict.ZTest,axis=0)[:self.testknn,:]
                    elif self.itestmode == 3:
                        n = YTrain.shape[1]
                        nsort = 5
                        for i in range(mtest):
                            cPredict.zPred[i, :] = np.zeros(n)
                            iones = np.argsort(-Zw[:, i])[:nsort]
                            for j in range(nsort - 1):
                                if Zw[iones[j],i]<0.04 or \
                                     Zw[iones[j],i]>10.0*Zw[iones[j+1],i]:
                                    iones = iones[:(j + 1)]
                                    break
                            cPredict.zPred[i, iones] = 1

                elif self.itestmode == 1:
                    YTrain = self.YKernel.get_train(self.itrain)
                    ndim = YTrain.shape[1]
                    i2boro = np.argsort(-Zw0, axis=0)[0:self.testknn, :].T
                    cPredict.zPred = np.zeros((mtest, ndim))
                    for i in range(mtest):
                        zsum = np.zeros(ndim)
                        for j in range(self.testknn):
                            zsum += YTrain[i2boro[i, j]]
                        cPredict.zPred[i, :] = 1 * (zsum >= self.testknn / 2)

            elif self.YKernel.ifeature == 1:
                Y0 = self.YKernel.get_Y0(self.itrain)
                cPredict.ZTest=np.dot(KYcross.T,(np.outer(cOptDual.alpha, \
                                              np.ones(mtest))*KXcross))
                cPredict.zPremax = cPredict.ZTest.max(0)
                cPredict.iPredCat = cPredict.ZTest.argmax(0)
                cPredict.zPred = Y0[cPredict.iPredCat]
                if self.itestmode == 2:
                    cPredict.knnPredCat=np.argsort \
                                         (-cPredict.ZTest,axis=0)[:self.testknn,:]
        elif self.iperceptron == 1:
            if itraindata == 0:
                X = self.XKernel[0].get_train_norm(self.itrain)
                for i in range(1, len(self.XKernel)):
                    X = np.hstack(
                        (X, self.XKernel[i].get_train_norm(self.itrain)))
            else:
                X = self.XKernel[0].get_test_norm(itest)
                for i in range(1, len(self.XKernel)):
                    X = np.hstack((X, self.XKernel[i].get_test_norm(itest)))
            cPredict = base.cls_predict()
            xmean = np.mean(X)
            mtest = X.shape[0]
            Zw=np.dot(cOptDual.W, \
                       np.hstack((X,np.ones((mtest,1))*xmean)).T)
            cPredict.ZTest = np.dot(YTrain, Zw)
            cPredict.zPremax = cPredict.ZTest.max(0)
            cPredict.iPredCat = cPredict.ZTest.argmax(0)
            cPredict.zPred = YTrain[cPredict.iPredCat]
            if self.itestmode == 2:
                cPredict.knnPredCat=np.argsort(-cPredict.ZTest, \
                                               axis=0)[:self.testknn,:]

        return (cPredict)