-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathutils.py
44 lines (39 loc) · 1.52 KB
/
utils.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
import torch
import numpy as np
from rdkit import Chem
def Variable(tensor):
"""Wrapper for torch.autograd.Variable that also accepts
numpy arrays directly and automatically assigns it to
the GPU. Be aware in case some operations are better
left to the CPU."""
if isinstance(tensor, np.ndarray):
tensor = torch.from_numpy(tensor)
if torch.cuda.is_available():
return torch.autograd.Variable(tensor).cuda()
return torch.autograd.Variable(tensor)
def decrease_learning_rate(optimizer, decrease_by=0.01):
"""Multiplies the learning rate of the optimizer by 1 - decrease_by"""
for param_group in optimizer.param_groups:
param_group['lr'] *= (1 - decrease_by)
def seq_to_smiles(seqs, voc):
"""Takes an output sequence from the RNN and returns the
corresponding SMILES."""
smiles = []
for seq in seqs.cpu().numpy():
smiles.append(voc.decode(seq))
return smiles
def fraction_valid_smiles(smiles):
"""Takes a list of SMILES and returns fraction valid."""
i = 0
for smile in smiles:
if Chem.MolFromSmiles(smile):
i += 1
return i / len(smiles)
def unique(arr):
# Finds unique rows in arr and return their indices
arr = arr.cpu().numpy()
arr_ = np.ascontiguousarray(arr).view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[1])))
_, idxs = np.unique(arr_, return_index=True)
if torch.cuda.is_available():
return torch.LongTensor(np.sort(idxs)).cuda()
return torch.LongTensor(np.sort(idxs))