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 modulated FiLM-style scaling.


🚀 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)
    scale_mode="film" # or "scale"
)

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 scale_mode Behaviour
False True Contextual bias only
True False "scale" Scale only: out * γ
True True "film" FiLM: out * (1 + γ) + β
True True "scale" Scale + shift: out * γ + β
False False Plain convolution (no modulation)

If context_dim is provided, at least one of use_scale or use_bias must be True.


🔧 Key features

  • ⚙️ Drop‑in replacement for nn.Conv1d / nn.Conv2d
    → Same arguments + optional context options.
  • 🧠 Global vector conditioning via learnable γ(c) and/or β(c)
  • 🔀 Modulation modes:
    • scale_mode="film": out * (1 + γ)
    • scale_mode="scale": out * γ
  • 🪶 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
  • 📤 Infer context vectors from unmodulated outputs with .infer_context()
  • Unit‑tested and documented

📦 Installation

pip install contextual-conv  # version 0.6.2 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)

🔎 Context inference

You can extract the context vector inferred from the output using:

context = conv.infer_context(x)

To also get the unmodulated output from the convolution layer:

context, raw_out = conv.infer_context(x, return_raw_output=True)

This is useful when you need both the input’s context and its original unmodulated features.


🧪 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.6.2.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.6.2-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: contextual_conv-0.6.2.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.6.2.tar.gz
Algorithm Hash digest
SHA256 a09d92f6f1b57efc83e4c72c2d5205c4262ec463e05f9eca9496a8f6a6769106
MD5 4cc5b2bf4a1ce3a6f65e19afaf15266c
BLAKE2b-256 f45872f9ae7c00cff67e84ae0eae87f1798a503e110fad30f236b49d608889cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for contextual_conv-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1f3a12272a0f17464de685a0ecc8d9660a2523f495c347d2a87d8b0cc2ed5e26
MD5 df3a84bfdafd1671af1e340a73f3df03
BLAKE2b-256 386437bd2d583a4bccc953ad2802bda98e2455c7ab67050f762fdd21c548c48d

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