Skip to content

Commit 6373a2f

Browse files
authoredMay 20, 2019
Add feature engineering notebook
1 parent 9a518ad commit 6373a2f

File tree

1 file changed

+738
-0
lines changed

1 file changed

+738
-0
lines changed
 

‎a_features.ipynb

+738
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,738 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"colab_type": "text",
7+
"id": "4f3CKqFUqL2-",
8+
"slideshow": {
9+
"slide_type": "slide"
10+
}
11+
},
12+
"source": [
13+
"# Trying out features"
14+
]
15+
},
16+
{
17+
"cell_type": "markdown",
18+
"metadata": {},
19+
"source": [
20+
"**Learning Objectives:**\n",
21+
" * Improve the accuracy of a model by adding new features with the appropriate representation"
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"The data is based on 1990 census data from California. This data is at the city block level, so these features reflect the total number of rooms in that block, or the total number of people who live on that block, respectively."
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {
34+
"colab_type": "text",
35+
"id": "6TjLjL9IU80G"
36+
},
37+
"source": [
38+
"## Set Up\n",
39+
"In this first cell, we'll load the necessary libraries."
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 1,
45+
"metadata": {},
46+
"outputs": [
47+
{
48+
"name": "stderr",
49+
"output_type": "stream",
50+
"text": [
51+
"/usr/local/envs/py2env/lib/python2.7/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
52+
" from ._conv import register_converters as _register_converters\n"
53+
]
54+
},
55+
{
56+
"name": "stdout",
57+
"output_type": "stream",
58+
"text": [
59+
"1.8.0\n"
60+
]
61+
}
62+
],
63+
"source": [
64+
"import math\n",
65+
"import shutil\n",
66+
"import numpy as np\n",
67+
"import pandas as pd\n",
68+
"import tensorflow as tf\n",
69+
"\n",
70+
"print(tf.__version__)\n",
71+
"tf.logging.set_verbosity(tf.logging.INFO)\n",
72+
"pd.options.display.max_rows = 10\n",
73+
"pd.options.display.float_format = '{:.1f}'.format"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {
79+
"colab_type": "text",
80+
"id": "ipRyUHjhU80Q"
81+
},
82+
"source": [
83+
"Next, we'll load our data set."
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 2,
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"df = pd.read_csv(\"https://storage.googleapis.com/ml_universities/california_housing_train.csv\", sep=\",\")"
93+
]
94+
},
95+
{
96+
"cell_type": "markdown",
97+
"metadata": {
98+
"colab_type": "text",
99+
"id": "HzzlSs3PtTmt",
100+
"slideshow": {
101+
"slide_type": "-"
102+
}
103+
},
104+
"source": [
105+
"## Examine and split the data\n",
106+
"\n",
107+
"It's a good idea to get to know your data a little bit before you work with it.\n",
108+
"\n",
109+
"We'll print out a quick summary of a few useful statistics on each column.\n",
110+
"\n",
111+
"This will include things like mean, standard deviation, max, min, and various quantiles."
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 3,
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"data": {
121+
"text/html": [
122+
"<div>\n",
123+
"<style scoped>\n",
124+
" .dataframe tbody tr th:only-of-type {\n",
125+
" vertical-align: middle;\n",
126+
" }\n",
127+
"\n",
128+
" .dataframe tbody tr th {\n",
129+
" vertical-align: top;\n",
130+
" }\n",
131+
"\n",
132+
" .dataframe thead th {\n",
133+
" text-align: right;\n",
134+
" }\n",
135+
"</style>\n",
136+
"<table border=\"1\" class=\"dataframe\">\n",
137+
" <thead>\n",
138+
" <tr style=\"text-align: right;\">\n",
139+
" <th></th>\n",
140+
" <th>longitude</th>\n",
141+
" <th>latitude</th>\n",
142+
" <th>housing_median_age</th>\n",
143+
" <th>total_rooms</th>\n",
144+
" <th>total_bedrooms</th>\n",
145+
" <th>population</th>\n",
146+
" <th>households</th>\n",
147+
" <th>median_income</th>\n",
148+
" <th>median_house_value</th>\n",
149+
" </tr>\n",
150+
" </thead>\n",
151+
" <tbody>\n",
152+
" <tr>\n",
153+
" <th>0</th>\n",
154+
" <td>-114.3</td>\n",
155+
" <td>34.2</td>\n",
156+
" <td>15.0</td>\n",
157+
" <td>5612.0</td>\n",
158+
" <td>1283.0</td>\n",
159+
" <td>1015.0</td>\n",
160+
" <td>472.0</td>\n",
161+
" <td>1.5</td>\n",
162+
" <td>66900.0</td>\n",
163+
" </tr>\n",
164+
" <tr>\n",
165+
" <th>1</th>\n",
166+
" <td>-114.5</td>\n",
167+
" <td>34.4</td>\n",
168+
" <td>19.0</td>\n",
169+
" <td>7650.0</td>\n",
170+
" <td>1901.0</td>\n",
171+
" <td>1129.0</td>\n",
172+
" <td>463.0</td>\n",
173+
" <td>1.8</td>\n",
174+
" <td>80100.0</td>\n",
175+
" </tr>\n",
176+
" <tr>\n",
177+
" <th>2</th>\n",
178+
" <td>-114.6</td>\n",
179+
" <td>33.7</td>\n",
180+
" <td>17.0</td>\n",
181+
" <td>720.0</td>\n",
182+
" <td>174.0</td>\n",
183+
" <td>333.0</td>\n",
184+
" <td>117.0</td>\n",
185+
" <td>1.7</td>\n",
186+
" <td>85700.0</td>\n",
187+
" </tr>\n",
188+
" <tr>\n",
189+
" <th>3</th>\n",
190+
" <td>-114.6</td>\n",
191+
" <td>33.6</td>\n",
192+
" <td>14.0</td>\n",
193+
" <td>1501.0</td>\n",
194+
" <td>337.0</td>\n",
195+
" <td>515.0</td>\n",
196+
" <td>226.0</td>\n",
197+
" <td>3.2</td>\n",
198+
" <td>73400.0</td>\n",
199+
" </tr>\n",
200+
" <tr>\n",
201+
" <th>4</th>\n",
202+
" <td>-114.6</td>\n",
203+
" <td>33.6</td>\n",
204+
" <td>20.0</td>\n",
205+
" <td>1454.0</td>\n",
206+
" <td>326.0</td>\n",
207+
" <td>624.0</td>\n",
208+
" <td>262.0</td>\n",
209+
" <td>1.9</td>\n",
210+
" <td>65500.0</td>\n",
211+
" </tr>\n",
212+
" </tbody>\n",
213+
"</table>\n",
214+
"</div>"
215+
],
216+
"text/plain": [
217+
" longitude latitude housing_median_age total_rooms total_bedrooms \\\n",
218+
"0 -114.3 34.2 15.0 5612.0 1283.0 \n",
219+
"1 -114.5 34.4 19.0 7650.0 1901.0 \n",
220+
"2 -114.6 33.7 17.0 720.0 174.0 \n",
221+
"3 -114.6 33.6 14.0 1501.0 337.0 \n",
222+
"4 -114.6 33.6 20.0 1454.0 326.0 \n",
223+
"\n",
224+
" population households median_income median_house_value \n",
225+
"0 1015.0 472.0 1.5 66900.0 \n",
226+
"1 1129.0 463.0 1.8 80100.0 \n",
227+
"2 333.0 117.0 1.7 85700.0 \n",
228+
"3 515.0 226.0 3.2 73400.0 \n",
229+
"4 624.0 262.0 1.9 65500.0 "
230+
]
231+
},
232+
"execution_count": 3,
233+
"metadata": {},
234+
"output_type": "execute_result"
235+
}
236+
],
237+
"source": [
238+
"df.head()"
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": 4,
244+
"metadata": {
245+
"cellView": "both",
246+
"colab": {
247+
"autoexec": {
248+
"startup": false,
249+
"wait_interval": 0
250+
},
251+
"test": {
252+
"output": "ignore",
253+
"timeout": 600
254+
}
255+
},
256+
"colab_type": "code",
257+
"id": "gzb10yoVrydW",
258+
"slideshow": {
259+
"slide_type": "slide"
260+
}
261+
},
262+
"outputs": [
263+
{
264+
"data": {
265+
"text/html": [
266+
"<div>\n",
267+
"<style scoped>\n",
268+
" .dataframe tbody tr th:only-of-type {\n",
269+
" vertical-align: middle;\n",
270+
" }\n",
271+
"\n",
272+
" .dataframe tbody tr th {\n",
273+
" vertical-align: top;\n",
274+
" }\n",
275+
"\n",
276+
" .dataframe thead th {\n",
277+
" text-align: right;\n",
278+
" }\n",
279+
"</style>\n",
280+
"<table border=\"1\" class=\"dataframe\">\n",
281+
" <thead>\n",
282+
" <tr style=\"text-align: right;\">\n",
283+
" <th></th>\n",
284+
" <th>longitude</th>\n",
285+
" <th>latitude</th>\n",
286+
" <th>housing_median_age</th>\n",
287+
" <th>total_rooms</th>\n",
288+
" <th>total_bedrooms</th>\n",
289+
" <th>population</th>\n",
290+
" <th>households</th>\n",
291+
" <th>median_income</th>\n",
292+
" <th>median_house_value</th>\n",
293+
" </tr>\n",
294+
" </thead>\n",
295+
" <tbody>\n",
296+
" <tr>\n",
297+
" <th>count</th>\n",
298+
" <td>17000.0</td>\n",
299+
" <td>17000.0</td>\n",
300+
" <td>17000.0</td>\n",
301+
" <td>17000.0</td>\n",
302+
" <td>17000.0</td>\n",
303+
" <td>17000.0</td>\n",
304+
" <td>17000.0</td>\n",
305+
" <td>17000.0</td>\n",
306+
" <td>17000.0</td>\n",
307+
" </tr>\n",
308+
" <tr>\n",
309+
" <th>mean</th>\n",
310+
" <td>-119.6</td>\n",
311+
" <td>35.6</td>\n",
312+
" <td>28.6</td>\n",
313+
" <td>2643.7</td>\n",
314+
" <td>539.4</td>\n",
315+
" <td>1429.6</td>\n",
316+
" <td>501.2</td>\n",
317+
" <td>3.9</td>\n",
318+
" <td>207300.9</td>\n",
319+
" </tr>\n",
320+
" <tr>\n",
321+
" <th>std</th>\n",
322+
" <td>2.0</td>\n",
323+
" <td>2.1</td>\n",
324+
" <td>12.6</td>\n",
325+
" <td>2179.9</td>\n",
326+
" <td>421.5</td>\n",
327+
" <td>1147.9</td>\n",
328+
" <td>384.5</td>\n",
329+
" <td>1.9</td>\n",
330+
" <td>115983.8</td>\n",
331+
" </tr>\n",
332+
" <tr>\n",
333+
" <th>min</th>\n",
334+
" <td>-124.3</td>\n",
335+
" <td>32.5</td>\n",
336+
" <td>1.0</td>\n",
337+
" <td>2.0</td>\n",
338+
" <td>1.0</td>\n",
339+
" <td>3.0</td>\n",
340+
" <td>1.0</td>\n",
341+
" <td>0.5</td>\n",
342+
" <td>14999.0</td>\n",
343+
" </tr>\n",
344+
" <tr>\n",
345+
" <th>25%</th>\n",
346+
" <td>-121.8</td>\n",
347+
" <td>33.9</td>\n",
348+
" <td>18.0</td>\n",
349+
" <td>1462.0</td>\n",
350+
" <td>297.0</td>\n",
351+
" <td>790.0</td>\n",
352+
" <td>282.0</td>\n",
353+
" <td>2.6</td>\n",
354+
" <td>119400.0</td>\n",
355+
" </tr>\n",
356+
" <tr>\n",
357+
" <th>50%</th>\n",
358+
" <td>-118.5</td>\n",
359+
" <td>34.2</td>\n",
360+
" <td>29.0</td>\n",
361+
" <td>2127.0</td>\n",
362+
" <td>434.0</td>\n",
363+
" <td>1167.0</td>\n",
364+
" <td>409.0</td>\n",
365+
" <td>3.5</td>\n",
366+
" <td>180400.0</td>\n",
367+
" </tr>\n",
368+
" <tr>\n",
369+
" <th>75%</th>\n",
370+
" <td>-118.0</td>\n",
371+
" <td>37.7</td>\n",
372+
" <td>37.0</td>\n",
373+
" <td>3151.2</td>\n",
374+
" <td>648.2</td>\n",
375+
" <td>1721.0</td>\n",
376+
" <td>605.2</td>\n",
377+
" <td>4.8</td>\n",
378+
" <td>265000.0</td>\n",
379+
" </tr>\n",
380+
" <tr>\n",
381+
" <th>max</th>\n",
382+
" <td>-114.3</td>\n",
383+
" <td>42.0</td>\n",
384+
" <td>52.0</td>\n",
385+
" <td>37937.0</td>\n",
386+
" <td>6445.0</td>\n",
387+
" <td>35682.0</td>\n",
388+
" <td>6082.0</td>\n",
389+
" <td>15.0</td>\n",
390+
" <td>500001.0</td>\n",
391+
" </tr>\n",
392+
" </tbody>\n",
393+
"</table>\n",
394+
"</div>"
395+
],
396+
"text/plain": [
397+
" longitude latitude housing_median_age total_rooms total_bedrooms \\\n",
398+
"count 17000.0 17000.0 17000.0 17000.0 17000.0 \n",
399+
"mean -119.6 35.6 28.6 2643.7 539.4 \n",
400+
"std 2.0 2.1 12.6 2179.9 421.5 \n",
401+
"min -124.3 32.5 1.0 2.0 1.0 \n",
402+
"25% -121.8 33.9 18.0 1462.0 297.0 \n",
403+
"50% -118.5 34.2 29.0 2127.0 434.0 \n",
404+
"75% -118.0 37.7 37.0 3151.2 648.2 \n",
405+
"max -114.3 42.0 52.0 37937.0 6445.0 \n",
406+
"\n",
407+
" population households median_income median_house_value \n",
408+
"count 17000.0 17000.0 17000.0 17000.0 \n",
409+
"mean 1429.6 501.2 3.9 207300.9 \n",
410+
"std 1147.9 384.5 1.9 115983.8 \n",
411+
"min 3.0 1.0 0.5 14999.0 \n",
412+
"25% 790.0 282.0 2.6 119400.0 \n",
413+
"50% 1167.0 409.0 3.5 180400.0 \n",
414+
"75% 1721.0 605.2 4.8 265000.0 \n",
415+
"max 35682.0 6082.0 15.0 500001.0 "
416+
]
417+
},
418+
"execution_count": 4,
419+
"metadata": {},
420+
"output_type": "execute_result"
421+
}
422+
],
423+
"source": [
424+
"df.describe()"
425+
]
426+
},
427+
{
428+
"cell_type": "markdown",
429+
"metadata": {},
430+
"source": [
431+
"Now, split the data into two parts -- training and evaluation."
432+
]
433+
},
434+
{
435+
"cell_type": "code",
436+
"execution_count": 5,
437+
"metadata": {},
438+
"outputs": [],
439+
"source": [
440+
"np.random.seed(seed=1) #makes result reproducible\n",
441+
"msk = np.random.rand(len(df)) < 0.8\n",
442+
"traindf = df[msk]\n",
443+
"evaldf = df[~msk]"
444+
]
445+
},
446+
{
447+
"cell_type": "markdown",
448+
"metadata": {},
449+
"source": [
450+
"## Training and Evaluation\n",
451+
"\n",
452+
"In this exercise, we'll be trying to predict **median_house_value** It will be our label (sometimes also called a target).\n",
453+
"\n",
454+
"We'll modify the feature_cols and input function to represent the features you want to use.\n",
455+
"\n",
456+
"We divide **total_rooms** by **households** to get **avg_rooms_per_house** which we expect to positively correlate with **median_house_value**. \n",
457+
"\n",
458+
"We also divide **population** by **total_rooms** to get **avg_persons_per_room** which we expect to negatively correlate with **median_house_value**."
459+
]
460+
},
461+
{
462+
"cell_type": "code",
463+
"execution_count": 6,
464+
"metadata": {},
465+
"outputs": [],
466+
"source": [
467+
"def add_more_features(df):\n",
468+
" df['avg_rooms_per_house'] = df['total_rooms'] / df['households'] #expect positive correlation\n",
469+
" df['avg_persons_per_room'] = df['population'] / df['total_rooms'] #expect negative correlation\n",
470+
" return df"
471+
]
472+
},
473+
{
474+
"cell_type": "code",
475+
"execution_count": 7,
476+
"metadata": {},
477+
"outputs": [],
478+
"source": [
479+
"# Create pandas input function\n",
480+
"def make_input_fn(df, num_epochs):\n",
481+
" return tf.estimator.inputs.pandas_input_fn(\n",
482+
" x = add_more_features(df),\n",
483+
" y = df['median_house_value'] / 100000, # will talk about why later in the course\n",
484+
" batch_size = 128,\n",
485+
" num_epochs = num_epochs,\n",
486+
" shuffle = True,\n",
487+
" queue_capacity = 1000,\n",
488+
" num_threads = 1\n",
489+
" )"
490+
]
491+
},
492+
{
493+
"cell_type": "code",
494+
"execution_count": 8,
495+
"metadata": {},
496+
"outputs": [],
497+
"source": [
498+
"# Define your feature columns\n",
499+
"def create_feature_cols():\n",
500+
" return [\n",
501+
" tf.feature_column.numeric_column('housing_median_age'),\n",
502+
" tf.feature_column.bucketized_column(tf.feature_column.numeric_column('latitude'), boundaries = np.arange(32.0, 42, 1).tolist()),\n",
503+
" tf.feature_column.numeric_column('avg_rooms_per_house'),\n",
504+
" tf.feature_column.numeric_column('avg_persons_per_room'),\n",
505+
" tf.feature_column.numeric_column('median_income')\n",
506+
" ]"
507+
]
508+
},
509+
{
510+
"cell_type": "code",
511+
"execution_count": 9,
512+
"metadata": {},
513+
"outputs": [],
514+
"source": [
515+
"# Create estimator train and evaluate function\n",
516+
"def train_and_evaluate(output_dir, num_train_steps):\n",
517+
" estimator = tf.estimator.LinearRegressor(model_dir = output_dir, feature_columns = create_feature_cols())\n",
518+
" train_spec = tf.estimator.TrainSpec(input_fn = make_input_fn(traindf, None), \n",
519+
" max_steps = num_train_steps)\n",
520+
" eval_spec = tf.estimator.EvalSpec(input_fn = make_input_fn(evaldf, 1), \n",
521+
" steps = None, \n",
522+
" start_delay_secs = 1, # start evaluating after N seconds, \n",
523+
" throttle_secs = 5) # evaluate every N seconds\n",
524+
" tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)"
525+
]
526+
},
527+
{
528+
"cell_type": "code",
529+
"execution_count": 10,
530+
"metadata": {},
531+
"outputs": [
532+
{
533+
"data": {
534+
"text/html": [
535+
"<p>TensorBoard was started successfully with pid 3832. Click <a href=\"/_proxy/39101/\" target=\"_blank\">here</a> to access it.</p>"
536+
]
537+
},
538+
"metadata": {},
539+
"output_type": "display_data"
540+
},
541+
{
542+
"data": {
543+
"text/plain": [
544+
"3832"
545+
]
546+
},
547+
"execution_count": 10,
548+
"metadata": {},
549+
"output_type": "execute_result"
550+
}
551+
],
552+
"source": [
553+
"# Launch tensorboard\n",
554+
"from google.datalab.ml import TensorBoard\n",
555+
"\n",
556+
"OUTDIR = './trained_model'\n",
557+
"TensorBoard().start(OUTDIR)"
558+
]
559+
},
560+
{
561+
"cell_type": "code",
562+
"execution_count": 11,
563+
"metadata": {},
564+
"outputs": [
565+
{
566+
"name": "stdout",
567+
"output_type": "stream",
568+
"text": [
569+
"INFO:tensorflow:Using default config.\n",
570+
"INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_task_type': 'worker', '_train_distribute': None, '_is_chief': True, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f87f5e93d10>, '_evaluation_master': '', '_save_checkpoints_steps': None, '_keep_checkpoint_every_n_hours': 10000, '_service': None, '_num_ps_replicas': 0, '_tf_random_seed': None, '_master': '', '_num_worker_replicas': 1, '_task_id': 0, '_log_step_count_steps': 100, '_model_dir': './trained_model', '_global_id_in_cluster': 0, '_save_summary_steps': 100}\n"
571+
]
572+
},
573+
{
574+
"name": "stderr",
575+
"output_type": "stream",
576+
"text": [
577+
"/usr/local/envs/py2env/lib/python2.7/site-packages/ipykernel/__main__.py:2: SettingWithCopyWarning: \n",
578+
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
579+
"Try using .loc[row_indexer,col_indexer] = value instead\n",
580+
"\n",
581+
"See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
582+
" from ipykernel import kernelapp as app\n",
583+
"/usr/local/envs/py2env/lib/python2.7/site-packages/ipykernel/__main__.py:3: SettingWithCopyWarning: \n",
584+
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
585+
"Try using .loc[row_indexer,col_indexer] = value instead\n",
586+
"\n",
587+
"See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
588+
" app.launch_new_instance()\n"
589+
]
590+
},
591+
{
592+
"name": "stdout",
593+
"output_type": "stream",
594+
"text": [
595+
"INFO:tensorflow:Running training and evaluation locally (non-distributed).\n",
596+
"INFO:tensorflow:Start train and evaluate loop. The evaluate will happen after 5 secs (eval_spec.throttle_secs) or training is finished.\n",
597+
"INFO:tensorflow:Calling model_fn.\n",
598+
"INFO:tensorflow:Done calling model_fn.\n",
599+
"INFO:tensorflow:Create CheckpointSaverHook.\n",
600+
"INFO:tensorflow:Graph was finalized.\n",
601+
"INFO:tensorflow:Running local_init_op.\n",
602+
"INFO:tensorflow:Done running local_init_op.\n",
603+
"INFO:tensorflow:Saving checkpoints for 1 into ./trained_model/model.ckpt.\n",
604+
"INFO:tensorflow:loss = 872.02, step = 1\n",
605+
"INFO:tensorflow:global_step/sec: 112.343\n",
606+
"INFO:tensorflow:loss = 117.18349, step = 101 (0.896 sec)\n",
607+
"INFO:tensorflow:global_step/sec: 204.178\n",
608+
"INFO:tensorflow:loss = 70.21603, step = 201 (0.485 sec)\n",
609+
"INFO:tensorflow:global_step/sec: 214.453\n",
610+
"INFO:tensorflow:loss = 63.330612, step = 301 (0.466 sec)\n",
611+
"INFO:tensorflow:global_step/sec: 219.124\n",
612+
"INFO:tensorflow:loss = 58.774532, step = 401 (0.457 sec)\n",
613+
"INFO:tensorflow:global_step/sec: 227.613\n",
614+
"INFO:tensorflow:loss = 42.22863, step = 501 (0.439 sec)\n",
615+
"INFO:tensorflow:Saving checkpoints for 593 into ./trained_model/model.ckpt.\n",
616+
"INFO:tensorflow:Loss for final step: 101.007065.\n",
617+
"INFO:tensorflow:Calling model_fn.\n",
618+
"INFO:tensorflow:Done calling model_fn.\n",
619+
"INFO:tensorflow:Starting evaluation at 2019-05-20-06:42:40\n",
620+
"INFO:tensorflow:Graph was finalized.\n",
621+
"INFO:tensorflow:Restoring parameters from ./trained_model/model.ckpt-593\n",
622+
"INFO:tensorflow:Running local_init_op.\n",
623+
"INFO:tensorflow:Done running local_init_op.\n",
624+
"INFO:tensorflow:Finished evaluation at 2019-05-20-06:42:40\n",
625+
"INFO:tensorflow:Saving dict for global step 593: average_loss = 0.5886677, global_step = 593, loss = 73.86689\n",
626+
"INFO:tensorflow:Calling model_fn.\n",
627+
"INFO:tensorflow:Done calling model_fn.\n",
628+
"INFO:tensorflow:Create CheckpointSaverHook.\n",
629+
"INFO:tensorflow:Graph was finalized.\n",
630+
"INFO:tensorflow:Restoring parameters from ./trained_model/model.ckpt-593\n",
631+
"INFO:tensorflow:Running local_init_op.\n",
632+
"INFO:tensorflow:Done running local_init_op.\n",
633+
"INFO:tensorflow:Saving checkpoints for 594 into ./trained_model/model.ckpt.\n",
634+
"INFO:tensorflow:loss = 40.234905, step = 594\n",
635+
"INFO:tensorflow:global_step/sec: 115.562\n",
636+
"INFO:tensorflow:loss = 76.32765, step = 694 (0.871 sec)\n",
637+
"INFO:tensorflow:global_step/sec: 221.796\n",
638+
"INFO:tensorflow:loss = 56.890873, step = 794 (0.448 sec)\n",
639+
"INFO:tensorflow:global_step/sec: 229.305\n",
640+
"INFO:tensorflow:loss = 75.9293, step = 894 (0.437 sec)\n",
641+
"INFO:tensorflow:global_step/sec: 201.07\n",
642+
"INFO:tensorflow:loss = 71.65114, step = 994 (0.498 sec)\n",
643+
"INFO:tensorflow:global_step/sec: 187.007\n",
644+
"INFO:tensorflow:loss = 82.430466, step = 1094 (0.533 sec)\n",
645+
"INFO:tensorflow:global_step/sec: 198.837\n",
646+
"INFO:tensorflow:loss = 66.29725, step = 1194 (0.503 sec)\n",
647+
"INFO:tensorflow:global_step/sec: 177.279\n",
648+
"INFO:tensorflow:loss = 60.814964, step = 1294 (0.564 sec)\n",
649+
"INFO:tensorflow:Saving checkpoints for 1342 into ./trained_model/model.ckpt.\n",
650+
"INFO:tensorflow:Loss for final step: 63.2088.\n",
651+
"INFO:tensorflow:Calling model_fn.\n",
652+
"INFO:tensorflow:Done calling model_fn.\n",
653+
"INFO:tensorflow:Starting evaluation at 2019-05-20-06:42:46\n",
654+
"INFO:tensorflow:Graph was finalized.\n",
655+
"INFO:tensorflow:Restoring parameters from ./trained_model/model.ckpt-1342\n",
656+
"INFO:tensorflow:Running local_init_op.\n",
657+
"INFO:tensorflow:Done running local_init_op.\n",
658+
"INFO:tensorflow:Finished evaluation at 2019-05-20-06:42:47\n",
659+
"INFO:tensorflow:Saving dict for global step 1342: average_loss = 0.608279, global_step = 1342, loss = 76.32774\n",
660+
"INFO:tensorflow:Calling model_fn.\n",
661+
"INFO:tensorflow:Done calling model_fn.\n",
662+
"INFO:tensorflow:Create CheckpointSaverHook.\n",
663+
"INFO:tensorflow:Graph was finalized.\n",
664+
"INFO:tensorflow:Restoring parameters from ./trained_model/model.ckpt-1342\n",
665+
"INFO:tensorflow:Running local_init_op.\n",
666+
"INFO:tensorflow:Done running local_init_op.\n",
667+
"INFO:tensorflow:Saving checkpoints for 1343 into ./trained_model/model.ckpt.\n",
668+
"INFO:tensorflow:loss = 54.984177, step = 1343\n",
669+
"INFO:tensorflow:global_step/sec: 122.555\n",
670+
"INFO:tensorflow:loss = 85.49204, step = 1443 (0.823 sec)\n",
671+
"INFO:tensorflow:global_step/sec: 201.463\n",
672+
"INFO:tensorflow:loss = 58.530693, step = 1543 (0.492 sec)\n",
673+
"INFO:tensorflow:global_step/sec: 219.231\n",
674+
"INFO:tensorflow:loss = 87.21201, step = 1643 (0.458 sec)\n",
675+
"INFO:tensorflow:global_step/sec: 198.469\n",
676+
"INFO:tensorflow:loss = 85.975655, step = 1743 (0.503 sec)\n",
677+
"INFO:tensorflow:global_step/sec: 187.707\n",
678+
"INFO:tensorflow:loss = 55.826767, step = 1843 (0.533 sec)\n",
679+
"INFO:tensorflow:global_step/sec: 178.708\n",
680+
"INFO:tensorflow:loss = 56.971794, step = 1943 (0.559 sec)\n",
681+
"INFO:tensorflow:Saving checkpoints for 2000 into ./trained_model/model.ckpt.\n",
682+
"INFO:tensorflow:Loss for final step: 64.786316.\n",
683+
"INFO:tensorflow:Calling model_fn.\n",
684+
"INFO:tensorflow:Done calling model_fn.\n",
685+
"INFO:tensorflow:Starting evaluation at 2019-05-20-06:42:52\n",
686+
"INFO:tensorflow:Graph was finalized.\n",
687+
"INFO:tensorflow:Restoring parameters from ./trained_model/model.ckpt-2000\n",
688+
"INFO:tensorflow:Running local_init_op.\n",
689+
"INFO:tensorflow:Done running local_init_op.\n",
690+
"INFO:tensorflow:Finished evaluation at 2019-05-20-06:42:53\n",
691+
"INFO:tensorflow:Saving dict for global step 2000: average_loss = 0.62208545, global_step = 2000, loss = 78.0602\n"
692+
]
693+
}
694+
],
695+
"source": [
696+
"# Run the model\n",
697+
"shutil.rmtree(OUTDIR, ignore_errors = True) # start fresh each time\n",
698+
"tf.summary.FileWriterCache.clear() # ensure filewriter cache is clear for TensorBoard events file\n",
699+
"train_and_evaluate(OUTDIR, 2000)"
700+
]
701+
},
702+
{
703+
"cell_type": "code",
704+
"execution_count": null,
705+
"metadata": {},
706+
"outputs": [],
707+
"source": []
708+
}
709+
],
710+
"metadata": {
711+
"colab": {
712+
"default_view": {},
713+
"name": "first_steps_with_tensor_flow.ipynb",
714+
"provenance": [],
715+
"version": "0.3.2",
716+
"views": {}
717+
},
718+
"kernelspec": {
719+
"display_name": "Python 2",
720+
"language": "python",
721+
"name": "python2"
722+
},
723+
"language_info": {
724+
"codemirror_mode": {
725+
"name": "ipython",
726+
"version": 2
727+
},
728+
"file_extension": ".py",
729+
"mimetype": "text/x-python",
730+
"name": "python",
731+
"nbconvert_exporter": "python",
732+
"pygments_lexer": "ipython2",
733+
"version": "2.7.15"
734+
}
735+
},
736+
"nbformat": 4,
737+
"nbformat_minor": 1
738+
}

0 commit comments

Comments
 (0)
Please sign in to comment.