-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d981f9
commit 0522639
Showing
3 changed files
with
213 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import math | ||
from tqdm import tqdm | ||
|
||
import numpy as np | ||
import torch | ||
|
||
from fairseq.data import Dictionary | ||
|
||
dictionary = Dictionary.load('data-bin/wikitext103-bpe/dict.txt') | ||
print(len(dictionary)) | ||
|
||
bpe_cont = "@@" | ||
bpe_toks = { | ||
i | ||
for i in range(len(dictionary)) | ||
if dictionary[i].endswith(bpe_cont) | ||
} | ||
|
||
bpe_len = len(bpe_cont) | ||
|
||
tokens = np.load('overfit_analysis/tokens.npy') | ||
lm_scores = np.load('overfit_analysis/lm_scores.npy') | ||
|
||
assert len(tokens) == len(lm_scores) | ||
|
||
lm_scores = torch.from_numpy(lm_scores).cuda() | ||
tgt_len = tokens.size | ||
skipped_toks = 0 | ||
for i in range(tgt_len - 1): | ||
if tokens[i].item() in bpe_toks: | ||
skipped_toks += 1 | ||
|
||
count = len(tokens) - skipped_toks | ||
|
||
knn_helping = 0 | ||
with open('ov_interpolation.txt', 'w') as outfile: | ||
for f in ['overfit_analysis/knn_scores.npy', | ||
'overfit_analysis/knn_recomp_scores.npy', | ||
'overfit_analysis/knn_ip_scores.npy', | ||
'overfit_analysis/knn_ip_recomp_scores.npy', | ||
'overfit_analysis/overfit129_lm_scores.npy', | ||
]: | ||
extra_scores = np.load(f) | ||
extra_scores = torch.from_numpy(extra_scores).cuda() | ||
combine_probs = torch.stack([lm_scores, extra_scores], dim=0) | ||
|
||
oracle_scores, argmaxs = torch.max(combine_probs, dim=0) | ||
|
||
oracle_ppl = torch.exp(-oracle_scores.sum() / count) | ||
|
||
if 'knn_scores.npy' in f: | ||
knn_helping = argmaxs | ||
|
||
match_knn = torch.sum(argmaxs == knn_helping).item() / len(tokens) | ||
extra_helping_percentage = torch.sum(argmaxs).item() / len(tokens) | ||
|
||
knn_helping_scores = -(combine_probs[0][knn_helping == 0].sum() + | ||
combine_probs[1][knn_helping == 1].sum()) | ||
|
||
knn_helping_ppl = torch.exp(knn_helping_scores / count) | ||
|
||
extra_only_ppl = torch.exp(-extra_scores.sum() / count) | ||
|
||
best_ppl = 1e10 | ||
best_lmbda = 0 | ||
for lmbda in np.linspace(0.0, 0.999, num=200): | ||
coeffs = torch.ones_like(combine_probs) | ||
coeffs[0] = np.log(1 - lmbda) | ||
coeffs[1] = np.log(lmbda) | ||
|
||
scores = torch.logsumexp(combine_probs + coeffs, dim=0) | ||
|
||
score_sum = scores.sum() | ||
|
||
avg_nll_loss = -score_sum / count / math.log(2) # convert to base 2 | ||
ppl = 2 ** avg_nll_loss.item() | ||
if ppl < best_ppl: | ||
best_ppl = ppl | ||
best_lmbda = lmbda | ||
|
||
outfile.write(f'{f}\t{extra_only_ppl}\t{best_lmbda}\t{best_ppl}\t{oracle_ppl}\t' | ||
f'{match_knn}\t{extra_helping_percentage}\t{knn_helping_ppl}\n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters