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]:



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]:



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]:



[[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)


[ 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)


[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]:



[[ 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]:



[[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]:



[[1 6]
 [7 4]]

In [ ]: