torchEBM 0.0.0__tar.gz
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.
- torchebm-0.0.0/PKG-INFO +7 -0
- torchebm-0.0.0/pyproject.toml +19 -0
- torchebm-0.0.0/setup.cfg +4 -0
- torchebm-0.0.0/torchEBM.egg-info/PKG-INFO +7 -0
- torchebm-0.0.0/torchEBM.egg-info/SOURCES.txt +17 -0
- torchebm-0.0.0/torchEBM.egg-info/dependency_links.txt +1 -0
- torchebm-0.0.0/torchEBM.egg-info/top_level.txt +1 -0
- torchebm-0.0.0/torchebm/__init__.py +0 -0
- torchebm-0.0.0/torchebm/core/__init__.py +0 -0
- torchebm-0.0.0/torchebm/core/energy_function.py +17 -0
- torchebm-0.0.0/torchebm/core/optimizer.py +0 -0
- torchebm-0.0.0/torchebm/core/sampler.py +13 -0
- torchebm-0.0.0/torchebm/core/trainer.py +29 -0
- torchebm-0.0.0/torchebm/cuda/__init__.py +0 -0
- torchebm-0.0.0/torchebm/examples/__init__.py +0 -0
- torchebm-0.0.0/torchebm/models/__init__.py +0 -0
- torchebm-0.0.0/torchebm/models/base_model.py +21 -0
- torchebm-0.0.0/torchebm/samplers/__init__.py +0 -0
- torchebm-0.0.0/torchebm/utils/__init__.py +0 -0
torchebm-0.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
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
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=42", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "torchEBM" # Replace with your package name
|
|
7
|
+
#version = "0.1.1"
|
|
8
|
+
dynamic = ["version"]
|
|
9
|
+
description = "Components and algorithms for energy-based models"
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
license = { file = "LICENSE" }
|
|
12
|
+
authors = [{ name="Soran Ghaderi", email="soran.gdr.cs@gmail.com" }]
|
|
13
|
+
dependencies = [
|
|
14
|
+
# List your dependencies here, e.g., "numpy", "torch"
|
|
15
|
+
]
|
|
16
|
+
[project.urls]
|
|
17
|
+
homepage = "https://github.com/soran-ghaderi/torchebm"
|
|
18
|
+
|
|
19
|
+
|
torchebm-0.0.0/setup.cfg
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
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
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
pyproject.toml
|
|
2
|
+
torchEBM.egg-info/PKG-INFO
|
|
3
|
+
torchEBM.egg-info/SOURCES.txt
|
|
4
|
+
torchEBM.egg-info/dependency_links.txt
|
|
5
|
+
torchEBM.egg-info/top_level.txt
|
|
6
|
+
torchebm/__init__.py
|
|
7
|
+
torchebm/core/__init__.py
|
|
8
|
+
torchebm/core/energy_function.py
|
|
9
|
+
torchebm/core/optimizer.py
|
|
10
|
+
torchebm/core/sampler.py
|
|
11
|
+
torchebm/core/trainer.py
|
|
12
|
+
torchebm/cuda/__init__.py
|
|
13
|
+
torchebm/examples/__init__.py
|
|
14
|
+
torchebm/models/__init__.py
|
|
15
|
+
torchebm/models/base_model.py
|
|
16
|
+
torchebm/samplers/__init__.py
|
|
17
|
+
torchebm/utils/__init__.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
torchebm
|
|
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
|