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

Method __init__

src/diffusion/gaussian_diffusion.py:119–170  ·  view source on GitHub ↗
(
        self,
        *,
        betas,
        model_mean_type,
        model_var_type,
        loss_type,
        rescale_timesteps=False,
    )

Source from the content-addressed store, hash-verified

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.
160 self.posterior_log_variance_clipped = np.log(
161 np.append(self.posterior_variance[1], self.posterior_variance[1:])
162 )
163 self.posterior_mean_coef1 = (
164 betas * np.sqrt(self.alphas_cumprod_prev) / (1.0 - self.alphas_cumprod)
165 )
166 self.posterior_mean_coef2 = (
167 (1.0 - self.alphas_cumprod_prev)
168 * np.sqrt(alphas)
169 / (1.0 - self.alphas_cumprod)
170 )
171
172 def q_mean_variance(self, x_start, t):
173 """

Callers

nothing calls this directly

Calls 1

logMethod · 0.80

Tested by

no test coverage detected