{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 교재 페이지 (210-211) #####"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5) Neural Nets(NN) for XOR"
   ]
  },
  {
   "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.7689432 \n",
      "Weight :\n",
      " [[-0.73786277]\n",
      " [-0.98397326]] \n",
      "bias :\n",
      " [-1.3740427]\n",
      "Hypothesis:\n",
      " [[0.7980325 ]\n",
      " [0.5962994 ]\n",
      " [0.6538894 ]\n",
      " [0.41391763]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 500 \n",
      "cost : 0.6945399 \n",
      "Weight :\n",
      " [[-0.13889387]\n",
      " [-0.14975941]] \n",
      "bias :\n",
      " [-0.1712202]\n",
      "Hypothesis:\n",
      " [[0.54270077]\n",
      " [0.505365  ]\n",
      " [0.5080809 ]\n",
      " [0.4706754 ]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 1000 \n",
      "cost : 0.6931746 \n",
      "Weight :\n",
      " [[-0.0200031 ]\n",
      " [-0.02047592]] \n",
      "bias :\n",
      " [-0.02400697]\n",
      "Hypothesis:\n",
      " [[0.5060015 ]\n",
      " [0.50088274]\n",
      " [0.50100094]\n",
      " [0.4958821 ]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 1500 \n",
      "cost : 0.6931477 \n",
      "Weight :\n",
      " [[-0.00282511]\n",
      " [-0.00284567]] \n",
      "bias :\n",
      " [-0.00336317]\n",
      "Hypothesis:\n",
      " [[0.5008408]\n",
      " [0.5001294]\n",
      " [0.5001345]\n",
      " [0.4994231]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 2000 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-0.00039678]\n",
      " [-0.00039767]] \n",
      "bias :\n",
      " [-0.00047118]\n",
      "Hypothesis:\n",
      " [[0.5001178 ]\n",
      " [0.50001836]\n",
      " [0.5000186 ]\n",
      " [0.49991918]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 2500 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-5.5622913e-05]\n",
      " [-5.5661869e-05]] \n",
      "bias :\n",
      " [-6.600684e-05]\n",
      "Hypothesis:\n",
      " [[0.5000165 ]\n",
      " [0.50000256]\n",
      " [0.5000026 ]\n",
      " [0.49998868]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 3000 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-7.7797395e-06]\n",
      " [-7.7829391e-06]] \n",
      "bias :\n",
      " [-9.254255e-06]\n",
      "Hypothesis:\n",
      " [[0.5000023 ]\n",
      " [0.50000036]\n",
      " [0.50000036]\n",
      " [0.49999842]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 3500 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-1.0861320e-06]\n",
      " [-1.0863521e-06]] \n",
      "bias :\n",
      " [-1.295544e-06]\n",
      "Hypothesis:\n",
      " [[0.5000003 ]\n",
      " [0.50000006]\n",
      " [0.50000006]\n",
      " [0.4999998 ]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 4000 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-1.4884695e-07]\n",
      " [-1.4906715e-07]] \n",
      "bias :\n",
      " [-2.3756289e-07]\n",
      "Hypothesis:\n",
      " [[0.50000006]\n",
      " [0.5       ]\n",
      " [0.5       ]\n",
      " [0.49999997]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 4500 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 5000 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 5500 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 6000 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 6500 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 7000 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 7500 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 8000 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 8500 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 9000 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 9500 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\n",
      "step : 10000 \n",
      "cost : 0.6931472 \n",
      "Weight :\n",
      " [[-8.8497018e-08]\n",
      " [-8.8717215e-08]] \n",
      "bias :\n",
      " [-1.1909821e-07]\n",
      "Hypothesis:\n",
      " [[0.5]\n",
      " [0.5]\n",
      " [0.5]\n",
      " [0.5]] \n",
      "Correct:\n",
      "  [[1.]\n",
      " [1.]\n",
      " [1.]\n",
      " [1.]] \n",
      "Accuracy:  0.5\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",
    "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(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(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)\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
}
