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. Create a tensor of the shape [2, 3] with all elements set to zero.
In [7]:
out = tf.zeros([2, 3])
print(out.eval())
assert np.allclose(out.eval(), np.zeros([2, 3]))
# tf.zeros == np.zeros
Q2. Let X be a tensor of [[1,2,3], [4,5,6]].
Create a tensor of the same shape and dtype as X with all elements set to zero.
In [8]:
_X = np.array([[1,2,3], [4,5,6]])
X = tf.convert_to_tensor(_X)
out = tf.zeros_like(X)
print(out.eval())
assert np.allclose(out.eval(), np.zeros_like(_X))
# tf.zeros_like == np.zeros_like
Q3. Create a tensor of shape [2, 3] with all elements set to one.
In [9]:
out = tf.ones([2, 3])
print(out.eval())
assert np.allclose(out.eval(), np.ones([2, 3]))
# tf.ones == np.ones
Q4. Let X be a tensor of [[1,2,3], [4,5,6]].
Create a tensor of the same shape and dtype as X with all elements set to one.
In [10]:
_X = np.array([[1,2,3], [4,5,6]])
X = tf.convert_to_tensor(_X)
out = tf.ones_like(X)
print(out.eval())
assert np.allclose(out.eval(), np.ones_like(_X))
# tf.ones_like == np.ones_like
Q5. Create a tensor of the shape [3, 2], with all elements of 5.
In [11]:
out1 = tf.fill([3, 2], 5)
out2 = tf.ones([3, 2]) * 5
out3 = tf.constant(5, shape=[3, 2])
assert np.allclose(out1.eval(), out2.eval())
assert np.allclose(out1.eval(), out3.eval())
assert np.allclose(out1.eval(), np.full([3, 2], 5))
print(out1.eval())
Q6. Create a constant tensor of [[1, 3, 5], [4, 6, 8]], with dtype=float32
In [12]:
out = tf.constant([[1, 3, 5], [4, 6, 8]], dtype=tf.float32)
print(out.eval())
assert np.allclose(out.eval(), np.array([[1, 3, 5], [4, 6, 8]], dtype=np.float32))
Q7. Create a constant tensor of the shape [2, 3], with all elements set to 4.
In [13]:
out = tf.constant(4, shape=[2, 3])
print(out.eval())
assert np.allclose(out.eval(), np.full([2, 3], 4))
Q8. Create a 1-D tensor of 50 evenly spaced elements between 5 and 10 inclusive.
In [14]:
out = tf.linspace(5., 10., 50)
print(out.eval())
assert np.allclose(out.eval(), np.linspace(5., 10., 50))
# tf.linspace == np.linspace
Q9. Create a tensor which looks like [10, 12, 14, 16, ..., 100].
In [15]:
out = tf.range(10, 101, 2)
print(out.eval())
assert np.allclose(out.eval(), np.arange(10, 101, 2))
# tf.range == np.arange
# Note that the end is exlcuded unlike tf.linspace
Q10. Create a random tensor of the shape [3, 2], with elements from a normal distribution of mean=0, standard deviation=2.
In [16]:
X = tf.random_normal([3, 2], 0, 2.)
print(X.eval())
# tf.random_normal is almost equivalent to np.random.normal
# But the order of the arguments is differnt.
# _X = np.random.normal(0, 2., [3, 2])
Q11. Create a random tensor of the shape [3, 2], with elements from a normal distribution of mean=0, standard deviation=1 such that any values don't exceed 2 standard deviations from the mean.
In [17]:
out = tf.truncated_normal([3, 2])
print(out.eval())
Q12. Create a random tensor of the shape [3, 2], with all elements from a uniform distribution that ranges from 0 to 2 (exclusive).
In [18]:
out = tf.random_uniform([3, 2], 0, 2)
print(out.eval())
# tf.random_uniform is almost equivalent to np.random.uniform
# But the order of the arguments is differnt.
# _X = np.random.uniform(0, 2., [3, 2])
Q13. Let X be a tensor of [[1, 2], [3, 4], [5, 6]]. Shuffle X along its first dimension.
In [19]:
_X = np.array([[1, 2], [3, 4], [5, 6]])
X = tf.constant(_X)
out = tf.random_shuffle(X)
print(out.eval())
# tf.random_shuffle() is not a in-place function unlike np.random_shuffle().
# np.random.shuffle(_X)
# print(_X)
Q14. Let X be a random tensor of the shape [10, 10, 3], with elements from a unit normal distribution. Crop X with the shape of [5, 5, 3].
In [20]:
X = tf.random_normal([10, 10, 3])
out = tf.random_crop(X, [5, 5, 3])
print(out.eval())
In [ ]: