• Stars
    star
    337
  • Rank 125,272 (Top 3 %)
  • Language
    Python
  • License
    Other
  • Created about 2 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PyTensor allows you to define, optimize, and efficiently evaluate mathematical expressions involving multi-dimensional arrays.

PyTensor logo

Tests Status Coverage

PyTensor is a fork of Aesara -- a Python library that allows one to define, optimize, and efficiently evaluate mathematical expressions involving multi-dimensional arrays.

Features

  • A hackable, pure-Python codebase
  • Extensible graph framework suitable for rapid development of custom operators and symbolic optimizations
  • Implements an extensible graph transpilation framework that currently provides compilation via C, JAX, and Numba
  • Based on one of the most widely-used Python tensor libraries: Theano

Getting started

import pytensor
from pytensor import tensor as pt

# Declare two symbolic floating-point scalars
a = pt.dscalar("a")
b = pt.dscalar("b")

# Create a simple example expression
c = a + b

# Convert the expression into a callable object that takes `(a, b)`
# values as input and computes the value of `c`.
f_c = pytensor.function([a, b], c)

assert f_c(1.5, 2.5) == 4.0

# Compute the gradient of the example expression with respect to `a`
dc = pytensor.grad(c, a)

f_dc = pytensor.function([a, b], dc)

assert f_dc(1.5, 2.5) == 1.0

# Compiling functions with `pytensor.function` also optimizes
# expression graphs by removing unnecessary operations and
# replacing computations with more efficient ones.

v = pt.vector("v")
M = pt.matrix("M")

d = a/a + (M + a).dot(v)

pytensor.dprint(d)
#  Add [id A]
#  β”œβ”€ ExpandDims{axis=0} [id B]
#  β”‚  └─ True_div [id C]
#  β”‚     β”œβ”€ a [id D]
#  β”‚     └─ a [id D]
#  └─ dot [id E]
#     β”œβ”€ Add [id F]
#     β”‚  β”œβ”€ M [id G]
#     β”‚  └─ ExpandDims{axes=[0, 1]} [id H]
#     β”‚     └─ a [id D]
#     └─ v [id I]

f_d = pytensor.function([a, v, M], d)

# `a/a` -> `1` and the dot product is replaced with a BLAS function
# (i.e. CGemv)
pytensor.dprint(f_d)
# Add [id A] 5
#  β”œβ”€ [1.] [id B]
#  └─ CGemv{inplace} [id C] 4
#     β”œβ”€ AllocEmpty{dtype='float64'} [id D] 3
#     β”‚  └─ Shape_i{0} [id E] 2
#     β”‚     └─ M [id F]
#     β”œβ”€ 1.0 [id G]
#     β”œβ”€ Add [id H] 1
#     β”‚  β”œβ”€ M [id F]
#     β”‚  └─ ExpandDims{axes=[0, 1]} [id I] 0
#     β”‚     └─ a [id J]
#     β”œβ”€ v [id K]
#     └─ 0.0 [id L]

See the PyTensor documentation for in-depth tutorials.

Installation

The latest release of PyTensor can be installed from PyPI using pip:

pip install pytensor

Or via conda-forge:

conda install -c conda-forge pytensor

The current development branch of PyTensor can be installed from GitHub, also using pip:

pip install git+https://github.com/pymc-devs/pytensor

Contributing

We welcome bug reports and fixes and improvements to the documentation.

For more information on contributing, please see the contributing guide.

A good place to start contributing is by looking through the issues here.

More Repositories

1

pymc

Bayesian Modeling and Probabilistic Programming in Python
Python
8,646
star
2

pymc-resources

PyMC educational resources
Jupyter Notebook
1,938
star
3

pymc2

THIS IS THE **OLD** PYMC PROJECT (VERSION 2). PLEASE USE PYMC INSTEAD:
Fortran
879
star
4

pymc4

Experimental PyMC interface for TensorFlow Probability. Official work on this project has been discontinued.
Jupyter Notebook
713
star
5

pymc-examples

Examples of PyMC models, including a library of Jupyter notebooks.
Python
285
star
6

nutpie

Python wrapper for nuts-rs
Jupyter Notebook
107
star
7

sunode

Solve ODEs fast, with support for PyMC
Jupyter Notebook
105
star
8

pymc-bart

Python
87
star
9

pymc-experimental

Jupyter Notebook
77
star
10

symbolic-pymc

Tools for the symbolic manipulation of PyMC models, Theano, and TensorFlow graphs.
Python
61
star
11

nuts-rs

A implementation of NUTS in rust
Rust
56
star
12

uq_chapter

Uncertainty quantification book chapter
CSS
49
star
13

pymc4_prototypes

Experimental code for porting PyMC to alternative backends
Jupyter Notebook
26
star
14

pymc-data-umbrella

Website: Data Umbrella & PyMC open source sessions
Jupyter Notebook
26
star
15

mcbackend

A backend for storing MCMC draws.
Python
14
star
16

pymc.io

PyMC project website and blog!
Jupyter Notebook
14
star
17

video-timestamps

Crowd sourced timestamps for PyMC Youtube videos
7
star
18

pymcon

Website for PyMCon
HTML
5
star
19

pymcon_web_series_website

HTML
5
star
20

pymc_workflow_analyzer

Python
3
star
21

pymc3-experimental

PyMC3 experimental features not ready to be included in PyMC3 (yet)
Python
3
star
22

brand

Branding resources, logos
2
star
23

paper_v5

Jupyter Notebook
2
star
24

pymc-doc

JavaScript
2
star
25

PyMC.tmbundle

TextMate bundle for PyMC
1
star
26

communication

1
star
27

design-notes

1
star
28

pymc-sphinx-theme

A thin sphinx theme to customize pydata-sphinx-theme consistently cross PyMC websites.
HTML
1
star