Control Flow


In [1]:
from __future__ import print_function
import tensorflow as tf
import numpy as np

In [2]:
from datetime import date
date.today()


Out[2]:
datetime.date(2017, 2, 22)

In [3]:
author = "kyubyong. https://github.com/Kyubyong/tensorflow-exercises"

In [4]:
tf.__version__


Out[4]:
'1.0.0'

In [5]:
np.__version__


Out[5]:
'1.12.0'

In [6]:
sess = tf.InteractiveSession()

NOTE on notation

  • _x, _y, _z, ...: NumPy 0-d or 1-d arrays
  • _X, _Y, _Z, ...: NumPy 2-d or higer dimensional arrays
  • x, y, z, ...: 0-d or 1-d tensors
  • X, Y, Z, ...: 2-d or higher dimensional tensors

Control Flow Operations

Q1. Let x and y be random 0-D tensors. Return x + y if x < y and x - y otherwise.


In [7]:
x = tf.random_uniform([])
y = tf.random_uniform([])
out = tf.cond(x < y, lambda: x + y,  lambda: x - y)
print(out.eval())

# This is equalvant to the following.
_x = np.random.uniform()
_y = np.random.uniform()
_out = np.where(_x < _y, _x + _y, _x - _y)
print(_out)


1.75876
0.9915587370245782

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.


In [8]:
x = tf.random_uniform([], minval=0, maxval=5, dtype=tf.int32)
y = tf.random_uniform([], minval=0, maxval=5, dtype=tf.int32)
out = tf.case({x < y: lambda: x + y, x > y: lambda: x - y}, default=lambda: tf.constant(0), exclusive=True)
print(out.eval())

# Compare
_x = np.random.randint(0, 5)
_y = np.random.randint(0, 5)
_out = np.select([_x > _y, _x < _y, _x == _y], [_x + _y, _x - _y, 0])
print(_out)


1
1

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.


In [9]:
_X = np.array([[-1, -2, -3], [0, 1, 2]])
X = tf.constant(_X)
Y = tf.zeros_like(X)
out = tf.equal(X, Y)
print(out.eval())

_Y = np.zeros_like(_X)
assert np.allclose(out.eval(), np.equal(_X, _Y))


[[False False False]
 [ True False False]]

Logical Operators

Q4. Given x and y below, return the truth value x AND/OR/XOR y element-wise.


In [10]:
x = tf.constant([True, False, False], tf.bool)
y = tf.constant([True, True, False], tf.bool)
out1 = tf.logical_and(x, y)
out2 = tf.logical_or(x, y)
out3 = tf.logical_xor(x, y)
print(out1.eval(), out2.eval(), out3.eval())


[ True False False] [ True  True False] [False  True False]

Q5. Given x, return the truth value of NOT x element-wise.


In [11]:
x = tf.constant([True, False, False], tf.bool)
out = tf.logical_not(x)
print(out.eval())


[False  True  True]

Comparison Operators

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.


In [12]:
X = tf.constant( [[-1, -2, -3], [0, 1, 2]] )
Y = tf.zeros_like(X)
out = tf.not_equal(X, Y)
print(out.eval())
_X = np.array([[-1, -2, -3], [0, 1, 2]])
_Y = np.zeros_like(_X)
assert np.allclose(out.eval(), np.not_equal(_X, _Y))
# tf.not_equal == np.not_equal


[[ True  True  True]
 [False  True  True]]

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.


In [13]:
X = tf.constant( [[-1, -2, -3], [0, 1, 2]] )
Y = tf.zeros_like(X)
out = tf.greater_equal(X, Y)
out2 = tf.logical_not(tf.less(X, Y))
assert np.allclose(out.eval(), out2.eval())
print(out.eval())
_X = np.array([[-1, -2, -3], [0, 1, 2]])
_Y = np.zeros_like(_X)
assert np.allclose(out.eval(), np.greater_equal(_X, _Y))
# tf.great_equal == np.greater_equal


[[False False False]
 [ True  True  True]]

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.


In [14]:
X = tf.constant([[1, 2], [3, 4]])
Y = tf.constant([[5, 6], [7, 8]])
Z = tf.constant([[True, False], [False, True]], tf.bool)
out = tf.where(Z, X, Y)
print(out.eval())


[[1 6]
 [7 4]]

In [ ]: