Skip to content

Commit 1fe0f17

Browse files
authored
notebook for simple xor; license headers (#38)
* notebook for simple xor; license headers * xor_summaries notebook
1 parent 41f0f72 commit 1fe0f17

File tree

6 files changed

+402
-1
lines changed

6 files changed

+402
-1
lines changed

workshop_sections/transfer_learning/cloudml/images_to_json.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
r"""Make a request JSON from local images to send to CloudML serving API.
14+
15+
"""Make a request JSON from local images to send to CloudML serving API.
1516
1617
Example usage:
1718
$ python images_to_json.py -o request.json img1.jpg img2.jpg

workshop_sections/xor/xor.ipynb

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"Copyright 2016 Google Inc. All Rights Reserved.\n",
8+
"Licensed under the Apache License, Version 2.0 (the \"License\");\n",
9+
"you may not use this file except in compliance with the License.\n",
10+
"You may obtain a copy of the License at\n",
11+
"\n",
12+
" http://www.apache.org/licenses/LICENSE-2.0\n",
13+
"\n",
14+
"Unless required by applicable law or agreed to in writing, software\n",
15+
"distributed under the License is distributed on an \"AS IS\" BASIS,\n",
16+
"WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
17+
"See the License for the specific language governing permissions and\n",
18+
"limitations under the License."
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"This notebook builds a model graph to learn XOR, then trains the model.\n",
26+
"\n",
27+
"First, do some imports."
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 1,
33+
"metadata": {
34+
"collapsed": true
35+
},
36+
"outputs": [],
37+
"source": [
38+
"import argparse\n",
39+
"import math\n",
40+
"\n",
41+
"import numpy as np\n",
42+
"\n",
43+
"import tensorflow as tf\n",
44+
"\n",
45+
"tf.logging.set_verbosity(tf.logging.INFO)"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"Now, we'll define a function to build our model graph.\n",
53+
"Note the definition of the loss and training ops."
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 2,
59+
"metadata": {
60+
"collapsed": true
61+
},
62+
"outputs": [],
63+
"source": [
64+
"\n",
65+
"def make_graph(features, labels, num_hidden=8):\n",
66+
" hidden_weights = tf.Variable(tf.truncated_normal(\n",
67+
" [2, num_hidden],\n",
68+
" stddev=1/math.sqrt(2)\n",
69+
" ))\n",
70+
"\n",
71+
" # Shape [4, num_hidden]\n",
72+
" hidden_activations = tf.nn.relu(tf.matmul(features, hidden_weights))\n",
73+
"\n",
74+
" output_weights = tf.Variable(tf.truncated_normal(\n",
75+
" [num_hidden, 1],\n",
76+
" stddev=1/math.sqrt(num_hidden)\n",
77+
" ))\n",
78+
"\n",
79+
" # Shape [4, 1]\n",
80+
" logits = tf.matmul(hidden_activations, output_weights)\n",
81+
"\n",
82+
" # Shape [4]\n",
83+
" predictions = tf.sigmoid(tf.squeeze(logits))\n",
84+
" loss = tf.reduce_mean(tf.square(predictions - tf.to_float(labels)))\n",
85+
"\n",
86+
" gs = tf.Variable(0, trainable=False)\n",
87+
" train_op = tf.train.GradientDescentOptimizer(0.2).minimize(\n",
88+
" loss, global_step=gs)\n",
89+
"\n",
90+
" return train_op, loss, gs\n"
91+
]
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"metadata": {},
96+
"source": [
97+
"Build the graph -- define the placeholders, and call make_graph().\n",
98+
"Then add an op to init the variables."
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 3,
104+
"metadata": {
105+
"collapsed": true
106+
},
107+
"outputs": [],
108+
"source": [
109+
"graph = tf.Graph()\n",
110+
"num_steps = 5000\n",
111+
"\n",
112+
"with graph.as_default():\n",
113+
" features = tf.placeholder(tf.float32, shape=[4, 2])\n",
114+
" labels = tf.placeholder(tf.int32, shape=[4])\n",
115+
"\n",
116+
" train_op, loss, gs = make_graph(features, labels)\n",
117+
" init = tf.global_variables_initializer()\n"
118+
]
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"In a Session, run a training loop using a small input dataset.\n",
125+
"You'll see the final loss value output after a short pause."
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 4,
131+
"metadata": {
132+
"collapsed": false
133+
},
134+
"outputs": [
135+
{
136+
"name": "stdout",
137+
"output_type": "stream",
138+
"text": [
139+
"INFO:tensorflow:Final loss is: 0.0627002418041\n"
140+
]
141+
}
142+
],
143+
"source": [
144+
"with tf.Session(graph=graph) as sess:\n",
145+
" init.run()\n",
146+
" step = 0\n",
147+
" xy = np.array([\n",
148+
" [True, False],\n",
149+
" [True, True],\n",
150+
" [False, False],\n",
151+
" [False, True]\n",
152+
" ], dtype=np.float)\n",
153+
" y_ = np.array([True, False, False, True], dtype=np.int32)\n",
154+
" while step < num_steps:\n",
155+
"\n",
156+
" _, step, loss_value = sess.run(\n",
157+
" [train_op, gs, loss],\n",
158+
" feed_dict={features: xy, labels: y_}\n",
159+
" )\n",
160+
" tf.logging.info('Final loss is: {}'.format(loss_value))\n"
161+
]
162+
}
163+
],
164+
"metadata": {
165+
},
166+
"nbformat": 4,
167+
"nbformat_minor": 2
168+
}

