Themenmodell mit Python implementieren (numpy)
Kürzlich habe ich Gibbs-Sampling für das LDA-Themenmodell in Python mit numpy implementiert, wobei ich einen Code von einer Site als Referenz genommen habe. In jeder Iteration der Gibbs-Abtastung wird ein (aktuelles) Wort entfernt, ein neues Thema für dieses Wort gemäß einer vom LDA-Modell abgeleiteten posterioren bedingten Wahrscheinlichkeitsverteilung abgetastet und die Anzahl der Wortthemen wie folgt aktualisiert:
<code>for m, doc in enumerate(docs): #m: doc id for n, t in enumerate(doc): #n: id of word inside document, t: id of the word globally # discount counts for word t with associated topic z z = z_m_n[m][n] n_m_z[m][z] -= 1 n_z_t[z, t] -= 1 n_z[z] -= 1 n_m[m] -= 1 # sample new topic for multinomial p_z_left = (n_z_t[:, t] + beta) / (n_z + V * beta) p_z_right = (n_m_z[m] + alpha) / ( n_m[m] + alpha * K) p_z = p_z_left * p_z_right p_z /= numpy.sum(p_z) new_z = numpy.random.multinomial(1, p_z).argmax() # set z as the new topic and increment counts z_m_n[m][n] = new_z n_m_z[m][new_z] += 1 n_z_t[new_z, t] += 1 n_z[new_z] += 1 n_m[m] += 1 </code>
Im obigen Code wird ein neues (einzelnes) z mit der multinomialen Scipy-Funktion abgetastet.
Nun möchte ich ein Joint Sentiment Topic Modell von implementierendieses Papier. Jetzt würde ich die folgenden Strukturen benötigen, um die erforderlichen Zählungen zu verfolgen:
<code>3D matrix containing # occurrences for a word for each topic, for each sentiment 3D matrix containing # occurrences for a topic, for each sentiment, for each document 2D matrix containing # occurrences for a topic, for each sentiment 2D matrix containing # occurrences for a sentiment for each document </code>
Und jetzt kommt das Problem: In diesem Gibbs-Sampler werden für jedes Wort in einem Dokument sowohl ein neues Thema als auch ein Sentiment-Label von einem bedingten Posterior abgetastet (Seite 4, Gleichung 5 des Papiers). Wie könnte ich diese 2 Werte jetzt in Python "abtasten"?
Danke im Voraus...