{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 교재 페이지 (209) #####"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4) 퍼셉트론(OR)-실습"
   ]
  },
  {
   "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",
      "step : 0 \n",
      "cost : 0.52098215 \n",
      "Weight :\n",
      " [[0.3956153 ]\n",
      " [0.15423974]] \n",
      "bias :\n",
      " [-1.3574451]\n",
      "Hypothesis:\n",
      " [[0.7953441 ]\n",
      " [0.8193108 ]\n",
      " [0.85233843]\n",
      " [0.8707155 ]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.75\n",
      "step : 250 \n",
      "cost : 0.2586857 \n",
      "Weight :\n",
      " [[1.7998836]\n",
      " [1.6984725]] \n",
      "bias :\n",
      " [0.05814381]\n",
      "Hypothesis:\n",
      " [[0.48546815]\n",
      " [0.8375796 ]\n",
      " [0.8509079 ]\n",
      " [0.9689379 ]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.75\n",
      "step : 500 \n",
      "cost : 0.16515973 \n",
      "Weight :\n",
      " [[2.7397382]\n",
      " [2.6913712]] \n",
      "bias :\n",
      " [0.7186043]\n",
      "Hypothesis:\n",
      " [[0.3277004]\n",
      " [0.877908 ]\n",
      " [0.8829982]\n",
      " [0.9910977]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.75\n",
      "step : 750 \n",
      "cost : 0.11955227 \n",
      "Weight :\n",
      " [[3.4226935]\n",
      " [3.3958724]] \n",
      "bias :\n",
      " [1.130256]\n",
      "Hypothesis:\n",
      " [[0.24411386]\n",
      " [0.9059891 ]\n",
      " [0.9082488 ]\n",
      " [0.9966261 ]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.75\n",
      "step : 1000 \n",
      "cost : 0.09293057 \n",
      "Weight :\n",
      " [[3.952882 ]\n",
      " [3.9362447]] \n",
      "bias :\n",
      " [1.4296656]\n",
      "Hypothesis:\n",
      " [[0.19315079]\n",
      " [0.9246018 ]\n",
      " [0.9257535 ]\n",
      " [0.9984368 ]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  1.0\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "x_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)\n",
    "y_data = np.array([[0], [1], [1], [1]], dtype=np.float32)\n",
    "X = tf.placeholder(tf.float32)\n",
    "Y = tf.placeholder(tf.float32)\n",
    "W = tf.Variable(tf.random_normal([2, 1]), name='weight')\n",
    "b = tf.Variable(tf.random_normal([1]), name='bias')\n",
    "\n",
    "# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))\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.1).minimize(cost)\n",
    "\n",
    "# Accuracy computation\n",
    "# True if hypothesis>0.2 else False\n",
    "predicted = tf.cast(hypothesis > 0.2, 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",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for step in range(1001):\n",
    "        sess.run(train, feed_dict={X: x_data, Y: y_data})\n",
    "        if step % 250 == 0:\n",
    "            print(\"step :\",step, \"\\ncost :\", sess.run(cost, feed_dict={X: x_data, Y: y_data}), \n",
    "                  \"\\nWeight :\\n\", sess.run(W), \"\\nbias :\\n\", sess.run(b))\n",
    "            h, c, a = sess.run([hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data})\n",
    "            print(\"Hypothesis:\\n\", h, \"\\nCorrect:\\n \", c, \"\\nAccuracy: \", a)"
   ]
  }
 ],
 "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
}
