MCPcopy Create free account
hub / github.com/Sin3DM/Sin3DM / GaussianDiffusion

Class GaussianDiffusion

src/diffusion/gaussian_diffusion.py:102–931  ·  view source on GitHub ↗

Utilities for training and sampling diffusion models. Ported directly from here, and then adapted over time to further experimentation. https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/diffusion_utils_2.py#L42 :param betas: a 1-D

Source from the content-addressed store, hash-verified

100
101
102class GaussianDiffusion:
103 """
104 Utilities for training and sampling diffusion models.
105
106 Ported directly from here, and then adapted over time to further experimentation.
107 https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/diffusion_utils_2.py#L42
108
109 :param betas: a 1-D numpy array of betas for each diffusion timestep,
110 starting at T and going to 1.
111 :param model_mean_type: a ModelMeanType determining what the model outputs.
112 :param model_var_type: a ModelVarType determining how variance is output.
113 :param loss_type: a LossType determining the loss function to use.
114 :param rescale_timesteps: if True, pass floating point timesteps into the
115 model so that they are always scaled like in the
116 original paper (0 to 1000).
117 """
118
119 def __init__(
120 self,
121 *,
122 betas,
123 model_mean_type,
124 model_var_type,
125 loss_type,
126 rescale_timesteps=False,
127 ):
128 self.model_mean_type = model_mean_type
129 self.model_var_type = model_var_type
130 self.loss_type = loss_type
131 self.rescale_timesteps = rescale_timesteps
132
133 # Use float64 for accuracy.
134 betas = np.array(betas, dtype=np.float64)
135 self.betas = betas
136 assert len(betas.shape) == 1, "betas must be 1-D"
137 assert (betas > 0).all() and (betas <= 1).all()
138
139 self.num_timesteps = int(betas.shape[0])
140
141 alphas = 1.0 - betas
142 self.alphas_cumprod = np.cumprod(alphas, axis=0)
143 self.alphas_cumprod_prev = np.append(1.0, self.alphas_cumprod[:-1])
144 self.alphas_cumprod_next = np.append(self.alphas_cumprod[1:], 0.0)
145 assert self.alphas_cumprod_prev.shape == (self.num_timesteps,)
146
147 # calculations for diffusion q(x_t | x_{t-1}) and others
148 self.sqrt_alphas_cumprod = np.sqrt(self.alphas_cumprod)
149 self.sqrt_one_minus_alphas_cumprod = np.sqrt(1.0 - self.alphas_cumprod)
150 self.log_one_minus_alphas_cumprod = np.log(1.0 - self.alphas_cumprod)
151 self.sqrt_recip_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod)
152 self.sqrt_recipm1_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod - 1)
153
154 # calculations for posterior q(x_{t-1} | x_t, x_0)
155 self.posterior_variance = (
156 betas * (1.0 - self.alphas_cumprod_prev) / (1.0 - self.alphas_cumprod)
157 )
158 # log calculation clipped because the posterior variance is 0 at the
159 # beginning of the diffusion chain.

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected