Spaces:
Sleeping
Sleeping
File size: 10,419 Bytes
95ba5bc |
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
import sys
from datetime import datetime
import torch
import numpy as np
class Logger(object):
def __init__(self, logpath, syspart=sys.stdout):
self.terminal = syspart
self.log = open(logpath, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
self.log.flush()
def flush(self):
# this flush method is needed for python 3 compatibility.
# this handles the flush command by doing nothing.
# you might want to specify some extra behavior here.
pass
def log(*args):
print(f'[{datetime.now()}]', *args)
class EMA:
def __init__(self, beta):
super().__init__()
self.beta = beta
def update_model_average(self, ma_model, current_model):
for current_params, ma_params in zip(current_model.parameters(), ma_model.parameters()):
old_weight, up_weight = ma_params.data, current_params.data
ma_params.data = self.update_average(old_weight, up_weight)
def update_average(self, old, new):
if old is None:
return new
return old * self.beta + (1 - self.beta) * new
def sum_except_batch(x):
return x.reshape(x.size(0), -1).sum(dim=-1)
def remove_mean(x):
mean = torch.mean(x, dim=1, keepdim=True)
x = x - mean
return x
def remove_mean_with_mask(x, node_mask):
masked_max_abs_value = (x * (1 - node_mask)).abs().sum().item()
assert masked_max_abs_value < 1e-5, f'Error {masked_max_abs_value} too high'
N = node_mask.sum(1, keepdims=True)
mean = torch.sum(x, dim=1, keepdim=True) / N
x = x - mean * node_mask
return x
def remove_partial_mean_with_mask(x, node_mask, center_of_mass_mask):
"""
Subtract center of mass of fragments from coordinates of all atoms
"""
x_masked = x * center_of_mass_mask
N = center_of_mass_mask.sum(1, keepdims=True)
mean = torch.sum(x_masked, dim=1, keepdim=True) / N
x = x - mean * node_mask
return x
def assert_mean_zero(x):
mean = torch.mean(x, dim=1, keepdim=True)
assert mean.abs().max().item() < 1e-4
def assert_mean_zero_with_mask(x, node_mask, eps=1e-10):
assert_correctly_masked(x, node_mask)
largest_value = x.abs().max().item()
error = torch.sum(x, dim=1, keepdim=True).abs().max().item()
rel_error = error / (largest_value + eps)
assert rel_error < 1e-2, f'Mean is not zero, relative_error {rel_error}'
def assert_partial_mean_zero_with_mask(x, node_mask, center_of_mass_mask, eps=1e-10):
assert_correctly_masked(x, node_mask)
x_masked = x * center_of_mass_mask
largest_value = x_masked.abs().max().item()
error = torch.sum(x_masked, dim=1, keepdim=True).abs().max().item()
rel_error = error / (largest_value + eps)
assert rel_error < 1e-2, f'Partial mean is not zero, relative_error {rel_error}'
def assert_correctly_masked(variable, node_mask):
assert (variable * (1 - node_mask)).abs().max().item() < 1e-4, \
'Variables not masked properly.'
def check_mask_correct(variables, node_mask):
for i, variable in enumerate(variables):
if len(variable) > 0:
assert_correctly_masked(variable, node_mask)
def center_gravity_zero_gaussian_log_likelihood(x):
assert len(x.size()) == 3
B, N, D = x.size()
assert_mean_zero(x)
# r is invariant to a basis change in the relevant hyperplane.
r2 = sum_except_batch(x.pow(2))
# The relevant hyperplane is (N-1) * D dimensional.
degrees_of_freedom = (N-1) * D
# Normalizing constant and logpx are computed:
log_normalizing_constant = -0.5 * degrees_of_freedom * np.log(2*np.pi)
log_px = -0.5 * r2 + log_normalizing_constant
return log_px
def sample_center_gravity_zero_gaussian(size, device):
assert len(size) == 3
x = torch.randn(size, device=device)
# This projection only works because Gaussian is rotation invariant around
# zero and samples are independent!
x_projected = remove_mean(x)
return x_projected
def center_gravity_zero_gaussian_log_likelihood_with_mask(x, node_mask):
assert len(x.size()) == 3
B, N_embedded, D = x.size()
assert_mean_zero_with_mask(x, node_mask)
# r is invariant to a basis change in the relevant hyperplane, the masked
# out values will have zero contribution.
r2 = sum_except_batch(x.pow(2))
# The relevant hyperplane is (N-1) * D dimensional.
N = node_mask.squeeze(2).sum(1) # N has shape [B]
degrees_of_freedom = (N-1) * D
# Normalizing constant and logpx are computed:
log_normalizing_constant = -0.5 * degrees_of_freedom * np.log(2*np.pi)
log_px = -0.5 * r2 + log_normalizing_constant
return log_px
def sample_center_gravity_zero_gaussian_with_mask(size, device, node_mask):
assert len(size) == 3
x = torch.randn(size, device=device)
x_masked = x * node_mask
# This projection only works because Gaussian is rotation invariant around
# zero and samples are independent!
# TODO: check it
x_projected = remove_mean_with_mask(x_masked, node_mask)
return x_projected
def standard_gaussian_log_likelihood(x):
# Normalizing constant and logpx are computed:
log_px = sum_except_batch(-0.5 * x * x - 0.5 * np.log(2*np.pi))
return log_px
def sample_gaussian(size, device):
x = torch.randn(size, device=device)
return x
def standard_gaussian_log_likelihood_with_mask(x, node_mask):
# Normalizing constant and logpx are computed:
log_px_elementwise = -0.5 * x * x - 0.5 * np.log(2*np.pi)
log_px = sum_except_batch(log_px_elementwise * node_mask)
return log_px
def sample_gaussian_with_mask(size, device, node_mask):
x = torch.randn(size, device=device)
x_masked = x * node_mask
return x_masked
def concatenate_features(x, h):
xh = torch.cat([x, h['categorical']], dim=2)
if 'integer' in h:
xh = torch.cat([xh, h['integer']], dim=2)
return xh
def split_features(z, n_dims, num_classes, include_charges):
assert z.size(2) == n_dims + num_classes + include_charges
x = z[:, :, 0:n_dims]
h = {'categorical': z[:, :, n_dims:n_dims+num_classes]}
if include_charges:
h['integer'] = z[:, :, n_dims+num_classes:n_dims+num_classes+1]
return x, h
# For gradient clipping
class Queue:
def __init__(self, max_len=50):
self.items = []
self.max_len = max_len
def __len__(self):
return len(self.items)
def add(self, item):
self.items.insert(0, item)
if len(self) > self.max_len:
self.items.pop()
def mean(self):
return np.mean(self.items)
def std(self):
return np.std(self.items)
def gradient_clipping(flow, gradnorm_queue):
# Allow gradient norm to be 150% + 2 * stdev of the recent history.
max_grad_norm = 1.5 * gradnorm_queue.mean() + 2 * gradnorm_queue.std()
# Clips gradient and returns the norm
grad_norm = torch.nn.utils.clip_grad_norm_(
flow.parameters(), max_norm=max_grad_norm, norm_type=2.0)
if float(grad_norm) > max_grad_norm:
gradnorm_queue.add(float(max_grad_norm))
else:
gradnorm_queue.add(float(grad_norm))
if float(grad_norm) > max_grad_norm:
print(f'Clipped gradient with value {grad_norm:.1f} while allowed {max_grad_norm:.1f}')
return grad_norm
def disable_rdkit_logging():
"""
Disables RDKit whiny logging.
"""
import rdkit.rdBase as rkrb
import rdkit.RDLogger as rkl
logger = rkl.logger()
logger.setLevel(rkl.ERROR)
rkrb.DisableLog('rdApp.error')
class FoundNaNException(Exception):
def __init__(self, x, h):
x_nan_idx = self.find_nan_idx(x)
h_nan_idx = self.find_nan_idx(h)
self.x_h_nan_idx = x_nan_idx & h_nan_idx
self.only_x_nan_idx = x_nan_idx.difference(h_nan_idx)
self.only_h_nan_idx = h_nan_idx.difference(x_nan_idx)
@staticmethod
def find_nan_idx(z):
idx = set()
for i in range(z.shape[0]):
if torch.any(torch.isnan(z[i])):
idx.add(i)
return idx
def get_batch_idx_for_animation(batch_size, batch_idx):
batch_indices = []
mol_indices = []
for idx in [0, 110, 360]:
if idx // batch_size == batch_idx:
batch_indices.append(idx % batch_size)
mol_indices.append(idx)
return batch_indices, mol_indices
# Rotation data augmntation
def random_rotation(x):
bs, n_nodes, n_dims = x.size()
device = x.device
angle_range = np.pi * 2
if n_dims == 2:
theta = torch.rand(bs, 1, 1).to(device) * angle_range - np.pi
cos_theta = torch.cos(theta)
sin_theta = torch.sin(theta)
R_row0 = torch.cat([cos_theta, -sin_theta], dim=2)
R_row1 = torch.cat([sin_theta, cos_theta], dim=2)
R = torch.cat([R_row0, R_row1], dim=1)
x = x.transpose(1, 2)
x = torch.matmul(R, x)
x = x.transpose(1, 2)
elif n_dims == 3:
# Build Rx
Rx = torch.eye(3).unsqueeze(0).repeat(bs, 1, 1).to(device)
theta = torch.rand(bs, 1, 1).to(device) * angle_range - np.pi
cos = torch.cos(theta)
sin = torch.sin(theta)
Rx[:, 1:2, 1:2] = cos
Rx[:, 1:2, 2:3] = sin
Rx[:, 2:3, 1:2] = - sin
Rx[:, 2:3, 2:3] = cos
# Build Ry
Ry = torch.eye(3).unsqueeze(0).repeat(bs, 1, 1).to(device)
theta = torch.rand(bs, 1, 1).to(device) * angle_range - np.pi
cos = torch.cos(theta)
sin = torch.sin(theta)
Ry[:, 0:1, 0:1] = cos
Ry[:, 0:1, 2:3] = -sin
Ry[:, 2:3, 0:1] = sin
Ry[:, 2:3, 2:3] = cos
# Build Rz
Rz = torch.eye(3).unsqueeze(0).repeat(bs, 1, 1).to(device)
theta = torch.rand(bs, 1, 1).to(device) * angle_range - np.pi
cos = torch.cos(theta)
sin = torch.sin(theta)
Rz[:, 0:1, 0:1] = cos
Rz[:, 0:1, 1:2] = sin
Rz[:, 1:2, 0:1] = -sin
Rz[:, 1:2, 1:2] = cos
x = x.transpose(1, 2)
x = torch.matmul(Rx, x)
#x = torch.matmul(Rx.transpose(1, 2), x)
x = torch.matmul(Ry, x)
#x = torch.matmul(Ry.transpose(1, 2), x)
x = torch.matmul(Rz, x)
#x = torch.matmul(Rz.transpose(1, 2), x)
x = x.transpose(1, 2)
else:
raise Exception("Not implemented Error")
return x.contiguous() |