torchEBM 0.0.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.

Potentially problematic release.


This version of torchEBM might be problematic. Click here for more details.

@@ -0,0 +1,8 @@
1
+ Metadata-Version: 2.1
2
+ Name: torchEBM
3
+ Version: 0.0.0
4
+ Summary: Components and algorithms for energy-based models
5
+ Author-email: Soran Ghaderi <soran.gdr.cs@gmail.com>
6
+ Project-URL: homepage, https://github.com/soran-ghaderi/torchebm
7
+ Description-Content-Type: text/markdown
8
+
@@ -0,0 +1,16 @@
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=0QjokVIBzjS2yfYpwHBwfS1wACFaJSfCSCw0qVVgKsw,507
6
+ torchebm/core/trainer.py,sha256=xNnGrD2njbT3vv4U-2cX79hGXiCSIRdetdHn3geaYdc,1129
7
+ torchebm/cuda/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ torchebm/examples/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ torchebm/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
+ torchebm/models/base_model.py,sha256=pQ5uvnIjsyywaZe675spSBrsot8lnX46UtCdX1EIgOM,541
11
+ torchebm/samplers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
+ torchebm/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
+ torchEBM-0.0.0.dist-info/METADATA,sha256=htAvjNhM4qAEUCh-PbivlP7K_e_RMmmGIBKV5edCyDE,270
14
+ torchEBM-0.0.0.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
15
+ torchEBM-0.0.0.dist-info/top_level.txt,sha256=FTDHo7yycQ1rtcscAqb-fXXaoOVdtugPVNEUnm_FYHs,9
16
+ torchEBM-0.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.1.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -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
@@ -0,0 +1,13 @@
1
+ from abc import ABC, abstractmethod
2
+ import torch
3
+
4
+ from torchebm.core.energy_function import EnergyFunction
5
+
6
+
7
+ class Sampler(ABC):
8
+ @abstractmethod
9
+ def sample(self, energy_function: EnergyFunction, initial_state: torch.Tensor, num_steps: int) -> torch.Tensor:
10
+ pass
11
+
12
+ def cuda_sample(self, energy_function: EnergyFunction, initial_state: torch.Tensor, num_steps: int) -> torch.Tensor:
13
+ return self.sample(energy_function, initial_state, num_steps) # Default to PyTorch implementation
@@ -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
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