{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 교재 페이지 (170-172) #####"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "12) Softmax classifier - 실습"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "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",
      "WARNING:tensorflow:From C:\\Users\\jsdata00010\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\ops\\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.cast instead.\n",
      "0 1.4704988\n",
      "200 0.35783318\n",
      "400 0.2696035\n",
      "600 0.22536103\n",
      "800 0.19696712\n",
      "1000 0.17623064\n",
      "1200 0.1600553\n",
      "1400 0.14695181\n",
      "1600 0.13606665\n",
      "1800 0.126854\n",
      "2000 0.11894042\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "x_data = [[5.8, 4, 1.2, 0.2], [5.7, 3.8, 1.7, 0.3], [4.5, 2.3, 1.3, 0.3], [7, 3.2, 4.7, 1.4], [6.7, 3, 5, 1.7], \n",
    "          [6.3, 2.3, 4.4, 1.3], [7.6, 3, 6.6, 2.1], [7.7, 2.8, 6.7, 2], [7.9, 3.8, 6.4, 2]]\n",
    "y_data = [[1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 0, 1], [0, 0, 1], [0, 0, 1]]\n",
    "X = tf.placeholder(\"float\", [None, 4])\n",
    "Y = tf.placeholder(\"float\", [None, 3])\n",
    "nb_classes = 3\n",
    "W = tf.Variable(tf.random_normal([4, nb_classes]), name='weight')\n",
    "b = tf.Variable(tf.random_normal([nb_classes]), name='bias')\n",
    "\n",
    "# tf.nn.softmax computes softmax activations\n",
    "# softmax = exp(logits) / reduce_sum(exp(logits), dim)\n",
    "hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)\n",
    "\n",
    "# Cross entropy cost/loss\n",
    "cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))\n",
    "optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.03).minimize(cost)\n",
    "\n",
    "# Launch graph\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for step in range(2001):\n",
    "        sess.run(optimizer, feed_dict={X: x_data, Y: y_data})\n",
    "        if step % 200 == 0:\n",
    "            print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data})) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 2.5622637\n",
      "200 0.43992555\n",
      "400 0.33706173\n",
      "600 0.27623644\n",
      "800 0.23504813\n",
      "1000 0.20521076\n",
      "1200 0.18258652\n",
      "1400 0.16483204\n",
      "1600 0.15051664\n",
      "1800 0.13871782\n",
      "2000 0.12881432\n",
      "WARNING:tensorflow:From <ipython-input-2-da876db2fe27>:9: arg_max (from tensorflow.python.ops.gen_math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `tf.math.argmax` instead\n",
      "[[9.9893659e-01 1.0633927e-03 2.2101447e-09]] [0]\n"
     ]
    }
   ],
   "source": [
    "# Launch graph\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for step in range(2001):\n",
    "        sess.run(optimizer, feed_dict={X: x_data, Y: y_data})\n",
    "        if step % 200 == 0:\n",
    "            print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data})) \n",
    "    a = sess.run(hypothesis, feed_dict={X: [[5.8, 4, 1.2, 0.2]]})\n",
    "    print(a, sess.run(tf.arg_max(a, 1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 2.6884649\n",
      "200 0.4551611\n",
      "400 0.342341\n",
      "600 0.2787046\n",
      "800 0.23648378\n",
      "1000 0.20627716\n",
      "1200 0.18355021\n",
      "1400 0.16579954\n",
      "1600 0.15152562\n",
      "1800 0.1397765\n",
      "2000 0.12991899\n",
      "[[9.9790335e-01 2.0966369e-03 3.7246193e-09]\n",
      " [3.5461061e-02 9.1616428e-01 4.8374623e-02]\n",
      " [8.7614421e-04 2.7703172e-01 7.2209215e-01]] [0 1 2]\n"
     ]
    }
   ],
   "source": [
    "# Launch graph\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for step in range(2001):\n",
    "        sess.run(optimizer, feed_dict={X: x_data, Y: y_data})\n",
    "        if step % 200 == 0:\n",
    "            print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}))    \n",
    "    all = sess.run(hypothesis, feed_dict={X: [[5.8, 4, 1.2, 0.2],\n",
    "                                              [7, 3.2, 4.7, 1.4],\n",
    "                                              [7.9, 3.8, 6.4, 2]]})\n",
    "    print(all, sess.run(tf.arg_max(all, 1)))"
   ]
  }
 ],
 "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
}
