def rel_argmax(rel_probs, length, ensure_tree=True):
if ensure_tree:<EOL><INDENT>rel_probs[:, ParserVocabulary.PAD] = <NUM_LIT:0><EOL>root = ParserVocabulary.ROOT<EOL>tokens = np.arange(<NUM_LIT:1>, length)<EOL>rel_preds = np.argmax(rel_probs, axis=<NUM_LIT:1>)<EOL>roots = np.where(rel_preds[tokens] == root)[<NUM_LIT:0>] + <NUM_LIT:1><EOL>if len(roots) < <NUM_LIT:1>:<EOL><INDENT>rel_preds[<NUM_LIT:1> + np.argmax(rel_probs[tokens, root])] = root<EOL><DEDENT>elif len(roots) > <NUM_LIT:1>:<EOL><INDENT>root_probs = rel_probs[roots, root]<EOL>rel_probs[roots, root] = <NUM_LIT:0><EOL>new_rel_preds = np.argmax(rel_probs[roots], axis=<NUM_LIT:1>)<EOL>new_rel_probs = rel_probs[roots, new_rel_preds] / root_probs<EOL>new_root = roots[np.argmin(new_rel_probs)]<EOL>rel_preds[roots] = new_rel_preds<EOL>rel_preds[new_root] = root<EOL><DEDENT>return rel_preds<EOL><DEDENT>else:<EOL><INDENT>rel_probs[:, ParserVocabulary.PAD] = <NUM_LIT:0><EOL>rel_preds = np.argmax(rel_probs, axis=<NUM_LIT:1>)<EOL>return rel_preds<EOL><DEDENT>
Fix the relation prediction by heuristic rules

    Parameters
    ----------
    rel_probs : NDArray
        seq_len x rel_size
    length :
        real sentence length
    ensure_tree :
        whether to apply rules
    Returns
    -------
    rel_preds : np.ndarray
        prediction of relations of size (seq_len,)