{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 교재 페이지 (147-148) #####"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6-1) Hypothesis using matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "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 Cost:  348.10272 \n",
      "Prediction:\n",
      " [12.008278 21.277145 23.861647 30.65607  32.11578  55.47819  55.82252\n",
      " 45.47918  76.14809 ]\n",
      "10000 Cost:  10.863274 \n",
      "Prediction:\n",
      " [34.355064 42.821743 44.299553 49.76662  51.54307  70.2765   71.43204\n",
      " 62.962887 87.31517 ]\n",
      "20000 Cost:  9.20147 \n",
      "Prediction:\n",
      " [34.03128  43.539833 43.854923 49.818493 51.660923 70.393845 71.308205\n",
      " 62.93976  87.25981 ]\n",
      "30000 Cost:  7.884106 \n",
      "Prediction:\n",
      " [33.738117 44.17571  43.455902 49.862713 51.763973 70.50015  71.19954\n",
      " 62.91919  87.21547 ]\n",
      "40000 Cost:  6.839821 \n",
      "Prediction:\n",
      " [33.477085 44.74183  43.10065  49.9021   51.855736 70.594864 71.10286\n",
      " 62.90091  87.1761  ]\n",
      "50000 Cost:  6.0119457 \n",
      "Prediction:\n",
      " [33.244686 45.245903 42.784374 49.937195 51.93747  70.67925  71.01685\n",
      " 62.8847   87.141136]\n",
      "60000 Cost:  5.355751 \n",
      "Prediction:\n",
      " [33.037697 45.694523 42.50272  49.968327 52.010105 70.75424  70.94015\n",
      " 62.87013  87.10986 ]\n",
      "70000 Cost:  4.8354807 \n",
      "Prediction:\n",
      " [32.85362  46.09419  42.252094 49.996197 52.074947 70.82106  70.871895\n",
      " 62.857265 87.08196 ]\n",
      "80000 Cost:  4.4230633 \n",
      "Prediction:\n",
      " [32.68971  46.44997  42.02896  50.020985 52.13265  70.880554 70.81116\n",
      " 62.845802 87.05717 ]\n",
      "90000 Cost:  4.096322 \n",
      "Prediction:\n",
      " [32.5437   46.766384 41.83029  50.042934 52.18388  70.93344  70.75706\n",
      " 62.83553  87.035095]\n",
      "100000 Cost:  3.8372488 \n",
      "Prediction:\n",
      " [32.41334  47.047874 41.653217 50.062393 52.229424 70.98083  70.70916\n",
      " 62.826515 87.01607 ]\n",
      "110000 Cost:  3.6318321 \n",
      "Prediction:\n",
      " [32.29786  47.29892  41.495914 50.07994  52.270187 71.02275  70.666306\n",
      " 62.81845  86.99846 ]\n",
      "120000 Cost:  3.4686704 \n",
      "Prediction:\n",
      " [32.194767 47.52278  41.355534 50.095535 52.306496 71.060135 70.628044\n",
      " 62.811222 86.98277 ]\n",
      "130000 Cost:  3.3394291 \n",
      "Prediction:\n",
      " [32.103485 47.72227  41.23095  50.109615 52.338993 71.0933   70.593925\n",
      " 62.80484  86.96845 ]\n",
      "140000 Cost:  3.2370698 \n",
      "Prediction:\n",
      " [32.02142  47.89902  41.119556 50.12174  52.36751  71.12302  70.56379\n",
      " 62.7991   86.9565  ]\n",
      "150000 Cost:  3.1559892 \n",
      "Prediction:\n",
      " [31.948858 48.05656  41.020782 50.132732 52.393085 71.149376 70.53692\n",
      " 62.794056 86.94553 ]\n",
      "160000 Cost:  3.0917628 \n",
      "Prediction:\n",
      " [31.884005 48.19635  40.932697 50.14229  52.41561  71.17277  70.51298\n",
      " 62.78945  86.93589 ]\n",
      "170000 Cost:  3.0409749 \n",
      "Prediction:\n",
      " [31.827349 48.321095 40.855156 50.151234 52.436047 71.19342  70.49167\n",
      " 62.785526 86.92674 ]\n",
      "180000 Cost:  3.0006268 \n",
      "Prediction:\n",
      " [31.776636 48.432117 40.78591  50.1591   52.454166 71.21186  70.4727\n",
      " 62.782005 86.91875 ]\n",
      "190000 Cost:  2.9685714 \n",
      "Prediction:\n",
      " [31.73064  48.530464 40.72365  50.165756 52.46997  71.22854  70.45601\n",
      " 62.77882  86.91236 ]\n",
      "200000 Cost:  2.9430833 \n",
      "Prediction:\n",
      " [31.688828 48.617638 40.6676   50.17134  52.483734 71.24361  70.44127\n",
      " 62.775925 86.907295]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "x1_data = [19., 23., 26., 29., 30., 43., 44., 38., 55.]\n",
    "x2_data = [16.6, 18.4, 17.1, 17.7, 18.2, 18.8, 19.2, 18.9, 19.5]\n",
    "x3_data = [13.,10., 12., 10., 10., 7., 8., 9., 5.]\n",
    "y_data = [33., 51., 40., 49., 50., 69., 70., 64., 89.]\n",
    "# placeholders for a tensor that will be always fed.\n",
    "x1 = tf.placeholder(tf.float32)\n",
    "x2 = tf.placeholder(tf.float32)\n",
    "x3 = tf.placeholder(tf.float32)\n",
    "Y = tf.placeholder(tf.float32)\n",
    "\n",
    "w1 = tf.Variable(tf.random_normal([1]), name='weight1')\n",
    "w2 = tf.Variable(tf.random_normal([1]), name='weight2')\n",
    "w3 = tf.Variable(tf.random_normal([1]), name='weight3')\n",
    "b = tf.Variable(tf.random_normal([1]), name='bias')\n",
    "hypothesis = x1 * w1 + x2 * w2 + x3 * w3 + b\n",
    "\n",
    "# cost/loss function\n",
    "cost = tf.reduce_mean(tf.square(hypothesis - Y))\n",
    "\n",
    "# Minimize. Need a very small learning rate for this data set\n",
    "optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)\n",
    "train = optimizer.minimize(cost)\n",
    "\n",
    "# Launch the graph in a session.\n",
    "sess = tf.Session()\n",
    "\n",
    "# Initializes global variables in the graph.\n",
    "sess.run(tf.global_variables_initializer())\n",
    "for step in range(200001):\n",
    "    cost_val, hy_val, _ = sess.run([cost, hypothesis, train],\n",
    "                                   feed_dict={x1: x1_data, x2: x2_data, x3: x3_data, Y: y_data})\n",
    "    if step % 10000 == 0:\n",
    "        print(step, \"Cost: \", cost_val, \"\\nPrediction:\\n\", hy_val)"
   ]
  }
 ],
 "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
}
