(
self,
*,
betas: Sequence[float],
model_mean_type: str,
model_var_type: str,
loss_type: str,
discretized_t0: bool = False,
channel_scales: Optional[np.ndarray] = None,
channel_biases: Optional[np.ndarray] = None,
)
| 142 | """ |
| 143 | |
| 144 | def __init__( |
| 145 | self, |
| 146 | *, |
| 147 | betas: Sequence[float], |
| 148 | model_mean_type: str, |
| 149 | model_var_type: str, |
| 150 | loss_type: str, |
| 151 | discretized_t0: bool = False, |
| 152 | channel_scales: Optional[np.ndarray] = None, |
| 153 | channel_biases: Optional[np.ndarray] = None, |
| 154 | ): |
| 155 | self.model_mean_type = model_mean_type |
| 156 | self.model_var_type = model_var_type |
| 157 | self.loss_type = loss_type |
| 158 | self.discretized_t0 = discretized_t0 |
| 159 | self.channel_scales = channel_scales |
| 160 | self.channel_biases = channel_biases |
| 161 | |
| 162 | # Use float64 for accuracy. |
| 163 | betas = np.array(betas, dtype=np.float64) |
| 164 | self.betas = betas |
| 165 | assert len(betas.shape) == 1, "betas must be 1-D" |
| 166 | assert (betas > 0).all() and (betas <= 1).all() |
| 167 | |
| 168 | self.num_timesteps = int(betas.shape[0]) |
| 169 | |
| 170 | alphas = 1.0 - betas |
| 171 | self.alphas_cumprod = np.cumprod(alphas, axis=0) |
| 172 | self.alphas_cumprod_prev = np.append(1.0, self.alphas_cumprod[:-1]) |
| 173 | self.alphas_cumprod_next = np.append(self.alphas_cumprod[1:], 0.0) |
| 174 | assert self.alphas_cumprod_prev.shape == (self.num_timesteps,) |
| 175 | |
| 176 | # calculations for diffusion q(x_t | x_{t-1}) and others |
| 177 | self.sqrt_alphas_cumprod = np.sqrt(self.alphas_cumprod) |
| 178 | self.sqrt_one_minus_alphas_cumprod = np.sqrt(1.0 - self.alphas_cumprod) |
| 179 | self.log_one_minus_alphas_cumprod = np.log(1.0 - self.alphas_cumprod) |
| 180 | self.sqrt_recip_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod) |
| 181 | self.sqrt_recipm1_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod - 1) |
| 182 | |
| 183 | # calculations for posterior q(x_{t-1} | x_t, x_0) |
| 184 | self.posterior_variance = ( |
| 185 | betas * (1.0 - self.alphas_cumprod_prev) / (1.0 - self.alphas_cumprod) |
| 186 | ) |
| 187 | # below: log calculation clipped because the posterior variance is 0 at the beginning of the diffusion chain |
| 188 | self.posterior_log_variance_clipped = np.log( |
| 189 | np.append(self.posterior_variance[1], self.posterior_variance[1:]) |
| 190 | ) |
| 191 | self.posterior_mean_coef1 = ( |
| 192 | betas * np.sqrt(self.alphas_cumprod_prev) / (1.0 - self.alphas_cumprod) |
| 193 | ) |
| 194 | self.posterior_mean_coef2 = ( |
| 195 | (1.0 - self.alphas_cumprod_prev) * np.sqrt(alphas) / (1.0 - self.alphas_cumprod) |
| 196 | ) |
| 197 | |
| 198 | def get_sigmas(self, t): |
| 199 | return _extract_into_tensor(self.sqrt_recipm1_alphas_cumprod, t, t.shape) |
nothing calls this directly
no outgoing calls
no test coverage detected