(self, data, targets, B, V, costs, norm_up, step_size0, num_sub, AB, AV, Anum, trounds,
                    lmbda, average_begin, average_size)
return B, V, AB, AV, Anum, trounds
targets: 0 unlabeled, 1 positive, -1 negative, 2 dummy, 0.5 less positive
targets: 0 unlabeled, 1 positive, -1 negative, 2 dummy, 0.5 less positive
targets : 0 unlabeled 1 positive - 1 negative 2 dummy 0 . 5 less positive
def train_model(self, data, targets, B, V, costs, norm_up, step_size0, num_sub, AB, AV, Anum, trounds,
                    lmbda, average_begin, average_size):
        """targets: 0 unlabeled, 1 positive, -1 negative, 2 dummy, 0.5 less positive"""
        targets = np.asarray(targets)
        # print(np.nonzero(targets == 2.0))
        if len(np.nonzero(targets == 2.0)[0]) == 0:
            targets = np.hstack((targets, 2 * np.ones((targets.shape[0], 1))))
        data = np.asarray(data)
        B = np.asarray(B)
        V = np.asarray(V)

        n, n_class = np.shape(targets)
        row_ind, col_ind = np.nonzero(targets >= 1)
        train_pairs = np.hstack((row_ind.reshape((-1,1)), col_ind.reshape((-1,1))))

        # tmpnums = np.sum(targets >= 1, axis=1)
        # train_pairs = np.zeros((sum(tmpnums), 1))
        # tmpidx = 0

        # for i in range(n):
        #     train_pairs[tmpidx: tmpidx + tmpnums[i]] = i+1
        #     tmpidx = tmpidx + tmpnums[i]

        # targets = targets.T
        # # tp = np.nonzero(targets.flatten() >= 1)
        # # print(tp[0])
        # # print(len(tp[0]))
        # train_pairs = np.hstack(
        #     (train_pairs,
        #      np.reshape([nz % n_class for nz in np.nonzero(targets.flatten(order='F') >= 1)[0]], newshape=(-1, 1))))
        # # train_pairs[np.nonzero(train_pairs[:, 1] == 0)[0], 1] = n_class
        # targets = targets.T

        n = np.shape(train_pairs)[0]

        random_idx = randperm(n - 1)
        # import scipy.io as scio
        # ld = scio.loadmat('F:\\alipy_doc\\alipy-additional-methods-source\\multi label\\AURO\\perm.mat')
        # random_idx = ld['random_idx'].flatten()-1

        for i in range(n):
            idx_ins = int(train_pairs[random_idx[i], 0])
            xins = data[int(idx_ins), :].T
            idx_class = int(train_pairs[random_idx[i], 1])
            if idx_class == n_class-1:
                idx_irr = np.nonzero(targets[idx_ins, :] == -1)[0]
            # elif idx_class == idxPs[idx_ins]:
            #     idx_irr = np.hstack((np.nonzero(targets[idx_ins, :] == -1)[0], int(idxNs[idx_ins]), n_class - 1))
            else:
                idx_irr = np.hstack((np.nonzero(targets[idx_ins, :] == -1)[0], n_class - 1))
            n_irr = len(idx_irr)

            By = B[:, idx_class * num_sub: (idx_class + 1) * num_sub]
            Vins = V.dot(xins)
            fy = np.max(By.T.dot(Vins), axis=0)
            idx_max_class = np.argmax(By.T.dot(Vins), axis=0)
            By = By[:, idx_max_class]
            fyn = np.NINF
            for j in range(n_irr):
                idx_pick = idx_irr[randperm(n_irr - 1, 1)[0]]
                # print(idx_irr, idx_pick)
                Byn = B[:, idx_pick * num_sub: (idx_pick + 1) * num_sub]
                # [fyn, idx_max_pick] = max(Byn.T.dot(Vins),[],1)
                # if Byn == []:
                #     print(0)
                tmp1 = Byn.T.dot(Vins)
                fyn = np.max(tmp1, axis=0)
                idx_max_pick = np.argmax(tmp1, axis=0)

                if fyn > fy - 1:
                    break

            if fyn > fy - 1:
                step_size = step_size0 / (1 + lmbda * trounds * step_size0)
                trounds = trounds + 1
                Byn = B[:, idx_pick * num_sub + idx_max_pick]
                loss = costs[math.floor(n_irr / (j + 1)) - 1]
                tmp1 = By + step_size * loss * Vins
                tmp3 = np.linalg.norm(tmp1)
                if tmp3 > norm_up:
                    tmp1 = tmp1 * norm_up / tmp3
                tmp2 = Byn - step_size * loss * Vins
                tmp3 = np.linalg.norm(tmp2)
                if tmp3 > norm_up:
                    tmp2 = tmp2 * norm_up / tmp3
                V -= step_size * loss * (
                    B[:, [idx_pick * num_sub + idx_max_pick, idx_class * num_sub + idx_max_class]].dot(
                        np.vstack((xins, -xins))))

                norms = np.linalg.norm(V, axis=0)
                idx_down = np.nonzero(norms > norm_up)[0]
                B[:, idx_class * num_sub + idx_max_class] = tmp1
                B[:, idx_pick * num_sub + idx_max_pick] = tmp2
                if idx_down.size > 0:
                    norms = norms[norms > norm_up]
                    for k in range(len(idx_down)):
                        V[:, idx_down[k]] = V[:, idx_down[k]] * norm_up / norms[k]
            if i == 0 or (trounds > average_begin and i % average_size == 0):
                AB = AB + B
                AV = AV + V
                Anum = Anum + 1

        return B, V, AB, AV, Anum, trounds
