forked from Mukayyyy/del_jtvae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_sopso.py
executable file
·145 lines (116 loc) · 4.91 KB
/
manage_sopso.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
141
142
143
144
145
from learner.dataset import FragmentDataset
from learner.sampler import Sampler
from learner.trainer import Trainer, save_ckpt
from utils.config import Config
from utils.parser import command_parser
from utils.plots import plot_paper_figures, plot_figures, plot_loss, plot_pareto_fronts
from utils.preprocess import preprocess_dataset
from utils.postprocess import postprocess_samples, postprocess_samples_del, postprocess_data_del, score_samples, dump_scores
from utils.filesystem import load_dataset
from learner.DEL import DEL
from learner.DEL_SOPSO import DEL_single
from learner.BO import BO
from rdkit import rdBase
rdBase.DisableLog('rdApp.*')
def train_model(config):
dataset = FragmentDataset(config)
vocab = dataset.get_vocab()
trainer = Trainer(config, vocab)
trainer.train(dataset.get_loader(), 0)
# can use this function in model evolution
def resume_model(config):
dataset = FragmentDataset(config)
vocab = dataset.get_vocab()
load_last = config.get('load_last')
trainer, epoch = Trainer.load(config, vocab, last=load_last)
trainer.train(dataset.get_loader(), epoch + 1)
def sample_model(config):
dataset = FragmentDataset(config)
vocab = dataset.get_vocab()
load_last = config.get('load_last')
trainer, epoch = Trainer.load(config, vocab, last=load_last)
sampler = Sampler(config, vocab, trainer.model)
seed = config.get('sampling_seed') if config.get('reproduce') else None
samples = sampler.sample(config.get('num_samples'), seed=seed)
dataset = load_dataset(config, kind="test")
_, scores = score_samples(samples, dataset)
is_max = dump_scores(config, scores, epoch)
if is_max:
save_ckpt(trainer, epoch, filename=f"best.pt")
config.save()
def sample_from_z_model(config):
"""
Reconstruction.
"""
dataset = FragmentDataset(config, kind='test')
vocab = dataset.get_vocab()
load_last = config.get('load_last') # boolean to indicate whether load last
trainer, epoch = Trainer.load(config, vocab, last=load_last)
sampler = Sampler(config, vocab, trainer.model)
seed = config.get('sampling_seed') if config.get('reproduce') else None
loader=dataset.get_loader() # load data
for idx, (src, tgt, lengths, properties) in enumerate(loader):
z, mu, logvar = sampler.encode_z(src,lengths)
samples = sampler.sample_from_z(z, seed=seed)
# concatenate samples
pass
dataset = load_dataset(config, kind="test")
_, scores = score_samples(samples, dataset)
is_max = dump_scores(config, scores, epoch)
if is_max:
save_ckpt(trainer, epoch, filename=f"best.pt")
config.save()
if __name__ == "__main__":
parser = command_parser() # defined in parser.py
args = vars(parser.parse_args())
command = args.pop('command')
if command == 'preprocess':
dataset = args.pop('dataset')
n_jobs = args.pop('n_jobs')
preprocess_dataset(dataset, n_jobs)
elif command == 'train':
config = Config(args.pop('dataset'), **args)
train_model(config)
elif command == 'resume':
run_dir = args.pop('run_dir')
config = Config.load(run_dir, **args)
resume_model(config)
elif command == 'sample':
args.update(use_gpu=False)
run_dir = args.pop('run_dir')
config = Config.load(run_dir, **args)
sample_model(config)
elif command == 'postprocess':
run_dir = args.pop('run_dir')
config = Config.load(run_dir, **args)
postprocess_samples(config, **args)
elif command == 'plot':
run_dir = args.pop('run_dir')
plot_paper_figures(run_dir)
plot_loss(run_dir, group='batch')
plot_loss(run_dir, group='epoch')
elif command == 'DEL':
config = Config(args.pop('dataset'), **args)
print(config)
if not config.get('single_objective'):
del1 = DEL(config)
else:
del1 = DEL_single(config)
del1.train()
#del1.save_population(config.get('num_generations'))
elif command == 'plot_del':
run_dir = args.pop('run_dir')
config = Config.load(run_dir, **args)
postprocess_data_del(config, use_train=False, prefix='new_pop', postfixes=['0','4','final'])
plot_figures(run_dir, DEL=True, filename='new_pop_aggregated')
postprocess_data_del(config, use_train=False, prefix='generated_from_random', postfixes=['0','4','final'])
plot_figures(run_dir, DEL=True, filename='generated_from_random_aggregated')
plot_loss(run_dir, group='batch')
plot_loss(run_dir, group='epoch')
plot_pareto_fronts(run_dir, fronts=[0,1,2,3,4], with_bo=False)
plot_pareto_fronts(run_dir, fronts=[0,1,2,3,4], with_bo=True)
elif command == 'BO':
run_dir = args.pop('run_dir')
config = Config.load(run_dir, **args)
bo1 = BO(config)
bo1.train()