D3pm
D3PM
Bases: Interpolant
离散去噪扩散概率模型 (D3PM) 插值器。
源代码位于 bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
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 |
|
__init__(time_distribution, prior_distribution, noise_schedule, device='cpu', last_time_idx=0, rng_generator=None)
初始化 D3PM 插值器。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
time_distribution
|
TimeDistribution
|
时间步长的分布,用于为扩散过程采样时间点。 |
必需 |
prior_distribution
|
PriorDistribution
|
变量的先验分布,用作扩散过程的起点。 |
必需 |
noise_schedule
|
DiscreteNoiseSchedule
|
噪声计划,定义在每个时间步添加的噪声量。 |
必需 |
device
|
str
|
运行插值器的设备,可以是 "cpu" 或 CUDA 设备(例如 "cuda:0")。默认为 "cpu"。 |
'cpu'
|
last_time_idx
|
int
|
在插值过程中要考虑的最后一个时间索引。默认为 0。 |
0
|
rng_generator
|
Optional[Generator]
|
用于可重复采样的可选 :class: |
None
|
源代码位于 bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
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 |
|
forward_process(data, t)
在时间 t 将前向过程应用于数据。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
data
|
Tensor
|
目标离散 ID |
必需 |
t
|
Tensor
|
时间 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
Tensor |
Tensor
|
应用前向过程后的 x(t) |
源代码位于 bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
196 197 198 199 200 201 202 203 204 205 206 |
|
interpolate(data, t)
使用离散插值方法进行插值。
此方法实现了 D3PM 论文(https://arxiv.org/pdf/2107.03006)中的公式 2,该公式计算了在给定输入数据和噪声的情况下,时间 t
处的插值离散状态 xt
,通过 q(xt|x0) = Cat(xt; p = x0*Qt_bar)。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
data
|
Tensor
|
要插值的输入数据。 |
必需 |
t
|
Tensor
|
要插值的时间步长。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
Tensor |
时间 |
源代码位于 bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
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 |
|
loss(logits, target, xt, time, mask=None, vb_scale=0.0)
计算模型预测和目标输出之间的交叉熵损失。
损失在批次 x 节点 x 类 logits 和目标批次 x 节点之间计算。如果提供了掩码,则仅针对非掩码元素计算损失。此外,如果 vb_scale 大于 0,则计算变分下界损失并将其添加到总损失中。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
logits
|
Tensor
|
来自模型的预测输出,形状为批次 x 节点 x 类。 |
必需 |
target
|
Tensor
|
模型预测的目标输出,形状为批次 x 节点。 |
必需 |
xt
|
Tensor
|
当前数据点。 |
必需 |
时间
|
Tensor
|
计算损失的时间。 |
必需 |
mask
|
Optional[Tensor]
|
数据点的掩码。默认为 None。 |
None
|
vb_scale
|
Float
|
变分下界损失的比例因子。默认为 0.0。 |
0.0
|
返回
名称 | 类型 | 描述 |
---|---|---|
Tensor |
计算出的损失张量。如果 aggregate 为 True,则损失和变分下界损失将被聚合,并且 |
|
作为单个张量返回。否则,损失和变分下界损失将作为单独的张量返回。 |
源代码位于 bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
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 |
|
step(model_out, t, xt, mask=None, temperature=1.0, model_out_is_logits=True)
在离散插值方法中执行单个步骤,从时间 t
的当前离散状态 xt
过渡到下一个状态。
此步骤涉及
- 使用模型输出
model_out
和时间t
的当前状态xt
计算预测的 q-后验 logits。 - 使用 Gumbel-Softmax 技巧从预测的 q-后验分布中采样下一个状态。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
model_out
|
Tensor
|
当前时间步的模型输出,用于计算预测的 q-后验 logits。 |
必需 |
t
|
Tensor
|
当前时间步,用于索引到过渡矩阵并计算预测的 q-后验 logits。 |
必需 |
xt
|
Tensor
|
时间 |
必需 |
mask
|
Optional[Tensor]
|
应用于下一个状态的可选掩码,可用于屏蔽某些标记或区域。默认为 None。 |
None
|
temperature
|
Float
|
用于 Gumbel-Softmax 技巧的温度,它控制采样过程的随机性。默认为 1.0。 |
1.0
|
model_out_is_logits
|
bool
|
一个标志,指示模型输出是否已采用 logits 形式。如果为 True,则假定输出为 logits;否则,将其转换为 logits。默认为 True。 |
True
|
返回
名称 | 类型 | 描述 |
---|---|---|
Tensor |
时间 |
源代码位于 bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
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 |
|