Пример #1
0
 def __init__(self):
     super().__init__()
     # --
     # the adapter idecs
     self.idec_ff = ConfEntryChoices({"yes": IdecConf.make_conf('ff'), "no": None}, "no")
     self.idec_satt = ConfEntryChoices({"yes": IdecConf.make_conf('satt'), "no": None}, "no")
     # --
     # how to deal with msent in training and testing (for some decoders)
     self.msent_loss_center = False
     self.msent_pred_center = False
Пример #2
0
 def __init__(self):
     # vocab dir
     self.vocab_save_dir = "./"  # place to store all vocabs
     self.vocab_load_dir = ""  # place to load pre-built vocabs
     self.vocab_force_rebuild = False  # force rebuild all vocabs
     # first is the encoder
     self.enc = ConfEntryChoices({
         "bert": ZTaskEncBertConf(),
         "plain": None
     }, "bert")
     # then specific tasks -- to be added!!
     self.mlm = ConfEntryChoices({"yes": ZTaskMlmConf(), "no": None}, "no")
     self.upos = ConfEntryChoices({
         "yes": ZTaskUposConf(),
         "no": None
     }, "no")
     self.udep = ConfEntryChoices({
         "yes": ZTaskUdepConf(),
         "no": None
     }, "no")
     self.udep2 = ConfEntryChoices(
         {
             "yes": ZTaskUdepConf().direct_update(name='udep2'),
             "no": None
         }, "no")
     self.pb1 = ConfEntryChoices(
         {
             "yes": ZTaskSrlConf.make_conf('pb1'),
             "no": None
         }, "no")
     self.pb2 = ConfEntryChoices(
         {
             "yes": ZTaskSrlConf.make_conf('pb2'),
             "no": None
         }, "no")
     self.pbS = ConfEntryChoices(
         {
             "yes": ZTaskSrl2Conf.make_conf('pbS'),
             "no": None
         }, "no")
     self.ee = ConfEntryChoices(
         {
             "yes": ZTaskSrlConf.make_conf('ee'),
             "no": None
         }, "no")
     self.fn = ConfEntryChoices(
         {
             "yes": ZTaskSrlConf.make_conf('fn'),
             "no": None
         }, "no")
Пример #3
0
 def __init__(self):
     # basic
     self.conf_output = "_conf"
     self.rgpu = -1
     self.cur_run = "1"
     self.debug = False
     self.do_train = True  # training
     self.do_test = False  # testing
     self.do_test_all = False  # testing all_df_files
     self.train_extras = ""
     self.test_extras = ""
     # paths
     self.run_dir = ""  # by default current one!
     self.src_dir = "src"
     self.voc_dir = "voc"
     self.dataset: DataConf = ConfEntryChoices(PRESET_DATA, "pb05")
     self.log_prefix = "_log"
     self.out_prefix = "_zout"
     # --
     # specific
     self.use_word_input = True  # input with word embeddings?
     self.use_bert_input = False  # input featured bert?
     self.use_rel_posi = True  # relative position
     self.assume_frame = False  # assume both trg and frame type
     self.assume_trg = False  # assume trg
     self.no_frame_label = False  # no frame label
     self.arg_mode = "seq"  # seq/span/head
     self.arg_seq_mod = "crf"  # mle/crf
     self.do_ms_train = False  # multi-source training?
Пример #4
0
 def make_conf_entry(ftag: str):  # make default entries
     from msp2.utils import ConfEntryChoices
     direct_conf = DirectExtractorConf()
     seq_conf = SeqExtractorConf()
     soft_conf = SoftExtractorConf()
     anchor_conf = AnchorExtractorConf()
     all_confs = [direct_conf, seq_conf, soft_conf, anchor_conf]
     for cc in all_confs:
         cc.ftag = ftag
     if ftag == "evt":  # simply use shead-mode for evt!!
         for cc in all_confs:
             cc.core_span_mode = "shead"
             cc.lab_conf.labeler_conf.e_dim = 512
             # cc.lab_conf.labeler_conf.sconf.pls_conf.hid_dim = 300
             # cc.lab_conf.labeler_conf.sconf.pls_conf.hid_nlayer = 1  # give one layer of hidden
             cc.lab_conf.labeler_conf.e_tie_weights = True  # tie weights for pred&lookup!
         direct_conf.span_conf.max_width = 1
     elif ftag == "ef" or ftag == "arg":
         pass
     else:
         raise NotImplementedError()
     # special for pair-scorer
     if ftag == "arg":
         for cc in all_confs:  # avoid large biaffine-tensor
             cc.lab_conf.labeler_conf.sconf.pas_conf.direct_update(
                 use_biaffine=False, use_ff2=True)
     # --
     return ConfEntryChoices(
         {
             "direct": direct_conf,
             "seq": seq_conf,
             "soft": soft_conf,
             "anchor": anchor_conf
         }, 'direct')
