Example #1
0
    datas = list(restaurants_train())

    for g, att_func in attentions:
        if att_func == rbf_attention:
            r.vectors[r.items["<UNK>"]] += 10e5
        else:
            r.vectors[r.items["<UNK>"]] *= 0

        for n_noun in noun_cands:
            a = aspects[:n_noun]
            for idx, (inst, y, label_set) in enumerate(datas):

                s = get_scores(inst,
                               a,
                               r,
                               label_set,
                               gamma=g,
                               remove_oov=False,
                               attention_func=att_func)

                y_pred = s.argmax(1)
                f1_macro = precision_recall_fscore_support(
                    y, y_pred, average="weighted")[:-1]  # noqa
                row = (g, fun2name[att_func], n_noun, idx, *f1_macro)
                df.append(row)
            pbar.update(1)
    df = pd.DataFrame(df,
                      columns=("gamma", "function", "n_noun", "dataset", "p",
                               "r", "f1"))
    df.to_csv("results_grid_search.csv")
Example #2
0
    for k, v in d.items():
        if k.lower() in r.items:
            nouns[k.lower()] += v

    if att == rbf_attention:
        r.vectors[r.items["<UNK>"]] = r.vectors.max()

    if att == rbf_attention:
        candidates, _ = zip(*nouns.most_common(BEST_RBF["n_noun"]))
    else:
        candidates, _ = zip(*nouns.most_common(BEST_ATT["n_noun"]))

    aspects = [[x] for x in candidates]

    for idx, (instances, y, label_set) in enumerate(datums):

        s = get_scores(instances,
                       aspects,
                       r,
                       label_set,
                       gamma=GAMMA,
                       remove_oov=False,
                       attention_func=att)

        y_pred = s.argmax(1)
        f1_score = precision_recall_fscore_support(y, y_pred)
        f1_macro = precision_recall_fscore_support(y,
                                                   y_pred,
                                                   average="weighted")
        scores[idx] = (f1_score, f1_macro)
Example #3
0
if __name__ == "__main__":

    scores = defaultdict(dict)
    r = Reach.load("embeddings/restaurant_vecs_w2v.vec",
                   unk_word="<UNK>")

    datums = list(restaurants_test())

    for path in sorted(glob("embeddings/restaurant_vecs_w2v.vec")):
        r = Reach.load(path, unk_word="<UNK>")

        for idx, (instances, y, label_set) in enumerate(datums):

            s = get_scores(instances,
                           [["food"]],
                           r,
                           label_set,
                           remove_oov=False,
                           attention_func=mean)

            y_pred = s.argmax(1)
            f1_score = precision_recall_fscore_support(y, y_pred)
            f1_macro = precision_recall_fscore_support(y,
                                                       y_pred,
                                                       average="weighted")
            scores[path][idx] = (f1_score, f1_macro)

    score_per_class = [[z[x][0][:-1] for x in range(3)]
                       for z in scores.values()]
    score_per_class = np.stack(score_per_class).mean(0)

    macro_score = [v[2][1][:-1] for v in scores.values()]