Constants_Sequences_and_Random_Values


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]:
datetime.date(2017, 2, 22)

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 [6]:
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

Constant Value Tensors

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


[[ 0.  0.  0.]
 [ 0.  0.  0.]]

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


[[0 0 0]
 [0 0 0]]

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


[[ 1.  1.  1.]
 [ 1.  1.  1.]]

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


[[1 1 1]
 [1 1 1]]

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())


[[5 5]
 [5 5]
 [5 5]]

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))


[[ 1.  3.  5.]
 [ 4.  6.  8.]]

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))


[[4 4 4]
 [4 4 4]]

Sequences

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


[  5.           5.10204077   5.20408154   5.3061223    5.40816307
   5.51020432   5.61224508   5.71428585   5.81632662   5.91836739
   6.02040815   6.12244892   6.22448969   6.32653046   6.4285717
   6.53061247   6.63265324   6.734694     6.83673477   6.93877554
   7.04081631   7.14285755   7.24489784   7.34693909   7.44897938
   7.55102062   7.65306139   7.75510216   7.85714293   7.95918369
   8.06122494   8.16326523   8.26530647   8.36734676   8.46938801
   8.5714283    8.67346954   8.77551079   8.87755108   8.97959232
   9.08163261   9.18367386   9.2857151    9.38775539   9.48979568
   9.59183693   9.69387817   9.79591846   9.89795876  10.        ]

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


[ 10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44
  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80
  82  84  86  88  90  92  94  96  98 100]

Random Tensors

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])


[[-0.44651982  1.04446733]
 [-3.47446632  2.4686904 ]
 [ 1.22045743 -3.3476913 ]]

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())


[[-0.0132482   1.10570967]
 [ 0.21080986  0.21604359]
 [ 0.34207392  0.2467791 ]]

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])


[[ 0.80732584  1.49800301]
 [ 0.22903824  0.97828937]
 [ 1.18014169  0.43648648]]

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)


[[1 2]
 [5 6]
 [3 4]]

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())


[[[ -8.05899918e-01   2.72292346e-01   2.16315291e-03]
  [  2.74491131e-01   6.07317924e-01   1.76837713e-01]
  [  1.89213589e-01  -6.06034577e-01   1.14093232e+00]
  [  1.29428029e+00  -6.79318488e-01   1.25192130e+00]
  [ -2.18161201e+00   8.26316059e-01   2.24443465e-01]]

 [[ -4.31469619e-01   1.74119067e+00   8.67258251e-01]
  [ -1.04736495e+00   6.93329647e-02   2.46455050e+00]
  [  6.71136856e-01   1.38696265e+00   4.60413188e-01]
  [ -5.97481489e-01   1.32574654e+00   3.99490744e-01]
  [  1.16463304e+00  -2.86259800e-01   2.08924357e-02]]

 [[  1.16547585e+00   8.66836131e-01  -1.37871552e+00]
  [ -5.92198253e-01   4.10155028e-01   4.00556743e-01]
  [ -1.84391201e+00   6.03951514e-01   1.51700044e+00]
  [ -6.46995664e-01  -1.00076616e+00  -2.53373098e+00]
  [  1.11354995e+00  -4.37033147e-01  -1.22310674e+00]]

 [[ -1.24956787e-01   1.99593633e-01  -2.10244715e-01]
  [ -3.06440592e-01  -2.36029923e-01  -4.63652372e-01]
  [  7.61949897e-01   1.82299927e-01  -5.73334515e-01]
  [ -3.50696027e-01   1.47090709e+00   7.79132247e-01]
  [  3.73292446e-01  -1.17061816e-01  -2.93730199e-01]]

 [[  1.15475014e-01  -8.85104761e-03   2.70997882e-01]
  [  5.39043248e-01  -1.22421873e+00   5.91688395e-01]
  [  9.84528363e-01   1.02289438e+00   3.06517988e-01]
  [ -8.25244248e-01   1.20455906e-01   2.35888982e+00]
  [ -6.59384608e-01  -3.17072958e-01  -1.63202643e-01]]]

In [ ]: