{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 교재 페이지 (172-175) #####"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "13) Softmax Cross entropy with logits - 실습"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(101, 16) (101, 1)\n",
      "one_hot: Tensor(\"one_hot:0\", shape=(?, 1, 7), dtype=float32)\n",
      "reshape one_hot: Tensor(\"Reshape:0\", shape=(?, 7), dtype=float32)\n",
      "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",
      "Step:     0\tCost: 10.559\tAcc: 4.95%\n",
      "Step:   100\tCost: 0.638\tAcc: 85.15%\n",
      "Step:   200\tCost: 0.357\tAcc: 89.11%\n",
      "Step:   300\tCost: 0.247\tAcc: 95.05%\n",
      "Step:   400\tCost: 0.192\tAcc: 96.04%\n",
      "Step:   500\tCost: 0.158\tAcc: 97.03%\n",
      "Step:   600\tCost: 0.135\tAcc: 98.02%\n",
      "Step:   700\tCost: 0.118\tAcc: 98.02%\n",
      "Step:   800\tCost: 0.105\tAcc: 100.00%\n",
      "Step:   900\tCost: 0.095\tAcc: 100.00%\n",
      "Step:  1000\tCost: 0.087\tAcc: 100.00%\n",
      "Step:  1100\tCost: 0.080\tAcc: 100.00%\n",
      "Step:  1200\tCost: 0.074\tAcc: 100.00%\n",
      "Step:  1300\tCost: 0.069\tAcc: 100.00%\n",
      "Step:  1400\tCost: 0.065\tAcc: 100.00%\n",
      "Step:  1500\tCost: 0.061\tAcc: 100.00%\n",
      "Step:  1600\tCost: 0.058\tAcc: 100.00%\n",
      "Step:  1700\tCost: 0.055\tAcc: 100.00%\n",
      "Step:  1800\tCost: 0.052\tAcc: 100.00%\n",
      "Step:  1900\tCost: 0.050\tAcc: 100.00%\n",
      "Step:  2000\tCost: 0.047\tAcc: 100.00%\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 6 Real Y: 6\n",
      "[True] Prediction: 6 Real Y: 6\n",
      "[True] Prediction: 6 Real Y: 6\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 5 Real Y: 5\n",
      "[True] Prediction: 4 Real Y: 4\n",
      "[True] Prediction: 4 Real Y: 4\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 5 Real Y: 5\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 5 Real Y: 5\n",
      "[True] Prediction: 5 Real Y: 5\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 5 Real Y: 5\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 6 Real Y: 6\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 5 Real Y: 5\n",
      "[True] Prediction: 4 Real Y: 4\n",
      "[True] Prediction: 6 Real Y: 6\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 2 Real Y: 2\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 6 Real Y: 6\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 2 Real Y: 2\n",
      "[True] Prediction: 6 Real Y: 6\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 2 Real Y: 2\n",
      "[True] Prediction: 6 Real Y: 6\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 6 Real Y: 6\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 5 Real Y: 5\n",
      "[True] Prediction: 4 Real Y: 4\n",
      "[True] Prediction: 2 Real Y: 2\n",
      "[True] Prediction: 2 Real Y: 2\n",
      "[True] Prediction: 3 Real Y: 3\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 1 Real Y: 1\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 5 Real Y: 5\n",
      "[True] Prediction: 0 Real Y: 0\n",
      "[True] Prediction: 6 Real Y: 6\n",
      "[True] Prediction: 1 Real Y: 1\n"
     ]
    }
   ],
   "source": [
    "#파워포인트 37페이지(softmax)\n",
    "# Predicting animal type based on various features\n",
    "xy = np.loadtxt('zoo1.csv', delimiter=',')\n",
    "x_data = xy[:101, :-1]\n",
    "y_data = xy[:101, [-1]]\n",
    "\n",
    "# Make sure the shape and data are OK\n",
    "print(x_data.shape, y_data.shape)\n",
    "nb_class = 7\n",
    "\n",
    "X = tf.placeholder(tf.float32, [None, 16])\n",
    "Y = tf.placeholder(tf.int32, [None, 1])\n",
    "\n",
    "Y_one_hot = tf.one_hot(Y, nb_class)\n",
    "print(\"one_hot:\", Y_one_hot)\n",
    "Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_class])\n",
    "print(\"reshape one_hot:\", Y_one_hot)\n",
    "\n",
    "\n",
    "W = tf.Variable(tf.random_normal([16, nb_class]), name='weight')\n",
    "b = tf.Variable(tf.random_normal([nb_class]), name='bias')\n",
    "# tf.nn.softmax computes softmax activations\n",
    "# softmax = exp(logits) / reduce_sum(exp(logits), dim)\n",
    "logits = tf.matmul(X, W) + b\n",
    "hypothesis = tf.nn.softmax(logits)\n",
    "# Cross entropy cost/loss\n",
    "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,\n",
    "                                                                 labels=tf.stop_gradient([Y_one_hot])))\n",
    "optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)\n",
    "\n",
    "prediction = tf.argmax(hypothesis, 1)\n",
    "correct_prediction = tf.equal(prediction, tf.argmax(Y_one_hot, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "\n",
    "\n",
    "# Launch graph\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for step in range(2001):\n",
    "        _, cost_val, acc_val = sess.run([optimizer, cost, accuracy], feed_dict={X: x_data, Y: y_data})\n",
    "        if step % 100 == 0:\n",
    "            print(\"Step: {:5}\\tCost: {:.3f}\\tAcc: {:.2%}\".format(step, cost_val, acc_val))\n",
    "# Let's see if we can predict\n",
    "    pred = sess.run(prediction, feed_dict={X: x_data})\n",
    "# y_data: (N,1) = flatten => (N, ) matches pred.shape\n",
    "    for p, y in zip(pred, y_data.flatten()):\n",
    "        print(\"[{}] Prediction: {} Real Y: {}\".\n",
    "              format(p == int(y), p, int(y)))\n",
    "    "
   ]
  }
 ],
 "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
}
