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]:
In [3]:
author = "kyubyong. https://github.com/Kyubyong/tensorflow-exercises"
In [4]:
tf.__version__
Out[4]:
In [5]:
np.__version__
Out[5]:
In [6]:
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)
out1 = tf.add(x, y)
out2 = x + y
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval()) # tf.add is the same as the `+` operator.
_out = np.add(_x, _y)
assert np.array_equal(out1.eval(), _out) # tf.add == np.add
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)
out1 = tf.subtract(x, y)
out2 = x - y
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval()) # tf.subtract is the same as the `-` operator.
_out = np.subtract(_x, _y)
assert np.array_equal(out1.eval(), _out) # tf.subtract == np.subtract
# `add`, `subtract`, `multiply`, `div`, and `mod` support broadcasting.
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)
out1 = tf.multiply(x, y)
out2 = x * y
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval()) # tf.multiply is the same as the `*` operator.
_out = np.multiply(_x, _y)
assert np.array_equal(out1.eval(), _out) # tf.multiply == np.multiply
Q4. Multiply x by 5 element-wise.
In [10]:
_x = np.array([1, 2, 3])
x = tf.convert_to_tensor(_x)
out1 = tf.scalar_mul(5, x)
out2 = x * 5
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval())
_out = _x * 5
assert np.array_equal(out1.eval(), _out)
Q5. Predict the result of this.
In [11]:
_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.
_out1 = _x // _y
assert np.array_equal(out1.eval(), _out1)
_out2 = _x / _y
assert np.array_equal(out2.eval(), _out2)
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)
out1 = tf.mod(x, y)
out2 = x % y
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval())# tf.mod() is the same as the `%` operator.
_out = _x % _y
assert np.array_equal(out1.eval(), _out)
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)
out1 = tf.cross(x, y)
print(out1.eval())
_out = np.cross(_x, _y)
assert np.array_equal(out1.eval(), _out) # tf.cross == np.cross
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)
out1 = tf.add_n([x, y, z])
out2 = x + y + z
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval())
Q9. Compute the absolute value of X element-wise.
In [15]:
_X = np.array([[1, -1], [3, -3]])
X = tf.convert_to_tensor(_X)
out = tf.abs(X)
print(out.eval())
_out = np.abs(_X)
assert np.array_equal(out.eval(), _out) # tf.abs == np.abs
Q10. Compute numerical negative value of x, elemet-wise.
In [16]:
_x = np.array([1, -1])
x = tf.convert_to_tensor(_x)
out1 = tf.negative(x)
out2 = -x
print(out.eval())
assert np.array_equal(out1.eval(), out2.eval())
_out = np.negative(_x)
assert np.array_equal(out1.eval(), _out) # tf.negative == np.negative
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)
out = tf.sign(x)
print(out.eval())
_out = np.sign(_x)
assert np.array_equal(out.eval(), _out) # tf.sign == np.sign
Q12. Compute the reciprocal of x, element-wise.
In [18]:
_x = np.array([1, 2, 2/10])
x = tf.convert_to_tensor(_x)
out1 = tf.reciprocal(x)
out2 = 1/x
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval())
_out = np.reciprocal(_x)
assert np.array_equal(out1.eval(), _out) # tf.reciprocal == np.reciprocal
Q13. Compute the square of x, element-wise.
In [19]:
_x = np.array([1, 2, -1])
x = tf.convert_to_tensor(_x)
out1 = tf.square(x)
out2 = x * x
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval())
_out = np.square(_x)
assert np.array_equal(out1.eval(), _out) # tf.square == np.square
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)
out1 = tf.round(x)
out2 = tf.floor(x)
out3 = tf.ceil(x)
print(out1.eval())
print(out2.eval())
print(out3.eval())
_out1 = np.around(_x)
_out2 = np.floor(_x)
_out3 = np.ceil(_x)
assert np.array_equal(out1.eval(), _out1) # tf.round == np.around
assert np.array_equal(out2.eval(), _out2) # tf.floor == np.floor
assert np.array_equal(out3.eval(), _out3) # tf.ceil == np.ceil
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)
out = tf.sqrt(x)
print(out.eval())
_out = np.sqrt(_x)
assert np.array_equal(out.eval(), _out) # tf.sqrt ≈ np.sqrt
# Note that in TensorFlow, the input tensor must be
# a float, whereas in Numpy, an integer is okay.
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)
out1 = tf.rsqrt(x)
out2 = tf.reciprocal(tf.sqrt(x))
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval())
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)
out = tf.pow(x, y)
print(out.eval())
_out = np.power(_x, _y)
assert np.array_equal(out.eval(), _out) # tf.pow == np.power
Q17. Compute $e^x$, element-wise.
In [24]:
_x = np.array([1., 2., 3.], np.float32)
x = tf.convert_to_tensor(_x)
out1 = tf.exp(x)
out2 = tf.pow(np.e, x) #np.e = 2.718281828459045
print(out1.eval())
assert np.allclose(out1.eval(), out2.eval())
_out = np.exp(_x)
assert np.array_equal(out1.eval(), _out) # tf.exp == np.exp
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)
out = tf.log(x)
print(out.eval())
_out = np.log(_x)
assert np.array_equal(out.eval(), _out) # tf.log == np.log
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)
out1 = tf.maximum(x, y)
out2 = tf.where(x > y, x, y)
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval())
_out = np.maximum(_x, _y)
assert np.array_equal(out1.eval(), _out) # tf.maximum == np.maximum
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)
out1 = tf.minimum(x, y)
out2 = tf.where(x < y, x, y)
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval())
_out = np.minimum(_x, _y)
assert np.array_equal(out1.eval(), _out) # tf.minimum == np.minimum
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)
out1 = tf.sin(x)
out2 = tf.cos(x)
out3 = tf.tan(x)
print(out1.eval())
print(out2.eval())
print(out3.eval())
_out1 = np.sin(_x)
_out2 = np.cos(_x)
_out3 = np.tan(_x)
assert np.allclose(out1.eval(), _out1) # tf.sin == np.sin
assert np.allclose(out2.eval(), _out2) # tf.cos == np.cos
assert np.allclose(out3.eval(), _out3) # tf.tan == np.tan
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)
out1 = tf.squared_difference(x, y)
out2 = tf.square(tf.subtract(x, y))
print(out1.eval())
assert np.array_equal(out1.eval(), out2.eval())
In [ ]: