示例#1
0
def load_feature_matrix(args):
    if args.feature_format % 3 == 0:
        X1 = HierarchicalMLModel.load_feature_matrix(args.input_inst_feat1)
        X2 = HierarchicalMLModel.load_feature_matrix(args.input_inst_feat2)
        X = smat.hstack([sk_normalize(X1, axis=1),
                         sk_normalize(X2, axis=1)]).tocsr()
    elif args.feature_format % 3 == 1 and args.input_inst_feat1:
        X = HierarchicalMLModel.load_feature_matrix(args.input_inst_feat1)
    elif args.feature_format % 3 == 2 and args.input_inst_feat2:
        X = HierarchicalMLModel.load_feature_matrix(args.input_inst_feat2)
    else:
        raise NotImplementedError(
            f"args.feature_format = {args.feature_format} is not supported.")
    if args.feature_format // 3 == 0:
        X = sk_normalize(X, axis=1, copy=False)
    return X
示例#2
0
文件: ranker.py 项目: xingz9/X-BERT
    def train(cls,
              X,
              Y,
              C,
              mode='full-model',
              shallow=False,
              solver_type=solver_dict['L2R_L2LOSS_SVC_DUAL'],
              Cp=1.0,
              Cn=1.0,
              threshold=0.1,
              max_iter=100,
              threads=-1,
              bias=-1.0,
              Z_pred=None):

        if mode in ['full-model', 'matcher']:
            if mode == 'full-model':
                prob = MLProblem(X, Y, C, bias=bias, Z_pred=Z_pred)
            elif mode == 'matcher':
                assert C is not None
                Y = Y.dot(C)
                prob = MLProblem(X, Y, bias=bias)

            hierarchical = True
            min_labels = 2
            if shallow:
                if prob.C is None:
                    min_labels = prob.Y.shape[1]
                else:
                    min_labels = prob.C.shape[1]
        elif mode == 'ranker':
            assert C is not None
            prob = MLProblem(X, Y, C, bias=bias)
            hierarchical = False
            min_labels = 2

        model = HierarchicalMLModel.train(prob,
                                          hierarchical=hierarchical,
                                          min_labels=min_labels,
                                          solver_type=solver_type,
                                          Cp=Cp,
                                          Cn=Cn,
                                          threshold=threshold,
                                          threads=threads,
                                          max_iter=max_iter)
        return cls(model)
示例#3
0
文件: ranker.py 项目: nanciwan/X-BERT
 def load(cls, model_folder):
     return cls(HierarchicalMLModel.load(model_folder))
示例#4
0
    def train(
        cls,
        X,
        Y,
        C,
        mode="full-model",
        shallow=False,
        solver_type=solver_dict["L2R_L2LOSS_SVC_DUAL"],
        Cp=1.0,
        Cn=1.0,
        threshold=0.1,
        max_iter=100,
        threads=-1,
        bias=-1.0,
        Z_pred=None,
        negative_sampling_scheme=None,
    ):
        if mode in ["full-model", "matcher"]:
            if mode == "full-model":
                prob = MLProblem(
                    X,
                    Y,
                    C,
                    Z_pred=Z_pred,
                    negative_sampling_scheme=negative_sampling_scheme,
                )
            elif mode == "matcher":
                assert C is not None
                Y = Y.dot(C)
                prob = MLProblem(X, Y, C=None)

            hierarchical = True
            min_labels = 2
            if shallow:
                if prob.C is None:
                    min_labels = prob.Y.shape[1]
                else:
                    min_labels = prob.C.shape[1]
        elif mode == "ranker":
            assert C is not None
            prob = MLProblem(
                X,
                Y,
                C,
                Z_pred=Z_pred,
                negative_sampling_scheme=negative_sampling_scheme,
            )
            hierarchical = False
            min_labels = 2

        model = HierarchicalMLModel.train(
            prob,
            hierarchical=hierarchical,
            min_labels=min_labels,
            solver_type=solver_type,
            Cp=Cp,
            Cn=Cn,
            threshold=threshold,
            threads=threads,
            bias=bias,
            max_iter=max_iter,
        )
        return cls(model)