-
Notifications
You must be signed in to change notification settings - Fork 19
/
Attention_vis.py
140 lines (110 loc) · 5.57 KB
/
Attention_vis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import pandas as pd
import numpy as np
import sys
import yaml
from tqdm.auto import tqdm
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from torch.nn.utils.clip_grad import clip_grad_norm
from transformers import AdamW, get_linear_schedule_with_warmup, RobertaModel, RobertaConfig, RobertaTokenizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, multilabel_confusion_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import KFold
from rdkit import Chem
import seaborn as sns
from pylab import rcParams
import matplotlib.pyplot as plt
from matplotlib import rc
from packaging import version
import torchmetrics
from torchmetrics import R2Score
from PolymerSmilesTokenization import PolymerSmilesTokenizer
from copy import deepcopy
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
class DownstreamRegression(nn.Module):
def __init__(self, drop_rate=0.1):
super(DownstreamRegression, self).__init__()
self.PretrainedModel = deepcopy(PretrainedModel)
self.PretrainedModel.resize_token_embeddings(len(tokenizer))
self.Regressor = nn.Sequential(
nn.Dropout(drop_rate),
nn.Linear(self.PretrainedModel.config.hidden_size, self.PretrainedModel.config.hidden_size),
nn.SiLU(),
nn.Linear(self.PretrainedModel.config.hidden_size, 1)
)
def forward(self, input_ids, attention_mask):
outputs = self.PretrainedModel(input_ids=input_ids, attention_mask=attention_mask)
logits = outputs.last_hidden_state[:, 0, :]
output = self.Regressor(logits)
return output
def main(attention_config):
if attention_config['task'] == 'pretrain':
smiles = attention_config['smiles']
else:
data = pd.read_csv(attention_config['file_path'])
smiles = data.values[attention_config['index'],0]
if attention_config['add_vocab_flag']:
vocab_sup = pd.read_csv(attention_config['vocab_sup_file'], header=None).values.flatten().tolist()
tokenizer.add_tokens(vocab_sup)
encoding = tokenizer(
str(smiles),
add_special_tokens=True,
max_length=attention_config['blocksize'],
return_token_type_ids=False,
padding="max_length",
truncation=True,
return_attention_mask=True,
return_tensors='pt',
)
input_ids = encoding["input_ids"].to(device)
attention_mask = encoding["attention_mask"].to(device)
if attention_config['task'] == 'pretrain':
outputs = PretrainedModel(input_ids=input_ids, attention_mask=attention_mask, output_attentions=True)
else:
model = DownstreamRegression(drop_rate=0).to(device)
checkpoint = torch.load(attention_config['model_path'])
model.load_state_dict(checkpoint['model'])
model = model.double()
model.eval()
with torch.no_grad():
outputs = model.PretrainedModel(input_ids=input_ids, attention_mask=attention_mask, output_attentions=True)
attention = outputs[-1]
fig, axes = plt.subplots(3,4, figsize=(attention_config['figsize_x'],attention_config['figsize_y']))
xticklabels = tokenizer.convert_ids_to_tokens(input_ids.squeeze())
if attention_config['task'] == 'pretrain':
for i in range(3):
for j in range(4):
sns.heatmap(attention[attention_config['layer']][0,4*i+j,:,:].cpu().detach().numpy(), ax = axes[i,j], xticklabels=xticklabels, yticklabels=xticklabels)
axes[i,j].set_title(label="Attention Head %s" % str(4*i+j+1), fontsize=attention_config['fontsize'])
axes[i,j].tick_params(labelsize=attention_config['labelsize'])
cbar = axes[i,j].collections[0].colorbar
# here set the labelsize by 20
cbar.ax.tick_params(labelsize=attention_config['labelsize'])
else:
yticklabels = ['Layer 1','Layer 2','Layer 3','Layer 4','Layer 5','Layer 6']
for i in range(3):
for j in range(4):
for layer in range(6):
attention_sub = attention[layer][0,4*i+j,0,:].cpu().detach().numpy().reshape(1,-1)
if layer == 0:
attention_CLS = attention_sub
else:
attention_CLS = np.vstack((attention_CLS, attention_sub))
sns.heatmap(attention_CLS, ax = axes[i,j], xticklabels=False)
axes[i, j].set_title(label="Attention Head %s" % str(4 * i + j + 1), fontsize=attention_config['fontsize'])
axes[i, j].set_yticklabels(rotation=attention_config['rotation'], labels=yticklabels)
axes[i, j].tick_params(labelsize=attention_config['labelsize'])
cbar = axes[i, j].collections[0].colorbar
# here set the labelsize by 20
cbar.ax.tick_params(labelsize=attention_config['labelsize'])
plt.savefig(attention_config['save_path'], bbox_inches='tight')
if __name__ == "__main__":
attention_config = yaml.load(open("config_attention.yaml", "r"), Loader=yaml.FullLoader)
"""Device"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
PretrainedModel = RobertaModel.from_pretrained(attention_config['pretrain_path']).to(device)
tokenizer = PolymerSmilesTokenizer.from_pretrained("roberta-base", max_len=attention_config['blocksize'])
main(attention_config)