MCPcopy Create free account
hub / github.com/CompVis/zigma / ICPlan

Class ICPlan

transport/path.py:18–136  ·  view source on GitHub ↗

Linear Coupling Plan

Source from the content-addressed store, hash-verified

16#################### Coupling Plans ####################
17
18class ICPlan:
19 """Linear Coupling Plan"""
20 def __init__(self, sigma=0.0):
21 self.sigma = sigma
22
23 def compute_alpha_t(self, t):
24 """Compute the data coefficient along the path"""
25 return t, 1
26
27 def compute_sigma_t(self, t):
28 """Compute the noise coefficient along the path"""
29 return 1 - t, -1
30
31 def compute_d_alpha_alpha_ratio_t(self, t):
32 """Compute the ratio between d_alpha and alpha"""
33 return 1 / t
34
35 def compute_drift(self, x, t):
36 """We always output sde according to score parametrization; """
37 t = expand_t_like_x(t, x)
38 alpha_ratio = self.compute_d_alpha_alpha_ratio_t(t)
39 sigma_t, d_sigma_t = self.compute_sigma_t(t)
40 drift = alpha_ratio * x
41 diffusion = alpha_ratio * (sigma_t ** 2) - sigma_t * d_sigma_t # beta_t in Table 2, from VE-SDE.
42
43 return -drift, diffusion
44
45 def compute_diffusion(self, x, t, form="constant", norm=1.0): #compute w_t in Eq 4.
46 """Compute the diffusion term of the SDE
47 Args:
48 x: [batch_dim, ...], data point
49 t: [batch_dim,], time vector
50 form: str, form of the diffusion term
51 norm: float, norm of the diffusion term
52 """
53 t = expand_t_like_x(t, x)
54 choices = {
55 "constant": norm,
56 "SBDM": norm * self.compute_drift(x, t)[1], #follow the calculation of w_t for SBDM
57 "sigma": norm * self.compute_sigma_t(t)[0], # This suggests the choice wt = σt in (4) to cancel this singularity (see Appendix A.3)
58 "linear": norm * (1 - t), #Table 2, seems never used
59 "decreasing": 0.25 * (norm * th.cos(np.pi * t) + 1) ** 2, #Table2, seems never used
60 "inccreasing-decreasing": norm * th.sin(np.pi * t) ** 2, #seems never used
61 }
62
63 try:
64 diffusion = choices[form]
65 except KeyError:
66 raise NotImplementedError(f"Diffusion form {form} not implemented")
67
68 return diffusion
69
70 def get_score_from_velocity(self, velocity, x, t): #Eq 9 in SiT paper by simple algebra
71 """Wrapper function: transfrom velocity prediction model to score
72 Args:
73 velocity: [batch_dim, ...] shaped tensor; velocity model output
74 x: [batch_dim, ...] shaped tensor; x_t data point
75 t: [batch_dim,] time tensor

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected