torchEBM 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- torchEBM-0.1.0.dist-info/METADATA +7 -0
- torchEBM-0.1.0.dist-info/RECORD +15 -0
- torchEBM-0.1.0.dist-info/WHEEL +5 -0
- torchEBM-0.1.0.dist-info/top_level.txt +1 -0
- torchebm/__init__.py +0 -0
- torchebm/core/__init__.py +0 -0
- torchebm/core/energy_function.py +17 -0
- torchebm/core/optimizer.py +0 -0
- torchebm/core/sampler.py +10 -0
- torchebm/core/trainer.py +29 -0
- torchebm/cuda/__init__.py +0 -0
- torchebm/models/__init__.py +0 -0
- torchebm/models/base_model.py +21 -0
- torchebm/samplers/__init__.py +0 -0
- torchebm/utils/__init__.py +0 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
torchebm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
torchebm/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
torchebm/core/energy_function.py,sha256=sozA5nIV6Li3mzbBSsWgzqSu-Pf1aqijhPhDdn8YqGE,481
|
|
4
|
+
torchebm/core/optimizer.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
torchebm/core/sampler.py,sha256=K_qpvbX5nInIhoV5oAfjE-xVXZf48hsz-BYrYHd2X3g,448
|
|
6
|
+
torchebm/core/trainer.py,sha256=xNnGrD2njbT3vv4U-2cX79hGXiCSIRdetdHn3geaYdc,1129
|
|
7
|
+
torchebm/cuda/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
torchebm/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
torchebm/models/base_model.py,sha256=pQ5uvnIjsyywaZe675spSBrsot8lnX46UtCdX1EIgOM,541
|
|
10
|
+
torchebm/samplers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
torchebm/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
torchEBM-0.1.0.dist-info/METADATA,sha256=ADUxPZ_uauBOP3X7QDc93NTwHGqWPdJtYFsHnvnQE_U,205
|
|
13
|
+
torchEBM-0.1.0.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
|
|
14
|
+
torchEBM-0.1.0.dist-info/top_level.txt,sha256=FTDHo7yycQ1rtcscAqb-fXXaoOVdtugPVNEUnm_FYHs,9
|
|
15
|
+
torchEBM-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
torchebm
|
torchebm/__init__.py
ADDED
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
import torch
|
|
3
|
+
|
|
4
|
+
class EnergyFunction(ABC):
|
|
5
|
+
@abstractmethod
|
|
6
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
@abstractmethod
|
|
10
|
+
def gradient(self, x: torch.Tensor) -> torch.Tensor:
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
def cuda_forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
14
|
+
return self.forward(x) # Default to PyTorch implementation
|
|
15
|
+
|
|
16
|
+
def cuda_gradient(self, x: torch.Tensor) -> torch.Tensor:
|
|
17
|
+
return self.gradient(x)
|
|
File without changes
|
torchebm/core/sampler.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
import torch
|
|
3
|
+
|
|
4
|
+
class Sampler(ABC):
|
|
5
|
+
@abstractmethod
|
|
6
|
+
def sample(self, energy_function: EnergyFunction, initial_state: torch.Tensor, num_steps: int) -> torch.Tensor:
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
def cuda_sample(self, energy_function: EnergyFunction, initial_state: torch.Tensor, num_steps: int) -> torch.Tensor:
|
|
10
|
+
return self.sample(energy_function, initial_state, num_steps) # Default to PyTorch implementation
|
torchebm/core/trainer.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .energy_function import EnergyFunction
|
|
3
|
+
from .sampler import Sampler
|
|
4
|
+
|
|
5
|
+
class ContrastiveDivergenceTrainer:
|
|
6
|
+
def __init__(self, energy_function: EnergyFunction, sampler: Sampler, learning_rate: float = 0.01):
|
|
7
|
+
self.energy_function = energy_function
|
|
8
|
+
self.sampler = sampler
|
|
9
|
+
self.optimizer = torch.optim.Adam(self.energy_function.parameters(), lr=learning_rate)
|
|
10
|
+
|
|
11
|
+
def train_step(self, real_data: torch.Tensor) -> dict:
|
|
12
|
+
self.optimizer.zero_grad()
|
|
13
|
+
|
|
14
|
+
# Positive phase
|
|
15
|
+
positive_energy = self.energy_function(real_data)
|
|
16
|
+
|
|
17
|
+
# Negative phase
|
|
18
|
+
initial_samples = torch.randn_like(real_data)
|
|
19
|
+
negative_samples = self.sampler.sample(self.energy_function, initial_samples, num_steps=10)
|
|
20
|
+
negative_energy = self.energy_function(negative_samples)
|
|
21
|
+
|
|
22
|
+
# Compute loss
|
|
23
|
+
loss = positive_energy.mean() - negative_energy.mean()
|
|
24
|
+
|
|
25
|
+
# Backpropagation
|
|
26
|
+
loss.backward()
|
|
27
|
+
self.optimizer.step()
|
|
28
|
+
|
|
29
|
+
return {"loss": loss.item(), "positive_energy": positive_energy.mean().item(), "negative_energy": negative_energy.mean().item()}
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
import torch
|
|
3
|
+
from ..core import EnergyFunction, Sampler
|
|
4
|
+
|
|
5
|
+
class BaseModel(ABC):
|
|
6
|
+
def __init__(self, energy_function: EnergyFunction, sampler: Sampler):
|
|
7
|
+
self.energy_function = energy_function
|
|
8
|
+
self.sampler = sampler
|
|
9
|
+
|
|
10
|
+
@abstractmethod
|
|
11
|
+
def energy(self, x: torch.Tensor) -> torch.Tensor:
|
|
12
|
+
pass
|
|
13
|
+
|
|
14
|
+
@abstractmethod
|
|
15
|
+
def sample(self, num_samples: int) -> torch.Tensor:
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def train_step(self, real_data: torch.Tensor) -> dict:
|
|
20
|
+
pass
|
|
21
|
+
|
|
File without changes
|
|
File without changes
|