def get_factor_expressions(fws, bws, tfemb, tfdict, valid_fes, sentence, spaths_x=None, cpaths_x=None): pw_z = parameter(w_z) pb_z = parameter(b_z) pw_f = parameter(w_f) pb_f = parameter(b_f) factexprs = {} sentlen = len(fws) sortedtfd = sorted(tfdict.keys()) targetspan = (sortedtfd[0], sortedtfd[-1]) for j in xrange(sentlen): istart = 0 if USE_SPAN_CLIP and j > ALLOWED_SPANLEN: istart = max(0, j - ALLOWED_SPANLEN) for i in xrange(istart, j + 1): spanlen = scalarInput(j - i + 1) logspanlen = scalarInput(math.log(j - i + 1)) spanwidth = sp_x[SpanWidth.howlongisspan(i, j)] spanpos = ap_x[ArgPosition.whereisarg((i, j), targetspan)] fbemb_ij_basic = concatenate([ fws[i][j], bws[i][j], tfemb, spanlen, logspanlen, spanwidth, spanpos ]) if USE_DEPS: outs = oh_s[OutHeads.getnumouts(i, j, sentence.outheads)] shp = spaths_x[sentence.shortest_paths[(i, j, targetspan[0])]] fbemb_ij = concatenate([fbemb_ij_basic, outs, shp]) elif USE_CONSTITS: isconstit = scalarInput((i, j) in sentence.constitspans) lca = ct_x[sentence.lca[(i, j)][1]] phrp = cpaths_x[sentence.cpaths[(i, j, targetspan[0])]] fbemb_ij = concatenate([fbemb_ij_basic, isconstit, lca, phrp]) else: fbemb_ij = fbemb_ij_basic for y in valid_fes: fctr = Factor(i, j, y) if USE_HIER and y in feparents: fefixed = esum([fe_x[y]] + [fe_x[par] for par in feparents[y]]) else: fefixed = fe_x[y] fbemb_ijy = concatenate([fefixed, fbemb_ij]) factexprs[fctr] = pw_f * rectify(pw_z * fbemb_ijy + pb_z) + pb_f return factexprs
ALL_FEATS_DIM = 2 * LSTMDIM \ + LUDIM \ + LUPOSDIM \ + FRMDIM \ + LSTMINPDIM \ + LSTMDIM \ + FEDIM \ + ARGPOSDIM \ + SPANDIM \ + 2 # spanlen and log spanlen features and is a constitspan if USE_DEPS: DEPHEADDIM = LSTMINPDIM + POSDIM DEPRELDIM = configuration["dependency_relation_dim"] OUTHEADDIM = OutHeads.size() PATHLSTMINPDIM = DEPHEADDIM + DEPRELDIM ALL_FEATS_DIM += OUTHEADDIM + PATHDIM if USE_CONSTITS: ALL_FEATS_DIM += 1 + PHRASEDIM # is a constit and what is it ALL_FEATS_DIM += PATHDIM NUMEPOCHS = configuration["num_epochs"] PATIENCE = configuration["patience"] LOSS_EVAL_EPOCH = configuration["eval_after_every_epochs"] DEV_EVAL_EPOCHS = configuration["dev_eval_epoch_frequency"] * LOSS_EVAL_EPOCH trainexamples = filter_long_ex(trainexamples, USE_SPAN_CLIP, ALLOWED_SPANLEN, NOTANFEID)