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]:
In [3]:
author = "kyubyong. https://github.com/Kyubyong/tensorflow-exercises"
In [4]:
tf.__version__
Out[4]:
In [5]:
np.__version__
Out[5]:
In [3]:
sess = tf.InteractiveSession()
NOTE on notation
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)
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)
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)
Q4. Multiply x by 5 element-wise.
In [10]:
_x = np.array([1, 2, 3])
x = tf.convert_to_tensor(_x)
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)
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)
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)
Q9. Compute the absolute value of X element-wise.
In [15]:
_X = np.array([[1, -1], [3, -3]])
X = tf.convert_to_tensor(_X)
Q10. Compute numerical negative value of x, elemet-wise.
In [16]:
_x = np.array([1, -1])
x = tf.convert_to_tensor(_x)
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)
Q12. Compute the reciprocal of x, element-wise.
In [18]:
_x = np.array([1, 2, 2/10])
x = tf.convert_to_tensor(_x)
Q13. Compute the square of x, element-wise.
In [19]:
_x = np.array([1, 2, -1])
x = tf.convert_to_tensor(_x)
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)
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)
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)
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)
Q17. Compute $e^x$, element-wise.
In [24]:
_x = np.array([1., 2., 3.], np.float32)
x = tf.convert_to_tensor(_x)
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)
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)
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)
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)
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)
In [ ]: