基于pytorch的轻量级bayes推理框架。
bayes-torch的Python项目详细描述
#bayes torch:一个轻量级的bayes推理框架
[爱德华](https://github.com/blei-lab/edward)(tensorflow)
[pymc](https://github.com/pymc-devs/pymc3)(theano),
[pyro](https://github.com/uber/pyro)(pytorch)它们的计算基础和抽象高api之间的关系是很尴尬。
因此,我们发现该项目在flexible
autograd库pytorch上实现了类stan的api。这是一个轻量级的框架,你可以直接编写联合似然函数来运行推理,而不是用stan、pymc或edward中的丑陋样式来进行伪采样。
pyro中的weired namebinding。
实y[n];
}
参数{
>实mu;
}
模型{
}
````
``python
sigma=数据(1.0)
def target():
target=正常(mu,sigma).log_prob(x).sum(0)
return target
````
>比较两个框架的完整代码:
``python
复位
import torch
=参数(0.0)
sigma=数据(1.0)
def target():
target=正常(mu,sigma).log-prob(x).sum(0)
return target
optimization(target)
打印(f‘优化:mu={mu.data}’)
>res=vb(target)
>q mu=res.params['mu']
>打印(f‘vb mu={q mu["loc"]}omega={q mu["omega"]}西格玛={torch.exp(q mu["omega"]})ω{q mu["omega"]}ω{q mu{q mu{q mu‘omega"]}}}西格玛{火炬{torch.exp(/>
>res=vb(target,q_size=10,n_epoch=200)
>打印(f'vb mu={q{q{q{q{mu‘loc"}}ω}ω{ω{ω{ω{ω{/>
跟踪=采样(目标,跟踪长度=300)
mu跟踪=torch.tensor([t['mu'].item()表示跟踪中的t])
print('sampling:mu={}sigma={}'。format(torch.mean(mu跟踪),torch.std(mu_trace))
import numpy as np
import pystan
实y[n];
}
参数{
实mu;
}
模型{
}
'
=_ x.numpy()
res2=sm.optimizing(data=dict(n=len(x,y=x),y=x))
>print(f'optimizing(stan):mu={res2["mu"]}')
>res3=sm.vb(data=dict(n=len(x,y=x),y=x))
>res3A=np.array(res3['sampler(m params)")
print(f'vb(stan(stan)打印(f'vb(stan)print(":"mu={res3A[0,:].mean()}sigma={res3A[0,:].std()}')
`````
>敌方位置探测示例:
``python
=数据(作战点)
敌人=参数(敌人点)将实值设为初始值,尽管随机初始值可能更合适
conflict_threshold=0.2
distance_threshold=1.0
tense=10.0
前一时态=5.0
def target():
friend撸emy=torch.cat((friend,敌人),0)
distance=cdist(战斗,敌友.min(dim=1)[0]
sd=torch.stack([friend.std(0),enest.std(0)]0)
conflict=torch.exp(norm_naive_bayes_predict(battle,mu,sd,logpc)).prod(1)
p=soft_cut_ge(conflict,conflict_threshold,tense=tense)*\
soft_cut le(distance,distance_threshold,时态=时态)
target=torch.log(p.sum(0)
+敌人。总和(0)。总和(0)
返回目标2
`````
<;img src="images/example.png">;
这意味着引入参数的方式有一些限制,例如,您不能定义"参数"列表
,希望bt能够找到它。
在"优化"中,bt运行标准sgd算法。
在"采样"中,bt经常使用hmc将变量替换为另一个(相同的形状)。
在"vb"中,bt将变量映射到正态变分分布对象,该对象包含变分参数mu和omega(`omega=log(sigma)`)。最后一个维度将由"vb"内部使用。
您始终可以检查原始代码以找出发生的任何情况。
Rajesh和Gelman,Andrew和Blei,David M
https://arxiv.org/abs/1603.00788
http://andrew gelman.com/wp content/uploads/2014/12/pp_nips2014.pdf
The no-u-turn sampler:Adaptive setting path lengs in Hamiltonian Monte Carlo.
Hoffman、Matthew D和Gelman、Andrew
https://arxiv.org/abs/1111.4246
关键词:Bayes Statistic Scientific
平台:未知
内容类型:文本/向下
[爱德华](https://github.com/blei-lab/edward)(tensorflow)
[pymc](https://github.com/pymc-devs/pymc3)(theano),
[pyro](https://github.com/uber/pyro)(pytorch)它们的计算基础和抽象高api之间的关系是很尴尬。
因此,我们发现该项目在flexible
autograd库pytorch上实现了类stan的api。这是一个轻量级的框架,你可以直接编写联合似然函数来运行推理,而不是用stan、pymc或edward中的丑陋样式来进行伪采样。
pyro中的weired namebinding。
实y[n];
}
参数{
>实mu;
}
模型{
}
````
``python
sigma=数据(1.0)
def target():
target=正常(mu,sigma).log_prob(x).sum(0)
return target
````
>比较两个框架的完整代码:
``python
复位
import torch
=参数(0.0)
sigma=数据(1.0)
def target():
target=正常(mu,sigma).log-prob(x).sum(0)
return target
optimization(target)
打印(f‘优化:mu={mu.data}’)
>res=vb(target)
>q mu=res.params['mu']
>打印(f‘vb mu={q mu["loc"]}omega={q mu["omega"]}西格玛={torch.exp(q mu["omega"]})ω{q mu["omega"]}ω{q mu{q mu{q mu‘omega"]}}}西格玛{火炬{torch.exp(/>
>res=vb(target,q_size=10,n_epoch=200)
>打印(f'vb mu={q{q{q{q{mu‘loc"}}ω}ω{ω{ω{ω{ω{/>
跟踪=采样(目标,跟踪长度=300)
mu跟踪=torch.tensor([t['mu'].item()表示跟踪中的t])
print('sampling:mu={}sigma={}'。format(torch.mean(mu跟踪),torch.std(mu_trace))
import numpy as np
import pystan
实y[n];
}
参数{
实mu;
}
模型{
}
'
=_ x.numpy()
res2=sm.optimizing(data=dict(n=len(x,y=x),y=x))
>print(f'optimizing(stan):mu={res2["mu"]}')
>res3=sm.vb(data=dict(n=len(x,y=x),y=x))
>res3A=np.array(res3['sampler(m params)")
print(f'vb(stan(stan)打印(f'vb(stan)print(":"mu={res3A[0,:].mean()}sigma={res3A[0,:].std()}')
`````
>敌方位置探测示例:
``python
=数据(作战点)
敌人=参数(敌人点)将实值设为初始值,尽管随机初始值可能更合适
conflict_threshold=0.2
distance_threshold=1.0
tense=10.0
前一时态=5.0
def target():
friend撸emy=torch.cat((friend,敌人),0)
distance=cdist(战斗,敌友.min(dim=1)[0]
sd=torch.stack([friend.std(0),enest.std(0)]0)
conflict=torch.exp(norm_naive_bayes_predict(battle,mu,sd,logpc)).prod(1)
p=soft_cut_ge(conflict,conflict_threshold,tense=tense)*\
soft_cut le(distance,distance_threshold,时态=时态)
target=torch.log(p.sum(0)
+敌人。总和(0)。总和(0)
返回目标2
`````
<;img src="images/example.png">;
这意味着引入参数的方式有一些限制,例如,您不能定义"参数"列表
,希望bt能够找到它。
在"优化"中,bt运行标准sgd算法。
在"采样"中,bt经常使用hmc将变量替换为另一个(相同的形状)。
在"vb"中,bt将变量映射到正态变分分布对象,该对象包含变分参数mu和omega(`omega=log(sigma)`)。最后一个维度将由"vb"内部使用。
您始终可以检查原始代码以找出发生的任何情况。
Rajesh和Gelman,Andrew和Blei,David M
https://arxiv.org/abs/1603.00788
http://andrew gelman.com/wp content/uploads/2014/12/pp_nips2014.pdf
The no-u-turn sampler:Adaptive setting path lengs in Hamiltonian Monte Carlo.
Hoffman、Matthew D和Gelman、Andrew
https://arxiv.org/abs/1111.4246
关键词:Bayes Statistic Scientific
平台:未知
内容类型:文本/向下