跳转到主要内容

优化编译器,用于在CPU和GPU上评估数学表达式。

项目描述

PyTensor logo

Tests Status Coverage

PyTensor 是一个 Python 库,允许用户定义、优化和高效评估涉及多维数组的数学表达式。它为 PyMC 提供计算后端。

功能

  • 可修改的纯 Python 代码库

  • 可扩展的图框架,适合快速开发自定义操作和符号优化

  • 实现了一个可扩展的图编译框架,目前支持通过 C、JAXNumba 编译

  • 与 PyTorch 和 TensorFlow 不同,PyTensor 保持一个静态图,可以就地修改以允许进行高级优化

入门

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]

请参阅 PyTensor 文档 以获取深入教程。

安装

您可以使用 pip 从 PyPI 安装 PyTensor 的最新版本

pip install pytensor

或通过 conda-forge

conda install -c conda-forge pytensor

可以从 GitHub 安装 PyTensor 的当前开发分支,也使用 pip

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

背景

PyTensor 是从 Aesara 分支出来的,Aesara 是从 Theano 分支出来的。

贡献

我们欢迎错误报告、修复和改进文档。

有关贡献的更多信息,请参阅 贡献指南

开始贡献的好方法是通过查看 这里 的问题。

项目详情


下载文件

下载适合您平台的文件。如果您不确定要选择哪个,请了解有关 安装包 的更多信息。

源代码分发

pytensor-2.25.5.tar.gz (3.6 MB 查看哈希值)

上传时间

构建分发

pytensor-2.25.5-py2.py3-none-any.whl (1.2 MB 查看哈希值)

上传于 Python 2 Python 3

pytensor-2.25.5-cp312-cp312-win_amd64.whl (1.5 MB 查看哈希值)

上传于 CPython 3.12 Windows x86-64

pytensor-2.25.5-cp312-cp312-musllinux_1_2_x86_64.whl (2.0 MB 查看哈希值)

上传于 CPython 3.12 musllinux: musl 1.2+ x86-64

pytensor-2.25.5-cp312-cp312-musllinux_1_2_i686.whl (2.0 MB 查看哈希值)

上传于 CPython 3.12 musllinux: musl 1.2+ i686

pytensor-2.25.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB 查看哈希值)

上传于 CPython 3.12 manylinux: glibc 2.17+ x86-64

pytensor-2.25.5-cp312-cp312-macosx_10_13_x86_64.whl (1.5 MB 查看哈希值)

上传于 CPython 3.12 macOS 10.13+ x86-64

pytensor-2.25.5-cp311-cp311-win_amd64.whl (1.5 MB 查看哈希值)

上传于 CPython 3.11 Windows x86-64

pytensor-2.25.5-cp311-cp311-musllinux_1_2_x86_64.whl (2.0 MB 查看哈希值)

上传于 CPython 3.11 musllinux: musl 1.2+ x86-64

pytensor-2.25.5-cp311-cp311-musllinux_1_2_i686.whl (2.0 MB 查看哈希值)

上传于 CPython 3.11 musllinux: musl 1.2+ i686

pytensor-2.25.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB 查看哈希值)

上传于 CPython 3.11 manylinux: glibc 2.17+ x86-64

pytensor-2.25.5-cp311-cp311-macosx_10_9_x86_64.whl (1.5 MB 查看哈希值)

上传于 CPython 3.11 macOS 10.9+ x86-64

pytensor-2.25.5-cp310-cp310-win_amd64.whl (1.3 MB 查看哈希值)

上传于 CPython 3.10 Windows x86-64

pytensor-2.25.5-cp310-cp310-musllinux_1_2_x86_64.whl (1.8 MB 查看哈希)

上传于 CPython 3.10 musllinux: musl 1.2+ x86-64

pytensor-2.25.5-cp310-cp310-musllinux_1_2_i686.whl (1.8 MB 查看哈希)

上传于 CPython 3.10 musllinux: musl 1.2+ i686

pytensor-2.25.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB 查看哈希)

上传于 CPython 3.10 manylinux: glibc 2.17+ x86-64

pytensor-2.25.5-cp310-cp310-macosx_10_9_x86_64.whl (1.3 MB 查看哈希)

上传于 CPython 3.10 macOS 10.9+ x86-64

支持者: