You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
387 lines
30 KiB
Plaintext
387 lines
30 KiB
Plaintext
1 year ago
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# SOM (Self Organizing Map, 자기조직화지도)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### 개요\n",
|
||
|
"* 비지도학습 중 하나로, 대뇌피질의 시각피질의 학습 과정을 모델화한 인공신경망으로써 자율 학습에 의한 클러스터링을 수행하는 알고리즘"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### 역사\n",
|
||
|
"* 1980년대 후반에 핀란드의 컴퓨터 과학자 Teuvo Kohonen에 의해 개발\n",
|
||
|
"* SOM은 분류, 군집화, 데이터 시각화 등 다양한 분야에서 사용되며, 인공신경망 분야에서 가장 성공적인 알고리즘 중 하나로 평가됨\n",
|
||
|
"* 초기에는 이미지 인식과 음성 인식 분야에서 주로 사용되었으나 현재는 데이터 분석 등의 다양한 분야에서도 사용됨"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### 특징\n",
|
||
|
"* 인간의 뇌에서 발견된 \"자기 조직화\" 원리를 모델링하여 입력 데이터의 유사성을 기반으로 뉴런들을 2D 혹은 3D 지도상에 배치함\n",
|
||
|
"* Topological Ordering (위상적 순서)에 따라 지도상에서 인접한 뉴런들이 입력 데이터의 유사성을 반영하여 군집화(Clusturing)함\n",
|
||
|
"입력 데이터에 대한 어떤 사전 정보나 라벨이 없이도, 데이터가 가지고 있는 패턴과 특성을 스스로 학습하고 이를 지도상으로 표현함\n",
|
||
|
"* 주로 데이터의 시각화 및 차원 축소에 사용되며, 입력 데이터의 분포를 파악하여 그룹화하여 시각적으로 표현하므로, 복잡한 데이터를 시각적으로 이해하고 분석하는 데 도움을 줌"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### 주요 용어\n",
|
||
|
"* 클러스터링(clustering): 데이터의 유사성에 기초하여 데이터를 몇몇의 그룹으로 분류하는 기법\n",
|
||
|
"* 입력층(input layer): 입력 벡터를 입력받는 층\n",
|
||
|
"* 경쟁층(competitive layer): 입력 벡터의 특성에 따라 입력 벡터가 한 점으로 클러스터링 되는 층\n",
|
||
|
"* 가중치(weight): 인공신경망에서 가중치는 각 입력 값에 대한 입력 값의 중요도를 값을 말함\n",
|
||
|
"* 노드(node): 경쟁층에서 입력 벡터들이 서로의 유사성에 의해 모이는 하나의 영역"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"![SOM Layer Structure](./images/SOM1.png)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### 알고리즘\n",
|
||
|
"1-1) 가중치 행렬 각 원소의 값을 임의의 0보다 크고 1보다 작은 값으로 초기화\n",
|
||
|
"\n",
|
||
|
"2-1) 입력 벡터와 경쟁층에 존재하는 j\n",
|
||
|
"개의 노드에 대해 입력 벡터와 노드 간의 거리 Dij\n",
|
||
|
"를 계산\n",
|
||
|
"\n",
|
||
|
"2-2) 현재 입력 벡터와 Dij\n",
|
||
|
"값이 가장 작은 경쟁층의 노드를 선택\n",
|
||
|
"\n",
|
||
|
"2-3) 해당 노드의 가중치와 이웃 노드의 가중치를 수정\n",
|
||
|
"\n",
|
||
|
"5-1) 현재 입력 벡터가 마지막 입력 벡터라면 다음 과정으로 이동하고, 그렇지 않다면 과정 2로 돌아간다\n",
|
||
|
"\n",
|
||
|
"6-1) 현재 반복 횟수가 최대 반복 횟수라면 알고리즘을 종료\n",
|
||
|
"\n",
|
||
|
"6-2) 현재 반복 횟수가 최대 반복 횟수가 아니면 현재 입력 벡터를 처음 입력 벡터로 설정하고 과정 2로 돌아간다"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"![SOM Algorithm](./images/SOM2.png)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### 참고\n",
|
||
|
"https://untitledtblog.tistory.com/5"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### SOM 예제 코드"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 69,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import numpy as np\n",
|
||
|
"import matplotlib.pyplot as plt"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 70,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"array([[0.76959259, 0.03105338],\n",
|
||
|
" [0.91933906, 0.8980492 ],\n",
|
||
|
" [0.74919767, 0.46775453],\n",
|
||
|
" [0.66010482, 0.70532455],\n",
|
||
|
" [0.07347433, 0.31437638],\n",
|
||
|
" [0.52162755, 0.75475037],\n",
|
||
|
" [0.07618824, 0.5595569 ],\n",
|
||
|
" [0.37694104, 0.88389 ],\n",
|
||
|
" [0.59261356, 0.49140373],\n",
|
||
|
" [0.11567152, 0.32084966],\n",
|
||
|
" [0.81627506, 0.7724125 ],\n",
|
||
|
" [0.7708446 , 0.98667885],\n",
|
||
|
" [0.57884267, 0.92658698],\n",
|
||
|
" [0.48815338, 0.04666865],\n",
|
||
|
" [0.07971729, 0.56926156],\n",
|
||
|
" [0.93371788, 0.20473364],\n",
|
||
|
" [0.71241481, 0.68994675],\n",
|
||
|
" [0.69855141, 0.55690579],\n",
|
||
|
" [0.87081581, 0.48218263],\n",
|
||
|
" [0.73239668, 0.47015934],\n",
|
||
|
" [0.48234206, 0.38374233],\n",
|
||
|
" [0.51407113, 0.3925697 ],\n",
|
||
|
" [0.97608349, 0.58044996],\n",
|
||
|
" [0.67676596, 0.51393561],\n",
|
||
|
" [0.37059459, 0.99168193],\n",
|
||
|
" [0.17015263, 0.17250751],\n",
|
||
|
" [0.6980668 , 0.47156525],\n",
|
||
|
" [0.9674704 , 0.29847506],\n",
|
||
|
" [0.85297608, 0.15389296],\n",
|
||
|
" [0.99135256, 0.95353731],\n",
|
||
|
" [0.75749781, 0.05744834],\n",
|
||
|
" [0.5850026 , 0.69425312],\n",
|
||
|
" [0.35572734, 0.77261987],\n",
|
||
|
" [0.96259553, 0.10738437],\n",
|
||
|
" [0.0178066 , 0.68856146],\n",
|
||
|
" [0.78219693, 0.6523648 ],\n",
|
||
|
" [0.18744908, 0.73438952],\n",
|
||
|
" [0.19713904, 0.56662014],\n",
|
||
|
" [0.11656314, 0.50708698],\n",
|
||
|
" [0.33800968, 0.03634445],\n",
|
||
|
" [0.64802194, 0.90416709],\n",
|
||
|
" [0.7668028 , 0.7619133 ],\n",
|
||
|
" [0.55338936, 0.78892134],\n",
|
||
|
" [0.20716839, 0.95280491],\n",
|
||
|
" [0.47688275, 0.30063862],\n",
|
||
|
" [0.22233577, 0.02612442],\n",
|
||
|
" [0.21021643, 0.66526042],\n",
|
||
|
" [0.87039248, 0.75634555],\n",
|
||
|
" [0.91421121, 0.60350262],\n",
|
||
|
" [0.32014712, 0.61909491],\n",
|
||
|
" [0.03320721, 0.69900356],\n",
|
||
|
" [0.0806021 , 0.68826953],\n",
|
||
|
" [0.52828811, 0.78636528],\n",
|
||
|
" [0.49048742, 0.60616199],\n",
|
||
|
" [0.07021355, 0.64982408],\n",
|
||
|
" [0.21773879, 0.55571391],\n",
|
||
|
" [0.02119119, 0.62099936],\n",
|
||
|
" [0.75589808, 0.88236869],\n",
|
||
|
" [0.99479197, 0.98599938],\n",
|
||
|
" [0.35444256, 0.84292181],\n",
|
||
|
" [0.90941177, 0.30758414],\n",
|
||
|
" [0.87616741, 0.18060184],\n",
|
||
|
" [0.22973575, 0.50353087],\n",
|
||
|
" [0.33569227, 0.82710827],\n",
|
||
|
" [0.03575231, 0.86915894],\n",
|
||
|
" [0.59710596, 0.91274408],\n",
|
||
|
" [0.61854338, 0.06007 ],\n",
|
||
|
" [0.69497861, 0.26505075],\n",
|
||
|
" [0.80347052, 0.93909288],\n",
|
||
|
" [0.8998602 , 0.34498986],\n",
|
||
|
" [0.96359265, 0.66780466],\n",
|
||
|
" [0.6180583 , 0.36145526],\n",
|
||
|
" [0.22507441, 0.24977 ],\n",
|
||
|
" [0.82067222, 0.65858647],\n",
|
||
|
" [0.26533719, 0.50457457],\n",
|
||
|
" [0.20615695, 0.52028333],\n",
|
||
|
" [0.78530409, 0.23287443],\n",
|
||
|
" [0.81894409, 0.32542015],\n",
|
||
|
" [0.66607148, 0.0717416 ],\n",
|
||
|
" [0.15435642, 0.57144805],\n",
|
||
|
" [0.60568206, 0.62824524],\n",
|
||
|
" [0.40981871, 0.51319754],\n",
|
||
|
" [0.14660118, 0.14759826],\n",
|
||
|
" [0.89091339, 0.12148139],\n",
|
||
|
" [0.48159329, 0.76667587],\n",
|
||
|
" [0.73766975, 0.76237422],\n",
|
||
|
" [0.32449576, 0.11193535],\n",
|
||
|
" [0.05351455, 0.37051478],\n",
|
||
|
" [0.15487227, 0.0884276 ],\n",
|
||
|
" [0.40902546, 0.73216362],\n",
|
||
|
" [0.6227993 , 0.54298886],\n",
|
||
|
" [0.70489165, 0.60729615],\n",
|
||
|
" [0.43506953, 0.26510733],\n",
|
||
|
" [0.17818975, 0.41551925],\n",
|
||
|
" [0.07820619, 0.31987945],\n",
|
||
|
" [0.27646196, 0.00825832],\n",
|
||
|
" [0.30999502, 0.67284126],\n",
|
||
|
" [0.6372436 , 0.48780002],\n",
|
||
|
" [0.66962877, 0.44225145],\n",
|
||
|
" [0.48185839, 0.17663244]])"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 70,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# 입력 데이터 생성\n",
|
||
|
"data = np.random.rand(100, 2)\n",
|
||
|
"# data = np.array([[0.8, 0.0064],\n",
|
||
|
"# [0.12, 0.2848],\n",
|
||
|
"# [0.24, 0.7472],\n",
|
||
|
"# [0.68, 0.3488]])\n",
|
||
|
"data"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 71,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# SOM 파라미터 설정\n",
|
||
|
"map_width = 10\n",
|
||
|
"map_height = 10\n",
|
||
|
"lr = 0.1\n",
|
||
|
"num_iterations = 1000\n",
|
||
|
"# map_width = 2\n",
|
||
|
"# map_height = 2\n",
|
||
|
"# lr = 0.1\n",
|
||
|
"# num_iterations = 1000"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 78,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"(10, 10, 2)\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAhYAAAENCAYAAABTviwWAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy88F64QAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAZMklEQVR4nO3de3DU9b3G8Se7MbsBknBRkEiAQK0Bg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BBQIBO9MC8Bz2BpBcoioWd9xxh3bt2tXgsrFjxyovL09PPvnkN5YDALA3gOQSVbHIyMhQfn5+g8tat26tDh06fONyAJDYG0Cy4Z03AQCANVH/Vcg/W7t2rYUxACQT9gaQuDhjAQAArKFYAAAAaygWAADAGooFAACw5pJfvNlcszeuUZsM+73mzrcnW8+UpNV5h5zkStI7ry9xktt/drGTXEla9lGms+yUselOco3DGt1j8gknucPK1ljPPJkS0RzrqbFR8J2PldYmzXru5xNyrWdK0p7ibCe5knR7zq7Gr9QM+7J6O8mVpMy/hR1mu8nNmOAoWFKn9BonucW3/NRJbl0kJOnFRq/HGQsAAGANxQIAAFhDsQAAANZQLAAAgDUUCwAAYA3FAgAAWEOxAAAA1lAsAACANRQLAABgDcUCAABYQ7EAAADWUCwAAIA1FAsAAGANxQIAAFhDsQAAANZQLAAAgDUUCwAAYA3FAgAAWEOxAAAA1lAsAACANRQLAABgDcUCAABYkxqvO/7x6xPkCwat57b5MsV6piR98deuTnIlqc//jHG
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 2 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# SOM 초기화\n",
|
||
|
"som_map = np.random.rand(map_width, map_height, 2)\n",
|
||
|
"print(som_map.shape)\n",
|
||
|
"fig, (ax1, ax2) = plt.subplots(1, 2)\n",
|
||
|
"ax1.imshow(som_map[:,:,0])\n",
|
||
|
"ax2.imshow(som_map[:,:,1])\n",
|
||
|
"plt.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 73,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# SOM 학습\n",
|
||
|
"for i in range(num_iterations):\n",
|
||
|
" # 랜덤한 입력 데이터 선택\n",
|
||
|
" input_data = data[np.random.choice(data.shape[0])]\n",
|
||
|
" \n",
|
||
|
" # 가장 유사한 뉴런 찾기\n",
|
||
|
" # 3D som_map과 input_data의 차이^2를 구하여 z축으로 모두 합한 거리\n",
|
||
|
" distances = np.sum((som_map - input_data) ** 2, axis=2) \n",
|
||
|
" # 위에서 구한 z축의 거리들 중 가장 작은 뉴런\n",
|
||
|
" winner = np.argmin(distances)\n",
|
||
|
" # 3D z축에서 선택된 가장 작은 뉴런을 2D map으로 맵핑한 좌표\n",
|
||
|
" x, y = np.unravel_index(winner, (map_width, map_height))\n",
|
||
|
" \n",
|
||
|
" # 학습률 계산\n",
|
||
|
" learning_rate = lr * (1 - i/num_iterations)\n",
|
||
|
" \n",
|
||
|
" # 뉴런 가중치 업데이트\n",
|
||
|
" for j in range(map_width):\n",
|
||
|
" for k in range(map_height):\n",
|
||
|
" dist = np.sqrt((x-j)**2 + (y-k)**2)\n",
|
||
|
" if dist < 3:\n",
|
||
|
" som_map[j, k] += learning_rate * (input_data - som_map[j, k])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 74,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"(10, 10, 2)\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAhYAAAENCAYAAABTviwWAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy88F64QAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAY2UlEQVR4nO3df2xV9f3H8de9t723LdyWXxattFCNGUpRmQWjLPsR+c443GaWmC2pGcPFLa4MGMkmuKAxpFbzXQz7qkMhEfl+B6J+M75zJtOQbuiXCOPXcKKTYmq0AwuCeG9L6b29536+f2x0305Ke8rnc2/Pvc9Hcv/g5N73eXN7zvu+7rn3nhMyxhgBAAB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39tN9JXUkq/dspJ3Uznyac1PXMyJ4LjlgAAABrCBYAAMAaggUAALCGYAEAAKwhWAAAAGsIFgAAwBqCBQAAsIZgAQAArCFYAAAAawgWAADAGoIFAACwhmABAACsIVgAAABrCBYAAMAaggUAALCGYAEAAKwhWAAAAGsIFgAAwBqCBQAAsIZgAQAArCFYAAAAawgWAADAmpJ8rbgiHFVF2H6uqTL91mtKUjzc56SuJI0vTTmpWzfptJO6ktRVGndW+0ws5qRu35QKJ3UlyYuGnNQNZe3X9FLBfT/RfrpakZT97cPLuvn7pTPuRqzpizipG/LcPBeSVHLWXe2Io9rhPjevKZJkkj1O6oZKo27qmpA0gqcjuBMGAACMOQQLAABgDcE
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 2 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# SOM 시각화\n",
|
||
|
"print(som_map.shape)\n",
|
||
|
"fig, (ax1, ax2) = plt.subplots(1, 2)\n",
|
||
|
"ax1.imshow(som_map[:,:,0])\n",
|
||
|
"ax2.imshow(som_map[:,:,1])\n",
|
||
|
"plt.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### 참고\n",
|
||
|
"ChatGPT"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]"
|
||
|
},
|
||
|
"orig_nbformat": 4,
|
||
|
"vscode": {
|
||
|
"interpreter": {
|
||
|
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 2
|
||
|
}
|