Math Part 1


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

Arithmetic Operators

Q1. Add x and y element-wise.


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


[0 0 0]

Q2. Subtract y from x element-wise.


In [8]:
_x = np.array([3, 4, 5])
_y = np.array(3)
x = tf.convert_to_tensor(_x)
y = tf.convert_to_tensor(_y)


[0 1 2]

Q3. Multiply x by y element-wise.


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


[ 3  0 -5]

Q4. Multiply x by 5 element-wise.


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


[ 5 10 15]

Q5. Predict the result of this.


In [4]:
_x = np.array([10, 20, 30], np.int32)
_y = np.array([2, 3, 5], np.int32)
x = tf.convert_to_tensor(_x)
y = tf.convert_to_tensor(_y)
out1 = tf.div(x, y)
out2 = tf.truediv(x, y)
print(np.array_equal(out1.eval(), out2.eval()))

print(out1.eval(), out1.eval().dtype) # tf.div() returns the same results as input tensors. 
print(out2.eval(), out2.eval().dtype)# tf.truediv() always returns floating point results.

Q6. Get the remainder of x / y element-wise.


In [12]:
_x = np.array([10, 20, 30], np.int32)
_y = np.array([2, 3, 7], np.int32)
x = tf.convert_to_tensor(_x)
y = tf.convert_to_tensor(_y)


[0 2 2]

Q7. Compute the pairwise cross product of x and y.


In [13]:
_x = np.array([1, 2, 3], np.int32)
_y = np.array([4, 5, 6], np.int32)
x = tf.convert_to_tensor(_x)
y = tf.convert_to_tensor(_y)


[-3  6 -3]

Basic Math Functions

Q8. Add x, y, and z element-wise.


In [14]:
_x = np.array([1, 2, 3], np.int32)
_y = np.array([4, 5, 6], np.int32)
_z = np.array([7, 8, 9], np.int32)
x = tf.convert_to_tensor(_x)
y = tf.convert_to_tensor(_y)
z = tf.convert_to_tensor(_y)


[ 9 12 15]

Q9. Compute the absolute value of X element-wise.


In [15]:
_X = np.array([[1, -1], [3, -3]])
X = tf.convert_to_tensor(_X)


[[1 1]
 [3 3]]

Q10. Compute numerical negative value of x, elemet-wise.


In [16]:
_x = np.array([1, -1])
x = tf.convert_to_tensor(_x)


[[1 1]
 [3 3]]

Q11. Compute an element-wise indication of the sign of x, element-wise.


In [17]:
_x = np.array([1, 3, 0, -1, -3])
x = tf.convert_to_tensor(_x)


[ 1  1  0 -1 -1]

Q12. Compute the reciprocal of x, element-wise.


In [18]:
_x = np.array([1, 2, 2/10])
x = tf.convert_to_tensor(_x)


[ 1.   0.5  5. ]

Q13. Compute the square of x, element-wise.


In [19]:
_x = np.array([1, 2, -1])
x = tf.convert_to_tensor(_x)


[1 4 1]

Q14. Predict the results of this, paying attention to the difference among the family functions.


In [20]:
_x = np.array([2.1, 1.5, 2.5, 2.9, -2.1, -2.5, -2.9])
x = tf.convert_to_tensor(_x)


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

Q15. Compute square root of x element-wise.


In [21]:
_x = np.array([1, 4, 9], dtype=np.float32)
x = tf.convert_to_tensor(_x)


[ 1.  2.  3.]

Q16. Compute the reciprocal of square root of x element-wise.


In [22]:
_x = np.array([1., 4., 9.])
x = tf.convert_to_tensor(_x)


[ 1.          0.5         0.33333333]

Q17. Compute $x^y$, element-wise.


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


[[ 1  4]
 [ 3 16]]

Q17. Compute $e^x$, element-wise.


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


[  2.71828175   7.38905621  20.08553696]

Q18. Compute natural logarithm of x element-wise.


In [25]:
_x = np.array([1, np.e, np.e**2])
x = tf.convert_to_tensor(_x)


[ 0.  1.  2.]

Q19. Compute the max of x and y element-wise.


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


[2 5 4]

Q20. Compute the min of x and y element-wise.


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


[1 3 2]

Q21. Compuete the sine, cosine, and tangent of x, element-wise.


In [28]:
_x = np.array([-np.pi, np.pi, np.pi/2])
x = tf.convert_to_tensor(_x)


[ -1.22464680e-16   1.22464680e-16   1.00000000e+00]
[ -1.00000000e+00  -1.00000000e+00   6.12323400e-17]
[  1.22464680e-16  -1.22464680e-16   1.63312394e+16]

Q22. Compute (x - y)(x - y) element-wise.


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


[1 4 9]

In [ ]: