示例#1
0
def dfpmin(*args):

    #Parameters
    args = list(args)
    xold = args[0] 
    n = args[1] 
    tolx = args[2] 
    gtol = args[3] 
    itmax = args[4] 
    bags = args[5] 
    baglabs = args[6] 

    xnew = xold
    
    fp,g = log_DD(xold,bags,baglabs)
    hessin = np.eye(2 * n)
    
    xi = - g
    sum = np.dot(xold , xold.T)
    stpmax = 100 * np.maximum(np.sqrt(sum),2 * n)
    for its in range(0,itmax):  
        iter = its
        pnew,fret,check = mil_lnsrch(xnew,n,fp,g,xnew,tolx,stpmax,bags,baglabs)
        fp = fret
        xi = pnew - xnew      #Update the line direction
        xnew = pnew           #Update the current pint
        test= np.max(abs(xi) / np.maximum(abs(xnew),1))   #Test for convergence on delta x
        if (test < tolx):
            return xnew,fret,iter
        dg = g                 #Save the old gradient
        dummy,g = log_DD(xnew,bags,baglabs)
        g = np.array([g])
        den = max(fret,1)     #Test for convergence on zero gradient
        test = np.max(np.abs(g)*(np.maximum(abs(xnew),1))) / den
        if (test < gtol):
            return xnew,fret,iter
        dg = g - dg
        hdg = np.dot(hessin, dg.T)
        fac = np.dot(dg , xi.T)
        fae = np.dot(dg , hdg)
        sumdg = np.dot(dg, dg.T)
        sumxi = np.dot(xi, xi.T)
        if (fac > np.sqrt(3e-08 * sumdg * sumxi)):
            fac = 1 / fac
            fad = 1 / fae
            dg = fac * xi - np.dot(fad, hdg.T)
            hessin = hessin + fac * (xi.T * xi) - fad * (hdg * hdg.T) + fae * (g.T * g)
        xi = ( np.dot(- hessin, g.T)).T
        
示例#2
0
def maxdd (*args):
    #Parameters
    args = list(args)
    spoints = args[0] 
    scales = args[1]
    bags = args[2]
    bagI = args[3]
    epochs = args[4]
    tol = args[5]

# initialize some parameters and storage

    num_start_points,dim = spoints.shape
    _ , dim = spoints.shape
    concepts = []
    maxConcept = []
    maxConcept.insert(0, np.concatenate((numpy.zeros(shape=(1,dim)) , numpy.ones(shape=(1,dim))),axis = 1))
    maxConcept.insert(1,0) 
    

# make several runs, starting with another startingpoint spoint.

    for i in range(0,num_start_points):
            #Meterle el if aqui del Print
        xold = np.concatenate((spoints[i],scales))
        fold, g = log_DD(xold,bags,bagI)
        p = -g
        sumx = np.dot(xold,xold.T)    #%Upper bound on step size
        stpmax = 100*max(np.sqrt(sumx),2*dim) #upper bound on step size
        #% Now do an iterative line-search to find the global minimum
        for iter in (0 ,epochs[0]):
            xnew,fnew,check = mil_lnsrch(xold,dim,fold,g,p,tol[3],stpmax,bags,bagI)
            xi = xnew-xold
            tst = max(abs(xi)/np.maximum(abs(xnew),1))
            if tst<tol[1]:
                break
            #Store for the next step
            p = -g
            xold = xnew
            fold = fnew
            sumx = np.dot(xold,xold.T)
            stpmax = 100*max(np.sqrt(sumx),2*dim)
    
        iterations=np.zeros(num_start_points)
    
        xnew,fret,iterations[i] = dfpmin(xnew,dim,tol[2],tol[3],epochs[1],bags,bagI)
    
        concepts.append([])
    
        concepts[i].append(xnew)
        concepts[i].append(np.exp(-fret))
        
        if concepts[i][1] > maxConcept[1]:
            maxConcept[0] = concepts[i][0]
            maxConcept[1] = concepts[i][1]
    return maxConcept, concepts
