Ddpm
DDPM
基类:Interpolant
降噪扩散概率模型 (DDPM) 插值器。
示例
>>> import torch
>>> from bionemo.moco.distributions.prior.continuous.gaussian import GaussianPrior
>>> from bionemo.moco.distributions.time.uniform import UniformTimeDistribution
>>> from bionemo.moco.interpolants.discrete_time.continuous.ddpm import DDPM
>>> from bionemo.moco.schedules.noise.discrete_noise_schedules import DiscreteCosineNoiseSchedule
>>> from bionemo.moco.schedules.inference_time_schedules import DiscreteLinearInferenceSchedule
ddpm = DDPM(
time_distribution = UniformTimeDistribution(discrete_time = True,...),
prior_distribution = GaussianPrior(...),
noise_schedule = DiscreteCosineNoiseSchedule(...),
)
model = Model(...)
# Training
for epoch in range(1000):
data = data_loader.get(...)
time = ddpm.sample_time(batch_size)
noise = ddpm.sample_prior(data.shape)
xt = ddpm.interpolate(data, noise, time)
x_pred = model(xt, time)
loss = ddpm.loss(x_pred, data, time)
loss.backward()
# Generation
x_pred = ddpm.sample_prior(data.shape)
for t in DiscreteLinearTimeSchedule(...).generate_schedule():
time = torch.full((batch_size,), t)
x_hat = model(x_pred, time)
x_pred = ddpm.step(x_hat, time, x_pred)
return x_pred
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
|
alpha_bar: torch.Tensor
property
返回 alpha bar 值。
alpha_bar_prev: torch.Tensor
property
返回之前的 alpha bar 值。
forward_data_schedule: torch.Tensor
property
返回前向数据计划。
forward_noise_schedule: torch.Tensor
property
返回前向噪声计划。
log_var: torch.Tensor
property
返回对数方差。
reverse_data_schedule: torch.Tensor
property
返回反向数据计划。
reverse_noise_schedule: torch.Tensor
property
返回反向噪声计划。
__init__(time_distribution, prior_distribution, noise_schedule, prediction_type=PredictionType.DATA, device='cpu', last_time_idx=0, rng_generator=None)
初始化 DDPM 插值器。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
time_distribution
|
TimeDistribution
|
时间步长的分布,用于为扩散过程采样时间点。 |
必需 |
prior_distribution
|
PriorDistribution
|
变量的先验分布,用作扩散过程的起点。 |
必需 |
noise_schedule
|
DiscreteNoiseSchedule
|
噪声计划,定义在每个时间步长添加的噪声量。 |
必需 |
prediction_type
|
PredictionType
|
预测类型,可以是“data”或另一种类型。默认为“data”。 |
DATA
|
device
|
str
|
运行插值器的设备,可以是“cpu”或 CUDA 设备(例如“cuda:0”)。默认为“cpu”。 |
'cpu'
|
last_time_idx
|
int
|
离散时间的最后一个时间索引。如果离散时间为 T-1, ..., 0,则设置为 0;如果为 T, ..., 1,则设置为 1。默认为 0。 |
0
|
rng_generator
|
Optional[Generator]
|
可选的 :class: |
None
|
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
calculate_velocity(data, t, noise)
计算给定数据、时间步长和噪声的速度项。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
data
|
Tensor
|
输入数据。 |
必需 |
t
|
Tensor
|
当前时间步长。 |
必需 |
noise
|
Tensor
|
噪声项。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
Tensor |
Tensor
|
计算出的速度项。 |
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
forward_process(data, t, noise=None)
从噪声和数据中获取给定时间 t 的 x(t)。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
data
|
Tensor
|
目标 |
必需 |
t
|
Tensor
|
时间 |
必需 |
noise
|
Tensor
|
来自 prior() 的噪声。默认为 None。 |
None
|
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
199 200 201 202 203 204 205 206 207 208 209 |
|
interpolate(data, t, noise)
从噪声和数据中获取给定时间 t 的 x(t)。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
data
|
Tensor
|
目标 |
必需 |
t
|
Tensor
|
时间 |
必需 |
noise
|
Tensor
|
来自 prior() 的噪声 |
必需 |
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
loss(model_pred, target, t=None, mask=None, weight_type='ones')
计算给定模型预测、数据样本和时间的损失。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
model_pred
|
Tensor
|
模型预测的输出。 |
必需 |
目标
|
Tensor
|
目标 |
必需 |
t
|
Tensor
|
模型预测的目标输出。 |
None
|
t
|
计算损失的时间。
|
mask |
None
|
Optional[Tensor]
|
str
|
数据点的掩码。默认为 None。 |
weight_type
|
返回
名称 | 类型 | 描述 |
---|---|---|
Tensor |
用于损失的权重类型。默认为“ones”。 |
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
|
'ones'
计算出的损失批张量。
loss_weight(raw_loss, t, weight_type)
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
根据给定的权重类型计算损失的权重。
|
Tensor
|
这些 data_to_noise 损失权重来源于 https://arxiv.org/pdf/2202.00512 的公式 (9)。 |
必需 |
t
|
Tensor
|
raw_loss |
必需 |
Optional[Tensor]
|
str
|
从模型预测和目标计算的原始损失。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
Tensor |
Tensor
|
t |
时间步长。
类型 | 描述 |
---|---|
weight_type
|
要使用的权重类型。可以是“ones”、“data_to_noise”或“noise_to_data”。 |
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
|
返回
损失的权重。
Raises
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
ValueError
|
Tensor
|
如果权重类型无法识别。 |
必需 |
process_data_prediction(model_output, sample, t)
|
Tensor
|
根据预测类型将模型输出转换为数据预测。 |
必需 |
t
|
Tensor
|
raw_loss |
必需 |
返回
类型 | 描述 |
---|---|
此转换源于《扩散模型快速采样的渐进式蒸馏》https://arxiv.org/pdf/2202.00512。给定模型输出和样本,我们根据预测类型将输出转换为数据预测。转换公式如下: - 对于“noise”预测类型: |
时间步长。
类型 | 描述 |
---|---|
weight_type
|
model_output |
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
模型的输出。
sample
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
ValueError
|
如果权重类型无法识别。 |
必需 | |
process_data_prediction(model_output, sample, t)
|
根据预测类型将模型输出转换为数据预测。 |
必需 | |
t
|
raw_loss |
必需 |
返回
类型 | 描述 |
---|---|
输入样本。 |
时间步长。
类型 | 描述 |
---|---|
weight_type
|
返回 |
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
基于预测类型的数据预测。
Raises
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
ValueError
|
Tensor
|
如果预测类型不是“noise”、“data”或“v_prediction”之一。 |
必需 |
process_noise_prediction(model_output, sample, t)
|
Tensor
|
执行与 process_data_prediction 相同的操作,但获取模型输出并转换为噪声。 |
必需 |
t
|
Tensor
|
raw_loss |
必需 |
返回
类型 | 描述 |
---|---|
返回 |
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
|
如果预测类型为“noise”,则将输入作为噪声。
Raises
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
ValueError
|
如果预测类型不是“noise”。 |
必需 |
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
453 454 455 456 457 458 459 |
|
score(x_hat, xt, t)
将数据预测转换为估计的得分函数。
参数
x_hat
预测的数据点。
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
|
xt
当前数据点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
返回
|
Tensor
|
估计的得分函数。 |
必需 |
t
|
Tensor
|
set_loss_weight_fn(fn) |
必需 |
process_noise_prediction(model_output, sample, t)
|
Tensor
|
将实例的 loss_weight 属性设置为给定的函数。 |
必需 |
t
|
计算损失的时间。
|
参数 |
None
|
fn
|
要设置为 loss_weight 属性的函数。此函数应接受三个参数:raw_loss、t 和 weight_type。
|
step(model_out, t, xt, mask=None, center=False, temperature=1.0) |
0.0
|
执行一步积分。
|
Args: model_out (Tensor): 模型的输出。 t (Tensor): 当前时间步长。 xt (Tensor): 当前数据点。 mask (Optional[Tensor], optional): 应用于数据的可选掩码。默认为 None。 center (bool, optional): 是否居中数据。默认为 False。 temperature (Float, optional): 低温采样的温度参数。默认为 1.0。
|
注意:温度参数控制采样过程中随机性的程度。温度 1.0 对应于标准扩散采样,而较低的温度(例如 0.5、0.2)会导致更少的随机性和更具确定性的样本。这对于需要更多地控制生成过程的任务可能很有用。 |
注意,对于离散时间,我们从 [T-1, ..., 1, 0] 中采样 T 步,因此我们采样 t = 0,因此需要掩码。对于连续时间,我们从 [1, 1 -dt, ..., dt] 开始 T 步,其中 s = t - 1,当 t = 0 时,即 dt 为 0
|
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
step_ddim(model_out, t, xt, mask=None, eta=0.0, center=False)
将数据预测转换为估计的得分函数。
参数
x_hat
预测的数据点。
源代码位于 bionemo/moco/interpolants/discrete_time/continuous/ddpm.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|