Math Part 2


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

Matrix Math Functions

Q1. Create a diagonal tensor with the diagonal values of x.


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

out = tf.diag(x)
print(out.eval())

_out = np.diag(_x)
assert np.array_equal(out.eval(), _out)


[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]

Q2. Extract the diagonal of X.


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

out = tf.diag_part(X)
print(out.eval())

_out = np.diag(_X)
assert np.array_equal(out.eval(), _out)
# Did you notice that np.diag() works like tf.diag or tf.diag_part 
# according to the rank/dimensions of the input array?


[1 2 3 4]

Q3. Permutate the dimensions of x such that the new tensor has shape (3, 4, 2).


In [9]:
_X = np.random.rand(2,3,4)
X = tf.convert_to_tensor(_X)

out = tf.transpose(X, [1, 2, 0])
print(out.get_shape())

_out = np.transpose(_X, [1, 2, 0])
assert np.array_equal(out.eval(), _out) # tf.transpose == np.transpose


(3, 4, 2)

Q4. Construct a 3 by 3 identity matrix.


In [10]:
out = tf.eye(3)
print(out.eval())

assert np.array_equal(out.eval(), np.eye(3)) # tf.eye == np.eye


[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

Q5. Predict the result of this.


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

diagonal_tensor = tf.matrix_diag(X)
diagonal_part = tf.matrix_diag_part(diagonal_tensor)

print("diagonal_tensor =\n", diagonal_tensor.eval())
print("diagonal_part =\n", diagonal_part.eval())


diagonal_tensor =
 [[[1 0 0 0]
  [0 2 0 0]
  [0 0 3 0]
  [0 0 0 4]]

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

Q6. Transpose the last two dimensions of x.


In [12]:
_X= np.random.rand(1, 2, 3, 4)
X = tf.convert_to_tensor(_X)

out1 = tf.matrix_transpose(X)
out2 = tf.transpose(X, [0, 1, 3, 2])
print(out1.eval().shape)
assert np.array_equal(out1.eval(), out2.eval())


(1, 2, 4, 3)

Q7. Multiply X by Y.


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

out = tf.matmul(X, Y)
print(out.eval())

_out = np.dot(_X, _Y)
assert np.array_equal(out.eval(), _out) # tf.matmul(2d, 2d) == np.dot(2d, 2d)


[[14 14]
 [32 32]]

Q8. Multiply X and Y. The first axis represents batches.


In [14]:
_X = np.arange(1, 13, dtype=np.int32).reshape((2, 2, 3))
_Y = np.arange(13, 25, dtype=np.int32).reshape((2, 3, 2))
X = tf.convert_to_tensor(_X)
Y = tf.convert_to_tensor(_Y)
out = tf.matmul(X, Y)
print(out.eval())

# Note that this is not the same as the following.
#_out = np.dot(_X, _Y)
#print(_out)
# in TF 1.0, batch_matmul was absorbed in matmul.


[[[ 94 100]
  [229 244]]

 [[508 532]
  [697 730]]]

Q9. Compute the determinant of X.


In [15]:
_X = np.arange(1, 5, dtype=np.float32).reshape((2, 2))
X = tf.convert_to_tensor(_X)

out = tf.matrix_determinant(X)
print(out.eval())


-2.0

Q10. Compute the inverse of X.


In [16]:
_X = np.arange(1, 5, dtype=np.float64).reshape((2, 2))
X = tf.convert_to_tensor(_X)

out = tf.matrix_inverse(X)
print(out.eval())

_out = np.linalg.inv(_X)
assert np.allclose(out.eval(), _out)


[[-2.   1. ]
 [ 1.5 -0.5]]

Q11. Get the lower-trianglular in the Cholesky decomposition of X.


In [17]:
_X = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]], np.float32)
X = tf.convert_to_tensor(_X)

out = tf.cholesky(X)
print(out.eval())

_out = np.linalg.cholesky(_X)
assert np.array_equal(out.eval(), _out)


[[ 2.  0.  0.]
 [ 6.  1.  0.]
 [-8.  5.  3.]]

Q12. Compute the eigenvalues and eigenvectors of X.


In [18]:
_X = np.diag((1, 2, 3))
X = tf.convert_to_tensor(_X, tf.float32)

eigenvals, eigenvecs = tf.self_adjoint_eig(X)
print("eigentvalues =\n", eigenvals.eval())
print("eigenvectors =\n", eigenvecs.eval())

_eigenvals, _eigenvecs = np.linalg.eig(_X)
assert np.allclose(eigenvals.eval(), _eigenvals)
assert np.allclose(eigenvecs.eval(), _eigenvecs)


