cs-cmyk/energy-constraint-networks
0
Energy-Based Constraint Networks
Pretrained weights for "Energy-Based Constraint Networks: Learning Structural Coherence Across Modalities"
Checkpoints
Text Domain (frozen BERT-base-uncased encoder)
Vision Domain (frozen DINOv2 ViT-B/14 encoder)
Combined Vision Results (structural + frequency + local texture)
Usage
Text
import torch
import torch.nn as nn
from model import ConstraintNetwork
from bert_encoder import BERTWindowEncoder
# Load encoder and model
encoder = BERTWindowEncoder("bert-base-uncased", "cuda", layer=-2, window_size=8)
model = ConstraintNetwork(d_model=384, d_state=64, vocab_size=None,
max_seq_len=32, dropout=0.15, alpha=0.3).cuda()
model.input_proj = nn.Linear(768, 384).cuda()
model.load_state_dict(torch.load("nl_bert_constraint_best.pt", map_location="cuda"))
model.eval()
# Evaluate a paragraph
text = "Marie Curie was born in Warsaw. She studied physics in Paris."
embs = encoder.encode_paragraph(text, max_windows=32).cuda()
if embs.shape[0] < 32:
pad = torch.zeros(32 - embs.shape[0], 768, device="cuda")
embs = torch.cat([embs, pad])
embs = embs.unsqueeze(0)
with torch.no_grad():
energy = model(embs)
print(f"Energy: {energy.item():+.4f}") # lower = more coherentVision (three-branch evaluation)
python eval_combined_final.py \
--struct_checkpoint pretrained_paired_best.pt \
--freq_checkpoint freq_only_best.pt \
--local_checkpoint local_only_best.pt \
--real_dir ff_c23_faces/original \
--fake_dirs ff_c23_faces/Deepfakes ff_c23_faces/Face2Face \
ff_c23_faces/FaceSwap ff_c23_faces/NeuralTextures \
ff_c23_faces/FaceShifter \
--max_images 5000Training from scratch
# Text: train on WikiText-103 (embeddings cached after first run)
python train_bert.py
# Vision: train three branches independently
python train_paired.py --real_dir ... --fake_dirs ... --pretrained vision_constraint_celeb.pt
python train_freq_only.py --real_dir ... --fake_dirs ...
python train_local_only.py --real_dir ... --fake_dirs ...Architecture
Input → SSM (×6) → Dual-head attention (×2) → Energy head → E(x)
linear causal + bidirectional mean + α·max
cost single W per head
no Q/K/V, no KV cache- Same architecture for both text and vision — only the input projection layer changes
- Each forward pass is stateless (no KV cache)
- Per-position energy decomposition enables violation localization
- α = 0.3 (insensitive in [0.2, 1.0] across both modalities)
Requirements
- Python 3.10+
- PyTorch 2.0+
- CUDA GPU
transformers(BERT, DINOv2)datasets(WikiText-103)mamba-ssm+causal-conv1d
Citation
@article{shinde2026energy,
title={Energy-Based Constraint Networks: Learning Structural Coherence Across Modalities},
author={Shinde, Chirag},
year={2026},
url={https://github.com/cs-cmyk/energy-constraint-networks}
}
