跳转到主要内容

基于HTTP的Stan接口,Stan是一个用于贝叶斯推理的包。

项目描述

Stan logo

pypi version

基于HTTP的REST接口到Stan,Stan是一个用于贝叶斯推理的包。

Stan C++包(Stan)的HTTP 1.1接口,httpstan是一个适配器,允许用户通过REST API与Stan C++库交互。此包旨在用作已知如何发出HTTP请求的前端的后端。此包的主要受众是开发者。

除了通过HTTP提供Stan命令行界面(CmdStan)的基本功能外,httpstan还提供以下功能

  • 自动缓存编译的Stan模型

  • 自动缓存Stan模型的样本

  • 并行采样

文档: https://httpstan.readthedocs.org.

需求

  • Linux或macOS

  • C++编译器:gcc ≥9.0或clang ≥10.0。

背景

httpstan 是一个 shim,允许客户端能够调用 Stan C++ 库的 stan::services 命名空间中的函数来执行基于 HTTP 的请求。最初,httpstan 是作为用 Python 编写的 Stan 接口的后端开发的,即 PyStan

稳定性与可维护性是该软件包的两个主要目标。

安装

$ python3 -m pip install httpstan

要从 PyPI 安装 httpstan,请确保您的系统满足以下要求:

  • Linux或macOS

  • x86-64 CPU

  • C++编译器:gcc ≥9.0或clang ≥10.0。

如果您的系统使用不同类型的 CPU,您应该可以使用以下命令从源代码安装:

# Build shared libraries
make

# Build the httpstan wheel on your system
python3 -m pip install poetry
python3 -m poetry build

# Install the wheel
python3 -m pip install dist/*.whl

使用方法

安装 httpstan 后,运行模块将开始在 localhost 的 8080 端口上监听。

python3 -m httpstan

在另一个终端中,向 http://localhost:8080/v1/models 发送 POST 请求,包含 Stan 程序代码以编译程序。

curl -H "Content-Type: application/json" \
    --data '{"program_code":"parameters {real y;} model {y ~ normal(0,1);}"}' \
    http://localhost:8080/v1/models

此请求将返回一个模型名称以及所有编译器输出。

{"compiler_output": "In file included from …", "stanc_warnings": "", "name": "models/xc2pdjb4"}

(模型 名称 取决于平台和 Stan 的版本。)

使用默认设置从该模型中抽取样本需要两个步骤:(1)启动采样操作;(2)检索操作输出(一旦完成)。

首先,我们发送一个请求来启动采样操作。

curl -H "Content-Type: application/json" \
    --data '{"function":"stan::services::sample::hmc_nuts_diag_e_adapt"}' \
    http://localhost:8080/v1/models/xc2pdjb4/fits

此请求指示 httpstan 从模型中描述的正态分布中抽取样本。函数名称从 Stan C++ 库中的 stan::services 命名空间中挑选出一个特定函数(有关详细信息,请参阅 Stan C++ 文档)。此请求将立即返回一个指向长期运行拟合操作的引用。

{"name": "operations/gkf54axb", "done": false, "metadata": {"fit": {"name": "models/xc2pdjb4/fits/gkf54axb"}}}

操作完成后,可以检索“拟合”。拟合的名称(例如 models/xc2pdjb4/fits/gkf54axb)包含在操作的 metadata 字段中。拟合以 JSON 编码消息序列的形式保存。这些消息使用换行符连接在一起。要检索这些消息,请将其保存到本地文件 myfit.jsonlines 中,并发出以下请求:

curl http://localhost:8080/v1/models/xc2pdjb4/fits/gkf54axb > myfit.jsonlines

保存到 myfit.jsonlines 中的 Stan “拟合” 聚合了所有消息。通过逐个读取它们,您可以恢复 Stan C++ 库发送的所有消息。

引用

我们感谢引用,因为它们让我们了解人们在使用此软件做了什么。引用还提供了使用证据,有助于获得资助。

在出版物中引用 httpstan 时使用:

Riddell, A.,Hartikainen, A.,& Carter, M. (2021). httpstan (4.4.0). https://pypi.ac.cn/project/httpstan

或者使用以下 BibTeX 条目

@misc{httpstan,
  title = {httpstan (4.4.0)},
  author = {Riddell, Allen and Hartikainen, Ari and Carter, Matthew},
  year = {2021},
  month = mar,
  howpublished = {PyPI}
}

请也引用 Stan。

许可证

ISC 许可证。

项目详情


下载文件

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

源代码分发

此版本没有可用的源代码分发文件。请参阅有关生成分发存档的教程。

构建分发

httpstan-4.13.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (45.6 MB 查看哈希值)

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

httpstan-4.13.0-cp312-cp312-macosx_13_0_x86_64.whl (39.6 MB 查看哈希值)

上传时间: CPython 3.12 macOS 13.0+ x86-64

httpstan-4.13.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (45.5 MB 查看哈希值)

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

httpstan-4.13.0-cp311-cp311-macosx_13_0_x86_64.whl (39.6 MB 查看哈希值)

上传时间: CPython 3.11 macOS 13.0+ x86-64

httpstan-4.13.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (45.5 MB 查看哈希值)

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

httpstan-4.13.0-cp310-cp310-macosx_13_0_x86_64.whl (39.6 MB 查看哈希值)

上传时间: CPython 3.10 macOS 13.0+ x86-64

由以下支持