<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Math Part 2
Step1:  NOTE on notation
* _x, _y, _z, ...
Step2:  Q2. Extract the diagonal of X.
Step3:  Q3. Permutate the dimensions of x such that the new tensor has shape (3, 4, 2).
Step4:  Q4. Construct a 3 by 3 identity matrix.
Step5:  Q5. Predict the result of this.
Step6:  Q6. Transpose the last two dimensions of x.
Step7:  Q7. Multiply X by Y.
Step8:  Q8. Multiply X and Y. The first axis represents batches.
Step9:  Q9. Compute the determinant of X.
Step10:  Q10. Compute the inverse of X.
Step11:  Q11. Get the lower-trianglular in the Cholesky decomposition of X.
Step12:  Q12. Compute the eigenvalues and eigenvectors of X.
Step13:  Q13. Compute the singular values of X.
Step14:  Reduction
Q14. Predict the results of these.
Step15:  Q15. Predict the results of these.
Step16:  Q16. Predict the results of these.
Step17:  Q17. Complete the einsum function that would yield the same result as the given function.
<ASSISTANT_TASK:>
Python Code:
from __future__ import print_function
import tensorflow as tf
import numpy as np
from datetime import date
date.today()
author = "kyubyong. https://github.com/Kyubyong/tensorflow-exercises"
tf.__version__
np.__version__
sess = tf.InteractiveSession()
Explanation: Math Part 2
End of explanation
_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)
Explanation: 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.
End of explanation
_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?
Explanation: Q2. Extract the diagonal of X.
End of explanation
_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
Explanation: Q3. Permutate the dimensions of x such that the new tensor has shape (3, 4, 2).
End of explanation
out = tf.eye(3)
print(out.eval())
assert np.array_equal(out.eval(), np.eye(3)) # tf.eye == np.eye
Explanation: Q4. Construct a 3 by 3 identity matrix.
End of explanation
_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())
Explanation: Q5. Predict the result of this.
End of explanation
_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())
Explanation: Q6. Transpose the last two dimensions of x.
End of explanation
_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)
Explanation: Q7. Multiply X by Y.
End of explanation
_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. 
Explanation: Q8. Multiply X and Y. The first axis represents batches.
End of explanation
_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())
Explanation: Q9. Compute the determinant of X.
End of explanation
_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)
Explanation: Q10. Compute the inverse of X.
End of explanation
_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)
Explanation: Q11. Get the lower-trianglular in the Cholesky decomposition of X.
End of explanation
_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)
Explanation: Q12. Compute the eigenvalues and eigenvectors of X.
End of explanation
_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)
Explanation: Q13. Compute the singular values of X.
End of explanation
_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.
Explanation: Reduction
Q14. Predict the results of these.
End of explanation
_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.
Explanation: Q15. Predict the results of these.
End of explanation
_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
Explanation: Q16. Predict the results of these.
End of explanation
_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())
Explanation: Q17. Complete the einsum function that would yield the same result as the given function.
End of explanation
<END_TASK>