jiant/models.py
def build_pair_sentence_module(task, d_inp, model, params):
    """ Build a pair classifier, shared if necessary """

    def build_pair_attn(d_in, d_hid_attn):
        """ Build the pair model """
        d_inp_model = 2 * d_in
        modeling_layer = s2s_e.by_name("lstm").from_params(
            Params(
                {
                    "input_size": d_inp_model,
                    "hidden_size": d_hid_attn,
                    "num_layers": 1,
                    "bidirectional": True,
                }
            )
        )
        pair_attn = AttnPairEncoder(model.vocab, modeling_layer, dropout=params["dropout"])
        return pair_attn

    # Build the "pooler", which does pools a variable length sequence
    #   possibly with a projection layer beforehand
    if params["attn"] and not model.use_bert:
        pooler = Pooler(project=False, d_inp=params["d_hid_attn"], d_proj=params["d_hid_attn"])
        d_out = params["d_hid_attn"] * 2
    else:
        pooler = Pooler(
            project=not model.use_bert,
            d_inp=d_inp,
            d_proj=params["d_proj"],
            pool_type=params["pool_type"],
        )
        d_out = d_inp if model.use_bert else params["d_proj"]

    # Build an attention module if necessary
    if params["shared_pair_attn"] and params["attn"] and not model.use_bert:  # shared attn
        if not hasattr(model, "pair_attn"):
            pair_attn = build_pair_attn(d_inp, params["d_hid_attn"])
            model.pair_attn = pair_attn
        else:
            pair_attn = model.pair_attn
    elif params["attn"] and not model.use_bert:  # non-shared attn
        pair_attn = build_pair_attn(d_inp, params["d_hid_attn"])
    else:  # no attn
        pair_attn = None

    # Build the classifier
    n_classes = task.n_classes if hasattr(task, "n_classes") else 1
    if model.use_bert:
        # BERT handles pair tasks by concatenating the inputs and classifying the joined
        # sequence, so we use a single sentence classifier
        if isinstance(task, WiCTask):
            d_out *= 3  # also pass the two contextual word representations
        log.info(f'Task {task.name} has a classifier with d_out {d_out}')
        classifier = Classifier.from_params(d_out, n_classes, params)
        module = SingleClassifier(pooler, classifier)
    else:
        d_out = d_out + d_inp if isinstance(task, WiCTask) else d_out
        classifier = Classifier.from_params(4 * d_out, n_classes, params)
        module = PairClassifier(pooler, classifier, pair_attn)
    return module
 Build a pair classifier, shared if necessary 
Build a pair classifier, shared if necessary
Build a pair classifier shared if necessary
def build_pair_sentence_module(task, d_inp, model, params):
    def build_pair_attn(d_in, d_hid_attn):
        d_inp_model = 2 * d_in
        modeling_layer = s2s_e.by_name("lstm").from_params(
            Params(
                {
                    "input_size": d_inp_model,
                    "hidden_size": d_hid_attn,
                    "num_layers": 1,
                    "bidirectional": True,
                }
            )
        )
        pair_attn = AttnPairEncoder(model.vocab, modeling_layer, dropout=params["dropout"])
        return pair_attn
    if params["attn"] and not model.use_bert:
        pooler = Pooler(project=False, d_inp=params["d_hid_attn"], d_proj=params["d_hid_attn"])
        d_out = params["d_hid_attn"] * 2
    else:
        pooler = Pooler(
            project=not model.use_bert,
            d_inp=d_inp,
            d_proj=params["d_proj"],
            pool_type=params["pool_type"],
        )
        d_out = d_inp if model.use_bert else params["d_proj"]
    if params["shared_pair_attn"] and params["attn"] and not model.use_bert:  
        if not hasattr(model, "pair_attn"):
            pair_attn = build_pair_attn(d_inp, params["d_hid_attn"])
            model.pair_attn = pair_attn
        else:
            pair_attn = model.pair_attn
    elif params["attn"] and not model.use_bert:  
        pair_attn = build_pair_attn(d_inp, params["d_hid_attn"])
    else:  
        pair_attn = None
    n_classes = task.n_classes if hasattr(task, "n_classes") else 1
    if model.use_bert:
        if isinstance(task, WiCTask):
            d_out *= 3  
        log.info(f'Task {task.name} has a classifier with d_out {d_out}')
        classifier = Classifier.from_params(d_out, n_classes, params)
        module = SingleClassifier(pooler, classifier)
    else:
        d_out = d_out + d_inp if isinstance(task, WiCTask) else d_out
        classifier = Classifier.from_params(4 * d_out, n_classes, params)
        module = PairClassifier(pooler, classifier, pair_attn)
    return module
def build_pair_sentence_module ( task , d_inp , model , params ) : def build_pair_attn ( d_in , d_hid_attn ) : """ Build the pair model """ d_inp_model = 2 * d_in modeling_layer = s2s_e . by_name ( "lstm" ) . from_params ( Params ( { "input_size" : d_inp_model , "hidden_size" : d_hid_attn , "num_layers" : 1 , "bidirectional" : True , } ) ) pair_attn = AttnPairEncoder ( model . vocab , modeling_layer , dropout = params [ "dropout" ] ) return pair_attn if params [ "attn" ] and not model . use_bert : pooler = Pooler ( project = False , d_inp = params [ "d_hid_attn" ] , d_proj = params [ "d_hid_attn" ] ) d_out = params [ "d_hid_attn" ] * 2 else : pooler = Pooler ( project = not model . use_bert , d_inp = d_inp , d_proj = params [ "d_proj" ] , pool_type = params [ "pool_type" ] , ) d_out = d_inp if model . use_bert else params [ "d_proj" ] if params [ "shared_pair_attn" ] and params [ "attn" ] and not model . use_bert : if not hasattr ( model , "pair_attn" ) : pair_attn = build_pair_attn ( d_inp , params [ "d_hid_attn" ] ) model . pair_attn = pair_attn else : pair_attn = model . pair_attn elif params [ "attn" ] and not model . use_bert : pair_attn = build_pair_attn ( d_inp , params [ "d_hid_attn" ] ) else : pair_attn = None n_classes = task . n_classes if hasattr ( task , "n_classes" ) else 1 if model . use_bert : if isinstance ( task , WiCTask ) : d_out *= 3 log . info ( f'Task {task.name} has a classifier with d_out {d_out}' ) classifier = Classifier . from_params ( d_out , n_classes , params ) module = SingleClassifier ( pooler , classifier ) else : d_out = d_out + d_inp if isinstance ( task , WiCTask ) else d_out classifier = Classifier . from_params ( 4 * d_out , n_classes , params ) module = PairClassifier ( pooler , classifier , pair_attn ) return module
Build a pair classifier, shared if necessary
Build a pair classifier shared if necessary
""" Build a pair classifier, shared if necessary """ """ Build the pair model """ # Build the "pooler", which does pools a variable length sequence #   possibly with a projection layer beforehand # Build an attention module if necessary # shared attn # non-shared attn # no attn # Build the classifier # BERT handles pair tasks by concatenating the inputs and classifying the joined # sequence, so we use a single sentence classifier # also pass the two contextual word representations
task
d_inp
model
params