-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from atticusg/main
Update visualization code for causal model and rotation matrices
- Loading branch information
Showing
3 changed files
with
39 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import seaborn | ||
import torch | ||
|
||
def rotation_token_heatmap(rotate_layer, | ||
tokens, | ||
token_size, | ||
variables, | ||
intervention_size): | ||
|
||
W = rotate_layer.weight.data | ||
in_dim, out_dim = W.shape | ||
|
||
assert in_dim % token_size == 0 | ||
assert in_dim / token_size >= len(tokens) | ||
|
||
assert out_dim % intervention_size == 0 | ||
assert out_dim / intervention_size >= len(variables) | ||
|
||
heatmap = [] | ||
for j in range(len(variables)): | ||
row = [] | ||
for i in range(len(tokens)): | ||
row.append(torch.norm(W[i*token_size:(i+1)*token_size, j*intervention_size:(j+1)*intervention_size])) | ||
mean = sum(row) | ||
heatmap.append([x/mean for x in row]) | ||
return seaborn.heatmap(heatmap, | ||
xticklabels=tokens, | ||
yticklabels=variables) |
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