Skip to content

Instantly share code, notes, and snippets.

@ecjang
Created February 11, 2018 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecjang/59630de413dae15bca8f4c10809d406c to your computer and use it in GitHub Desktop.
Save ecjang/59630de413dae15bca8f4c10809d406c to your computer and use it in GitHub Desktop.
006. Logistic_Regression
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `11. ML lec 5-1 Logistic Classification의 가설 함수 정의`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Regression (HCG)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- H : Hypothesis / C : Cost Function / G : Gradient Decent\n",
"- 주어진 자료를 가지고 가설을 세우고 코스트를 점차적으로 줄여나가는 방법\n",
"- cost 는 가설과 실제값 과의 차이값\n",
"- cost의 최소값을 찾는 방법이 Gradient Decent.\n",
"- Gradient Decent에서 alpha 값은 움직이는 값"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Hypothesis : $ H(x) = WX $\n",
"2. Cost : $ cost(W) = \\frac{1}{m}\\sum (H(x) = WX)^2 $\n",
"3. Gradicent Decent : $ W := W - \\alpha \\frac{\\sigma}{\\sigma W} cost(W) $"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Classification : 0 or 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Spam Detection : Spam or Ham\n",
"- Facebook feed : Show or Hide\n",
"- Credit Card Fraudulent Transaction detection : Legitimate/draud"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 많은 자료들을 0과 1로 표현하려고 하니 문제점들이 발생.\n",
"- 너무 큰 입력값이나, 애매한 중간값을 계산하기 어려움.\n",
"- 그래서 입력값들을 0과 1사이로 정확하게 바꾸어 줄 수 있는 함수를 고민."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Sigmod : Curved in two direction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$ g(z) = \\frac{1}{(1+e^{-W^{t}X})} $$ "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- S자의 완만한 그래프가 그려짐. 값이 커지면 1에 가까워 지고, 작아지면 0에 가까워 진다.\n",
"- e로 시작하는 계산식이 0일 경우 -> 1/1이 되어서 최댓값인 1이 된다.\n",
"- e로 시작하는 계산식이 매우 클 때 -> 1/e 꼴이 되어 최소값 0이 된다.\n",
"- $WX$가 0일 경우 -> 지수가 0이 되고 결국 1/2가 되어 중간값 0.5가 된다."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `12. ML lec 5-2 Logistic Regression의 cost 함수 설명`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"기존 함수 : $ H(x) = Wx + b $ \n",
"<BR>\n",
"시그모이드 적용 : $ H(X) = \\frac{1}{1 + e{-W^{t}X}} $"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img width=\"70%\" align=\"left\" src=\"img/cost_function.png\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 시그모이드를 적용한 함수를 그리면 울퉁불퉁해진다.\n",
"- 이대로 최적화 함수를 적용하면 최저점을 잘 못 인식할 수 있는 문제발생.\n",
"- 일부 최저점(Local Minimum)이 아닌 전체 최저점(Global Minimum)을 구해야 한다."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### New Cost Function for Logistic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$ cost(W) = \\frac{1}{m} \\sum c(H(x), y) $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\\begin{equation}\n",
"f(x)=\\left \\{\\begin{array}{ll}\n",
"\\ -log(H(x)) : y = 1 \\\\\n",
"\\ -log(1- H(x)) : y = 0 \\\\\n",
"\\end{array}\n",
"\\right.\n",
"\\end{equation}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$ c:(H(x), y) = ylog(H(x)) - (1-y)log(1 - H(x)) $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Minimize cost : Grdient Decent Algorithm"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"\n",
"# cost function\n",
"cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(hypothesis) + (1-Y)*tf.log(1-hypothesis)))\n",
"\n",
"# Minimize\n",
"a = tf.Variable(0.1) # Learning rate, alpha\n",
"optimizer = tf.train.GradientDescentOptimizer(a)\n",
"train = optimizer.minimize(cost)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "python3.5",
"language": "python",
"name": "python3.5"
},
"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.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment