Beispiel #1
0
 def __init__(self, conf: ZDecoderUDEPConf, name: str, vocab_udep: SimpleVocab, ref_enc: ZEncoder, **kwargs):
     super().__init__(conf, name, **kwargs)
     conf: ZDecoderUDEPConf = self.conf
     self.vocab_udep = vocab_udep
     _enc_dim, _head_dim = ref_enc.get_enc_dim(), ref_enc.get_head_dim()
     # --
     self.helper = ZDecoderUDEPHelper(conf, self.vocab_udep)
     self._label_idx_root = vocab_udep.get("root")  # get root's index for decoding
     # --
     # nodes
     self.depth_node: IdecNode = conf.depth_conf.make_node(_isize=_enc_dim, _nhead=_head_dim, _csize=1)
     self.udep_node: IdecNode = conf.udep_conf.make_node(_isize=_enc_dim, _nhead=_head_dim, _csize=len(vocab_udep))
Beispiel #2
0
 def __init__(self, cons: Constrainer, src_vocab: SimpleVocab, trg_vocab: SimpleVocab, conf: ConstrainerNodeConf, **kwargs):
     super().__init__(conf, **kwargs)
     conf: ConstrainerNodeConf = self.conf
     # --
     # input vocab
     if src_vocab is None:  # make our own src_vocab
         cons_keys = sorted(cons.cmap.keys())  # simply get all the keys
         src_vocab = SimpleVocab.build_by_static(cons_keys, pre_list=["non"], post_list=None)  # non==0!
     # output vocab
     assert trg_vocab is not None
     out_size = len(trg_vocab)  # output size is len(trg_vocab)
     trg_is_seq_vocab = isinstance(trg_vocab, SeqVocab)
     _trg_get_f = (lambda x: trg_vocab.get_range_by_basename(x)) if trg_is_seq_vocab else (lambda x: trg_vocab.get(x))
     # set it up
     _vec = np.full((len(src_vocab), out_size), 0., dtype=np.float32)
     assert src_vocab.non == 0
     _vec[0] = 1.  # by default: src-non is all valid!
     _vec[:,0] = 1.  # by default: trg-non is all valid!
     # --
     stat = {"k_skip": 0, "k_hit": 0, "v_skip": 0, "v_hit": 1}
     for k, v in cons.cmap.items():
         idx_k = src_vocab.get(k)
         if idx_k is None:
             stat["k_skip"] += 1
             continue  # skip no_hit!
         stat["k_hit"] += 1
         for k2 in v.keys():
             idx_k2 = _trg_get_f(k2)
             if idx_k2 is None:
                 stat["v_skip"] += 1
                 continue
             stat["v_hit"] += 1
             if trg_is_seq_vocab:
                 _vec[idx_k, idx_k2[0]:idx_k2[1]] = 1.  # hit range
             else:
                 _vec[idx_k, idx_k2] = 1.  # hit!!
     zlog(f"Setup ConstrainerNode ok: vec={_vec.shape}, stat={stat}")
     # --
     self.cons = cons
     self.src_vocab = src_vocab
     self.trg_vocab = trg_vocab
     self.vec = BK.input_real(_vec)