-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_generator_special.py
136 lines (108 loc) · 4.58 KB
/
train_generator_special.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
import pickle
import os
import torch
from torch import nn
from tqdm import tqdm
import torch.nn.functional as F
from torch.utils.data import DataLoader
import pytorch_lightning as pl
import numpy as np
import pandas as pd
from sklearn import metrics
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping
from models import MoralTransformer, MoralTransformerSpecial
from models.custom_transformer_classifier import OneHotMoralClassifier
from data import NewsDataset
import sys
# assert len(sys.argv) > 1
# exp_idx = int(sys.argv[1])
exp_idx = 0
# [moral tokens to, lr, moral mode, content loss metric, include moral loss]
experiments = [
# ['decoder', 1e-6, 'identity', 'normalized_pairwise', False],
# ['encoder', 1e-6, 'identity', 'normalized_pairwise', False],
# ['decoder', 1e-6, 'random', 'normalized_pairwise', True],
['decoder', 1e-6, 'id+random', 'embedding_normalized_pairwise', True],
# ['encoder', 1e-6, 'random', 'normalized_pairwise', True],
# ['injection', 1e-6, 'identity', 'normalized_pairwise', False], # TODO IF TIME
# ['injection', 1e-6, 'random', 'normalized_pairwise', True], # TODO IF TIME
]
def train(gpus):
print("Loading data...")
# file = open('headlines_cnn_bart_split.pkl', 'rb')
file = open('data/nela-covid-2020/combined/headlines_cnn_bart_split.pkl', 'rb')
data = pickle.load(file)
file.close()
print("Data loaded")
exp = experiments[exp_idx]
feed_moral_tokens_to = exp[0]
lr = exp[1]
moral_mode = exp[2]
use_content_loss = bool(exp[3])
content_loss_type = exp[3]
use_moral_loss = exp[4]
exp_name = '_'.join([feed_moral_tokens_to, str(lr), moral_mode, str(content_loss_type), str(use_moral_loss)])
exp_name = "RESUME " + exp_name
# exp_name='TMP'
# exp_name += '_content_weighted_10x'
# exp_name += '_moral_weighted_10x'
# exp_name += ''
print(exp_name)
# stuff to keep
freeze_encoder = True
freeze_decoder = False
include_moral_tokens = True
if feed_moral_tokens_to == 'injection':
freeze_encoder = False
include_moral_tokens = False
data['train'] = data['train']
train_dataset = NewsDataset(data['train'], moral_mode=moral_mode, include_moral_tokens=include_moral_tokens)
val_dataset = NewsDataset(data['val'], moral_mode=moral_mode, include_moral_tokens=include_moral_tokens)
test_dataset = NewsDataset(data['test'], moral_mode=moral_mode, include_moral_tokens=include_moral_tokens)
train_loader = DataLoader(train_dataset, batch_size=8, num_workers=4, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=8, num_workers=4)
# ------------
# training
# ------------
print('Loading discriminator...')
discriminator = OneHotMoralClassifier({}, use_mask=False)
discriminator.load_state_dict(torch.load('saved_models/discriminator_titlemorals_state.pkl'))
print('Discriminator loaded')
model = MoralTransformerSpecial(
lr=lr,
discriminator=discriminator,
use_content_loss=use_content_loss,
contextual_injection=(not include_moral_tokens),
freeze_encoder=freeze_encoder,
freeze_decoder=freeze_decoder,
feed_moral_tokens_to=feed_moral_tokens_to,
content_loss_type=content_loss_type,
use_moral_loss=use_moral_loss,
# content_loss_weighting=10,
# moral_loss_weighting=10,
)
# model.load_state_dict(torch.load('experiments/decoder_1e-06_id+random_normalized_pairwise_False/checkpoints/epoch=9-step=26589.ckpt')['state_dict'])
checkpoint_callback= ModelCheckpoint(dirpath=os.path.join("./experiments", exp_name, "checkpoints"), save_top_k=1, save_last=True, monitor='train_loss', mode='min')
trainer = Trainer(gpus=gpus,
# auto_lr_find=False, # use to explore LRs
# distributed_backend='dp',
resume_from_checkpoint='saved_models/special_finetuned_30.ckpt',
# max_epochs=30,
max_epochs=50,
callbacks=[checkpoint_callback],
)
# LR Exploration
# lr_finder = trainer.tuner.lr_find(model, train_loader, val_loader)
# new_lr = lr_finder.suggestion()
# print(new_lr)
trainer.fit(model, train_loader, val_loader)
with open(os.path.join("./experiments", exp_name, 'loss_history.pkl'), 'wb') as f:
pickle.dump(model.loss_history, f)
print("Training Done")
# ------------
# testing
# ------------
if __name__ == '__main__':
gpus = 1 if torch.cuda.is_available() else None
train(gpus)