PyTorch convolutional layers with global context conditioning
Project description
ContextualConv
ContextualConv – PyTorch convolutional layers with global context conditioning: per‑channel bias, scale, or FiLM‑style scale + bias.
🚀 Quick start
from contextual_conv import ContextualConv2d
import torch
# FiLM‑style (scale + bias)
conv = ContextualConv2d(
in_channels=16,
out_channels=32,
kernel_size=3,
padding=1,
context_dim=10, # size of global vector c
h_dim=64, # optional MLP hidden dim
use_scale=True, # γ(c)
use_bias=True # β(c)
)
x = torch.randn(8, 16, 32, 32) # feature map
c = torch.randn(8, 10) # context vector
out = conv(x, c) # shape: (8, 32, 32, 32)
Modes at a glance
use_scale |
use_bias |
Behaviour |
|---|---|---|
False |
True |
Contextual bias (original behaviour) |
True |
False |
Per‑channel scale only |
True |
True |
FiLM – scale and bias |
If both flags are False, the constructor raises ValueError.
🔧 Key features
- ⚙️ Drop‑in replacement for
nn.Conv1d/nn.Conv2d
→ Same arguments + optional context options. - 🧠 Global vector conditioning via learnable γ(c) and/or β(c).
- 🪶 Lightweight – one small MLP (or single
Linear) per layer. - 🧑🔬 FiLM ready – reproduce Feature‑wise Linear Modulation with two lines.
- 🧩 Modular – combine with any architecture, works on CPU / GPU.
- ✅ Unit‑tested and documented.
📦 Installation
pip install contextual-conv # coming soon: v0.3.0
Or install from source:
git clone https://github.com/abbassix/ContextualConv.git
cd ContextualConv
pip install -e .[dev]
📐 Context vector details
- Shape:
(B, context_dim)
(one global descriptor per sample – class label embedding, latent code, etc.) - Processed by a
ContextProcessor:Linear(context_dim, out_dim)(bias‑only / scale‑only)- Small MLP if
h_dimis set.
- Output dims:
out_channels→ bias or scale2 × out_channels→ FiLM (scale + bias)
🧪 Running tests
pytest tests/
📘 Documentation
Full API reference & tutorials: https://contextualconv.readthedocs.io
🤝 Contributing
Bug reports, feature requests, and PRs are welcome! See CONTRIBUTING.md.
📄 License
GNU GPLv3 – see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file contextual_conv-0.3.0.tar.gz.
File metadata
- Download URL: contextual_conv-0.3.0.tar.gz
- Upload date:
- Size: 18.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f0d40ff03884e37ab79a7a8e92a6150e1493ebab5539b048688421d76a562de
|
|
| MD5 |
7d384396c81896f8bf6c32160424227e
|
|
| BLAKE2b-256 |
e04cd057666c2cd9a84f40871294bb06b1437cc7a94d859ea1e25e12a27943a6
|
File details
Details for the file contextual_conv-0.3.0-py3-none-any.whl.
File metadata
- Download URL: contextual_conv-0.3.0-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76a8771213184e8ff3df3ea34d410faebc058b1344af70971c956eabb4a24026
|
|
| MD5 |
6a109649df76a356fa209b408d890642
|
|
| BLAKE2b-256 |
88b23c7df144a7305b19125ccb7aab8d9e07b42fc06d41adc9d09c805515cd53
|