Пример #5
0
 def __init__(self):
     from .model import ZsfpModelConf
     from .model2 import MtlModelConf
     self.dconf = DConf()  # data conf
     self.tconf = TConf()  # train/test conf
     self.mconf = ConfEntryChoices(
         {
             "basic": ZsfpModelConf(),
             "mtl": MtlModelConf()
         }, 'basic')
Пример #6
0
 def __init__(self):
     # reading and writing formats
     # self.R = ReaderGetterConf()
     from .run import MyDataReaderConf
     self.R = MyDataReaderConf()  # reader + optional extra functionalities
     self.W = WriterGetterConf()
     # --
     # data paths: train/dev can be multiple!
     self.train: List[str] = []
     self.train_props: List[Dict] = []  # extra properties for R
     self.dev: List[str] = []
     self.test = ""
     self.test_do_score = False  # do score instead of predict!!
     self.cache_data = True  # turn off if large data
     self.cache_shuffle = True  # shuffle cache if using cache
     self.dict_dir = "./"
     self.dict_frame_file = ""  # read frame file for building types
     # -----
     # for multi-source training (len(train)>1); note: currently three should be enough
     self.ms_stop_idx = 0  # with which to consider the end of an epoch
     self.ms_train_budget0 = SVConf().direct_update(val=1., which_idx="eidx", mode="none", min_val=0.)
     self.ms_train_budget1 = SVConf().direct_update(val=1., which_idx="eidx", mode="none", min_val=0.)
     self.ms_train_budget2 = SVConf().direct_update(val=1., which_idx="eidx", mode="none", min_val=0.)
     # -----
     # save name for trainer not here!!
     self.model_load_name = "zmodel.best.m"  # load name
     self.model_load_strict = True  # strict loading?
     self.output = "zout.json"
     self.eval_conf = ConfEntryChoices(
         {'frame': FrameEvalConf(), 'fn': MyFNEvalConf(), 'pb': MyPBEvalConf(), 'dpar': DparEvalConf(), 'none': None}, 'frame')
     self.eval2_conf = ConfEntryChoices(  # secondary eval
         {'frame': FrameEvalConf(), 'fn': MyFNEvalConf(), 'pb': MyPBEvalConf(), 'dpar': DparEvalConf(), 'none': None}, 'none')
     # pretrain
     self.pretrain_wv_file = ""
     self.pretrain_scale = 1.0
     # (not used anymore) self.pretrain_init_nohit = 1.0
     self.pretrain_hits_outf = ""  # output a by-product of filtered embeddings
     # extra for testing
     self.test_extra_pretrain_wv_files = []
     # thresholds for word
     self.word_rthres = 200000  # rank <= this
     self.word_fthres = 1  # freq >= this
Пример #7
0
 def __init__(self):
     super().__init__()
     # --
     # basic
     self.bert_model = "bert-base-multilingual-cased"  # or "bert-base-multilingual-cased", "bert-base-cased", "bert-large-cased", "bert-base-chinese", "roberta-base", "roberta-large", "xlm-roberta-base", "xlm-roberta-large", ...
     self.bert_cache_dir = ""  # dir for downloading
     self.bert_no_pretrain = False
     self.bert_ft = True  # whether fine-tune the model
     # --
     # extra layers
     self.gcn = ConfEntryChoices({'yes': ZGcn0Conf(), 'no': None}, 'no')
Пример #8
0
 def __init__(self):
     # node names
     self.g_names = []
     # optimizer
     self.g_optim = ConfEntryChoices({"yes": OptimConf(), "no": None}, "no")
     # lrate factor (by default 1.0)
     self.g_lrf = SVConf().direct_update(val=1.,
                                         which_idx="eidx",
                                         mode="none",
                                         min_val=0.)
     # margin (by default 0.0)
     self.g_margin = SVConf().direct_update(val=0.)
Пример #9
0
 def __init__(self):
     super().__init__()
     # components
     # -- input + enc
     self.enc_conf = ConfEntryChoices({"plain": ZEncoderPlainConf(), "bert": ZEncoderBertConf()}, "bert")
     # -- dec
     self.srl_conf = ConfEntryChoices({"srl": ZDecoderSRLConf(), "srl2": ZDecoderSRL2Conf(), "none": None}, "srl2")  # srl
     self.pos_conf = ConfEntryChoices({"upos": ZDecoderUPOSConf(), "none": None}, "none")  # upos
     self.dep_conf = ConfEntryChoices({"udep": ZDecoderUDEPConf(), "none": None}, "none")  # udep
     # --
     self.frame_based = False  # whether inst should be frame or sent?
     # decode
     self.decode_sent_thresh_diff = 20  # sent diff thresh in decoding
     self.decode_sent_thresh_batch = 8  # num sent one time
     # =====
     # aug model forward!
     self.aug_model_confs = []  # "." means self!!
     self.aug_model_paths = []  # should match confs
     self.aug_times = 0  # maybe repeat for example, in MC-dropout
     self.aug_training_flag = False  # when aug forward, training or not?
     # =====
     # score model forward!
     self.score_times = 1  # repeat for scoring, for estimating model confidence.
     self.score_training_flag = False  # when score forward, training or not?
     # =====
     # special backward
     self.pcgrad = ConfEntryChoices({"yes": PCGradHelperConf(), "none": None}, "none")
     self.opt_sparse = ConfEntryChoices({"yes": OptimSparseHelperConf(), "none": None}, "none")
