forked from hunkim/DeepLearningZeroToAll
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,292 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Lab 05 Logistic Classification(Regression)\n", | ||
"* Logistic Classfication은 True or False와 같은 Binary나 복수개의 다항 분류에 쓰입니다 (Bernoulli Distribution)\n", | ||
"\n", | ||
"### 기본 Library 선언 및 Tensorflow 버전 확인" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"1.10.0\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"%matplotlib inline\n", | ||
"import tensorflow as tf\n", | ||
"\n", | ||
"print(tf.__version__)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 강의에 설명할 Data입니다\n", | ||
"* x_data가 2차원 배열이기에 2차원 공간에 표현하여 x1과 x2를 기준으로 y_data 0과 1로 구분하는 예제입니다\n", | ||
"* Logistic Classification 통해 보라색과 노란색 y_data(Label)을 구분해 보겠습니다." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAEKCAYAAAAB0GKPAAAABHNCSVQICAgIfAhk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Hch21EBNyRZmGRev4vp2MHAcuCvm2HHryaZ2eUBDYxJLMks4ErgrKp6qt/1dKmq1lfVEcBs4KgkE3r4Mck7gWVVtbDftWxnx1bVa4CTgI82Q84T1VTgNcAFVXUk8AzwyS4PaGBMUs1Y/pXAZVV1Vb/r2V6aS/YbgRP7XUvHjgF+uxnT/wbwliR/09+SuldVS5t/lwFXA0f1t6JOLQGW9Fwtf5vRAOmMgTEJNTeBLwLuq6ov9LueriUZSrJH83wG8Dbg/v5W1a2q+lRVza6qucCpwD9U1b/tc1mdSjKzmcRBMzRzAjBhZz9W1S+Ah5O8oml6K9DpxJWpXb75ziDJFcBxwN5JlgDnVdVF/a2qc8cApwN3NeP6AOdU1Xf7WFOX9gcuTTLA6Iekb1bVpJhmOsnsC1w9+nmIqcDlVXVdf0vq3MeBy5oZUg8AH+zyYJN+Wq0kqR2HpCRJrRgYkqRWDAxJUisGhiSpFQNDktSKgSFtB0muS/LEZPnVWE1MBoa0fXye0e++SDstA0MaR0lel+TOZg2Omc36G6+qqv8FrOx3fdKLMem/6S2Np6q6Nck1wGeAGcDfVNWE/XkKTS4GhjT+zgduBZ4D/kOfa5HGjUNS0vjbC5gF7AZM73Mt0rgxMKTx91fAnwCXAZ/tcy3SuHFIShpHSd4PrK2qy5tfx/1RkrcAnwZ+E5jV/Cryh6rq+n7WKm0rf61WktSKQ1KSpFYMDElSKwaGJKkVA0OS1IqBIUlqxcCQJLViYEiSWjEwJEmt/H8I22QHPezQRwAAAABJRU5ErkJggg==\n", | ||
"text/plain": [ | ||
"<Figure size 432x288 with 1 Axes>" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"x_data = [[1, 2],\n", | ||
" [2, 3],\n", | ||
" [3, 1],\n", | ||
" [4, 3],\n", | ||
" [5, 3],\n", | ||
" [6, 2]]\n", | ||
"y_data = [[0],\n", | ||
" [0],\n", | ||
" [0],\n", | ||
" [1],\n", | ||
" [1],\n", | ||
" [1]]\n", | ||
"\n", | ||
"x1 = [x[0] for x in x_data]\n", | ||
"x2 = [x[1] for x in x_data]\n", | ||
"\n", | ||
"colors = [int(y[0] % 3) for y in y_data]\n", | ||
"plt.scatter(x1,x2, c=colors)\n", | ||
"\n", | ||
"plt.xlabel(\"x1\")\n", | ||
"plt.ylabel(\"x2\")\n", | ||
"plt.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 위 Data를 기준으로 가설의 검증을 통해 Logistic Classification 모델을 만들도록 하겠습니다\n", | ||
"* X,Y는 실재 학습에 쓰일 Data (placeholder)\n", | ||
"* W와 b은 학습을 통해 생성되는 모델에 쓰이는 Wegith와 Bias (초기값을 variable : 0이나 Random값으로 가능 tf.random_normal([2, 1]) )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"X = tf.placeholder(tf.float32, shape=[None, 2])\n", | ||
"Y = tf.placeholder(tf.float32, shape=[None, 1])\n", | ||
"\n", | ||
"W = tf.Variable(tf.zeros([2,1]), name='weight')\n", | ||
"b = tf.Variable(tf.zeros([1]), name='bias')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Sigmoid 함수를 가설로 선언합니다\n", | ||
"* Sigmoid는 아래 그래프와 같이 0과 1의 값만을 리턴합니다 tf.sigmoid(tf.matmul(X, W) + b)와 같습니다\n", | ||
"\n", | ||
"$$\n", | ||
"\\begin{align}\n", | ||
"sigmoid(x) & = \\frac{1}{1+e^{-x}} \\\\\\\\\\\n", | ||
"\\end{align}\n", | ||
"$$\n", | ||
"\n", | ||
"![sigmoid](https://upload.wikimedia.org/wikipedia/commons/8/88/Logistic-curve.svg)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"hypothesis = tf.div(1., 1. + tf.exp(tf.matmul(X, W) + b))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 가설을 검증할 Cost 함수를 정의합니다\n", | ||
"$$\n", | ||
"\\begin{align}\n", | ||
"cost(h(x),y) & = −log(h(x)) & if & y=1 \\\\\\\\\\\n", | ||
"cost(h(x),y) & = -log(1−h(x)) & if & y=0\n", | ||
"\\end{align}\n", | ||
"$$\n", | ||
"* 위 두수식을 합치면 아래과 같습니다\n", | ||
"$$\n", | ||
"\\begin{align}\n", | ||
"cost(h(x),y) & = −y log(h(x))−(1−y)log(1−h(x))\n", | ||
"\\end{align}\n", | ||
"$$" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *\n", | ||
" tf.log(1 - hypothesis))\n", | ||
"\n", | ||
"train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 추론한 값은 0.5를 기준(Sigmoid 그래프 참조)로 0과 1의 값을 리턴합니다.\n", | ||
"* Sigmoid 함수를 통해 예측값이 0.5보다 크면 1을 반환하고 0.5보다 작으면 0으로 반환합니다." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 가설을 통해 실재 값과 비교한 정확도를 측정합니다" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.int32))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Tensorflow를 통한 실행을 위해 Session를 선언합니다.\n", | ||
"* 위의 Data를 Cost함수를 통해 학습시킨 후 모델을 생성합니다. \n", | ||
"* 새로운 Data를 통한 검증 수행 [5,2]의 Data로 테스트 수행 (그래프상 1이 나와야 정상입니다)\n", | ||
"* 새로운 Data를 통한 검증 수행 [2,2]의 Data로 테스트 수행 (그래프상 0이 나와야 정상입니다)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"0 0.6931472\n", | ||
"1000 0.41448987\n", | ||
"2000 0.34960732\n", | ||
"3000 0.3013888\n", | ||
"4000 0.26361898\n", | ||
"5000 0.23360115\n", | ||
"6000 0.20939068\n", | ||
"7000 0.18956773\n", | ||
"8000 0.17310147\n", | ||
"9000 0.15924035\n", | ||
"10000 0.1474307\n", | ||
"\n", | ||
"Hypothesis: [[0.02987642]\n", | ||
" [0.1576593 ]\n", | ||
" [0.30070737]\n", | ||
" [0.78328896]\n", | ||
" [0.9407705 ]\n", | ||
" [0.98057085]] \n", | ||
"Correct (Y): [[0.]\n", | ||
" [0.]\n", | ||
" [0.]\n", | ||
" [1.]\n", | ||
" [1.]\n", | ||
" [1.]] \n", | ||
"Accuracy: 1\n", | ||
"[5,2] : [[1.]]\n", | ||
"[2,2] : [[0.]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"test_x1 = [[5,2]] \n", | ||
"test_x2 = [[2,2]] \n", | ||
"\n", | ||
"with tf.Session() as sess:\n", | ||
" sess.run(tf.global_variables_initializer())\n", | ||
" for step in range(10001):\n", | ||
" cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})\n", | ||
" if step % 1000 == 0:\n", | ||
" print(step, cost_val)\n", | ||
"\n", | ||
" # Accuracy report\n", | ||
" h, c, a = sess.run([hypothesis, predicted, accuracy],\n", | ||
" feed_dict={X: x_data, Y: y_data})\n", | ||
" print(\"\\nHypothesis: \", h, \"\\nCorrect (Y): \", c, \"\\nAccuracy: \", a)\n", | ||
"# print(\"W value : \", sess.run(W))\n", | ||
"# print(\"b value : \", sess.run(b))\n", | ||
" print(\"[5,2] : \", sess.run(predicted,feed_dict={X:test_x1}))\n", | ||
" print(\"[2,2] : \", sess.run(predicted,feed_dict={X:test_x2}))" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "conda_tensorflow_p36", | ||
"language": "python", | ||
"name": "conda_tensorflow_p36" | ||
}, | ||
"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.6.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |