{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 교재 페이지 (221) #####"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1) XOR with NN"
   ]
  },
  {
   "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.7058966 \n",
      "Weight :\n",
      " [array([[0.28229183, 1.6208019 ],\n",
      "       [0.32244548, 0.11042064]], dtype=float32), array([[0.6710524 ],\n",
      "       [0.18655425]], dtype=float32)] \n",
      "bias :\n",
      " [array([1.2386961 , 0.07634072], dtype=float32), array([-0.35093445], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.56615806]\n",
      " [0.5758265 ]\n",
      " [0.58842444]\n",
      " [0.59596586]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 500 \n",
      "cost : 0.69199944 \n",
      "Weight :\n",
      " [array([[0.37562186, 1.6249433 ],\n",
      "       [0.40996972, 0.18509975]], dtype=float32), array([[0.5682002],\n",
      "       [0.108172 ]], dtype=float32)] \n",
      "bias :\n",
      " [array([1.2187263 , 0.08007725], dtype=float32), array([-0.5429902], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.48795718]\n",
      " [0.49830887]\n",
      " [0.5052116 ]\n",
      " [0.51292527]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 1000 \n",
      "cost : 0.6909075 \n",
      "Weight :\n",
      " [array([[0.50193447, 1.6379132 ],\n",
      "       [0.5335542 , 0.29592013]], dtype=float32), array([[0.6000667 ],\n",
      "       [0.16609582]], dtype=float32)] \n",
      "bias :\n",
      " [array([1.203191  , 0.09463494], dtype=float32), array([-0.6163307], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.48304412]\n",
      " [0.49823806]\n",
      " [0.50814563]\n",
      " [0.5181718 ]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 1500 \n",
      "cost : 0.68849546 \n",
      "Weight :\n",
      " [array([[0.66045815, 1.6712643 ],\n",
      "       [0.69276094, 0.48206705]], dtype=float32), array([[0.64430654],\n",
      "       [0.27808377]], dtype=float32)] \n",
      "bias :\n",
      " [array([1.1701788 , 0.12395217], dtype=float32), array([-0.74188685], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.4743928 ]\n",
      " [0.49894485]\n",
      " [0.51296604]\n",
      " [0.5266762 ]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 2000 \n",
      "cost : 0.6825542 \n",
      "Weight :\n",
      " [array([[0.85186166, 1.7600791 ],\n",
      "       [0.88755167, 0.8082572 ]], dtype=float32), array([[0.6884911 ],\n",
      "       [0.49725014]], dtype=float32)] \n",
      "bias :\n",
      " [array([1.1082416 , 0.17022069], dtype=float32), array([-0.95569026], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.4580123 ]\n",
      " [0.5029576 ]\n",
      " [0.5204991 ]\n",
      " [0.54044116]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 2500 \n",
      "cost : 0.6681217 \n",
      "Weight :\n",
      " [array([[1.0654068, 1.9771857],\n",
      "       [1.104322 , 1.335308 ]], dtype=float32), array([[0.6982789],\n",
      "       [0.9006544]], dtype=float32)] \n",
      "bias :\n",
      " [array([1.0143417 , 0.17356054], dtype=float32), array([-1.3005725], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.4258464 ]\n",
      " [0.5150764 ]\n",
      " [0.53168046]\n",
      " [0.5606574 ]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 3000 \n",
      "cost : 0.6393125 \n",
      "Weight :\n",
      " [array([[1.2727941, 2.398088 ],\n",
      "       [1.3115717, 2.0423925]], dtype=float32), array([[0.6336041],\n",
      "       [1.527623 ]], dtype=float32)] \n",
      "bias :\n",
      " [array([ 0.91806257, -0.01316671], dtype=float32), array([-1.7688327], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.3641849 ]\n",
      " [0.5382742 ]\n",
      " [0.54979587]\n",
      " [0.588031  ]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 3500 \n",
      "cost : 0.59745157 \n",
      "Weight :\n",
      " [array([[1.4311712, 3.0164049],\n",
      "       [1.4672099, 2.8458488]], dtype=float32), array([[0.45426464],\n",
      "       [2.3111415 ]], dtype=float32)] \n",
      "bias :\n",
      " [array([ 0.87719643, -0.35698253], dtype=float32), array([-2.2630322], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.27076644]\n",
      " [0.57089144]\n",
      " [0.57691467]\n",
      " [0.61841744]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 4000 \n",
      "cost : 0.558478 \n",
      "Weight :\n",
      " [array([[1.5099862, 3.683443 ],\n",
      "       [1.543681 , 3.601586 ]], dtype=float32), array([[0.16390012],\n",
      "       [3.0755808 ]], dtype=float32)] \n",
      "bias :\n",
      " [array([ 0.89275926, -0.64787704], dtype=float32), array([-2.6641495], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.18369591]\n",
      " [0.6010202 ]\n",
      " [0.60365903]\n",
      " [0.6383474 ]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.75\n",
      "step : 4500 \n",
      "cost : 0.5310279 \n",
      "Weight :\n",
      " [array([[1.5089244, 4.26241  ],\n",
      "       [1.5427039, 4.218372 ]], dtype=float32), array([[-0.18665437],\n",
      "       [ 3.722604  ]], dtype=float32)] \n",
      "bias :\n",
      " [array([ 0.88813126, -0.83731544], dtype=float32), array([-2.9328773], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.12559047]\n",
      " [0.62145215]\n",
      " [0.62276506]\n",
      " [0.6467655 ]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.75\n",
      "step : 5000 \n",
      "cost : 0.51134884 \n",
      "Weight :\n",
      " [array([[1.4293973, 4.7314916],\n",
      "       [1.4669447, 4.704892 ]], dtype=float32), array([[-0.58561814],\n",
      "       [ 4.2665243 ]], dtype=float32)] \n",
      "bias :\n",
      " [array([ 0.7710452, -0.9618413], dtype=float32), array([-3.0866392], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.09051865]\n",
      " [0.6346027 ]\n",
      " [0.6356401 ]\n",
      " [0.6474758 ]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.75\n",
      "step : 5500 \n",
      "cost : 0.4896742 \n",
      "Weight :\n",
      " [array([[1.2468615, 5.1136203],\n",
      "       [1.295872 , 5.096607 ]], dtype=float32), array([[-1.1140361],\n",
      "       [ 4.7622313]], dtype=float32)] \n",
      "bias :\n",
      " [array([ 0.3723062, -1.0588121], dtype=float32), array([-3.142555], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.07072312]\n",
      " [0.6456263 ]\n",
      " [0.64763236]\n",
      " [0.63701063]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.75\n",
      "step : 6000 \n",
      "cost : 0.43166965 \n",
      "Weight :\n",
      " [array([[1.1019142, 5.4320755],\n",
      "       [1.1647083, 5.4232883]], dtype=float32), array([[-2.1293619],\n",
      "       [ 5.248603 ]], dtype=float32)] \n",
      "bias :\n",
      " [array([-0.73020416, -1.1928117 ], dtype=float32), array([-3.1587093], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.06726852]\n",
      " [0.6730982 ]\n",
      " [0.68026376]\n",
      " [0.58351386]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.75\n",
      "step : 6500 \n",
      "cost : 0.2954951 \n",
      "Weight :\n",
      " [array([[1.7419977, 5.7029266],\n",
      "       [1.7555038, 5.7007437]], dtype=float32), array([[-3.8382313],\n",
      "       [ 5.8143625]], dtype=float32)] \n",
      "bias :\n",
      " [array([-2.3043497, -1.473214 ], dtype=float32), array([-3.2069743], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.07789981]\n",
      " [0.7537077 ]\n",
      " [0.75596327]\n",
      " [0.41629875]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.75\n",
      "step : 7000 \n",
      "cost : 0.16427958 \n",
      "Weight :\n",
      " [array([[2.5239906, 5.9241447],\n",
      "       [2.5247738, 5.9230895]], dtype=float32), array([[-5.5113163],\n",
      "       [ 6.479769 ]], dtype=float32)] \n",
      "bias :\n",
      " [array([-3.660336 , -1.8310235], dtype=float32), array([-3.2807584], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.07419184]\n",
      " [0.85227376]\n",
      " [0.85238767]\n",
      " [0.2293098 ]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.75\n",
      "step : 7500 \n",
      "cost : 0.10002865 \n",
      "Weight :\n",
      " [array([[3.0511472, 6.091738 ],\n",
      "       [3.051175 , 6.090893 ]], dtype=float32), array([[-6.682182],\n",
      "       [ 7.057276]], dtype=float32)] \n",
      "bias :\n",
      " [array([-4.5323434, -2.1129262], dtype=float32), array([-3.402132], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.06226125]\n",
      " [0.90784097]\n",
      " [0.9078523 ]\n",
      " [0.13278732]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [0.]] \n",
      "Accuracy:  1.0\n",
      "step : 8000 \n",
      "cost : 0.06933303 \n",
      "Weight :\n",
      " [array([[3.3981783, 6.2216907],\n",
      "       [3.3981361, 6.220954 ]], dtype=float32), array([[-7.48786  ],\n",
      "       [ 7.5238314]], dtype=float32)] \n",
      "bias :\n",
      " [array([-5.0987663, -2.312063 ], dtype=float32), array([-3.5395448], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.05181545]\n",
      " [0.9358547 ]\n",
      " [0.9358586 ]\n",
      " [0.08747524]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [0.]] \n",
      "Accuracy:  1.0\n",
      "step : 8500 \n",
      "cost : 0.052424207 \n",
      "Weight :\n",
      " [array([[3.6447458, 6.326819 ],\n",
      "       [3.644691 , 6.326159 ]], dtype=float32), array([[-8.08061 ],\n",
      "       [ 7.907225]], dtype=float32)] \n",
      "bias :\n",
      " [array([-5.4985123, -2.4561517], dtype=float32), array([-3.6737325], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.04385251]\n",
      " [0.9515526 ]\n",
      " [0.951555  ]\n",
      " [0.06343436]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [0.]] \n",
      "Accuracy:  1.0\n",
      "step : 9000 \n",
      "cost : 0.041933082 \n",
      "Weight :\n",
      " [array([[3.8318944, 6.414728 ],\n",
      "       [3.8318357, 6.4141254]], dtype=float32), array([[-8.543102],\n",
      "       [ 8.23066 ]], dtype=float32)] \n",
      "bias :\n",
      " [array([-5.800372, -2.565078], dtype=float32), array([-3.7986107], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.03781307]\n",
      " [0.96133804]\n",
      " [0.9613398 ]\n",
      " [0.04908374]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [0.]] \n",
      "Accuracy:  1.0\n",
      "step : 9500 \n",
      "cost : 0.034847904 \n",
      "Weight :\n",
      " [array([[3.9809434, 6.490061 ],\n",
      "       [3.980885 , 6.489506 ]], dtype=float32), array([[-8.919894],\n",
      "       [ 8.509673]], dtype=float32)] \n",
      "bias :\n",
      " [array([-6.039699 , -2.6507537], dtype=float32), array([-3.9131722], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.03314286]\n",
      " [0.96794766]\n",
      " [0.96794915]\n",
      " [0.03972358]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [0.]] \n",
      "Accuracy:  1.0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step : 10000 \n",
      "cost : 0.029762499 \n",
      "Weight :\n",
      " [array([[4.1038847, 6.55583  ],\n",
      "       [4.103827 , 6.5553102]], dtype=float32), array([[-9.236728],\n",
      "       [ 8.7547  ]], dtype=float32)] \n",
      "bias :\n",
      " [array([-6.2363   , -2.7203562], dtype=float32), array([-4.0180316], dtype=float32)]\n",
      "Hypothesis:\n",
      " [[0.02944958]\n",
      " [0.9726853 ]\n",
      " [0.9726864 ]\n",
      " [0.03320596]] \n",
      "Correct:\n",
      "  [[0.]\n",
      " [1.]\n",
      " [1.]\n",
      " [0.]] \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], [0]], dtype=np.float32)\n",
    "X = tf.placeholder(tf.float32)\n",
    "Y = tf.placeholder(tf.float32)\n",
    "W1 = tf.Variable(tf.random_normal([2, 2]), name='weight1')\n",
    "b1 = tf.Variable(tf.random_normal([2]), name='bias1')\n",
    "layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)\n",
    "\n",
    "W2 = tf.Variable(tf.random_normal([2, 1]), name='weight2')\n",
    "b2 = tf.Variable(tf.random_normal([1]), name='bias2')\n",
    "hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)\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",
    "# 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",
    "# Launch graph\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for step in range(10001):\n",
    "        sess.run(train, feed_dict={X: x_data, Y: y_data})\n",
    "        if step % 500 == 0:\n",
    "            print(\"step :\",step, \"\\ncost :\", sess.run(cost, feed_dict={X: x_data, Y: y_data}), \n",
    "                  \"\\nWeight :\\n\", sess.run([W1, W2]), \"\\nbias :\\n\", sess.run([b1, b2]))\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
}
