Format: application/octet-stream
⬇ Download File (22.4 KB)
   1  {
   2   "cells": [
   3    {
   4     "cell_type": "code",
   5     "execution_count": 1,
   6     "id": "b55ea657",
   7     "metadata": {},
   8     "outputs": [
   9      {
  10       "name": "stderr",
  11       "output_type": "stream",
  12       "text": [
  13        "UsageError: Line magic function `%tensorflow_version` not found.\n"
  14       ]
  15      }
  16     ],
  17     "source": [
  18      "import tensorflow as tf\n",
  19      "\n",
  20      "import pandas as pd\n",
  21      "%tensorflow_version x.x"
  22     ]
  23    },
  24    {
  25     "cell_type": "code",
  26     "execution_count": 2,
  27     "id": "ef71b515",
  28     "metadata": {},
  29     "outputs": [],
  30     "source": [
  31      "CSV_COLUMN_NAMES = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species']\n",
  32      "SPECIES = ['Setosa', 'Versicolor', 'Virginica']\n",
  33      "# Lets define some constants to help us later on"
  34     ]
  35    },
  36    {
  37     "cell_type": "code",
  38     "execution_count": 3,
  39     "id": "6fa4fab5",
  40     "metadata": {},
  41     "outputs": [],
  42     "source": [
  43      "train_path = tf.keras.utils.get_file(\n",
  44      "    \"iris_training.csv\", \"https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv\")\n",
  45      "test_path = tf.keras.utils.get_file(\n",
  46      "    \"iris_test.csv\", \"https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv\")\n",
  47      "\n",
  48      "train = pd.read_csv(train_path, names=CSV_COLUMN_NAMES, header=0)\n",
  49      "test = pd.read_csv(test_path, names=CSV_COLUMN_NAMES, header=0)\n",
  50      "# Here we use keras (a module inside of TensorFlow) to grab our datasets and read them into a pandas dataframe"
  51     ]
  52    },
  53    {
  54     "cell_type": "code",
  55     "execution_count": 4,
  56     "id": "ee48e114",
  57     "metadata": {},
  58     "outputs": [
  59      {
  60       "data": {
  61        "text/html": [
  62         "<div>\n",
  63         "<style scoped>\n",
  64         "    .dataframe tbody tr th:only-of-type {\n",
  65         "        vertical-align: middle;\n",
  66         "    }\n",
  67         "\n",
  68         "    .dataframe tbody tr th {\n",
  69         "        vertical-align: top;\n",
  70         "    }\n",
  71         "\n",
  72         "    .dataframe thead th {\n",
  73         "        text-align: right;\n",
  74         "    }\n",
  75         "</style>\n",
  76         "<table border=\"1\" class=\"dataframe\">\n",
  77         "  <thead>\n",
  78         "    <tr style=\"text-align: right;\">\n",
  79         "      <th></th>\n",
  80         "      <th>SepalLength</th>\n",
  81         "      <th>SepalWidth</th>\n",
  82         "      <th>PetalLength</th>\n",
  83         "      <th>PetalWidth</th>\n",
  84         "      <th>Species</th>\n",
  85         "    </tr>\n",
  86         "  </thead>\n",
  87         "  <tbody>\n",
  88         "    <tr>\n",
  89         "      <th>0</th>\n",
  90         "      <td>6.4</td>\n",
  91         "      <td>2.8</td>\n",
  92         "      <td>5.6</td>\n",
  93         "      <td>2.2</td>\n",
  94         "      <td>2</td>\n",
  95         "    </tr>\n",
  96         "    <tr>\n",
  97         "      <th>1</th>\n",
  98         "      <td>5.0</td>\n",
  99         "      <td>2.3</td>\n",
 100         "      <td>3.3</td>\n",
 101         "      <td>1.0</td>\n",
 102         "      <td>1</td>\n",
 103         "    </tr>\n",
 104         "    <tr>\n",
 105         "      <th>2</th>\n",
 106         "      <td>4.9</td>\n",
 107         "      <td>2.5</td>\n",
 108         "      <td>4.5</td>\n",
 109         "      <td>1.7</td>\n",
 110         "      <td>2</td>\n",
 111         "    </tr>\n",
 112         "    <tr>\n",
 113         "      <th>3</th>\n",
 114         "      <td>4.9</td>\n",
 115         "      <td>3.1</td>\n",
 116         "      <td>1.5</td>\n",
 117         "      <td>0.1</td>\n",
 118         "      <td>0</td>\n",
 119         "    </tr>\n",
 120         "    <tr>\n",
 121         "      <th>4</th>\n",
 122         "      <td>5.7</td>\n",
 123         "      <td>3.8</td>\n",
 124         "      <td>1.7</td>\n",
 125         "      <td>0.3</td>\n",
 126         "      <td>0</td>\n",
 127         "    </tr>\n",
 128         "  </tbody>\n",
 129         "</table>\n",
 130         "</div>"
 131        ],
 132        "text/plain": [
 133         "   SepalLength  SepalWidth  PetalLength  PetalWidth  Species\n",
 134         "0          6.4         2.8          5.6         2.2        2\n",
 135         "1          5.0         2.3          3.3         1.0        1\n",
 136         "2          4.9         2.5          4.5         1.7        2\n",
 137         "3          4.9         3.1          1.5         0.1        0\n",
 138         "4          5.7         3.8          1.7         0.3        0"
 139        ]
 140       },
 141       "execution_count": 4,
 142       "metadata": {},
 143       "output_type": "execute_result"
 144      }
 145     ],
 146     "source": [
 147      "train.head()"
 148     ]
 149    },
 150    {
 151     "cell_type": "code",
 152     "execution_count": 5,
 153     "id": "963fb2a5",
 154     "metadata": {},
 155     "outputs": [
 156      {
 157       "data": {
 158        "text/html": [
 159         "<div>\n",
 160         "<style scoped>\n",
 161         "    .dataframe tbody tr th:only-of-type {\n",
 162         "        vertical-align: middle;\n",
 163         "    }\n",
 164         "\n",
 165         "    .dataframe tbody tr th {\n",
 166         "        vertical-align: top;\n",
 167         "    }\n",
 168         "\n",
 169         "    .dataframe thead th {\n",
 170         "        text-align: right;\n",
 171         "    }\n",
 172         "</style>\n",
 173         "<table border=\"1\" class=\"dataframe\">\n",
 174         "  <thead>\n",
 175         "    <tr style=\"text-align: right;\">\n",
 176         "      <th></th>\n",
 177         "      <th>SepalLength</th>\n",
 178         "      <th>SepalWidth</th>\n",
 179         "      <th>PetalLength</th>\n",
 180         "      <th>PetalWidth</th>\n",
 181         "    </tr>\n",
 182         "  </thead>\n",
 183         "  <tbody>\n",
 184         "    <tr>\n",
 185         "      <th>0</th>\n",
 186         "      <td>6.4</td>\n",
 187         "      <td>2.8</td>\n",
 188         "      <td>5.6</td>\n",
 189         "      <td>2.2</td>\n",
 190         "    </tr>\n",
 191         "    <tr>\n",
 192         "      <th>1</th>\n",
 193         "      <td>5.0</td>\n",
 194         "      <td>2.3</td>\n",
 195         "      <td>3.3</td>\n",
 196         "      <td>1.0</td>\n",
 197         "    </tr>\n",
 198         "    <tr>\n",
 199         "      <th>2</th>\n",
 200         "      <td>4.9</td>\n",
 201         "      <td>2.5</td>\n",
 202         "      <td>4.5</td>\n",
 203         "      <td>1.7</td>\n",
 204         "    </tr>\n",
 205         "    <tr>\n",
 206         "      <th>3</th>\n",
 207         "      <td>4.9</td>\n",
 208         "      <td>3.1</td>\n",
 209         "      <td>1.5</td>\n",
 210         "      <td>0.1</td>\n",
 211         "    </tr>\n",
 212         "    <tr>\n",
 213         "      <th>4</th>\n",
 214         "      <td>5.7</td>\n",
 215         "      <td>3.8</td>\n",
 216         "      <td>1.7</td>\n",
 217         "      <td>0.3</td>\n",
 218         "    </tr>\n",
 219         "  </tbody>\n",
 220         "</table>\n",
 221         "</div>"
 222        ],
 223        "text/plain": [
 224         "   SepalLength  SepalWidth  PetalLength  PetalWidth\n",
 225         "0          6.4         2.8          5.6         2.2\n",
 226         "1          5.0         2.3          3.3         1.0\n",
 227         "2          4.9         2.5          4.5         1.7\n",
 228         "3          4.9         3.1          1.5         0.1\n",
 229         "4          5.7         3.8          1.7         0.3"
 230        ]
 231       },
 232       "execution_count": 5,
 233       "metadata": {},
 234       "output_type": "execute_result"
 235      }
 236     ],
 237     "source": [
 238      "train_y = train.pop('Species')\n",
 239      "test_y = test.pop('Species')\n",
 240      "train.head() # the species column is now gone"
 241     ]
 242    },
 243    {
 244     "cell_type": "code",
 245     "execution_count": 6,
 246     "id": "c6b15550",
 247     "metadata": {},
 248     "outputs": [
 249      {
 250       "data": {
 251        "text/plain": [
 252         "(120, 4)"
 253        ]
 254       },
 255       "execution_count": 6,
 256       "metadata": {},
 257       "output_type": "execute_result"
 258      }
 259     ],
 260     "source": [
 261      "train.shape"
 262     ]
 263    },
 264    {
 265     "cell_type": "code",
 266     "execution_count": 7,
 267     "id": "9115509c",
 268     "metadata": {},
 269     "outputs": [],
 270     "source": [
 271      "def input_fn(features, labels, training=True, batch_size=256):\n",
 272      "    # Convert the inputs to a Dataset.\n",
 273      "    dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))\n",
 274      "\n",
 275      "    # Shuffle and repeat if you are in training mode.\n",
 276      "    if training:\n",
 277      "        dataset = dataset.shuffle(1000).repeat()\n",
 278      "    \n",
 279      "    return dataset.batch(batch_size)\n"
 280     ]
 281    },
 282    {
 283     "cell_type": "code",
 284     "execution_count": 8,
 285     "id": "20fa02ec",
 286     "metadata": {},
 287     "outputs": [
 288      {
 289       "data": {
 290        "text/plain": [
 291         "Index(['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'], dtype='object')"
 292        ]
 293       },
 294       "execution_count": 8,
 295       "metadata": {},
 296       "output_type": "execute_result"
 297      }
 298     ],
 299     "source": [
 300      "train.columns"
 301     ]
 302    },
 303    {
 304     "cell_type": "code",
 305     "execution_count": null,
 306     "id": "d366c23c",
 307     "metadata": {},
 308     "outputs": [],
 309     "source": []
 310    },
 311    {
 312     "cell_type": "code",
 313     "execution_count": 22,
 314     "id": "cd619f6c",
 315     "metadata": {},
 316     "outputs": [
 317      {
 318       "name": "stdout",
 319       "output_type": "stream",
 320       "text": [
 321        "[NumericColumn(key='SepalLength', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), NumericColumn(key='SepalWidth', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), NumericColumn(key='PetalLength', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), NumericColumn(key='PetalWidth', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)]\n"
 322       ]
 323      }
 324     ],
 325     "source": [
 326      "# Feature columns describe how to use the input.\n",
 327      "my_feature_columns = []\n",
 328      "for key in train.keys():\n",
 329      "    my_feature_columns.append(tf.feature_column.numeric_column(key=key))\n",
 330      "print(my_feature_columns)"
 331     ]
 332    },
 333    {
 334     "cell_type": "code",
 335     "execution_count": null,
 336     "id": "2e3fb92e",
 337     "metadata": {},
 338     "outputs": [],
 339     "source": []
 340    },
 341    {
 342     "cell_type": "code",
 343     "execution_count": 26,
 344     "id": "06356fdb",
 345     "metadata": {},
 346     "outputs": [
 347      {
 348       "name": "stdout",
 349       "output_type": "stream",
 350       "text": [
 351        "INFO:tensorflow:Using default config.\n",
 352        "WARNING:tensorflow:Using temporary folder as model directory: C:\\Users\\PC\\AppData\\Local\\Temp\\tmpw5gteoqp\n",
 353        "INFO:tensorflow:Using config: {'_model_dir': 'C:\\\\Users\\\\PC\\\\AppData\\\\Local\\\\Temp\\\\tmpw5gteoqp', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true\n",
 354        "graph_options {\n",
 355        "  rewrite_options {\n",
 356        "    meta_optimizer_iterations: ONE\n",
 357        "  }\n",
 358        "}\n",
 359        ", '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_experimental_max_worker_delay_secs': None, '_session_creation_timeout_secs': 7200, '_checkpoint_save_graph_def': True, '_service': None, '_cluster_spec': ClusterSpec({}), '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}\n"
 360       ]
 361      }
 362     ],
 363     "source": [
 364      "# Build a DNN with 2 hidden layers with 30 and 10 hidden nodes each.\n",
 365      "classifier = tf.estimator.DNNClassifier(\n",
 366      "    feature_columns=my_feature_columns,\n",
 367      "    # Two hidden layers of 30 and 10 nodes respectively.\n",
 368      "    hidden_units=[30, 10],\n",
 369      "    # The model must choose between 3 classes.\n",
 370      "    n_classes=3)"
 371     ]
 372    },
 373    {
 374     "cell_type": "code",
 375     "execution_count": 29,
 376     "id": "0d26312e",
 377     "metadata": {},
 378     "outputs": [
 379      {
 380       "name": "stdout",
 381       "output_type": "stream",
 382       "text": [
 383        "INFO:tensorflow:Calling model_fn.\n",
 384        "INFO:tensorflow:Done calling model_fn.\n",
 385        "INFO:tensorflow:Create CheckpointSaverHook.\n",
 386        "INFO:tensorflow:Graph was finalized.\n",
 387        "INFO:tensorflow:Restoring parameters from C:\\Users\\PC\\AppData\\Local\\Temp\\tmpw5gteoqp\\model.ckpt-5000\n",
 388        "INFO:tensorflow:Running local_init_op.\n",
 389        "INFO:tensorflow:Done running local_init_op.\n",
 390        "INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 5000...\n",
 391        "INFO:tensorflow:Saving checkpoints for 5000 into C:\\Users\\PC\\AppData\\Local\\Temp\\tmpw5gteoqp\\model.ckpt.\n",
 392        "INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 5000...\n",
 393        "INFO:tensorflow:loss = 0.5807495, step = 5000\n",
 394        "INFO:tensorflow:global_step/sec: 224.697\n",
 395        "INFO:tensorflow:loss = 0.5753113, step = 5100 (0.455 sec)\n",
 396        "INFO:tensorflow:global_step/sec: 363.861\n",
 397        "INFO:tensorflow:loss = 0.5740016, step = 5200 (0.265 sec)\n",
 398        "INFO:tensorflow:global_step/sec: 384.582\n",
 399        "INFO:tensorflow:loss = 0.5694176, step = 5300 (0.270 sec)\n",
 400        "INFO:tensorflow:global_step/sec: 370.273\n",
 401        "INFO:tensorflow:loss = 0.5604342, step = 5400 (0.270 sec)\n",
 402        "INFO:tensorflow:global_step/sec: 370.646\n",
 403        "INFO:tensorflow:loss = 0.5612904, step = 5500 (0.270 sec)\n",
 404        "INFO:tensorflow:global_step/sec: 390.591\n",
 405        "INFO:tensorflow:loss = 0.55436283, step = 5600 (0.260 sec)\n",
 406        "INFO:tensorflow:global_step/sec: 364.927\n",
 407        "INFO:tensorflow:loss = 0.56003606, step = 5700 (0.270 sec)\n",
 408        "INFO:tensorflow:global_step/sec: 376.495\n",
 409        "INFO:tensorflow:loss = 0.54480207, step = 5800 (0.256 sec)\n",
 410        "INFO:tensorflow:global_step/sec: 364.242\n",
 411        "INFO:tensorflow:loss = 0.5537437, step = 5900 (0.284 sec)\n",
 412        "INFO:tensorflow:global_step/sec: 370.557\n",
 413        "INFO:tensorflow:loss = 0.5386242, step = 6000 (0.260 sec)\n",
 414        "INFO:tensorflow:global_step/sec: 356.845\n",
 415        "INFO:tensorflow:loss = 0.5248498, step = 6100 (0.284 sec)\n",
 416        "INFO:tensorflow:global_step/sec: 357.425\n",
 417        "INFO:tensorflow:loss = 0.5319332, step = 6200 (0.276 sec)\n",
 418        "INFO:tensorflow:global_step/sec: 370.046\n",
 419        "INFO:tensorflow:loss = 0.54681563, step = 6300 (0.285 sec)\n",
 420        "INFO:tensorflow:global_step/sec: 370.809\n",
 421        "INFO:tensorflow:loss = 0.5326811, step = 6400 (0.265 sec)\n",
 422        "INFO:tensorflow:global_step/sec: 384.596\n",
 423        "INFO:tensorflow:loss = 0.524839, step = 6500 (0.260 sec)\n",
 424        "INFO:tensorflow:global_step/sec: 357.266\n",
 425        "INFO:tensorflow:loss = 0.52544343, step = 6600 (0.270 sec)\n",
 426        "INFO:tensorflow:global_step/sec: 384.526\n",
 427        "INFO:tensorflow:loss = 0.5194293, step = 6700 (0.270 sec)\n",
 428        "INFO:tensorflow:global_step/sec: 357.263\n",
 429        "INFO:tensorflow:loss = 0.52056277, step = 6800 (0.275 sec)\n",
 430        "INFO:tensorflow:global_step/sec: 326.909\n",
 431        "INFO:tensorflow:loss = 0.5185188, step = 6900 (0.305 sec)\n",
 432        "INFO:tensorflow:global_step/sec: 381.894\n",
 433        "INFO:tensorflow:loss = 0.5095637, step = 7000 (0.270 sec)\n",
 434        "INFO:tensorflow:global_step/sec: 367.031\n",
 435        "INFO:tensorflow:loss = 0.50311697, step = 7100 (0.270 sec)\n",
 436        "INFO:tensorflow:global_step/sec: 370.592\n",
 437        "INFO:tensorflow:loss = 0.50471646, step = 7200 (0.270 sec)\n",
 438        "INFO:tensorflow:global_step/sec: 337.027\n",
 439        "INFO:tensorflow:loss = 0.50227714, step = 7300 (0.290 sec)\n",
 440        "INFO:tensorflow:global_step/sec: 267.667\n",
 441        "INFO:tensorflow:loss = 0.49768665, step = 7400 (0.370 sec)\n",
 442        "INFO:tensorflow:global_step/sec: 312.817\n",
 443        "INFO:tensorflow:loss = 0.5037429, step = 7500 (0.330 sec)\n",
 444        "INFO:tensorflow:global_step/sec: 344.544\n",
 445        "INFO:tensorflow:loss = 0.49504936, step = 7600 (0.280 sec)\n",
 446        "INFO:tensorflow:global_step/sec: 322.756\n",
 447        "INFO:tensorflow:loss = 0.4894077, step = 7700 (0.317 sec)\n",
 448        "INFO:tensorflow:global_step/sec: 370.289\n",
 449        "INFO:tensorflow:loss = 0.4844525, step = 7800 (0.273 sec)\n",
 450        "INFO:tensorflow:global_step/sec: 270.313\n",
 451        "INFO:tensorflow:loss = 0.4757103, step = 7900 (0.370 sec)\n",
 452        "INFO:tensorflow:global_step/sec: 232.386\n",
 453        "INFO:tensorflow:loss = 0.47764954, step = 8000 (0.430 sec)\n",
 454        "INFO:tensorflow:global_step/sec: 202.148\n",
 455        "INFO:tensorflow:loss = 0.47041565, step = 8100 (0.498 sec)\n",
 456        "INFO:tensorflow:global_step/sec: 194.02\n",
 457        "INFO:tensorflow:loss = 0.47540414, step = 8200 (0.502 sec)\n",
 458        "INFO:tensorflow:global_step/sec: 322.831\n",
 459        "INFO:tensorflow:loss = 0.47354245, step = 8300 (0.320 sec)\n",
 460        "INFO:tensorflow:global_step/sec: 312.526\n",
 461        "INFO:tensorflow:loss = 0.47148192, step = 8400 (0.320 sec)\n",
 462        "INFO:tensorflow:global_step/sec: 281.666\n",
 463        "INFO:tensorflow:loss = 0.46770182, step = 8500 (0.350 sec)\n",
 464        "INFO:tensorflow:global_step/sec: 259.983\n",
 465        "INFO:tensorflow:loss = 0.46649587, step = 8600 (0.390 sec)\n",
 466        "INFO:tensorflow:global_step/sec: 243.798\n",
 467        "INFO:tensorflow:loss = 0.47011107, step = 8700 (0.400 sec)\n",
 468        "INFO:tensorflow:global_step/sec: 250.005\n",
 469        "INFO:tensorflow:loss = 0.4677349, step = 8800 (0.400 sec)\n",
 470        "INFO:tensorflow:global_step/sec: 263.088\n",
 471        "INFO:tensorflow:loss = 0.45345965, step = 8900 (0.380 sec)\n",
 472        "INFO:tensorflow:global_step/sec: 322.652\n",
 473        "INFO:tensorflow:loss = 0.4608584, step = 9000 (0.310 sec)\n",
 474        "INFO:tensorflow:global_step/sec: 333.432\n",
 475        "INFO:tensorflow:loss = 0.45972937, step = 9100 (0.300 sec)\n",
 476        "INFO:tensorflow:global_step/sec: 350.458\n",
 477        "INFO:tensorflow:loss = 0.4605282, step = 9200 (0.290 sec)\n",
 478        "INFO:tensorflow:global_step/sec: 339.181\n",
 479        "INFO:tensorflow:loss = 0.44045654, step = 9300 (0.300 sec)\n",
 480        "INFO:tensorflow:global_step/sec: 370.31\n",
 481        "INFO:tensorflow:loss = 0.44937348, step = 9400 (0.270 sec)\n",
 482        "INFO:tensorflow:global_step/sec: 370.708\n",
 483        "INFO:tensorflow:loss = 0.4390704, step = 9500 (0.260 sec)\n",
 484        "INFO:tensorflow:global_step/sec: 370.148\n",
 485        "INFO:tensorflow:loss = 0.4425897, step = 9600 (0.270 sec)\n",
 486        "INFO:tensorflow:global_step/sec: 333.239\n",
 487        "INFO:tensorflow:loss = 0.44484222, step = 9700 (0.310 sec)\n",
 488        "INFO:tensorflow:global_step/sec: 357.147\n",
 489        "INFO:tensorflow:loss = 0.43750608, step = 9800 (0.278 sec)\n",
 490        "INFO:tensorflow:global_step/sec: 384.755\n",
 491        "INFO:tensorflow:loss = 0.43584466, step = 9900 (0.262 sec)\n",
 492        "INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 10000...\n",
 493        "INFO:tensorflow:Saving checkpoints for 10000 into C:\\Users\\PC\\AppData\\Local\\Temp\\tmpw5gteoqp\\model.ckpt.\n",
 494        "INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 10000...\n",
 495        "INFO:tensorflow:Loss for final step: 0.43271506.\n"
 496       ]
 497      },
 498      {
 499       "data": {
 500        "text/plain": [
 501         "<tensorflow_estimator.python.estimator.canned.dnn.DNNClassifierV2 at 0x1a8e5854250>"
 502        ]
 503       },
 504       "execution_count": 29,
 505       "metadata": {},
 506       "output_type": "execute_result"
 507      }
 508     ],
 509     "source": [
 510      "classifier.train(\n",
 511      "    input_fn=lambda: input_fn(train, train_y, training=True),\n",
 512      "    steps=5000)\n",
 513      "# We include a lambda to avoid creating an inner function previously"
 514     ]
 515    },
 516    {
 517     "cell_type": "code",
 518     "execution_count": null,
 519     "id": "4ce41522",
 520     "metadata": {},
 521     "outputs": [],
 522     "source": []
 523    },
 524    {
 525     "cell_type": "code",
 526     "execution_count": 30,
 527     "id": "99fa6f45",
 528     "metadata": {},
 529     "outputs": [
 530      {
 531       "name": "stdout",
 532       "output_type": "stream",
 533       "text": [
 534        "INFO:tensorflow:Calling model_fn.\n",
 535        "INFO:tensorflow:Done calling model_fn.\n",
 536        "INFO:tensorflow:Starting evaluation at 2021-06-07T12:27:37\n",
 537        "INFO:tensorflow:Graph was finalized.\n",
 538        "INFO:tensorflow:Restoring parameters from C:\\Users\\PC\\AppData\\Local\\Temp\\tmpw5gteoqp\\model.ckpt-10000\n",
 539        "INFO:tensorflow:Running local_init_op.\n",
 540        "INFO:tensorflow:Done running local_init_op.\n",
 541        "INFO:tensorflow:Inference Time : 0.39522s\n",
 542        "INFO:tensorflow:Finished evaluation at 2021-06-07-12:27:37\n",
 543        "INFO:tensorflow:Saving dict for global step 10000: accuracy = 0.93333334, average_loss = 0.49777383, global_step = 10000, loss = 0.49777383\n",
 544        "INFO:tensorflow:Saving 'checkpoint_path' summary for global step 10000: C:\\Users\\PC\\AppData\\Local\\Temp\\tmpw5gteoqp\\model.ckpt-10000\n",
 545        "\n",
 546        "Test set accuracy: 0.933\n",
 547        "\n"
 548       ]
 549      }
 550     ],
 551     "source": [
 552      "eval_result = classifier.evaluate(\n",
 553      "    input_fn=lambda: input_fn(test, test_y, training=False))\n",
 554      "\n",
 555      "print('\\nTest set accuracy: {accuracy:0.3f}\\n'.format(**eval_result))"
 556     ]
 557    },
 558    {
 559     "cell_type": "code",
 560     "execution_count": null,
 561     "id": "982a0bf5",
 562     "metadata": {},
 563     "outputs": [],
 564     "source": []
 565    },
 566    {
 567     "cell_type": "code",
 568     "execution_count": 34,
 569     "id": "87aaee08",
 570     "metadata": {},
 571     "outputs": [
 572      {
 573       "name": "stdout",
 574       "output_type": "stream",
 575       "text": [
 576        "Please type numeric values as prompted.\n",
 577        "SepalLength: 5\n",
 578        "SepalLength: 2.5\n",
 579        "SepalWidth: 15\n",
 580        "SepalWidth: 5\n",
 581        "SepalWidth: 5.5\n",
 582        "PetalLength: 2.75\n",
 583        "PetalWidth: 15.1\n",
 584        "INFO:tensorflow:Calling model_fn.\n",
 585        "INFO:tensorflow:Done calling model_fn.\n",
 586        "INFO:tensorflow:Graph was finalized.\n",
 587        "INFO:tensorflow:Restoring parameters from C:\\Users\\PC\\AppData\\Local\\Temp\\tmpw5gteoqp\\model.ckpt-10000\n",
 588        "INFO:tensorflow:Running local_init_op.\n",
 589        "INFO:tensorflow:Done running local_init_op.\n",
 590        "Prediction is \"Virginica\" (91.7%)\n"
 591       ]
 592      }
 593     ],
 594     "source": [
 595      "def input_fn(features, batch_size=256):\n",
 596      "    # Convert the inputs to a Dataset without labels.\n",
 597      "    return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size)\n",
 598      "\n",
 599      "features = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth']\n",
 600      "predict = {}\n",
 601      "\n",
 602      "print(\"Please type numeric values as prompted.\")\n",
 603      "for feature in features:\n",
 604      "    valid = True\n",
 605      "    while valid: \n",
 606      "        val = input(feature + \": \")\n",
 607      "        if not val.isdigit(): valid = False\n",
 608      "\n",
 609      "    predict[feature] = [float(val)]\n",
 610      "\n",
 611      "predictions = classifier.predict(input_fn=lambda: input_fn(predict))\n",
 612      "for pred_dict in predictions:\n",
 613      "    class_id = pred_dict['class_ids'][0]\n",
 614      "    probability = pred_dict['probabilities'][class_id]\n",
 615      "\n",
 616      "    print('Prediction is \"{}\" ({:.1f}%)'.format(\n",
 617      "        SPECIES[class_id], 100 * probability))\n"
 618     ]
 619    },
 620    {
 621     "cell_type": "code",
 622     "execution_count": null,
 623     "id": "04440f40",
 624     "metadata": {},
 625     "outputs": [],
 626     "source": []
 627    }
 628   ],
 629   "metadata": {
 630    "kernelspec": {
 631     "display_name": "Python 3",
 632     "language": "python",
 633     "name": "python3"
 634    },
 635    "language_info": {
 636     "codemirror_mode": {
 637      "name": "ipython",
 638      "version": 3
 639     },
 640     "file_extension": ".py",
 641     "mimetype": "text/x-python",
 642     "name": "python",
 643     "nbconvert_exporter": "python",
 644     "pygments_lexer": "ipython3",
 645     "version": "3.8.8"
 646    }
 647   },
 648   "nbformat": 4,
 649   "nbformat_minor": 5
 650  }
 651