workshop_sections/xor/xor.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
114
import argparse
215
import math
316

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"Copyright 2016 Google Inc. All Rights Reserved.\n",
8+
"Licensed under the Apache License, Version 2.0 (the \"License\");\n",
9+
"you may not use this file except in compliance with the License.\n",
10+
"You may obtain a copy of the License at\n",
11+
"\n",
12+
" http://www.apache.org/licenses/LICENSE-2.0\n",
13+
"\n",
14+
"Unless required by applicable law or agreed to in writing, software\n",
15+
"distributed under the License is distributed on an \"AS IS\" BASIS,\n",
16+
"WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
17+
"See the License for the specific language governing permissions and\n",
18+
"limitations under the License."
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"This notebook builds a model graph to learn XOR, then trains the model. This version shows how to write summary information, that you can view in Tensorboard.\n",
26+
"\n",
27+
"First, do some imports."
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {
34+
"collapsed": true
35+
},
36+
"outputs": [],
37+
"source": [
38+
"import argparse\n",
39+
"import math\n",
40+
"\n",
41+
"import numpy as np\n",
42+
"\n",
43+
"import tensorflow as tf\n",
44+
"\n",
45+
"tf.logging.set_verbosity(tf.logging.INFO)\n"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"Now, we'll define a function to build our model graph.\n",
53+
"Note the definition of the loss and training ops.\n",
54+
"\n",
55+
"Note also all the tf.summary information that is being defined."
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"metadata": {
62+
"collapsed": true
63+
},
64+
"outputs": [],
65+
"source": [
66+
"def make_graph(features, labels, num_hidden=8):\n",
67+
" hidden_weights = tf.Variable(tf.truncated_normal(\n",
68+
" [2, num_hidden],\n",
69+
" stddev=1/math.sqrt(2)\n",
70+
" ))\n",
71+
" tf.summary.image('hidden_weights', tf.expand_dims([hidden_weights], -1))\n",
72+
"\n",
73+
" # Shape [4, num_hidden]\n",
74+
" hidden_activations = tf.nn.relu(tf.matmul(features, hidden_weights))\n",
75+
"\n",
76+
" output_weights = tf.Variable(tf.truncated_normal(\n",
77+
" [num_hidden, 1],\n",
78+
" stddev=1/math.sqrt(num_hidden)\n",
79+
" ))\n",
80+
"\n",
81+
" # Shape [4, 1]\n",
82+
" logits = tf.matmul(hidden_activations, output_weights)\n",
83+
"\n",
84+
" # Shape [4]\n",
85+
" predictions = tf.sigmoid(tf.squeeze(logits))\n",
86+
" loss = tf.reduce_mean(tf.square(predictions - tf.to_float(labels)))\n",
87+
" tf.summary.scalar('loss', loss)\n",
88+
"\n",
89+
" accuracy, update_acc = tf.contrib.metrics.streaming_accuracy(\n",
90+
" predictions > 0.5, labels)\n",
91+
" tf.summary.scalar('accuracy', accuracy)\n",
92+
"\n",
93+
" gs = tf.Variable(0, trainable=False)\n",
94+
" optimizer = tf.train.GradientDescentOptimizer(0.2)\n",
95+
"\n",
96+
" grads_and_vars = optimizer.compute_gradients(loss)\n",
97+
"\n",
98+
" gradients = zip(grads_and_vars)[0]\n",
99+
" tf.summary.histogram('gradients', gradients)\n",
100+
"\n",
101+
" train_op = optimizer.apply_gradients(grads_and_vars, global_step=gs)\n",
102+
"\n",
103+
" return train_op, loss, gs, update_acc\n"
104+
]
105+
},
106+
{
107+
"cell_type": "markdown",
108+
"metadata": {},
109+
"source": [
110+
"Build the graph -- define the placeholders, and call `make_graph()`.\n",
111+
"Then add an op to init the variables.\n",
112+
"Note also the tf.summary.merge_all() call to create a `summary_op`."
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": null,
118+
"metadata": {
119+
"collapsed": true
120+
},
121+
"outputs": [],
122+
"source": [
123+
"summaries_every = 10\n",
124+
"num_steps = 5000\n",
125+
"output_dir = '/tmp/xor/output'\n",
126+
"\n",
127+
"graph = tf.Graph()\n",
128+
"\n",
129+
"with graph.as_default():\n",
130+
" features = tf.placeholder(tf.float32, shape=[4, 2])\n",
131+
" labels = tf.placeholder(tf.int32, shape=[4])\n",
132+
"\n",
133+
" train_op, loss, gs, update_acc = make_graph(features, labels)\n",
134+
" init = tf.global_variables_initializer()\n",
135+
" init_local = tf.local_variables_initializer()\n",
136+
" summary_op = tf.summary.merge_all()\n"
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"metadata": {},
142+
"source": [
143+
"In a Session, run a training loop using a small input dataset.\n",
144+
"You can adjust the frequency of summary-writing if you like."
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {
151+
"collapsed": false
152+
},
153+
"outputs": [],
154+
"source": [
155+
"writer = tf.summary.FileWriter(output_dir, graph=graph, flush_secs=1)\n",
156+
"\n",
157+
"with tf.Session(graph=graph) as sess:\n",
158+
" init.run()\n",
159+
" init_local.run()\n",
160+
" step = 0\n",
161+
" xy = np.array([\n",
162+
" [True, False],\n",
163+
" [True, True],\n",
164+
" [False, False],\n",
165+
" [False, True]\n",
166+
" ], dtype=np.float)\n",
167+
" y_ = np.array([True, False, False, True], dtype=np.int32)\n",
168+
" while step < num_steps:\n",
169+
"\n",
170+
" _, _, step, loss_value, summaries = sess.run(\n",
171+
" [train_op, update_acc, gs, loss, summary_op],\n",
172+
" feed_dict={features: xy, labels: y_}\n",
173+
" )\n",
174+
" if step % summaries_every == 0:\n",
175+
" writer.add_summary(summaries, global_step=step)\n",
176+
" tf.logging.info('Wrote summaries at step {}'.format(step))"
177+
]
178+
},
179+
{
180+
"cell_type": "markdown",
181+
"metadata": {},
182+
"source": [
183+
"To see the results, take a look at the summary information in Tensorboard while the training is running, or after it has finished. Run the following in a shell window, pointing the logdir arg to the output directory.\n",
184+
"\n",
185+
"$ tensorboard --logdir=/tmp/xor/output"
186+
]
187+
}
188+
],
189+
"metadata": {
190+
},
191+
"nbformat": 4,
192+
"nbformat_minor": 2
193+
}

0 commit comments

Comments
 (0)