text
stringlengths
1
93.6k
Returns:
a dictionary of diffusion hyperparameters including:
T (int), Beta/Alpha/Alpha_bar/Sigma (torch.tensor on cpu, shape=(T, ))
"""
Beta = torch.linspace(beta_0, beta_T, T)
Alpha = 1 - Beta
Alpha_bar = Alpha + 0
Beta_tilde = Beta + 0
for t in range(1, T):
Alpha_bar[t] *= Alpha_bar[t-1]
Beta_tilde[t] *= (1-Alpha_bar[t-1]) / (1-Alpha_bar[t])
Sigma = torch.sqrt(Beta_tilde)
_dh = {}
_dh["T"], _dh["Beta"], _dh["Alpha"], _dh["Alpha_bar"], _dh["Sigma"] = T, Beta, Alpha, Alpha_bar, Sigma
diffusion_hyperparams = _dh
return diffusion_hyperparams
def bisearch(f, domain, target, eps=1e-8):
"""
find smallest x such that f(x) > target
Parameters:
f (function): function
domain (tuple): x in (left, right)
target (float): target value
Returns:
x (float)
"""
#
sign = -1 if target < 0 else 1
left, right = domain
for _ in range(1000):
x = (left + right) / 2
if f(x) < target:
right = x
elif f(x) > (1 + sign * eps) * target:
left = x
else:
break
return x
def get_VAR_noise(S, schedule='linear'):
"""
Compute VAR noise levels
Parameters:
S (int): approximante diffusion process length
schedule (str): linear or quadratic
Returns:
np array of noise levels, size = (S, )
"""
target = np.prod(1 - np.linspace(diffusion_config["beta_0"], diffusion_config["beta_T"], diffusion_config["T"]))
if schedule == 'linear':
g = lambda x: np.linspace(diffusion_config["beta_0"], x, S)
domain = (diffusion_config["beta_0"], 0.99)
elif schedule == 'quadratic':
g = lambda x: np.array([diffusion_config["beta_0"] * (1+i*x) ** 2 for i in range(S)])
domain = (0.0, 0.95 / np.sqrt(diffusion_config["beta_0"]) / S)
else:
raise NotImplementedError
f = lambda x: np.prod(1 - g(x))
largest_var = bisearch(f, domain, target, eps=1e-4)
return g(largest_var)
def get_STEP_step(S, schedule='linear'):
"""
Compute STEP steps
Parameters:
S (int): approximante diffusion process length
schedule (str): linear or quadratic
Returns:
np array of steps, size = (S, )
"""
if schedule == 'linear':
c = (diffusion_config["T"] - 1.0) / (S - 1.0)
list_tau = [np.floor(i * c) for i in range(S)]
elif schedule == 'quadratic':
list_tau = np.linspace(0, np.sqrt(diffusion_config["T"] * 0.8), S) ** 2
else:
raise NotImplementedError
return [int(s) for s in list_tau]
def _log_gamma(x):
# Gamma(x+1) ~= sqrt(2\pi x) * (x/e)^x (1 + 1 / 12x)
y = x - 1
return np.log(2 * np.pi * y) / 2 + y * (np.log(y) - 1) + np.log(1 + 1 / (12 * y))