Skip to main content

A light-weight PyTorch companion for building stochastic surrogate models

Project description

pypolymix logo

Docs Code style
Docs Code style: black

Pypolymix

pypolymix is a light-weight PyTorch companion for building stochastic surrogate models.

Documentation

Project documentation lives at https://sandialabs.github.io/pypolymix.

Installation

From PyPI:

python -m pip install pypolymix

Optional extras:

python -m pip install "pypolymix[examples]"
python -m pip install "pypolymix[docs]"
python -m pip install "pypolymix[dev]"

For local development:

python -m pip install -e ".[dev,docs,examples]"

Quickstart

Fit a 1D polynomial chaos model with a mix of stochastic and deterministic coefficients:

import torch

from pypolymix.parameter_groups import DeterministicGroup, IIDGaussianGroup
from pypolymix.surrogate_models import PolynomialChaosExpansion
from pypolymix import StochasticModel

torch.manual_seed(0)
x = torch.linspace(-1, 1, 200).unsqueeze(-1)
y = torch.sin(3 * x) + 0.1 * torch.randn_like(x)

surrogate_model = PolynomialChaosExpansion(num_inputs=1, num_outputs=1, degree=5)
num_params = surrogate_model.num_params()

parameter_groups = [
    IIDGaussianGroup("stochastic", 2),
    DeterministicGroup("deterministic", num_params - 2),
]

model = StochasticModel(surrogate_model=surrogate_model, parameter_groups=parameter_groups)

optimizer = torch.optim.Adam(model.parameters(), lr=5e-3)
for _ in range(500):
    optimizer.zero_grad()
    preds = model(x, num_samples=16).mean(dim=0)
    data_loss = torch.mean((preds - y) ** 2)
    loss = data_loss + 1e-3 * model.distribution_loss()
    loss.backward()
    optimizer.step()

with torch.no_grad():
    samples = model(x, num_samples=100)
    mean = samples.mean(dim=0)
    std = samples.std(dim=0)

Polynomial chaos example

Development

If you use Poetry:

poetry install --with dev,docs,examples

Install the pre-commit hook:

pre-commit install

Examples

Notebooks live in docs/examples/ and are rendered on the documentation site.

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

pypolymix-0.1.5.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

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

pypolymix-0.1.5-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

Details for the file pypolymix-0.1.5.tar.gz.

File metadata

  • Download URL: pypolymix-0.1.5.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pypolymix-0.1.5.tar.gz
Algorithm Hash digest
SHA256 2ea3bb95e2f397f6a1af3e8bb6d0d36c7f0315f56dd7485c010373ba7cd5028a
MD5 73e9ed68c7b3800e7b07ffbb6470b82a
BLAKE2b-256 b3ae2fee13c57a7cc8a090b3f5ed8a1c85b7689e8bd7d49872e2c1e55420c825

See more details on using hashes here.

File details

Details for the file pypolymix-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: pypolymix-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 19.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pypolymix-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d0a0f9f336c7e12d8be1b9bd43033709e210a82c6ada2f1ff93a067b44bc45ae
MD5 8fbceac2bf3402bf905558aeaba69f9a
BLAKE2b-256 7223d25b669a14c0d67fbe8fa983d6f0a3bd00335d41ba37199e798acb4c1aab

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