Self-Organizing Map (SOM) 網路亦為 Kohonen 神經網路,透過反覆運算優化目標函式來達成對資料分群 (Clustering)。SOM 為非監督學習的一類,優點為可將 N 維度的資料映射(mapping)至 2 維的空間上,並維持資料中的拓樸特性。其次,可透過調整權重係數,促使神經網路收斂。
SOM 神經網路是一個兩層前饋神經網路,僅含輸入層與輸出層,兩層間的各神經元是連接的,網路中沒有隱含層,輸入層含有 m 個神經元,神經元個數與輸入樣本的特徵數相同,而輸出層有 n 個神經元,神經元間與鄰近的神經元兩兩相接,故 SOM 中兩類型的權值:
SOM 架構如下圖:
SOM 神經網路採用競爭學習演算法,在學習過程中相互競爭,對於每次輸入的樣本向量,對競爭勝利的神經元與鄰近神經元進行權重係數的修改,其他區域神經元的權重則不變。
packageName <- c("kohonen")
for(i in 1:length(packageName)) {
if(!(packageName[i] %in% rownames(installed.packages()))) {
install.packages(packageName[i])
}
}
lapply(packageName, require, character.only = TRUE)
## Loading required package: kohonen
## [[1]]
## [1] TRUE
使用 kohonen 套件內建的資料 wines。wines 收集多筆酒的特徵資料,包含頻果酸(malic acid), 鎂(magnesium), 酚類數值(tot. phenols) 等。
data(wines)
head(wines, 5)
## alcohol malic acid ash ash alkalinity magnesium tot. phenols
## [1,] 13.20 1.78 2.14 11.2 100 2.65
## [2,] 13.16 2.36 2.67 18.6 101 2.80
## [3,] 14.37 1.95 2.50 16.8 113 3.85
## [4,] 13.24 2.59 2.87 21.0 118 2.80
## [5,] 14.20 1.76 2.45 15.2 112 3.27
## flavonoids non-flav. phenols proanth col. int. col. hue OD ratio
## [1,] 2.76 0.26 1.28 4.38 1.05 3.40
## [2,] 3.24 0.30 2.81 5.68 1.03 3.17
## [3,] 3.49 0.24 2.18 7.80 0.86 3.45
## [4,] 2.69 0.39 1.82 4.32 1.04 2.93
## [5,] 3.39 0.34 1.97 6.75 1.05 2.85
## proline
## [1,] 1050
## [2,] 1185
## [3,] 1480
## [4,] 735
## [5,] 1450
som.data.idx <- sample(nrow(wines), 100)
som.training.data <- wines[som.data.idx, ]
som.testing.data <- wines[-som.data.idx, ]
在將資料送入 SOM 學習時,需要將每個特徵的資料進行 scaling,透過計算離中心程度方式來計算,即 $,可參考底下的參數 (center = TRU)。
# the prototype of som
# data: 已經 scale 的資料框
# grid:
# |- somgrid: 依照 x, y 及拓樸方法產生一系列網路的單元,此也決定輸出神經元數目
# |- xdim: (繪圖時) x 方向神經元數
# |- ydim: (繪圖時) y 方向神經元數
# |- topo: (繪圖時)呈現方式,有 "hexagonal" 與 "rectangular" 兩類
# |- neighbourhood.fct: 輸出神經元之間的相鄰函式,有 "gaussian" 與 "bubble" 兩類
som(data, grid = somgrid(xdim,ydim,topo="hexagonal",neighbourhood.fct="gaussian"))
# scale 目的將值進行調整,可分為有中心(center = TRUE)或無中心(center = FALSE)兩類
som.training.data.scale <- scale(som.training.data)
som.model <- som(som.training.data.scale, grid = somgrid(5,5,"hexagonal","gaussian"))
summary(som.model)
## SOM of size 5x5 with a hexagonal topology and a gaussian neighbourhood function.
## Training data included of 100 objects
## The number of layers is 1
## Mean distance to the closest unit in the map: 3.491524
由可以看出 SOM 為一個 5x5 hexagonal 狀的拓樸,輸出神經元相鄰函式使用 gaussian。
# type 為繪圖方式,預設為 codes,有其他含 changes, counts, mappng, ... 等
plot(som.model, type="codes")
plot(som.model, type="counts")
由上述兩張圖可以看出每個輸出神經元使用的特徵為何,及在神經元中使用的特徵各自權重比例。
SOM 為非監督式學習,透過 predict 函式可以了解每筆測試資料屬於哪個神經元的分群結果。
som.testing.data.scale <- scale(som.testing.data)
som.pred <- predict(som.model, som.testing.data.scale)
# 顯示出分群結果
som.pred$unit.classif
## [1] 1 8 1 8 1 7 1 1 12 2 7 2 7 8 11 12 6 7 1 16 6 2 22
## [24] 14 22 13 17 22 12 13 20 18 18 19 25 20 25 20 18 18 22 23 20 21 19 18
## [47] 24 18 19 14 2 24 24 24 24 9 9 14 10 15 5 15 10 15 5 4 4 4 5
## [70] 4 4 5 4 4 10 4 4