dar-tau commited on
Commit
7889ca8
1 Parent(s): b3fc1da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -138,10 +138,16 @@ def run_interpretation(raw_original_prompt, raw_interpretation_prompt, max_new_t
138
  generation_texts = tokenizer.batch_decode(generated)
139
 
140
  # try identifying important layers
141
- vectors_to_compare = interpreted_vectors # torch.tensor(global_state.sentence_transformer.encode(generation_texts))
 
 
 
 
 
 
142
  avoid_first, avoid_last = 2, 1 # layers that are usually never important
143
- vectors_to_compare = vectors_to_compare[avoid_first:-avoid_last]
144
- diff_score = F.normalize(vectors_to_compare, dim=-1).diff(dim=0).norm(dim=-1)
145
  important_idxs = avoid_first + diff_score.topk(k=int(np.ceil(0.1 * len(generation_texts)))).indices.cpu().numpy()
146
 
147
  # create GUI output
 
138
  generation_texts = tokenizer.batch_decode(generated)
139
 
140
  # try identifying important layers
141
+ # vectors_to_compare = interpreted_vectors # torch.tensor(global_state.sentence_transformer.encode(generation_texts))
142
+ # diff_score = F.normalize(vectors_to_compare, dim=-1).diff(dim=0).norm(dim=-1)
143
+ bags_of_words = [set(tokenizer.tokenize(text)) for text in generation_texts]
144
+ diff_score = torch.tensor([
145
+ len(bags_of_words[i+1] & bags_of_words[i]) / np.sqrt(len(bags_of_words[i+1]) * len(bags_of_words[i]))
146
+ for i in range(len(bags_of_words)-1)
147
+ ])
148
  avoid_first, avoid_last = 2, 1 # layers that are usually never important
149
+ assert avoid_first >= 1 # due to .diff() we will not be able to compute a score for the first layer
150
+ diff_score = diff_score[avoid_first-1 : len(diff_score)-avoid_last]
151
  important_idxs = avoid_first + diff_score.topk(k=int(np.ceil(0.1 * len(generation_texts)))).indices.cpu().numpy()
152
 
153
  # create GUI output