示例#3
0
 def predict(self, test_bags):
     """
     @param test_bags : a sequence of n bags; each bag is an m-by-k array-like
         object containing m instances with k features
     """
     bagsT = []
     for i in range(0, len(test_bags)):
         bagsT.append(test_bags[i])
     _, dimT = bagsT[0].shape
     nT = len(bagsT)
     outT = np.zeros(nT)
     for i in range(0, nT):
         outT[i], _ = log_DD(self._maxConcept, [test_bags[i]], [1])
     outT = outT.reshape(len(outT), 1)
     predicted = self._model.predict(outT)
     return predicted, outT
示例#4
0
    def fit(self,
            train_bags,
            train_labels,
            alf=10,
            spoints=10,
            epochs=np.array([3, 3]),
            frac=1,
            tol=[1e-4, 1e-4, 1e-4, 1e-4]):
        """
        @param train_bags : a sequence of n bags; each bag is an m-by-k array-like
            object containing m instances with k features
        @param train_labels : an array-like object of length n containing -1/+1 labels
        """
        self._spoints = spoints
        self._epochs = epochs
        self._frac = frac
        self._tol = tol
        self._alf = alf
        bagI = train_labels
        nrobags = len(train_bags)
        #_, dim = X.shape
        _, dim = train_bags[0].shape
        self._epochs = self._epochs * dim

        index = np.array(np.where(train_labels == 1))
        index = index.transpose()

        pbags = []  #positive Bags
        for l in range(0, len(index)):
            indice = index[l][0]
            pbags.append(train_bags[indice])

        #Poner la Condicion de if spoints is Empty Coger Todos
        #FALTA PONER TODOS LOS CONDICIONALES QUE TIENE EN MATLAB
        startpoint = np.vstack(pbags)
        I = np.random.permutation(len(startpoint))

        #PONER AQUI TODAS LAS CONDICIONALES DE SPOINTS

        if alf < 1:
            print('Hacer Algo')  #k = max(round(alf*length(I)),1);
        else:
            k = alf

        if k > len(startpoint):
            k = len(startpoint)
        else:
            startpoint = startpoint[I[0:k]]

        scales = np.matlib.repmat(0.1, k, dim)
        pointlogp = np.matlib.repmat(inf, k, 1)

        #start the optimization k times:
        for i in range(0, k):
            bestinst = []
            logp1, _ = log_DD(np.concatenate((startpoint[i, :], scales[i, :])),
                              train_bags, bagI)
            #do a few runs to optimize the concept and scales in an EM fashion:
            for r in range(0, 10):
                # find the best fitting instance per bag [ Los mas cercanos a ese starting point en cada Bolsa]]
                bestinst = []
                for j in range(0, nrobags):

                    dff = train_bags[j] - np.matlib.repmat(
                        startpoint[i], len(train_bags[j]), 1)
                    dff = np.dot(dff**2, scales[i].reshape(len(scales[i]), 1))
                    J = np.argmin(dff)
                    bestinst.append(np.array([train_bags[j][J]]))
                #run the maxDD on only the best instances
                self._maxConcept, concepts = maxdd(np.array([startpoint[i]]),
                                                   scales[i], bestinst, bagI,
                                                   self._epochs, tol)
                end = len(self._maxConcept[0])
                #                print('Range i '+str(i)+' Range r '+str(r)+' End : '+str(end)+' Starpoint[i]: '+str(len(startpoint[i]))+' Scales[i] : '+str(len(scales[i]))+' bestinst : '+str(len(bestinst)))
                startpoint[i] = self._maxConcept[0][0:dim]
                scales[i] = self._maxConcept[0][dim:end]
                # do we improve?
                logp0 = logp1
                logp1, _ = log_DD(self._maxConcept[0], train_bags, bagI)
                if (abs(np.exp(-logp1) - np.exp(-logp0)) <
                        0.01 * np.exp(-logp0)):
                    break
            pointlogp[i] = logp1
        # now we did it k times, what is the best one?
        J = np.argmin(pointlogp)
        self._maxConcept = np.concatenate((startpoint[J], scales[J]))
        # invent a threshold...:
        out = np.zeros(nrobags)
        for i in range(0, nrobags):
            out[i], _ = log_DD(self._maxConcept, [train_bags[i]], [1])
        model = LogisticRegression()
        out = out.reshape(len(out), 1)
        self._model = model.fit(out, train_labels)