torchebm.utils ¶
Utility functions for TorchEBM.
center_crop_arr(pil_image, image_size) ¶
Center crop and resize image to target size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pil_image | Image | PIL image to crop. | required |
image_size | int | Target size for square crop. | required |
Returns:
| Type | Description |
|---|---|
Image | Center-cropped PIL image. |
Source code in torchebm/utils/image.py
create_npz_from_sample_folder(sample_dir, num=50000) ¶
Build .npz file from folder of PNG samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_dir | str | Directory containing numbered PNG files. | required |
num | int | Number of samples to include. | 50000 |
Returns:
| Type | Description |
|---|---|
str | Path to created .npz file. |
Source code in torchebm/utils/image.py
load_checkpoint(checkpoint_path, model, ema_model=None, optimizer=None, device=None) ¶
Load training checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint_path | str | Path to checkpoint file. | required |
model | Module | Model to load weights into. | required |
ema_model | Optional[Module] | EMA model to load (optional). | None |
optimizer | Optional[Optimizer] | Optimizer to load state (optional). | None |
device | Optional[device] | Device to map tensors to. | None |
Returns:
| Type | Description |
|---|---|
Dict[str, Any] | Dictionary with checkpoint contents. |
Source code in torchebm/utils/training.py
plot_2d_energy_landscape(model, x_range=(-5, 5), y_range=(-5, 5), resolution=100, log_scale=False, cmap='viridis', title=None, show_colorbar=True, save_path=None, fig_size=(8, 6), contour=True, contour_levels=20, device=None) ¶
Plots a 2D energy landscape of a model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | BaseModel | The model to visualize. | required |
x_range | Tuple[float, float] | The range for the x-axis. | (-5, 5) |
y_range | Tuple[float, float] | The range for the y-axis. | (-5, 5) |
resolution | int | The number of points in each dimension. | 100 |
log_scale | bool | Whether to use a log scale for the energy values. | False |
cmap | str | The colormap to use. | 'viridis' |
title | Optional[str] | The title of the plot. | None |
show_colorbar | bool | Whether to show a colorbar. | True |
save_path | Optional[str] | The path to save the figure. | None |
fig_size | Tuple[int, int] | The size of the figure. | (8, 6) |
contour | bool | Whether to overlay contour lines. | True |
contour_levels | int | The number of contour levels. | 20 |
device | Optional[str] | The device to use for computation. | None |
Returns:
| Type | Description |
|---|---|
Figure | plt.Figure: The matplotlib figure object. |
Source code in torchebm/utils/visualization.py
plot_3d_energy_landscape(model, x_range=(-5, 5), y_range=(-5, 5), resolution=50, log_scale=False, cmap='viridis', title=None, show_colorbar=True, save_path=None, fig_size=(10, 8), alpha=0.9, elev=30, azim=-45, device=None) ¶
Plots a 3D surface visualization of a 2D energy landscape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | BaseModel | The model to visualize. | required |
x_range | Tuple[float, float] | The range for the x-axis. | (-5, 5) |
y_range | Tuple[float, float] | The range for the y-axis. | (-5, 5) |
resolution | int | The number of points in each dimension. | 50 |
log_scale | bool | Whether to use a log scale for the energy values. | False |
cmap | str | The colormap to use. | 'viridis' |
title | Optional[str] | The title of the plot. | None |
show_colorbar | bool | Whether to show a colorbar. | True |
save_path | Optional[str] | The path to save the figure. | None |
fig_size | Tuple[int, int] | The size of the figure. | (10, 8) |
alpha | float | The transparency of the surface. | 0.9 |
elev | float | The elevation angle for the 3D view. | 30 |
azim | float | The azimuth angle for the 3D view. | -45 |
device | Optional[str] | The device to use for computation. | None |
Returns:
| Type | Description |
|---|---|
Figure | plt.Figure: The matplotlib figure object. |
Source code in torchebm/utils/visualization.py
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 | |
plot_sample_trajectories(trajectories, model=None, x_range=None, y_range=None, resolution=100, log_scale=False, cmap='viridis', title=None, show_colorbar=True, save_path=None, fig_size=(8, 6), trajectory_colors=None, trajectory_alpha=0.7, line_width=1.0, device=None) ¶
Plots sample trajectories, optionally on an energy landscape background.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trajectories | Tensor | A tensor of trajectories of shape | required |
model | Optional[BaseModel] | The model to visualize as a background. | None |
x_range | Optional[Tuple[float, float]] | The range for the x-axis. If | None |
y_range | Optional[Tuple[float, float]] | The range for the y-axis. If | None |
resolution | int | The number of points in each dimension for the energy grid. | 100 |
log_scale | bool | Whether to use a log scale for the energy values. | False |
cmap | str | The colormap to use for the energy background. | 'viridis' |
title | Optional[str] | The title of the plot. | None |
show_colorbar | bool | Whether to show a colorbar. | True |
save_path | Optional[str] | The path to save the figure. | None |
fig_size | Tuple[int, int] | The size of the figure. | (8, 6) |
trajectory_colors | Optional[List[str]] | A list of colors for the trajectories. | None |
trajectory_alpha | float | The transparency of the trajectory lines. | 0.7 |
line_width | float | The width of the trajectory lines. | 1.0 |
device | Optional[str] | The device to use for computation. | None |
Returns:
| Type | Description |
|---|---|
Figure | plt.Figure: The matplotlib figure object. |
Source code in torchebm/utils/visualization.py
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 | |
plot_samples_on_energy(model, samples, x_range=(-5, 5), y_range=(-5, 5), resolution=100, log_scale=False, cmap='viridis', title=None, show_colorbar=True, save_path=None, fig_size=(8, 6), contour=True, contour_levels=20, sample_color='red', sample_alpha=0.5, sample_size=5, device=None) ¶
Plots samples on a 2D energy landscape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | BaseModel | The model to visualize. | required |
samples | Tensor | A tensor of samples of shape | required |
x_range | Tuple[float, float] | The range for the x-axis. | (-5, 5) |
y_range | Tuple[float, float] | The range for the y-axis. | (-5, 5) |
resolution | int | The number of points in each dimension. | 100 |
log_scale | bool | Whether to use a log scale for the energy values. | False |
cmap | str | The colormap to use. | 'viridis' |
title | Optional[str] | The title of the plot. | None |
show_colorbar | bool | Whether to show a colorbar. | True |
save_path | Optional[str] | The path to save the figure. | None |
fig_size | Tuple[int, int] | The size of the figure. | (8, 6) |
contour | bool | Whether to overlay contour lines. | True |
contour_levels | int | The number of contour levels. | 20 |
sample_color | str | The color of the samples. | 'red' |
sample_alpha | float | The transparency of the samples. | 0.5 |
sample_size | float | The size of the sample markers. | 5 |
device | Optional[str] | The device to use for computation. | None |
Returns:
| Type | Description |
|---|---|
Figure | plt.Figure: The matplotlib figure object. |
Source code in torchebm/utils/visualization.py
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 | |
requires_grad(model, flag=True) ¶
Set requires_grad flag for all model parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | Module | Model to modify. | required |
flag | bool | Whether parameters require gradients. | True |
Source code in torchebm/utils/training.py
save_checkpoint(model, optimizer, step, checkpoint_dir, ema_model=None, args=None) ¶
Save training checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | Module | Model to save. | required |
optimizer | Optimizer | Optimizer state. | required |
step | int | Current training step. | required |
checkpoint_dir | str | Directory for checkpoints. | required |
ema_model | Optional[Module] | EMA model (optional). | None |
args | Optional[Dict[str, Any]] | Additional arguments to save. | None |
Returns:
| Type | Description |
|---|---|
str | Path to saved checkpoint. |
Source code in torchebm/utils/training.py
update_ema(ema_model, model, decay=0.9999) ¶
Update EMA model parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ema_model | Module | Exponential moving average model. | required |
model | Module | Current model. | required |
decay | float | EMA decay rate. | 0.9999 |