आप लागू कर सकते हैं झील प्राधिकरण देखरेख PyMC निम्नलिखित चित्रमय मॉडल में अव्यक्त चर जानने के लिए महानगरों नमूना का उपयोग करता है के साथ:
प्रशिक्षण कोष 10 फिल्म समीक्षा (5 सकारात्मक और 5 नकारात्मक) के होते हैं साथ प्रत्येक दस्तावेज़ के लिए संबंधित स्टार रेटिंग के साथ। स्टार रेटिंग को प्रतिक्रिया चर के रूप में जाना जाता है जो प्रत्येक दस्तावेज़ से जुड़े ब्याज की मात्रा है। दस्तावेजों और प्रतिक्रिया चर को गुप्त विषयों को खोजने के लिए संयुक्त रूप से मॉडलिंग किया जाता है जो भविष्य में लेबल किए गए दस्तावेज़ों के लिए प्रतिक्रिया चर की भविष्यवाणी करेंगे। अधिक जानकारी के लिए, original paper देखें। निम्नलिखित कोड पर विचार करें:
import pymc as pm
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
train_corpus = ["exploitative and largely devoid of the depth or sophistication ",
"simplistic silly and tedious",
"it's so laddish and juvenile only teenage boys could possibly find it funny",
"it shows that some studios firmly believe that people have lost the ability to think",
"our culture is headed down the toilet with the ferocity of a frozen burrito",
"offers that rare combination of entertainment and education",
"the film provides some great insight",
"this is a film well worth seeing",
"a masterpiece four years in the making",
"offers a breath of the fresh air of true sophistication"]
test_corpus = ["this is a really positive review, great film"]
train_response = np.array([3, 1, 3, 2, 1, 5, 4, 4, 5, 5]) - 3
#LDA parameters
num_features = 1000 #vocabulary size
num_topics = 4 #fixed for LDA
tfidf = TfidfVectorizer(max_features = num_features, max_df=0.95, min_df=0, stop_words = 'english')
#generate tf-idf term-document matrix
A_tfidf_sp = tfidf.fit_transform(train_corpus) #size D x V
print "number of docs: %d" %A_tfidf_sp.shape[0]
print "dictionary size: %d" %A_tfidf_sp.shape[1]
#tf-idf dictionary
tfidf_dict = tfidf.get_feature_names()
K = num_topics # number of topics
V = A_tfidf_sp.shape[1] # number of words
D = A_tfidf_sp.shape[0] # number of documents
data = A_tfidf_sp.toarray()
#Supervised LDA Graphical Model
Wd = [len(doc) for doc in data]
alpha = np.ones(K)
beta = np.ones(V)
theta = pm.Container([pm.CompletedDirichlet("theta_%s" % i, pm.Dirichlet("ptheta_%s" % i, theta=alpha)) for i in range(D)])
phi = pm.Container([pm.CompletedDirichlet("phi_%s" % k, pm.Dirichlet("pphi_%s" % k, theta=beta)) for k in range(K)])
z = pm.Container([pm.Categorical('z_%s' % d, p = theta[d], size=Wd[d], value=np.random.randint(K, size=Wd[d])) for d in range(D)])
@pm.deterministic
def zbar(z=z):
zbar_list = []
for i in range(len(z)):
hist, bin_edges = np.histogram(z[i], bins=K)
zbar_list.append(hist/float(np.sum(hist)))
return pm.Container(zbar_list)
eta = pm.Container([pm.Normal("eta_%s" % k, mu=0, tau=1.0/10**2) for k in range(K)])
y_tau = pm.Gamma("tau", alpha=0.1, beta=0.1)
@pm.deterministic
def y_mu(eta=eta, zbar=zbar):
y_mu_list = []
for i in range(len(zbar)):
y_mu_list.append(np.dot(eta, zbar[i]))
return pm.Container(y_mu_list)
#response likelihood
y = pm.Container([pm.Normal("y_%s" % d, mu=y_mu[d], tau=y_tau, value=train_response[d], observed=True) for d in range(D)])
# cannot use p=phi[z[d][i]] here since phi is an ordinary list while z[d][i] is stochastic
w = pm.Container([pm.Categorical("w_%i_%i" % (d,i), p = pm.Lambda('phi_z_%i_%i' % (d,i), lambda z=z[d][i], phi=phi: phi[z]),
value=data[d][i], observed=True) for d in range(D) for i in range(Wd[d])])
model = pm.Model([theta, phi, z, eta, y, w])
mcmc = pm.MCMC(model)
mcmc.sample(iter=1000, burn=100, thin=2)
#visualize topics
phi0_samples = np.squeeze(mcmc.trace('phi_0')[:])
phi1_samples = np.squeeze(mcmc.trace('phi_1')[:])
phi2_samples = np.squeeze(mcmc.trace('phi_2')[:])
phi3_samples = np.squeeze(mcmc.trace('phi_3')[:])
ax = plt.subplot(221)
plt.bar(np.arange(V), phi0_samples[-1,:])
ax = plt.subplot(222)
plt.bar(np.arange(V), phi1_samples[-1,:])
ax = plt.subplot(223)
plt.bar(np.arange(V), phi2_samples[-1,:])
ax = plt.subplot(224)
plt.bar(np.arange(V), phi3_samples[-1,:])
plt.show()
प्रशिक्षण आंकड़ों को देखते हुए (मनाया शब्द और प्रतिक्रिया चर), हम साथ-साथ प्रतिक्रिया चर (वाई) की भविष्यवाणी के लिए वैश्विक विषयों (बीटा) और प्रतिगमन गुणांक (ईटीए) सीख सकते हैं प्रत्येक दस्तावेज़ (थेटा) के लिए विषय अनुपात के लिए।
: आदेश सीखा बीटा और ईटा दिया Y की भविष्यवाणी करने के लिए, हम एक नया मॉडल है, जहां हम Y का पालन नहीं करते और निम्न परिणाम प्राप्त करने के लिए पहले सीखा बीटा और ईटा का उपयोग परिभाषित कर सकते हैं यहां हमने टेस्ट कॉर्पस के लिए एक सकारात्मक समीक्षा (लगभग 2 दी गई समीक्षा रेटिंग रेंज -2 से 2) की भविष्यवाणी की है: "यह वास्तव में सकारात्मक समीक्षा है, महान फिल्म" जैसा कि बाद में हिस्टोग्राम के मोड द्वारा दिखाया गया है सही। पूर्ण कार्यान्वयन के लिए ipython notebook देखें।
अन्य, और नया, दृष्टिकोण जो आंशिक रूप से लेबल किए गए एलडीए में दिखने लायक हो सकता है। [लिंक] (http://research.microsoft.com/en-us/um/people/sdumais/kdd2011-pldp-final.pdf) यह आवश्यकता को आराम देता है कि प्रशिक्षण सेट में प्रत्येक दस्तावेज़ में एक लेबल होना चाहिए। – metaforge