基于HTTP的Stan接口,Stan是一个用于贝叶斯推理的包。
项目描述
基于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的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a452f600e314f107f6bc17e43905ea2f3aaeebf8b8b98350e861aacf84476382 |
|
MD5 | 832cc1a7cfb2ecf7d36c84201c94ce81 |
|
BLAKE2b-256 | d06e069e28610b85d93341ce830b1470aa103d19ba0eded27ab718264041e2c1 |
哈希值 for httpstan-4.13.0-cp312-cp312-macosx_13_0_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 046cc66f0adbf7e149361ccaa76a0e1478ed865490d1288eb91ef5007d4de590 |
|
MD5 | 841018b1039413c031a3502f6fec2502 |
|
BLAKE2b-256 | 88d5e7998ed6558debc5029f7d6f1dc60bee494ff41d2a77dc20df464a885abf |
哈希值 for httpstan-4.13.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 7b490d6c0e8b7048d1b58dfb79db561ae1879cb3bde3012d024ef374c0308469 |
|
MD5 | 5211ca5cccf33fe489a9511dbf37cc7e |
|
BLAKE2b-256 | 861f4b508886d3e545aab614c20dbd5f9b1dcb7645dc180942c77474c999cf45 |
哈希值 for httpstan-4.13.0-cp311-cp311-macosx_13_0_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d0c0807843d75629f61876319d5715e6a4efcc0c7b55fdbaa970baa86e7d2b09 |
|
MD5 | 57b926c9b9aeb026a83c95d918263893 |
|
BLAKE2b-256 | 0310811a8cc58cfefd114b85197444b55ad97e9fa1905e452d92ee04cd61f80f |
哈希值 for httpstan-4.13.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fc16ee696c16aa57e35e588b5b3219224a43897589861bdd6e1e610f7b6197b2 |
|
MD5 | 082f68216b68bb0729ac04a0f5665e02 |
|
BLAKE2b-256 | a6770492bf3a332d24ea986acb74c9b1258d17a2636e7f681ebd6db74de49db5 |
哈希值 for httpstan-4.13.0-cp310-cp310-macosx_13_0_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d74077e482391586da9510847bd56cb7feb6fbed8b32bfc741c9067f1bb5cd17 |
|
MD5 | 2730c49dade85663fe00bee0b2dda7f0 |
|
BLAKE2b-256 | eb349df0d62d250c8ab58a137b4f957d1962072c4c3aa1abb51240bf3a2fad44 |