Skip to content

Commit 585857f

Browse files
authored
Cleaned and refactored for TF 1.0.
1 parent 20e094a commit 585857f

File tree

2 files changed

+311
-194
lines changed

2 files changed

+311
-194
lines changed

Control_Flow.ipynb

Lines changed: 132 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"outputs": [],
1717
"source": [
18+
"from __future__ import print_function\n",
1819
"import tensorflow as tf\n",
1920
"import numpy as np"
2021
]
@@ -27,17 +28,19 @@
2728
},
2829
"outputs": [
2930
{
30-
"name": "stdout",
31-
"output_type": "stream",
32-
"text": [
33-
"0.10.0\n",
34-
"1.11.1\n"
35-
]
31+
"data": {
32+
"text/plain": [
33+
"datetime.date(2017, 2, 22)"
34+
]
35+
},
36+
"execution_count": 2,
37+
"metadata": {},
38+
"output_type": "execute_result"
3639
}
3740
],
3841
"source": [
39-
"print tf.__version__\n",
40-
"print np.__version__"
42+
"from datetime import date\n",
43+
"date.today()"
4144
]
4245
},
4346
{
@@ -48,44 +51,93 @@
4851
},
4952
"outputs": [],
5053
"source": [
51-
"sess = tf.InteractiveSession()"
54+
"author = \"kyubyong. https://github.com/Kyubyong/tensorflow-exercises\""
5255
]
5356
},
5457
{
55-
"cell_type": "markdown",
56-
"metadata": {},
58+
"cell_type": "code",
59+
"execution_count": 4,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [
64+
{
65+
"data": {
66+
"text/plain": [
67+
"'1.0.0'"
68+
]
69+
},
70+
"execution_count": 4,
71+
"metadata": {},
72+
"output_type": "execute_result"
73+
}
74+
],
5775
"source": [
58-
"Q1. Let x and y be random 0-D tensors. Return x + y \n",
59-
"if x < y and x - y otherwise."
76+
"tf.__version__"
6077
]
6178
},
6279
{
6380
"cell_type": "code",
64-
"execution_count": 37,
81+
"execution_count": 5,
6582
"metadata": {
6683
"collapsed": false
6784
},
6885
"outputs": [
6986
{
70-
"name": "stdout",
71-
"output_type": "stream",
72-
"text": [
73-
"0.469627\n"
74-
]
87+
"data": {
88+
"text/plain": [
89+
"'1.12.0'"
90+
]
91+
},
92+
"execution_count": 5,
93+
"metadata": {},
94+
"output_type": "execute_result"
7595
}
7696
],
77-
"source": []
97+
"source": [
98+
"np.__version__"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 6,
104+
"metadata": {
105+
"collapsed": true
106+
},
107+
"outputs": [],
108+
"source": [
109+
"sess = tf.InteractiveSession()"
110+
]
78111
},
79112
{
80113
"cell_type": "markdown",
81114
"metadata": {},
82115
"source": [
83-
"Q2. Let x and y be 0-D int tensors randomly selected from 0 to 5. Return x + y 2 if x < y, x - y elif x > y, 0 otherwise."
116+
"NOTE on notation\n",
117+
"* _x, _y, _z, ...: NumPy 0-d or 1-d arrays\n",
118+
"* _X, _Y, _Z, ...: NumPy 2-d or higer dimensional arrays\n",
119+
"* x, y, z, ...: 0-d or 1-d tensors\n",
120+
"* X, Y, Z, ...: 2-d or higher dimensional tensors"
121+
]
122+
},
123+
{
124+
"cell_type": "markdown",
125+
"metadata": {},
126+
"source": [
127+
"## Control Flow Operations"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"metadata": {},
133+
"source": [
134+
"Q1. Let x and y be random 0-D tensors. Return x + y \n",
135+
"if x < y and x - y otherwise."
84136
]
85137
},
86138
{
87139
"cell_type": "code",
88-
"execution_count": 11,
140+
"execution_count": 7,
89141
"metadata": {
90142
"collapsed": false
91143
},
@@ -94,7 +146,8 @@
94146
"name": "stdout",
95147
"output_type": "stream",
96148
"text": [
97-
"1\n"
149+
"1.75876\n",
150+
"0.9915587370245782\n"
98151
]
99152
}
100153
],
@@ -104,12 +157,12 @@
104157
"cell_type": "markdown",
105158
"metadata": {},
106159
"source": [
107-
"Q3. Let x be a tensor [[-1, -2, -3], [0, 1, 2]] and y be a tensor of zeros with the same shape as x. Return a boolean tensor that yields Trues if x equals y elementwise."
160+
"Q2. Let x and y be 0-D int32 tensors randomly selected from 0 to 5. Return x + y 2 if x < y, x - y elif x > y, 0 otherwise."
108161
]
109162
},
110163
{
111164
"cell_type": "code",
112-
"execution_count": 44,
165+
"execution_count": 8,
113166
"metadata": {
114167
"collapsed": false
115168
},
@@ -118,8 +171,8 @@
118171
"name": "stdout",
119172
"output_type": "stream",
120173
"text": [
121-
"[[False False False]\n",
122-
" [ True False False]]\n"
174+
"1\n",
175+
"1\n"
123176
]
124177
}
125178
],
@@ -129,12 +182,12 @@
129182
"cell_type": "markdown",
130183
"metadata": {},
131184
"source": [
132-
"Q4. Let x be a tensor [[-1, -2, -3], [0, 1, 2]] and y be a tensor of zeros with the same shape as x. Return a boolean tensor that yields Trues if x does not equal y elementwise."
185+
"Q3. Let X be a tensor [[-1, -2, -3], [0, 1, 2]] and Y be a tensor of zeros with the same shape as X. Return a boolean tensor that yields True if X equals Y elementwise."
133186
]
134187
},
135188
{
136189
"cell_type": "code",
137-
"execution_count": 45,
190+
"execution_count": 9,
138191
"metadata": {
139192
"collapsed": false
140193
},
@@ -143,8 +196,8 @@
143196
"name": "stdout",
144197
"output_type": "stream",
145198
"text": [
146-
"[[ True True True]\n",
147-
" [False True True]]\n"
199+
"[[False False False]\n",
200+
" [ True False False]]\n"
148201
]
149202
}
150203
],
@@ -154,12 +207,19 @@
154207
"cell_type": "markdown",
155208
"metadata": {},
156209
"source": [
157-
"Q5. Let x be a tensor [[-1, -2, -3], [0, 1, 2]] and y be a tensor of zeros with the same shape as x. Return a boolean tensor that yields Trues if x is greater than or equal to y elementwise."
210+
"## Logical Operators"
211+
]
212+
},
213+
{
214+
"cell_type": "markdown",
215+
"metadata": {},
216+
"source": [
217+
"Q4. Given x and y below, return the truth value x AND/OR/XOR y element-wise."
158218
]
159219
},
160220
{
161221
"cell_type": "code",
162-
"execution_count": 46,
222+
"execution_count": 10,
163223
"metadata": {
164224
"collapsed": false
165225
},
@@ -168,23 +228,25 @@
168228
"name": "stdout",
169229
"output_type": "stream",
170230
"text": [
171-
"[[False False False]\n",
172-
" [ True True True]]\n"
231+
"[ True False False] [ True True False] [False True False]\n"
173232
]
174233
}
175234
],
176-
"source": []
235+
"source": [
236+
"x = tf.constant([True, False, False], tf.bool)\n",
237+
"y = tf.constant([True, True, False], tf.bool)\n"
238+
]
177239
},
178240
{
179241
"cell_type": "markdown",
180242
"metadata": {},
181243
"source": [
182-
"Q6. Let x be a tensor [[-1, -2, -3], [0, 1, 2]] and y be a tensor of zeros with the same shape as x. Return a boolean tensor that yields Trues if x is less than or equal to y elementwise.\n"
244+
"Q5. Given x, return the truth value of NOT x element-wise."
183245
]
184246
},
185247
{
186248
"cell_type": "code",
187-
"execution_count": 47,
249+
"execution_count": 11,
188250
"metadata": {
189251
"collapsed": false
190252
},
@@ -193,23 +255,31 @@
193255
"name": "stdout",
194256
"output_type": "stream",
195257
"text": [
196-
"[[ True True True]\n",
197-
" [ True False False]]\n"
258+
"[False True True]\n"
198259
]
199260
}
200261
],
201-
"source": []
262+
"source": [
263+
"x = tf.constant([True, False, False], tf.bool)\n"
264+
]
265+
},
266+
{
267+
"cell_type": "markdown",
268+
"metadata": {},
269+
"source": [
270+
"## Comparison Operators"
271+
]
202272
},
203273
{
204274
"cell_type": "markdown",
205275
"metadata": {},
206276
"source": [
207-
"Q7. Let x be a 0-D tensor randomly selected from -5 to 5. Return a boolean tensor that yields Trues if x is less than 3 and x is greater than 0.\n"
277+
"Q6. Let X be a tensor [[-1, -2, -3], [0, 1, 2]] and Y be a tensor of zeros with the same shape as x. Return a boolean tensor that yields True if X does not equal Y elementwise."
208278
]
209279
},
210280
{
211281
"cell_type": "code",
212-
"execution_count": 49,
282+
"execution_count": 12,
213283
"metadata": {
214284
"collapsed": false
215285
},
@@ -218,7 +288,8 @@
218288
"name": "stdout",
219289
"output_type": "stream",
220290
"text": [
221-
"True\n"
291+
"[[ True True True]\n",
292+
" [False True True]]\n"
222293
]
223294
}
224295
],
@@ -228,12 +299,12 @@
228299
"cell_type": "markdown",
229300
"metadata": {},
230301
"source": [
231-
"Q8. Let x be a tensor [[1, 2], [3, 4]], y be a tensor [[5, 6], [7, 8]], and z be a boolean tensor [[True, False], [False, True]]. Create a 2*2 tensor such that each element corresponds to x if True, otherise y."
302+
"Q7. Let X be a tensor [[-1, -2, -3], [0, 1, 2]] and Y be a tensor of zeros with the same shape as X. Return a boolean tensor that yields True if X is greater than or equal to Y elementwise."
232303
]
233304
},
234305
{
235306
"cell_type": "code",
236-
"execution_count": 51,
307+
"execution_count": 13,
237308
"metadata": {
238309
"collapsed": false
239310
},
@@ -242,8 +313,8 @@
242313
"name": "stdout",
243314
"output_type": "stream",
244315
"text": [
245-
"[[1 6]\n",
246-
" [7 4]]\n"
316+
"[[False False False]\n",
317+
" [ True True True]]\n"
247318
]
248319
}
249320
],
@@ -253,12 +324,12 @@
253324
"cell_type": "markdown",
254325
"metadata": {},
255326
"source": [
256-
"Q9. Let x be a tensor [1, 2, 3, ..., 100]. Extract elements of x greater than 30."
327+
"Q8. Let X be a tensor [[1, 2], [3, 4]], Y be a tensor [[5, 6], [7, 8]], and Z be a boolean tensor [[True, False], [False, True]]. Create a 2*2 tensor such that each element corresponds to X if Z is True, otherise Y."
257328
]
258329
},
259330
{
260331
"cell_type": "code",
261-
"execution_count": 75,
332+
"execution_count": 14,
262333
"metadata": {
263334
"collapsed": false
264335
},
@@ -267,14 +338,14 @@
267338
"name": "stdout",
268339
"output_type": "stream",
269340
"text": [
270-
"[ 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48\n",
271-
" 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66\n",
272-
" 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84\n",
273-
" 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100]\n"
341+
"[[1 6]\n",
342+
" [7 4]]\n"
274343
]
275344
}
276345
],
277-
"source": []
346+
"source": [
347+
"\n"
348+
]
278349
},
279350
{
280351
"cell_type": "code",
@@ -287,22 +358,23 @@
287358
}
288359
],
289360
"metadata": {
361+
"anaconda-cloud": {},
290362
"kernelspec": {
291-
"display_name": "Python 2",
363+
"display_name": "Python [conda root]",
292364
"language": "python",
293-
"name": "python2"
365+
"name": "conda-root-py"
294366
},
295367
"language_info": {
296368
"codemirror_mode": {
297369
"name": "ipython",
298-
"version": 2
370+
"version": 3
299371
},
300372
"file_extension": ".py",
301373
"mimetype": "text/x-python",
302374
"name": "python",
303375
"nbconvert_exporter": "python",
304-
"pygments_lexer": "ipython2",
305-
"version": "2.7.6"
376+
"pygments_lexer": "ipython3",
377+
"version": "3.5.2"
306378
}
307379
},
308380
"nbformat": 4,

0 commit comments

Comments
 (0)