在本篇文章中將解釋高斯混合模型(GMM)的關鍵部分背後的數學原理,即期望最大化(EM),以及如何將這些概念轉換為Python。 這個故事的重點是EM或M-Step。
注意:這不是有關端到端GMM算法的全面說明。 要進行更深入的研究,請參閱我們以前翻譯的文章。
期望最大化GMM中有一系列步驟,通常稱為「期望最大化」,簡稱「 EM」。 要解釋如何理解EM數學,請首先考慮您可能要處理的模型。
樣本由圖形上的點表示。這些點形成一些不同的斑點。每個斑點都有一個中心,每個點都與每個斑點的中心相距一定距離。給定GMM模型數據,目標通常是根據最接近的中心按其樣本點標記其他樣本。有些點距離一個或多個中心幾乎相等,因此,我們希望基於某種概率來標記點。
EM用到的符號要學習如何學習機器學習算法,您一生中需要一些希臘語。 因為算法中符號基本上都是以希臘文表示的。 儘管可能會想掩蓋基礎知識,但是對單個希臘字母的簡單掌握可以幫助您理解算法中的重要概念。
算法可能會令人生畏且令人困惑。 例如,乍看之下,高度集中的希臘符號有時足以使人窒息。 但是不要浪費時間,我們在這裡只要考慮現在要使用的符號即可
除此以外,我們也有一些英文字母在EM中代表GMM的意思。通常,英文字母圍繞著希臘字母,就像小領航魚圍著大鯊魚遊動。就像小魚一樣,英文字母有一個重要的作用,它為如何解釋算法提供了指導。
現在我們已經隔離了方程的每個組成部分,讓我們通過檢查M-Step,將它們組合成一些常用的數學短語,這些短語對於用EM語言進行對話很重要。
簇,高斯,字母J或K,有時還包括C:通常都是同一件事-如果我們有3個簇,那麼您可能會聽到「每個高斯」,「每個j」,「每個高斯j」或 「對於每個K組件」-這些都是談論相同3個簇的不同方法。 在數據方面,我們可以繪製(x,y)樣本/點的數組,並查看它們如何形成簇。
# a 2D array of samples [features and targets]
# the last column, targets [0,1,2], represent three clusters
# the first two columns are the points that make up our features
# each feature is just a set of points (x,y) in 2D space
# each row is a sample and cluster label
[[-7.72642091 -8.39495682 2. ]
[ 5.45339605 0.74230537 1. ]
[-2.97867201 9.55684617 0. ]
[ 6.04267315 0.57131862 1. ] ...]
軟分類(Soft Assignments),概率,響應度(Responsibility):聚類的一個主要思想是我們希望為每個樣本找到一個數字,以告訴我們樣本屬於哪個聚類。 在GMM中,對於我們評估的每個樣本,我們可能會返回代表「每個高斯j的響應度」,每個「軟分類」或每個「概率」的值。
這些階段通常都是關於同一件事的,但響應度與概率之間存在關鍵區別。
# an array of assignment data about the 2D array of samples
# each column represents a cluster
# each row represents data about each sample
# in each row, we have the probability that a sample belongs to one of three clusters - it adds up to 1 (as it should)
# but the sum of each column is a big number number (not 1)
print(assignments)
# sample output: an array of assignment data
[[1.00000000e+000 2.82033618e-118 1.13001412e-070]
[9.21706438e-074 1.00000000e+000 3.98146031e-029]
[4.40884339e-099 5.66602768e-053 1.00000000e+000]...]
print(np.sum(assignments[0])
# sample output: the sum across each row is 1
1
print(np.sum(assignments[:, 0])
# sample output: the sum in each col is a big number that varies
# Little Gamma: the really small numbers in each column
# Big Gamma: the sum of each column, or 33.0 in this sample33.0
大寫伽瑪,小寫伽瑪,J,N,x和i:EM中的核心任務是為每個群集優化三組參數,或者「對於每個j,優化w(),mew( )和方差()。」 換句話說,群集的權重(),群集的中心點()和群集的方差()是多少?
對於權重(),我們將「大寫伽瑪」除以特徵總數。 從更早的時候開始,我們就知道每個聚類j的大寫伽瑪只是將給定聚類的每個樣本的分配值相加的結果(該數字之和不等於1)。 如下圖所示
對於EM期間高斯的權重參數,請考慮一些簡單的事情,例如添加數字列表,然後將其除以樣本總數。
對於mew (),不是像我們之前那樣將所有小寫伽瑪加到一個小寫伽瑪中,而是對每個聚類j和每個樣本i將小寫伽瑪與特徵x進行矩陣乘法。 如下圖所示
請記住,mew只是每個簇的中心點-如果我們有3個簇,而我們的樣本都是x,y坐標,那麼mew將是3個x,y坐標的數組,每個簇一個。
# for figure 4 - mew (mu)
# same array of assignment data as before
# each column is a cluster of little gammas
print(assignments)
[[1.00000000e+000 2.82033618e-118 1.13001412e-070]
[9.21706438e-074 1.00000000e+000 3.98146031e-029]
[4.40884339e-099 5.66602768e-053 1.00000000e+000]...]
# the little gammas of cluster 0 is just column 0
[[1.00000000e+000 ]
[9.21706438e-074 ]
[4.40884339e-099 ]...]
# same array of sample data as before
# the first two columns are the x,y coordinates
# the last column is the cluster label of the sample
print(features)
[[-7.72642091 -8.39495682 2. ]
[ 5.45339605 0.74230537 1. ]
[-2.97867201 9.55684617 0. ]
[ 6.04267315 0.57131862 1. ] ...]
# for features, we just need its points
[[-7.72642091 -8.39495682 ]
[ 5.45339605 0.74230537 ]
[-2.97867201 9.55684617 ]
[ 6.04267315 0.57131862 ] ...]
# if using numpy (np) for matrix multiplication
# for cluster 0 ...
big_gamma = np.sum(assignments[:, 0]
mew = np.matmul(assignments[:, 0], features) / big_gamma
# returns an array of mew
[[-2.66780392 8.93576069]
[-6.95170962 -6.67621669]
[ 4.49951001 1.93892013]]
對於方差(),請考慮到現在,我們有了點和中心點-隨著方差的出現,我們基本上正在評估每個樣本的點(每個i的x)到每個群集的中心點(每個i的mew)的距離。 用EM語言來說,有些人可能會說「 xi減去mewi乘以Big Gamma j。」
# for figure 5 - variance
# a sampling of variance for cluster 0 of n clusters
# given arrays for features and assignments...
x_i = features
big_gamma = np.sum(assignments[:, 0]
mew = np.matmul(assignments[:, 0], features) / big_gamma
numerator = np.matmul(assignments[:, 0], (x_i - mew) ** 2)
variance = numerator / big_gamma
# returns an array of variance
[[0.6422345 1.06006186]
[0.65254746 0.9274831 ]
[0.95031461 0.92519751]]
以上步驟都是關於EM中的M-Step或最大化-所有關於權值、mew和方差的都是關於優化的;但是,初始賦值數組呢?我們如何得到每個樣本的概率數組這是EM中的E-Step,也就是期望。
在E-Step中,我們嘗試用貝葉斯規則猜出每個點的分配-這會產生一組值,這些值指示每個點對高斯的響應度或概率。最初會與猜測值(後驗值)相差很遠,但是在通過E-Step和M-Step循環之後,這些猜測會變得更好,更接近客觀的地面真理。
GMM算法重複M-Step 和 E-Step直到收斂。例如,收斂性可能是迭代的最大次數,或者當每輪猜測之間的差異變得非常小時。希望最終的結果是,數據中的每個樣本都有一個軟分配的標籤。
總結在這篇文章中,我介紹了M-Step的高斯混合模型算法的期望最大化階段的導航部分的理解。雖然從表面上看,數學似乎太複雜而無法處理,但我們可以通過理解其各個部分來處理其複雜性。例如,一些關鍵的理解,如發音的希臘符號和應用它們的操作與NumPy是重要的,以掌握總體概念。
作者:Justin Chae
deephub翻譯組
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺「網易號」用戶上傳並發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.