Пример #10
0
 def __init__(self):
     super().__init__()
     # --
     # general specifications
     self.evt_ftag = "evt"
     self.evt_span_mode = "shead"
     self.arg_ftag = "ef"  # special target for args
     self.arg_span_mode = "span"
     self.arg_only_rank1 = True  # only consider rank1 arg
     # --
     # for predicate
     self.binary_evt = True  # no prediction of detailed lemma+sense?
     self.evt_conf = IdecSingleConf()  # predicate
     self.loss_evt = 1.0
     self.evt_label_smoothing = 0.
     self.evt_loss_weight_non = 1.0
     self.evt_loss_sample_neg = 1.0  # how much of neg to include
     # --
     # for argument
     self.arg_conf: IdecConf = \
         ConfEntryChoices({"pair_emb": IdecPairwiseConf(), "pair_att": IdecAttConf()}, "pair_emb")  # argument
     self.loss_arg = 1.0
     self.arg_label_smoothing = 0.
     self.arg_loss_sample_neg = 1.0  # how much of neg to include
     self.arg_loss_inc_neg_evt = 0.  # need to include bad evts? (no args!): a small one is ok
     # special for arguments (BIO tagging)
     self.arg_use_bio = True  # use BIO mode!
     self.arg_pred_use_seq_cons = False  # use bio constraints in prediction
     self.arg_beam_k = 20
     # --
     # for arg2: aug arg loss
     self.arg2_conf: IdecConf = \
         ConfEntryChoices({"pair_emb": IdecPairwiseConf(), "pair_att": IdecAttConf()}, "pair_emb")  # argument
     self.loss_arg2 = 0.0
     self.arg2_label_smoothing = 0.
     self.arg2_loss_sample_neg = 1.0  # how much of neg to include
     # --
     # special decoding!
     self.evt_pred_use_posi = False  # special mode: assume gold posi
     self.evt_pred_use_all = False  # special debug mode: assume all as targets!
Пример #11
0
 def __init__(self):
     super().__init__()
     self._isize = -1
     # whether do dec's selection?
     self.do_dsel = True
     self.dsel_conf = DSelectorConf()
     self.dsel_nonhit_zero = True  # for the nonhit subwords, feed 0, otherwise feed start
     # special seq mode
     self.do_seq_mode = "none"  # none/sel/pool
     self.seq_sel_key = "None"  # this idx is over original seq (not subword seq)
     self.seq_pool = "idx0"  # pooling over all items: idx0/avg/max
     # --
     # extra gate for feeding
     _gate_conf = MLPConf().direct_conf(osize=1, dim_hid=256, n_hid_layer=1)
     _gate_conf.out_conf.no_drop = True  # no drop here
     self.gate = ConfEntryChoices({"yes": _gate_conf, "no": None}, "no")
Пример #12
0
 def __init__(self):
     super().__init__()
     # --
     # for depth (distance to root)
     self.depth_conf = IdecSingleConf()  # one score for depth!
     self.depth_label_smoothing = 0.
     self.loss_depth = 1.0
     self.depth_nonroot_space = 0.  # how much space left for nonroot ones? 0 means binary!
     # for dep
     self.udep_conf: IdecConf = \
         ConfEntryChoices({"pair_emb": IdecPairwiseConf(), "pair_att": IdecAttConf()}, "pair_emb")  # argument
     self.loss_udep = 1.0
     self.udep_label_smoothing = 0.
     self.udep_loss_sample_neg = 0.05  # how much of neg to include: for each token, (n-1)/n is neg examples
     # special
     self.udep_pred_root_penalty = -0.  # penalty for root edges
     self.udep_train_use_cache = True
