-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinetune_rnn.py
114 lines (97 loc) · 5.23 KB
/
finetune_rnn.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
import argparse
import os
import pickle
import pandas as pd
import torch
import torch.nn as nn
from data.util import get_trec_dataset, get_mnist_dataset
from models.layers.util import sign_to_str, calculate_ber
from models.util import seed_everything
from trainer.rnn import Trainer, PrivateTrainer
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--output-dir', type=str, dest='output_dir', help='output folder')
parser.add_argument('--seed', type=int, dest='seed', default=1234, help='seed for experiment')
parser.add_argument('--epochs', type=int, dest='epochs', default=5, help='number of epochs')
parser.add_argument('--batch-size', type=int, dest='batch_size', default=64, help='batch size per steps')
parser.add_argument('--max-sentence-length', type=int, dest='max_sentence_length', default=30,
help='max sentence length, only used in nlp task')
parser.add_argument('--pretrained-path', type=str, dest='pretrained_path', help='path to saved pretrained model',
required=True)
args = parser.parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
seed = args.seed
seed_everything(seed)
batch_size = args.batch_size
epochs = args.epochs
max_sentence_length = args.max_sentence_length
lr = 0.00001
save_dir = args.output_dir
os.makedirs(save_dir, exist_ok=True)
with open(os.path.join(args.pretrained_path, 'keyed_kwargs_{}.pickle'.format(seed)), 'rb') as f:
keyed_kwargs = pickle.load(f)
dataset = keyed_kwargs['dataset']
trigger_size = keyed_kwargs['trigger_size']
trigger_batch_size = keyed_kwargs['trigger_batch_size']
vocab = None
pad_idx = 0
if os.path.isfile(os.path.join(args.pretrained_path, 'trigger_dataloader_{}.pth'.format(seed))):
print('found trigger dataset')
trigger_dataloader = torch.load(os.path.join(args.pretrained_path, 'trigger_dataloader_{}.pth'.format(seed)))
if dataset == 'trec':
train_dataloader, valid_dataloader, _, vocab = get_trec_dataset(num_workers=2,
batch_size=batch_size,
trigger_size=trigger_size,
trigger_batch_size=trigger_batch_size,
max_sentence_length=max_sentence_length)
else:
train_dataloader, valid_dataloader, _ = get_mnist_dataset(num_workers=2,
batch_size=batch_size,
trigger_size=trigger_size,
trigger_batch_size=trigger_batch_size)
else:
trigger_dataloader = None
if dataset == 'trec':
train_dataloader, valid_dataloader, vocab = get_trec_dataset(num_workers=2, batch_size=batch_size,
max_sentence_length=max_sentence_length)
else:
train_dataloader, valid_dataloader = get_mnist_dataset(num_workers=2, batch_size=batch_size)
if dataset == 'trec':
num_words = len(vocab.itos)
pad_idx = vocab.stoi['<pad>']
model = torch.load(os.path.join(args.pretrained_path, 'model_{}.pth'.format(seed)))
# remove sign loss from model
model.rnn.sign_loss = None
keyed_kwargs['key'] = model.key.cpu().clone()
# fine tune with small learning rate
optimizer = torch.optim.Adam(model.parameters(), lr)
criterion = nn.CrossEntropyLoss()
trainer = Trainer(model, optimizer, criterion, device)
train_res, test_res, trigger_res = [], [], []
for e in range(1, epochs + 1):
tra = trainer.train(e, train_dataloader, use_key=False)
tes = trainer.test(valid_dataloader, use_key=False)
if trigger_dataloader:
tri = trainer.test(trigger_dataloader, use_key=False, msg='Trigger testing')
trigger_res.append(tri)
train_res.append(tra)
test_res.append(tes)
train_df = pd.DataFrame(train_res)
test_df = pd.DataFrame(test_res)
train_df.to_csv(os.path.join(save_dir, 'train_{}.csv'.format(seed)))
test_df.to_csv(os.path.join(save_dir, 'valid_{}.csv'.format(seed)))
print('average training time per epoch:', train_df['time'].mean())
if trigger_dataloader:
trigger_df = pd.DataFrame(trigger_res)
trigger_df.to_csv(os.path.join(save_dir, 'trigger_{}.csv'.format(seed)))
torch.save(model, os.path.join(save_dir, 'model_{}.pth'.format(seed)))
print('evaluating model on original key')
trainer = PrivateTrainer(model, optimizer, criterion, device)
te = trainer.test(valid_dataloader)
tri = trainer.test(trigger_dataloader, msg='Trigger testing')
print('\nsignature:')
try:
print(sign_to_str(model.get_signature().cpu().detach().numpy(), len(keyed_kwargs['signature'])))
except UnicodeError:
pass
print('ber:', calculate_ber(model.get_signature().cpu().detach().numpy(), keyed_kwargs['signature']))