pymc3中可重用模型的decorator
sampled的Python项目详细描述
采样
decorator用于pymc3中的可重用模型
为pymc3的可重用模型提供语法糖。这使您可以将创建生成模型与使用模型分开。 下面是创建模型的示例: 下面是如何使用模型: 你也可以用它来建立图形网络——这里是Koller和Friedman的“概率图形模型”的学生示例的连续版本,第3章: 观察结果可以传递到任何节点,我们可以观察到这如何改变后验期望: 引用import numpy as np
import pymc3 as pm
from sampled import sampled
@sampled
def linear_model(X, y):
shape = X.shape
X = pm.Normal('X', mu=np.mean(X, axis=0), sd=np.std(X, axis=0), shape=shape)
coefs = pm.Normal('coefs', mu=np.zeros(shape[1]), sd=np.ones(shape[1]), shape=shape[1])
pm.Normal('y', mu=np.dot(X, coefs), sd=np.ones(shape[0]), shape=shape[0])
X = np.random.normal(size=(1000, 10))
w = np.random.normal(size=10)
y = X.dot(w) + np.random.normal(scale=0.1, size=1000)
with linear_model(X=X, y=y):
sampled_coefs = pm.sample(draws=1000, tune=500)
np.allclose(sampled_coefs.get_values('coefs').mean(axis=0), w, atol=0.1) # True
@sampled
def student():
difficulty = pm.Beta('difficulty', alpha=5, beta=5)
intelligence = pm.Beta('intelligence', alpha=5, beta=5)
SAT = pm.Beta('SAT', alpha=20 * intelligence, beta=20 * (1 - intelligence))
grade_avg = 0.5 + 0.5 * tt.sqrt((1 - difficulty) * intelligence)
grade = pm.Beta('grade', alpha=20 * grade_avg, beta=20 * (1 - grade_avg))
recommendation = pm.Binomial('recommendation', n=1, p=0.7 * grade)
# no prior knowledge
with student():
prior = pm.sample(draws=1000, tune=500)
prior.get_values('recommendation').mean() # 0.502
# 99th percentile SAT score --> higher chance of a recommendation
with student(SAT=0.99):
good_sats = pm.sample(draws=1000, tune=500)
good_sats.get_values('recommendation').mean() # 0.543
# A good grade in a hard class --> very high chance of recommendation
with student(difficulty=0.99, grade=0.99):
hard_class_good_grade = pm.sample(draws=1000, tune=500)
hard_class_good_grade.get_values('recommendation').mean() # 0.705