python - PyMC3:每次都给出不同的结果
问题描述
我已经定义了一个对数似然函数,并且我有一个变量在均匀分布上进行采样。我确保对数似然函数为相同的输入返回相同的结果。但是当我采样时,每次分布都有些不同(在相同的范围内)。
到底是怎么回事?
import pymc3 as mc
import theano.tensor as tt
SAMPLES = 1000
TUNING_SAMPLES = 100
N_CORES = 10
N_CHAINS = 2
#(logl_ThetaFromChoices is defined above with the input)
# use PyMC3 to sampler from log-likelihood
with mc.Model() as modelFindTheta:
theta = mc.Uniform('theta', lower=-200.0, upper=200.0)
# convert m and c to a tensor vector
theta = tt.as_tensor_variable(theta)
def callOp(v):
return logl_ThetaFromChoices(v)
mc.DensityDist('logl_ThetaFromChoices', callOp, observed={'v': theta})
step1 = mc.Metropolis()
trace_theta = mc.sample(SAMPLES,
tune=TUNING_SAMPLES,
discard_tuned_samples=True,
chains=N_CHAINS,
cores=N_CORES,
step=step1)
'alpha' == 这里的 Theta
解决方案
由于它涉及随机数生成,因此需要设置种子以获得可重复的结果。对于 PyMC3,这是通过pymc3.sampling.sample() 方法random_seed
中的参数完成的。
推荐阅读
- javascript - Discord.js 错误:它正在删除整个数组。Quick.db 和 discord.js
- angular - 如何在有角度但使用客户端凭据授予流程的情况下使用 azure 服务?
- pandas-bokeh - 散景代码打开弹出窗口,在按钮单击时显示数据表
- database - 什么是 Sybase/Officewyse/SQLanywhere 主客户端?
- python - Python Web Scraping 相同的类
- java - 在控制台中将多个字符串模式打印到一行中
- intellij-idea - 最佳文件更新工作流程?
- node.js - MongoDB 的 JSON 输入
- php - 为什么我的会话变量不起作用?
- sql - SQL 查询具有一列不同的多列