def train_ex(): agent.load_state_dict(torch.load(params['pr_path'] + '.pkg', map_location=utils.dev)) prior = GraphModel(voc) prior.load_state_dict(torch.load(params['ft_path'] + '.pkg', map_location=utils.dev)) evolver = GraphExplorer(agent, mutate=prior) evolver.batch_size = BATCH_SIZE evolver.epsilon = float(OPT.get('-e', '1e-2')) evolver.sigma = float(OPT.get('-b', '0.00')) evolver.scheme = OPT.get('-s', 'WS') evolver.repeat = 1 keys = ['A2A', 'QED'] A2A = utils.Predictor('output/env/RF_%s_CHEMBL251.pkg' % z, type=z) QED = utils.Property('QED') # Chose the desirability function objs = [A2A, QED] if evolver.scheme == 'WS': mod1 = utils.ClippedScore(lower_x=3, upper_x=10) mod2 = utils.ClippedScore(lower_x=0, upper_x=1.0) ths = [0.5, 0] else: mod1 = utils.ClippedScore(lower_x=3, upper_x=6.5) mod2 = utils.ClippedScore(lower_x=0, upper_x=1.0) ths = [0.99, 0] mods = [mod1, mod2] evolver.env = utils.Env(objs=objs, mods=mods, keys=keys, ths=ths) # import evolve as agent evolver.out = root + '/%s_%s_%.0e' % (alg, evolver.scheme, evolver.epsilon) evolver.fit(train_loader, test_loader=valid_loader)
def rl_train(): opts, args = getopt.getopt(sys.argv[1:], "a:e:b:g:c:s:z:") OPT = dict(opts) case = OPT['-c'] if '-c' in OPT else 'OBJ1' z = OPT['-z'] if '-z' in OPT else 'REG' alg = OPT['-a'] if '-a' in OPT else 'smile' os.environ[ "CUDA_VISIBLE_DEVICES"] = OPT['-g'] if '-g' in OPT else "0,1,2,3" voc = utils.VocSmiles(init_from_file="data/chembl_voc.txt", max_len=100) agent = GPT2Model(voc, n_layer=12) agent.load_state_dict( torch.load(params['pr_path'] + '.pkg', map_location=utils.dev)) prior = GPT2Model(voc, n_layer=12) prior.load_state_dict( torch.load(params['ft_path'] + '.pkg', map_location=utils.dev)) evolver = SmilesExplorer(agent, mutate=prior) evolver.batch_size = BATCH_SIZE evolver.epsilon = float(OPT.get('-e', '1e-2')) evolver.sigma = float(OPT.get('-b', '0.00')) evolver.scheme = OPT.get('-s', 'WS') evolver.repeat = 1 keys = ['A2A', 'QED'] A2A = utils.Predictor('output/env/RF_%s_CHEMBL251.pkg' % z, type=z) QED = utils.Property('QED') # Chose the desirability function objs = [A2A, QED] if evolver.scheme == 'WS': mod1 = utils.ClippedScore(lower_x=3, upper_x=10) mod2 = utils.ClippedScore(lower_x=0, upper_x=1) ths = [0.5, 0] else: mod1 = utils.ClippedScore(lower_x=3, upper_x=6.5) mod2 = utils.ClippedScore(lower_x=0, upper_x=0.5) ths = [0.99, 0] mods = [mod1, mod2] if case == 'OBJ3' else [mod1, mod2] evolver.env = utils.Env(objs=objs, mods=mods, keys=keys, ths=ths) root = 'output/%s_%s' % (alg, time.strftime('%y%m%d_%H%M%S', time.localtime())) os.mkdir(root) copy2(alg + '_ex.py', root) copy2(alg + '.py', root) # import evolve as agent evolver.out = root + '/%s_%s_%s_%s_%.0e' % (alg, evolver.scheme, z, case, evolver.epsilon) evolver.fit(data_loader, test_loader=test_loader)
]: # agent_path = 'output/%s_%s_256.pkg' % (path, method) print(agent_path) agent.load_state_dict(torch.load(agent_path)) z = 'REG' keys = ['A2A'] A2A = utils.Predictor('output/env/RF_%s_CHEMBL251.pkg' % z, type=z) QED = utils.Property('QED') # Chose the desirability function objs = [A2A, QED] ths = [6.5, 0.0] env = utils.Env(objs=objs, mods=None, keys=keys, ths=ths) if method in ['atom']: data = pd.read_table('data/ligand_mf_brics_test.txt') # data = data.sample(BATCH_SIZE * 10) data = torch.from_numpy(data.values).long().view( len(data), voc.max_len, -1) loader = DataLoader(data, batch_size=BATCH_SIZE) out = '%s.txt' % agent_path else: data = pd.read_table('data/%s_test.txt' % dataset).Input.drop_duplicates() # data = data.sample(BATCH_SIZE * 10) data = voc.encode([seq.split(' ')[:-1] for seq in data.values]) loader = DataLoader(data, batch_size=BATCH_SIZE)
TEAMPLATEFILE = 'template.html' DATAFILE = 'data.json' import utils env = utils.Env() def template2html(datapath=None,templatepath=None): templatepath = templatepath if os.path.exists(datapath) else or os.path.join(env.home,DATAFILE) if not os.path.exists(templatepath): from string import Template open ( s = Template('$who likes $what') s.substitute(who='tim', what='kung pao') 'tim likes kung pao' d = dict(who='tim') Template('Give $who $100').substitute(d)
if __name__ == '__main__': for z in ['REG']: # Construct the environment with three predictors and desirability functions keys = ['A1', 'A2A', 'ERG'] A1 = utils.Predictor('output/env/RF_%s_CHEMBL226.pkg' % z, type=z) A2A = utils.Predictor('output/env/RF_%s_CHEMBL251.pkg' % z, type=z) ERG = utils.Predictor('output/env/RF_%s_CHEMBL240.pkg' % z, type=z) mod1 = utils.ClippedScore(lower_x=4, upper_x=6.5) mod2 = utils.ClippedScore(lower_x=9, upper_x=6.5) mod3 = utils.ClippedScore(lower_x=7.5, upper_x=5) objs = [A1, A2A, ERG] models = { 'output/lstm_ligand.pkg': 'benchmark/FINE-TUNE_%s_%s.tsv' % (z, case), 'output/lstm_chembl.pkg': 'benchmark/PRE-TRAIN_%s_%s.tsv' % (z, case) } for case in ['OBJ1', 'OBJ3']: if case == 'OBJ3': mods = [mod1, mod1, mod3] else: mods = [mod2, mod1, mod3] env = utils.Env(objs=objs, mods=mods, keys=keys) for input, output in models.items(): df = pd.DataFrame() df['Smiles'] = sampling(input, output) scores = env(df['Smiles'], is_smiles=True) df.to_csv(output, index=False, sep='\t')
A2A = utils.Predictor('output/env/RF_%s_CHEMBL251.pkg' % z, type=z) ERG = utils.Predictor('output/env/RF_%s_CHEMBL240.pkg' % z, type=z) # Chose the desirability function objs = [A1, A2A, ERG] if scheme == 'WS': mod1 = utils.ClippedScore(lower_x=3, upper_x=10) mod2 = utils.ClippedScore(lower_x=10, upper_x=3) ths = [0.5] * 3 else: mod1 = utils.ClippedScore(lower_x=3, upper_x=6.5) mod2 = utils.ClippedScore(lower_x=10, upper_x=6.5) ths = [0.99] * 3 mods = [mod1, mod1, mod2] if case == 'OBJ3' else [mod2, mod1, mod2] env = utils.Env(objs=objs, mods=mods, keys=keys, ths=ths) root = 'output/%s_%s_%s_%s/'% (alg, case, scheme, time.strftime('%y%m%d_%H%M%S', time.localtime())) os.mkdir(root) copy2('models/rlearner.py', root) copy2('trainer.py', root) pr_path = 'output/lstm_chembl' ft_path = 'output/lstm_ligand' voc = utils.Voc(init_from_file="data/voc.txt") agent = generator.Generator(voc) agent.load_state_dict(torch.load(ft_path + '.pkg')) prior = generator.Generator(voc) prior.load_state_dict(torch.load(pr_path + '.pkg'))