{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 교재 페이지 (161-162) #####"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9-1) Logistic (regression) classifier - 실습"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From C:\\Users\\jsdata00010\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\framework\\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n",
      "0 0.34881485\n",
      "500 0.34880117\n",
      "1000 0.34880036\n",
      "1500 0.34879968\n",
      "2000 0.34879893\n",
      "2500 0.34879827\n",
      "3000 0.34879756\n",
      "3500 0.348797\n",
      "4000 0.34879643\n",
      "4500 0.34879583\n",
      "5000 0.34879535\n",
      "5500 0.34879482\n",
      "6000 0.34879422\n",
      "6500 0.34879377\n",
      "7000 0.34879336\n",
      "7500 0.34879294\n",
      "8000 0.34879252\n",
      "8500 0.34879214\n",
      "9000 0.3487918\n",
      "9500 0.3487913\n",
      "10000 0.34879103\n",
      "10500 0.3487907\n",
      "11000 0.34879035\n",
      "11500 0.34879017\n",
      "12000 0.34878987\n",
      "12500 0.34878957\n",
      "13000 0.34878927\n",
      "13500 0.3487891\n",
      "14000 0.34878877\n",
      "14500 0.34878856\n",
      "15000 0.34878838\n",
      "15500 0.3487882\n",
      "16000 0.348788\n",
      "16500 0.34878784\n",
      "17000 0.34878764\n",
      "17500 0.3487874\n",
      "18000 0.34878728\n",
      "18500 0.34878707\n",
      "19000 0.34878695\n",
      "19500 0.34878683\n",
      "20000 0.34878674\n",
      "\n",
      "Hypothesis:  [[0.0192976 ]\n",
      " [0.12050569]\n",
      " [0.26554748]\n",
      " [0.42810357]\n",
      " [0.6078172 ]\n",
      " [0.6639006 ]\n",
      " [0.9788401 ]\n",
      " [0.9833219 ]\n",
      " [0.93222386]\n",
      " [0.9988249 ]] \n",
      "Correct (Y):  [[0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.8\n",
      "Cost, W, b 의 값은 다음과 같습니다. Cost:  0.34878674 W:  [[0.2425796]] B:  [[-6.596662]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "x_data = [[11],[19],[23],[26],[29],[30],[43],[44],[38],[55]]\n",
    "y_data = [[0],[0],[1],[0],[0],[1],[1],[1],[1],[1]]\n",
    "\n",
    "X = tf.placeholder(tf.float32, shape=[None, 1])\n",
    "Y = tf.placeholder(tf.float32, shape=[None, 1])\n",
    "\n",
    "W = tf.Variable(tf.constant(0.245, shape = [1,1]), name='weight')\n",
    "b = tf.Variable(tf.constant(-6.65, shape = [1,1]), name='bias')\n",
    "\n",
    "#W = tf.Variable(tf.random_normal([1,1], mean=0.001, stddev=0.001), name='weight')\n",
    "#b = tf.Variable(tf.random_normal([1]), name='bias')\n",
    "# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W) + b))\n",
    "hypothesis = tf.sigmoid(tf.matmul(X, W) + b)\n",
    "\n",
    "# cost/loss function\n",
    "cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))\n",
    "train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)\n",
    "\n",
    "# Accuracy computation\n",
    "# True if hypothesis>0.5 else False\n",
    "predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)\n",
    "accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))\n",
    "\n",
    "# Launch graph\n",
    "with tf.Session() as sess:\n",
    "    # Initialize TensorFlow variables\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for step in range(20001):\n",
    "        cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})\n",
    "        if step % 500 == 0:\n",
    "            print(step, cost_val)\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",
    "    training_cost = sess.run(cost, feed_dict={X: x_data, Y: y_data})\n",
    "    print(\"Cost, W, b 의 값은 다음과 같습니다. Cost: \", training_cost, \"W: \",sess.run(W), \"B: \", sess.run(b))"
   ]
  }
 ],
 "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
