{ "info": { "author": "yiyuezhuo", "author_email": "616271835@163.com", "bugtrack_url": null, "classifiers": [], "description": " # Bayes-torch: A light weight bayes inference framework\n\n \n\n Though there're a lot of bayes inference modeling lib/language\n\n such as [stan](https://github.com/stan-dev/stan), \n\n [edward](https://github.com/blei-lab/edward) (tensorflow) \n\n [pymc](https://github.com/pymc-devs/pymc3) (theano), \n\n [pyro](https://github.com/uber/pyro) (pytorch) the relation between\n\n their computation ground and absract high API is awkward.\n\n \n\n So the project is found to implment stan-like API on the flexible\n\n autograd library, pytorch. It's a light-weight framework, you will\n\n directly write joint likelihood function to run inference instead of\n\n fake sampling statment in stan, pymc or ugly style in Edward, \n\n weired namebinding in pyro.\n\n \n\n ## Example\n\n \n\n We can implement following stan model as such:\n\n \n\n ```\n\n data {\n\n int N;\n\n real y[N];\n\n }\n\n parameters {\n\n real mu;\n\n }\n\n model {\n\n y ~ normal(mu, 1);\n\n }\n\n ```\n\n \n\n torch-bayes model code:\n\n \n\n ```python\n\n mu = Parameter(0.0) # optimizing/vb/sampling init value\n\n sigma = Data(1.0)\n\n X = Data(_X)\n\n \n\n def target():\n\n target = Normal(mu,sigma).log_prob(X).sum(0)\n\n return target\n\n ```\n\n \n\n Full code comparing two framework:\n\n \n\n ```python\n\n from bayestorch import Parameter,Data,optimizing,vb,sampling,reset\n\n import torch\n\n from torch.distributions import Normal\n\n \n\n _X = torch.arange(10)\n\n print(_X.mean())\n\n \n\n # torch-bayes model\n\n \n\n mu = Parameter(0.0)\n\n sigma = Data(1.0)\n\n X = Data(_X)\n\n \n\n def target():\n\n target = Normal(mu,sigma).log_prob(X).sum(0)\n\n return target\n\n \n\n optimizing(target)\n\n print(f'optimizing: mu={mu.data}')\n\n \n\n res = vb(target)\n\n q_mu = res.params['mu']\n\n print(f'vb mu={q_mu[\"loc\"]} omega={q_mu[\"omega\"]} sigma={torch.exp(q_mu[\"omega\"])}')\n\n \n\n reset()\n\n \n\n mu = Parameter(0.0)\n\n \n\n res = vb(target, q_size = 10, n_epoch=200)\n\n q_mu = res.params['mu']\n\n print(f'vb mu={q_mu[\"loc\"]} omega={q_mu[\"omega\"]} sigma={torch.exp(q_mu[\"omega\"])}')\n\n \n\n trace = sampling(target,trace_length=300)\n\n mu_trace = torch.tensor([t['mu'].item() for t in trace])\n\n print('sampling: mu={} sigma={}'.format(torch.mean(mu_trace), torch.std(mu_trace)))\n\n \n\n \n\n # stan model\n\n \n\n \n\n import numpy as np\n\n import pystan\n\n stan_code = '''\n\n data {\n\n int N;\n\n real y[N];\n\n }\n\n parameters {\n\n real mu;\n\n }\n\n model {\n\n y ~ normal(mu, 1);\n\n }\n\n '''\n\n sm = pystan.StanModel(model_code = stan_code)\n\n \n\n _X = _X.numpy()\n\n res2 = sm.optimizing(data = dict(N = len(_X), y = _X))\n\n print(f'optimizing(stan): mu={res2[\"mu\"]}')\n\n res3 = sm.vb(data = dict(N = len(_X), y = _X))\n\n res3a=np.array(res3['sampler_params'])\n\n print(f'vb(stan): mu={res3a[0,:].mean()} sigma={res3a[0,:].std()}')\n\n \n\n ```\n\n \n\n Enemy location detecting example:\n\n \n\n ```python\n\n # model\n\n friend = Data(friend_point)\n\n battle = Data(battle_point)\n\n enemy = Parameter(enemy_point) # set real value as init value, though maybe a randomed init is more proper\n\n \n\n logPC = Data(_logPC)\n\n \n\n conflict_threshold = 0.2\n\n distance_threshold = 1.0\n\n tense = 10.0\n\n alpha = 5.0\n\n prior_threshold = 5.0\n\n prior_tense = 5.0\n\n \n\n def target():\n\n friend_enemy = torch.cat((friend, enemy),0)\n\n \n\n distance = cdist(battle, friend_enemy).min(dim=1)[0]\n\n \n\n \n\n mu = torch.stack([friend.mean(0),enemy.mean(0)],0)\n\n sd = torch.stack([friend.std(0),enemy.std(0)],0)\n\n \n\n conflict = torch.exp(norm_naive_bayes_predict(battle, mu, sd, logPC)).prod(1)\n\n p = soft_cut_ge(conflict,conflict_threshold, tense = tense) * \\\n\n soft_cut_le(distance, distance_threshold, tense = tense)\n\n \n\n target= torch.log(p).sum(0)\n\n return target\n\n \n\n def target2():\n\n target1 = target()\n\n # location prior\n\n target2 = target1 + enemy.sum(0).sum(0)\n\n return target2\n\n ```\n\n \n\n \n\n \n\n ## Basic principle\n\n \n\n bayes-torch(BT) use `target_f.__globals__` to access and change variables labeled `Parameter` or `Data`. \n\n It implies some limit to way to introduce parameters, for example you can't define a list of `Parameter`\n\n and expect BT can find it.\n\n \n\n In `optimizing`, BT run standard SGD algorithm. \n\n In `sampling`, BT will frequently replace variable with another(same shape) using HMC. \n\n In `vb`, BT will map a variable to a normal variational distribution object, that contain variational\n\n parameters mu and omega (`omega = log(sigma)`). The last dimention will be used by `vb` innerly.\n\n So code such as `sum(-1)` or `sum()`(reduce to scaler) will raise error.\n\n \n\n However, the thin implementation(`core.py`) only consists of 300- lines, \n\n you can always check origin code to figure out what happen anyway.\n\n \n\n ## Reference\n\n \n\n Automatic Differentiation Variational Inference\n\n \n\n Kucukelbir, Alp and Tran, Dustin and Ranganath, Rajesh and Gelman, Andrew and Blei, David M\n\n \n\n https://arxiv.org/abs/1603.00788\n\n \n\n Fully automatic variational inference of differentiable probability models\n\n \n\n Kucukelbir, Alp and Ranganath, Rajesh and Gelman, Andrew and Blei, David\n\n \n\n http://andrewgelman.com/wp-content/uploads/2014/12/pp_workshop_nips2014.pdf\n\n \n\n The No-U-turn sampler: adaptively setting path lengths in Hamiltonian Monte Carlo.\n\n \n\n Hoffman, Matthew D and Gelman, Andrew\n\n \n\n https://arxiv.org/abs/1111.4246\nKeywords: bayes statistic scientific\nPlatform: UNKNOWN\nDescription-Content-Type: text/markdown\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/yiyuezhuo/bayes-torch", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "bayes-torch", "package_url": "https://pypi.org/project/bayes-torch/", "platform": "", "project_url": "https://pypi.org/project/bayes-torch/", "project_urls": { "Bug Reports": "https://github.com/yiyuezhuo/bayes-torch/issues", "Homepage": "https://github.com/yiyuezhuo/bayes-torch", "Source": "https://github.com/yiyuezhuo/bayes-torch" }, "release_url": "https://pypi.org/project/bayes-torch/0.0.4/", "requires_dist": null, "requires_python": "", "summary": "A light weight bayes inference framework based on pytorch.", "version": "0.0.4" }, "last_serial": 3981860, "releases": { "0.0.3": [ { "comment_text": "", "digests": { "md5": "93a205f1dca3e33dc211eb5c80f4cfa5", "sha256": "7809e9ddb7adcd67df1df2c79cea0fea2102430b0ec4099f6084658e43d64abb" }, "downloads": -1, "filename": "bayes-torch-0.0.3.tar.gz", "has_sig": false, "md5_digest": "93a205f1dca3e33dc211eb5c80f4cfa5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6605, "upload_time": "2018-04-22T11:59:39", "url": "https://files.pythonhosted.org/packages/e1/c5/f5c9c8d7f98cb961ce68d11d1f76bebe082c2bfb61a4d35605b6820e3b8d/bayes-torch-0.0.3.tar.gz" } ], "0.0.4": [ { "comment_text": "", "digests": { "md5": "6b22f257a52ba6589a5f8072fae9b521", "sha256": "275f21f1edc29bf1312612810ee24d3f5fcb32fb7c277e7f574fd8251fd5329e" }, "downloads": -1, "filename": "bayes_torch-0.0.4-py3-none-any.whl", "has_sig": false, "md5_digest": "6b22f257a52ba6589a5f8072fae9b521", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 4140, "upload_time": "2018-06-20T15:13:19", "url": "https://files.pythonhosted.org/packages/9c/e9/1ca7ad3b79f2fcbde56bd7cd1d0fe5d1afa958b79428038700e54c2cee61/bayes_torch-0.0.4-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "315090fdf5d8cc7356f6aa68248bcba4", "sha256": "1538db5f468fb7636450f5d4a20132fd3166a5a7b304d6ba624e8c3c7ecdbce3" }, "downloads": -1, "filename": "bayes-torch-0.0.4.tar.gz", "has_sig": false, "md5_digest": "315090fdf5d8cc7356f6aa68248bcba4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6072, "upload_time": "2018-06-20T15:13:20", "url": "https://files.pythonhosted.org/packages/60/ae/39528f5d1e8fef4602b3bf01d69694fc8192700185129a4cc0c9fa7a6761/bayes-torch-0.0.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "6b22f257a52ba6589a5f8072fae9b521", "sha256": "275f21f1edc29bf1312612810ee24d3f5fcb32fb7c277e7f574fd8251fd5329e" }, "downloads": -1, "filename": "bayes_torch-0.0.4-py3-none-any.whl", "has_sig": false, "md5_digest": "6b22f257a52ba6589a5f8072fae9b521", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 4140, "upload_time": "2018-06-20T15:13:19", "url": "https://files.pythonhosted.org/packages/9c/e9/1ca7ad3b79f2fcbde56bd7cd1d0fe5d1afa958b79428038700e54c2cee61/bayes_torch-0.0.4-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "315090fdf5d8cc7356f6aa68248bcba4", "sha256": "1538db5f468fb7636450f5d4a20132fd3166a5a7b304d6ba624e8c3c7ecdbce3" }, "downloads": -1, "filename": "bayes-torch-0.0.4.tar.gz", "has_sig": false, "md5_digest": "315090fdf5d8cc7356f6aa68248bcba4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6072, "upload_time": "2018-06-20T15:13:20", "url": "https://files.pythonhosted.org/packages/60/ae/39528f5d1e8fef4602b3bf01d69694fc8192700185129a4cc0c9fa7a6761/bayes-torch-0.0.4.tar.gz" } ] }