eigentvalues =
 [ 1.  2.  3.]
eigenvectors =
 [[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

Q13. Compute the singular values of X.


In [19]:
_X = np.array(
[[1, 0, 0, 0, 2], 
 [0, 0, 3, 0, 0], 
 [0, 0, 0, 0, 0], 
 [0, 2, 0, 0, 0]], dtype=np.float32)
X = tf.convert_to_tensor(_X)

out = tf.svd(X, compute_uv=False)
print(out.eval())

_out = np.linalg.svd(_X, compute_uv=False)
assert np.allclose(out.eval(), _out)


[ 3.          2.23606801  2.          0.        ]

Reduction

Q14. Predict the results of these.


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

outs = [tf.reduce_sum(X),
        tf.reduce_sum(X, axis=0),
        tf.reduce_sum(X, axis=1, keep_dims=True),
        "",
        tf.reduce_prod(X),
        tf.reduce_prod(X, axis=0),
        tf.reduce_prod(X, axis=1, keep_dims=True),
        "",
        tf.reduce_min(X),
        tf.reduce_min(X, axis=0),
        tf.reduce_min(X, axis=1, keep_dims=True),
        "",
        tf.reduce_max(X),
        tf.reduce_max(X, axis=0),
        tf.reduce_max(X, axis=1, keep_dims=True),
        "",
        tf.reduce_mean(X),
        tf.reduce_mean(X, axis=0),
        tf.reduce_mean(X, axis=1, keep_dims=True)
           ]
           
for out in outs:
    if out == "":
        print()
    else:
        print("->", out.eval())
# If you remove the common suffix "reduce_", you will get the same 
# result in numpy.


-> 36
-> [ 6  8 10 12]
-> [[10]
 [26]]

-> 40320
-> [ 5 12 21 32]
-> [[  24]
 [1680]]

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

-> 8
-> [5 6 7 8]
-> [[4]
 [8]]

-> 4
-> [3 4 5 6]
-> [[2]
 [6]]

Q15. Predict the results of these.


In [21]:
_X = np.array([[True, True],
              [False, False]], np.bool)
X = tf.convert_to_tensor(_X)

outs = [tf.reduce_all(X),
        tf.reduce_all(X, axis=0),
        tf.reduce_all(X, axis=1, keep_dims=True),
        "",
        tf.reduce_any(X),
        tf.reduce_any(X, axis=0),
        tf.reduce_any(X, axis=1, keep_dims=True),
        ]

for out in outs:
    if out == "":
        print()
    else:
        print("->", out.eval())

# If you remove the common suffix "reduce_", you will get the same 
# result in numpy.


-> False
-> [False False]
-> [[ True]
 [False]]

-> True
-> [ True  True]
-> [[ True]
 [False]]

Q16. Predict the results of these.


In [22]:
_X = np.array([[0, 1, 0],
              [1, 1, 0]])
X = tf.convert_to_tensor(_X)

outs = [tf.count_nonzero(X),
        tf.count_nonzero(X, axis=0),
        tf.count_nonzero(X, axis=1, keep_dims=True),
        ]

for out in outs:
    print("->", out.eval())

# tf.count_nonzero == np.count_nonzero


-> 3
-> [1 2 0]
-> [[1]
 [2]]

Q17. Complete the einsum function that would yield the same result as the given function.


In [23]:
_X = np.arange(1, 7).reshape((2, 3))
_Y = np.arange(1, 7).reshape((3, 2))

X = tf.convert_to_tensor(_X)
Y = tf.convert_to_tensor(_Y)

# Matrix multiplication
out1 = tf.matmul(X, Y)
out1_ = tf.einsum('ij,jk->ik', X, Y)
assert np.allclose(out1.eval(), out1_.eval())

# Dot product
flattened = tf.reshape(X, [-1])
out2 = tf.reduce_sum(flattened * flattened)
out2_ = tf.einsum('i,i->', flattened, flattened)
assert np.allclose(out2.eval(), out2_.eval())

# Outer product
expanded_a = tf.expand_dims(flattened, 1) # shape: (6, 1)
expanded_b = tf.expand_dims(flattened, 0) # shape: (1, 6)
out3 = tf.matmul(expanded_a, expanded_b)
out3_ = tf.einsum('i,j->ij', flattened, flattened)
assert np.allclose(out3.eval(), out3_.eval())

# Transpose
out4 = tf.transpose(X) # shape: (3, 2)
out4_ = tf.einsum('ij->ji', X)
assert np.allclose(out4.eval(), out4_.eval())

In [ ]: