BaseContrastiveDivergence
Methods and Attributes¶
Bases: BaseLoss
Abstract base class for Contrastive Divergence (CD) based loss functions.
Contrastive Divergence is a family of methods for training energy-based models that approximate the gradient of the log-likelihood by comparing the energy between real data samples (positive phase) and model samples (negative phase) generated through MCMC sampling.
This class provides the common structure for CD variants, including standard CD, Persistent CD (PCD), and others.
Source code in torchebm/core/base_loss.py
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 |
|
forward
abstractmethod
¶
Compute CD loss given real data samples.
This method should implement the specifics of the contrastive divergence variant, typically: 1. Generate negative samples using the MCMC sampler 2. Compute energies for real and negative samples 3. Calculate the contrastive loss
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Real data samples (positive samples). |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor]
|
Tuple[torch.Tensor, torch.Tensor]: - loss: The contrastive divergence loss - pred_x: Generated negative samples |
Source code in torchebm/core/base_loss.py
initialize_persistent_chain
¶
Initialize the persistent chain with random noise.
For persistent CD variants, this method initializes the persistent chain buffer with random noise. This is typically called the first time the loss is computed or when the batch size changes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shape
|
Tuple[int, ...]
|
Shape of the initial chain state. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The initialized chain. |
Source code in torchebm/core/base_loss.py
compute_loss
abstractmethod
¶
Compute the contrastive divergence loss from positive and negative samples.
This method defines how the loss is calculated given real samples (positive phase) and samples from the model (negative phase). Typical implementations compute the difference between mean energies of positive and negative samples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Real data samples (positive samples). |
required |
pred_x
|
Tensor
|
Generated negative samples. |
required |
*args
|
Additional positional arguments. |
()
|
|
**kwargs
|
Additional keyword arguments. |
{}
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The contrastive divergence loss |
Source code in torchebm/core/base_loss.py
to
¶
Move loss to specified device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
Union[str, device]
|
Target device for computations. |
required |
Returns:
Type | Description |
---|---|
BaseContrastiveDivergence
|
The loss function instance moved to the specified device. |