Пример #13
0
 def __init__(self):
     super().__init__()
     # --
     # udep pairwise label
     self.idec_udep_lab = ConfEntryChoices({'idec1': IdecConf.make_conf('pairwise'), 'idec2': Idec2Conf()}, 'idec1')
     self.loss_udep_lab = 1.
     self.lab_udep = ZLabelConf().direct_update(fixed_nil_val=0., loss_neg_sample=-2.)  # 2 neg samples
     # special for labels: note: only utilized in training!!
     self.udep_ignore_labels = []  # ignore these (by masking out)
     self.udep_nilout_labels = []  # make them nil
     # root score (simply binary)
     self.idec_udep_root = IdecConf.make_conf('score')
     self.loss_udep_root = 0.  # make it smaller weight
     self.lab_root = ZLabelConf().direct_update(fixed_nil_val=0.)
     # --
     # decoding
     self.dec_no_root_score = True  # no root score when decoding
     self.udep_no_decode = False  # simply skip decoding (used in special cases!)
Пример #14
0
 def __init__(self):
     super().__init__()
     # --
     # basic dims
     self.ndim = -1
     self.nlab = -1
     # --
     # apply layers
     self.app_layers = []  # which layers to apply this idec
     self.feed_layers = [
     ]  # which layers to further allow feed (first should have 'app')
     # --
     # loss & pred
     self.hconf: IdecHelperConf = ConfEntryChoices(
         {
             "simple": IdecHelperSimpleConf(),
             "cw": IdecHelperCWConf(),
             "cw2": IdecHelperCW2Conf()
         }, "simple")
Пример #15
0
 def __init__(self):
     super().__init__()
     # files
     self.take_ratio = 1.0
     self.main = ReaderGetterConf()  # as gold files
     self.gold = ""
     self.align_gold_sent = False
     self.fake_gold_frames = False  # put syntax frames to gold
     # eval with preds
     self.extra = ReaderGetterConf()
     self.preds = []  # list of preds
     self.econf: FrameEvalConf = ConfEntryChoices(
         {
             'frame': FrameEvalConf(),
             'fn': MyFNEvalConf(),
             'pb': MyPBEvalConf()
         }, 'frame')
     # self.econf = FrameEvalConf()
     # others
     self.gold_set_ee_heads = False  # try to auto assign heads
     self.pred_set_ee_heads = False  # try to auto assign heads
Пример #16
0
 def __init__(self):
     super().__init__()
     # --
     self._csize = -1  # number of classes (final output labels)
     # todo(+N): not good design, but use this for the indirection ...
     self.emb_size = -1  # if >0, we further stack an affine layer for final csize, otherwise directly take inputs!
     self.input_act = 'linear'  # activation for the input
     # fixed_nil
     self.fixed_nil_val: float = None  # fix NIL as what?
     # loss
     self.loss_do_sel = False  # sel then loss maybe mainly to save some space
     self.loss_neg_sample = 1.  # how much of neg(NIL) to include in loss, >0 means percentage, <0 means ratio
     self.loss_full_alpha = 1.  # ordinarily against ALL
     # extra binary mode (must set fixed_nil_val=True)
     self.use_nil_as_binary = False  # use NIL score as binary score
     self.loss_binary_alpha = 0.  # gold against NIL
     # extra all-binary mode (must set nil=0)
     self.loss_allbinary_alpha = 0.
     # special mode: CRF!
     self.crf = ConfEntryChoices({
         'yes': ZLinearCrfConf(),
         'no': None
     }, 'no')
     self.pred_crf_add = 100.  # todo(+N): simply increase unary scores after decoding
Пример #17
0
 def __init__(self):
     super().__init__()
     # --
     self._isize = -1  # input size (emb)
     self._nhead = -1  # input size (num_head)
     self._csize = -1  # number of classes
     # core scorer
     self.cs = CoreScorerConf()
     self.share_cs = True
     # apply layers
     self.app_layers = []  # which layers to apply this idec
     self.app_ts = [
     ]  # temp-scales, mainly for prediction!! (by default 1., no ts)
     self.app_feeds = []  # whether allow feed (by default 0)
     self.app_detach_scales = [
     ]  # gradient scaling at detaching, 0 means full detach! (by default 1.)
     self.app_input_fixed_mask = [
     ]  # apply fixed mask at input, 0 means no mask (by default 0.)
     # loss & pred
     self.hconf: IdecHelperConf = ConfEntryChoices(
         {
             "simple": IdecHelperSimpleConf(),
             "simple2": IdecHelperSimple2Conf()
         }, "simple2")
Пример #18
0
 def __init__(self):
     super().__init__()
     # --
     self.conn = ConnectorConf()
     self.core = ConfEntryChoices({"ff": CoreFfConf(), "satt": CoreSattConf(),
                                   "score": CoreScorerConf(), "pairwise": CorePairwiseConf()}, "ff")
Пример #19
0
 def __init__(self):
     from .model import ZmtlModelConf
     self.dconf = DConf()  # data conf
     self.tconf = TConf()  # train/test conf
     self.mconf = ConfEntryChoices({"mtl": ZmtlModelConf()}, 'mtl')