def train_model ( self , data , targets , B , V , costs , norm_up , step_size0 , num_sub , AB , AV , Anum , trounds , lmbda , average_begin , average_size ) : targets = np . asarray ( targets ) # print(np.nonzero(targets == 2.0)) if len ( np . nonzero ( targets == 2.0 ) [ 0 ] ) == 0 : targets = np . hstack ( ( targets , 2 * np . ones ( ( targets . shape [ 0 ] , 1 ) ) ) ) data = np . asarray ( data ) B = np . asarray ( B ) V = np . asarray ( V ) n , n_class = np . shape ( targets ) row_ind , col_ind = np . nonzero ( targets >= 1 ) train_pairs = np . hstack ( ( row_ind . reshape ( ( - 1 , 1 ) ) , col_ind . reshape ( ( - 1 , 1 ) ) ) ) # tmpnums = np.sum(targets >= 1, axis=1) # train_pairs = np.zeros((sum(tmpnums), 1)) # tmpidx = 0 # for i in range(n): #     train_pairs[tmpidx: tmpidx + tmpnums[i]] = i+1 #     tmpidx = tmpidx + tmpnums[i] # targets = targets.T # # tp = np.nonzero(targets.flatten() >= 1) # # print(tp[0]) # # print(len(tp[0])) # train_pairs = np.hstack( #     (train_pairs, #      np.reshape([nz % n_class for nz in np.nonzero(targets.flatten(order='F') >= 1)[0]], newshape=(-1, 1)))) # # train_pairs[np.nonzero(train_pairs[:, 1] == 0)[0], 1] = n_class # targets = targets.T n = np . shape ( train_pairs ) [ 0 ] random_idx = randperm ( n - 1 ) # import scipy.io as scio # ld = scio.loadmat('F:\\alipy_doc\\alipy-additional-methods-source\\multi label\\AURO\\perm.mat') # random_idx = ld['random_idx'].flatten()-1 for i in range ( n ) : idx_ins = int ( train_pairs [ random_idx [ i ] , 0 ] ) xins = data [ int ( idx_ins ) , : ] . T idx_class = int ( train_pairs [ random_idx [ i ] , 1 ] ) if idx_class == n_class - 1 : idx_irr = np . nonzero ( targets [ idx_ins , : ] == - 1 ) [ 0 ] # elif idx_class == idxPs[idx_ins]: #     idx_irr = np.hstack((np.nonzero(targets[idx_ins, :] == -1)[0], int(idxNs[idx_ins]), n_class - 1)) else : idx_irr = np . hstack ( ( np . nonzero ( targets [ idx_ins , : ] == - 1 ) [ 0 ] , n_class - 1 ) ) n_irr = len ( idx_irr ) By = B [ : , idx_class * num_sub : ( idx_class + 1 ) * num_sub ] Vins = V . dot ( xins ) fy = np . max ( By . T . dot ( Vins ) , axis = 0 ) idx_max_class = np . argmax ( By . T . dot ( Vins ) , axis = 0 ) By = By [ : , idx_max_class ] fyn = np . NINF for j in range ( n_irr ) : idx_pick = idx_irr [ randperm ( n_irr - 1 , 1 ) [ 0 ] ] # print(idx_irr, idx_pick) Byn = B [ : , idx_pick * num_sub : ( idx_pick + 1 ) * num_sub ] # [fyn, idx_max_pick] = max(Byn.T.dot(Vins),[],1) # if Byn == []: #     print(0) tmp1 = Byn . T . dot ( Vins ) fyn = np . max ( tmp1 , axis = 0 ) idx_max_pick = np . argmax ( tmp1 , axis = 0 ) if fyn > fy - 1 : break if fyn > fy - 1 : step_size = step_size0 / ( 1 + lmbda * trounds * step_size0 ) trounds = trounds + 1 Byn = B [ : , idx_pick * num_sub + idx_max_pick ] loss = costs [ math . floor ( n_irr / ( j + 1 ) ) - 1 ] tmp1 = By + step_size * loss * Vins tmp3 = np . linalg . norm ( tmp1 ) if tmp3 > norm_up : tmp1 = tmp1 * norm_up / tmp3 tmp2 = Byn - step_size * loss * Vins tmp3 = np . linalg . norm ( tmp2 ) if tmp3 > norm_up : tmp2 = tmp2 * norm_up / tmp3 V -= step_size * loss * ( B [ : , [ idx_pick * num_sub + idx_max_pick , idx_class * num_sub + idx_max_class ] ] . dot ( np . vstack ( ( xins , - xins ) ) ) ) norms = np . linalg . norm ( V , axis = 0 ) idx_down = np . nonzero ( norms > norm_up ) [ 0 ] B [ : , idx_class * num_sub + idx_max_class ] = tmp1 B [ : , idx_pick * num_sub + idx_max_pick ] = tmp2 if idx_down . size > 0 : norms = norms [ norms > norm_up ] for k in range ( len ( idx_down ) ) : V [ : , idx_down [ k ] ] = V [ : , idx_down [ k ] ] * norm_up / norms [ k ] if i == 0 or ( trounds > average_begin and i % average_size == 0 ) : AB = AB + B AV = AV + V Anum = Anum + 1 return B , V , AB , AV , Anum , trounds