{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 교재 페이지 (180-182) #####"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "16) Learning rate, Evaluation - 실습"
   ]
  },
  {
   "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 10.386779\n",
      "200 0.30465776\n",
      "400 0.24253613\n",
      "600 0.20449117\n",
      "800 0.17816526\n",
      "1000 0.15874352\n",
      "1200 0.14375313\n",
      "1400 0.1317818\n",
      "1600 0.12196398\n",
      "1800 0.11373928\n",
      "2000 0.10672906\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": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 15.949824\n",
      "10 15.110094\n",
      "20 11.461439\n",
      "30 11.743774\n",
      "40 0.646675\n",
      "50 0.036938574\n",
      "60 2.0785892\n",
      "70 10.980524\n",
      "80 5.154892\n",
      "90 0.11618548\n",
      "100 0.5117892\n",
      "110 0.4350509\n",
      "120 7.576338\n",
      "130 nan\n",
      "140 nan\n",
      "150 nan\n",
      "160 nan\n",
      "170 nan\n",
      "180 nan\n",
      "190 nan\n",
      "200 nan\n",
      "210 nan\n",
      "220 nan\n",
      "230 nan\n",
      "240 nan\n",
      "250 nan\n",
      "260 nan\n",
      "270 nan\n",
      "280 nan\n",
      "290 nan\n",
      "300 nan\n",
      "310 nan\n",
      "320 nan\n",
      "330 nan\n",
      "340 nan\n",
      "350 nan\n",
      "360 nan\n",
      "370 nan\n",
      "380 nan\n",
      "390 nan\n",
      "400 nan\n",
      "410 nan\n",
      "420 nan\n",
      "430 nan\n",
      "440 nan\n",
      "450 nan\n",
      "460 nan\n",
      "470 nan\n",
      "480 nan\n",
      "490 nan\n",
      "500 nan\n",
      "510 nan\n",
      "520 nan\n",
      "530 nan\n",
      "540 nan\n",
      "550 nan\n",
      "560 nan\n",
      "570 nan\n",
      "580 nan\n",
      "590 nan\n",
      "600 nan\n",
      "610 nan\n",
      "620 nan\n",
      "630 nan\n",
      "640 nan\n",
      "650 nan\n",
      "660 nan\n",
      "670 nan\n",
      "680 nan\n",
      "690 nan\n",
      "700 nan\n",
      "710 nan\n",
      "720 nan\n",
      "730 nan\n",
      "740 nan\n",
      "750 nan\n",
      "760 nan\n",
      "770 nan\n",
      "780 nan\n",
      "790 nan\n",
      "800 nan\n",
      "810 nan\n",
      "820 nan\n",
      "830 nan\n",
      "840 nan\n",
      "850 nan\n",
      "860 nan\n",
      "870 nan\n",
      "880 nan\n",
      "890 nan\n",
      "900 nan\n",
      "910 nan\n",
      "920 nan\n",
      "930 nan\n",
      "940 nan\n",
      "950 nan\n",
      "960 nan\n",
      "970 nan\n",
      "980 nan\n",
      "990 nan\n",
      "1000 nan\n",
      "1010 nan\n",
      "1020 nan\n",
      "1030 nan\n",
      "1040 nan\n",
      "1050 nan\n",
      "1060 nan\n",
      "1070 nan\n",
      "1080 nan\n",
      "1090 nan\n",
      "1100 nan\n",
      "1110 nan\n",
      "1120 nan\n",
      "1130 nan\n",
      "1140 nan\n",
      "1150 nan\n",
      "1160 nan\n",
      "1170 nan\n",
      "1180 nan\n",
      "1190 nan\n",
      "1200 nan\n",
      "1210 nan\n",
      "1220 nan\n",
      "1230 nan\n",
      "1240 nan\n",
      "1250 nan\n",
      "1260 nan\n",
      "1270 nan\n",
      "1280 nan\n",
      "1290 nan\n",
      "1300 nan\n",
      "1310 nan\n",
      "1320 nan\n",
      "1330 nan\n",
      "1340 nan\n",
      "1350 nan\n",
      "1360 nan\n",
      "1370 nan\n",
      "1380 nan\n",
      "1390 nan\n",
      "1400 nan\n",
      "1410 nan\n",
      "1420 nan\n",
      "1430 nan\n",
      "1440 nan\n",
      "1450 nan\n",
      "1460 nan\n",
      "1470 nan\n",
      "1480 nan\n",
      "1490 nan\n",
      "1500 nan\n",
      "1510 nan\n",
      "1520 nan\n",
      "1530 nan\n",
      "1540 nan\n",
      "1550 nan\n",
      "1560 nan\n",
      "1570 nan\n",
      "1580 nan\n",
      "1590 nan\n",
      "1600 nan\n",
      "1610 nan\n",
      "1620 nan\n",
      "1630 nan\n",
      "1640 nan\n",
      "1650 nan\n",
      "1660 nan\n",
      "1670 nan\n",
      "1680 nan\n",
      "1690 nan\n",
      "1700 nan\n",
      "1710 nan\n",
      "1720 nan\n",
      "1730 nan\n",
      "1740 nan\n",
      "1750 nan\n",
      "1760 nan\n",
      "1770 nan\n",
      "1780 nan\n",
      "1790 nan\n",
      "1800 nan\n",
      "1810 nan\n",
      "1820 nan\n",
      "1830 nan\n",
      "1840 nan\n",
      "1850 nan\n",
      "1860 nan\n",
      "1870 nan\n",
      "1880 nan\n",
      "1890 nan\n",
      "1900 nan\n",
      "1910 nan\n",
      "1920 nan\n",
      "1930 nan\n",
      "1940 nan\n",
      "1950 nan\n",
      "1960 nan\n",
      "1970 nan\n",
      "1980 nan\n",
      "1990 nan\n",
      "2000 nan\n",
      "WARNING:tensorflow:From <ipython-input-3-61ac3c7899d2>:27: 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",
      "[[nan nan nan]] [0]\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.9).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 % 10 == 0:\n",
    "            print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data})) \n",
    "    a = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9]]})\n",
    "    print(a, sess.run(tf.arg_max(a, 1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 12.094679\n",
      "200 12.094679\n",
      "400 12.094679\n",
      "600 12.094679\n",
      "800 12.094679\n",
      "1000 12.094679\n",
      "1200 12.094679\n",
      "1400 12.094679\n",
      "1600 12.094679\n",
      "1800 12.094679\n",
      "2000 12.094679\n",
      "[[9.9999893e-01 5.2081528e-08 1.0592121e-06]] [0]\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=1e-10).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})) \n",
    "    a = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9]]})\n",
    "    print(a, sess.run(tf.arg_max(a, 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
}
