Math Part 3


In [2]:
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, 23)

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

Scan

Q1. Compute the cumulative sum of X along the second axis.


In [7]:
_X = np.array([[1,2,3], [4,5,6]])
X = tf.convert_to_tensor(_X)


[[ 1  3  6]
 [ 4  9 15]]

Q2. Compute the cumulative product of X along the second axis.


In [8]:
_X = np.array([[1,2,3], [4,5,6]])
X = tf.convert_to_tensor(_X)


[[  1   2   6]
 [  4  20 120]]

Segmentation

Q3. Compute the sum along the first two elements and the last two elements of X separately.


In [9]:
_X = np.array(
    [[1,2,3,4], 
     [-1,-2,-3,-4], 
     [-10,-20,-30,-40],
     [10,20,30,40]])
X = tf.convert_to_tensor(_X)


[[0 0 0 0]
 [0 0 0 0]]

Q4. Compute the product along the first two elements and the last two elements of X separately.


In [10]:
_X = np.array(
    [[1,2,3,4], 
     [1,1/2,1/3,1/4], 
     [1,2,3,4],
     [-1,-1,-1,-1]])
X = tf.convert_to_tensor(_X)


[[ 1.  1.  1.  1.]
 [-1. -2. -3. -4.]]

Q5. Compute the minimum along the first two elements and the last two elements of X separately.


In [11]:
_X = np.array(
    [[1,4,5,7], 
     [2,3,6,8], 
     [1,2,3,4],
     [-1,-2,-3,-4]])
X = tf.convert_to_tensor(_X)


[[ 1  3  5  7]
 [-1 -2 -3 -4]]

Q6. Compute the maximum along the first two elements and the last two elements of X separately.


In [12]:
_X = np.array(
    [[1,4,5,7], 
     [2,3,6,8], 
     [1,2,3,4],
     [-1,-2,-3,-4]])
X = tf.convert_to_tensor(_X)


[[2 4 6 8]
 [1 2 3 4]]

Q7. Compute the mean along the first two elements and the last two elements of X separately.


In [13]:
_X = np.array(
    [[1,2,3,4], 
     [5,6,7,8], 
     [-1,-2,-3,-4],
     [-5,-6,-7,-8]])
X = tf.convert_to_tensor(_X)


[[ 3  4  5  6]
 [-3 -4 -5 -6]]

Q8. Compute the sum along the second and fourth and the first and third elements of X separately in the order.


In [14]:
_X = np.array(
    [[1,2,3,4], 
     [-1,-2,-3,-4], 
     [-10,-20,-30,-40],
     [10,20,30,40]])
X = tf.convert_to_tensor(_X)


[[  9  18  27  36]
 [ -9 -18 -27 -36]]

Sequence Comparison and Indexing

Q9. Get the indices of maximum and minimum values of X along the second axis.


In [15]:
_X = np.random.permutation(10).reshape((2, 5))
print("_X =", _X)
X = tf.convert_to_tensor(_X)


_X = [[0 8 2 3 1]
 [4 6 7 9 5]]
[1 3]
[0 0]

Q10. Find the unique elements of x that are not present in y.


In [16]:
_x = np.array([0, 1, 2, 5, 0])
_y = np.array([0, 1, 4])
x = tf.convert_to_tensor(_x)
y = tf.convert_to_tensor(_y)


[2 5]

Q11. Return the elements of X, if X < 4, otherwise X*10.


In [17]:
_X = np.arange(1, 10).reshape(3, 3)
X = tf.convert_to_tensor(_X)


[[ 1  2  3]
 [40 50 60]
 [70 80 90]]

Q12. Get unique elements and their indices from x.


In [18]:
_x = np.array([1, 2, 6, 4, 2, 3, 2])
x = tf.convert_to_tensor(_x)


[1 2 6 4 3]
[0 1 2 3 1 4 1]
sorted unique elements = [1 2 3 4 6]
indices = [0 1 4 3 1 2 1]

Q13. Compute the edit distance between hypothesis and truth.


In [19]:
# Check the documentation on tf.SparseTensor if you are not
# comfortable with sparse tensor.
hypothesis = tf.SparseTensor(
    [[0, 0],[0, 1],[0, 2],[0, 4]],
    ["a", "b", "c", "a"],
    (1, 5)) 
# Note that this is equivalent to the dense tensor.
# [["a", "b", "c", 0, "a"]]

truth = tf.SparseTensor(
    [[0, 0],[0, 2],[0, 4]],
    ["a", "c", "b"],
    (1, 6))
# This is equivalent to the dense tensor.
# [["a", 0, "c", 0, "b", 0]]


[ 2.]
[ 0.66666669]

In [ ]: