Skip to main content

PyTorch convolutional layers with global context conditioning

Project description

ContextualConv

PyPI version CI Docs Coverage License: GPL v3

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  # version 0.4.0 on PyPI

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_dim is set.
  • Output dims:
    • out_channels → bias or scale
    • 2 × out_channels → FiLM (scale + bias)

🧪 Running tests

Run the full test suite with coverage:

pytest --cov=contextual_conv --cov-report=term-missing

📘 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contextual_conv-0.4.0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

contextual_conv-0.4.0-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file contextual_conv-0.4.0.tar.gz.

File metadata

  • Download URL: contextual_conv-0.4.0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for contextual_conv-0.4.0.tar.gz
Algorithm Hash digest
SHA256 0f7d49585014375f2e84a8a64660db95beccee63b1ed7c96e89fdb1e53beb99c
MD5 fc316d6751d9c18ff8f527117c611092
BLAKE2b-256 76ce9267a707a0a01944c3da60ba1c39628b9b28d7a9c739dba9d8598d84d842

See more details on using hashes here.

File details

Details for the file contextual_conv-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for contextual_conv-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b9e7c5941988845196b56e0e897c81e22758af04a37d4bde7479b7562616f47a
MD5 7b0a6a828cdc0d1d11bcf42aa57abb00
BLAKE2b-256 de0353476e2177e63ab1f4655c47a29b0887d7b237386a134e994cd86bc67d4d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page