ContrastiveDivergence
Methods and Attributes¶
Bases: BaseContrastiveDivergence
Standard Contrastive Divergence (CD-k) loss.
CD approximates the log-likelihood gradient by running an MCMC sampler for k_steps to generate negative samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | The energy-based model to train. | required | |
sampler | The MCMC sampler for generating negative samples. | required | |
k_steps | The number of MCMC steps (k in CD-k). | 10 | |
persistent | If True, uses Persistent CD with a replay buffer. | False | |
buffer_size | Size of the replay buffer for PCD. | 10000 | |
init_steps | Number of MCMC steps to warm up the buffer. | 100 | |
new_sample_ratio | Fraction of new random samples for PCD chains. | 0.05 | |
energy_reg_weight | Weight for energy regularization term. | 0.001 | |
use_temperature_annealing | Whether to use temperature annealing. | False | |
min_temp | Minimum temperature for annealing. | 0.01 | |
max_temp | Maximum temperature for annealing. | 2.0 | |
temp_decay | Decay rate for temperature annealing. | 0.999 | |
dtype | Data type for computations. | float32 | |
device | Device for computations. | device('cpu') |
Example
Source code in torchebm/losses/contrastive_divergence.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 | |
use_temperature_annealing instance-attribute ¶
forward ¶
Computes the Contrastive Divergence loss and generates negative samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Tensor | A batch of real data samples (positive samples). | required |
*args | Additional positional arguments. | () | |
**kwargs | Additional keyword arguments. | {} |
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor] | Tuple[torch.Tensor, torch.Tensor]: - The scalar CD loss value. - The generated negative samples. |
Source code in torchebm/losses/contrastive_divergence.py
compute_loss ¶
Computes the Contrastive Divergence loss from positive and negative samples.
The loss is the difference between the mean energy of positive samples and the mean energy of 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 scalar loss value. |