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. Let X be a tensor of [["1.1", "2.2"], ["3.3", "4.4"]]. Convert the datatype of X to float32.
In [7]:
Q2. Let X be a tensor [[1, 2], [3, 4]] of int32. Convert the data type of X to float64.
In [8]:
Q3. Let X be a tensor [[1, 2], [3, 4]] of int32. Convert the data type of X to float32.
In [9]:
Q4. Let X be a tensor [[1, 2], [3, 4]] of float32. Convert the data type of X to int32.
In [10]:
Q5. Let X be a tensor [[1, 2], [3, 4]] of float32. Convert the data type of X to int64.
In [11]:
Q6. Let X be a tensor of [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]. Create a tensor representing the shape of X.
In [12]:
Q7. Let X be a tensor of [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]) and y be a tensor [10, 20]. Create a list of tensors representing the shape of X and y.
In [13]:
Q8. Let X be a tensor of [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]. Create a tensor representing the size (=total number of elements) of X.
In [14]:
Q9. Let X be a tensor of [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]. Create a tensor representing the rank (=number of dimensions) of X.
In [15]:
Q10. Let X be tf.ones([10, 10, 3]). Reshape X so that the size of the second dimension equals 150.
In [16]:
Q11. Let X be tf.ones([10, 10, 1, 1]). Remove all the dimensions of size 1 in X.
In [17]:
Q12. Let X be tf.ones([10, 10, 1, 1]). Remove only the third dimension in X.
In [18]:
Q13. Let X be tf.ones([10, 10]). Add a dimension of 1 at the end of X.
In [19]:
Q14. Let X be a tensor
[[[1, 1, 1], [2, 2, 2]],
[[3, 3, 3], [4, 4, 4]],
[[5, 5, 5], [6, 6, 6]]].
Extract the [[[3, 3, 3], [5, 5, 5]] from X.
In [20]:
Q15. Let X be a tensor of
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]].
Extract the [[1, 2], [5, 6], [9, 10]]] from X.
In [21]:
Q16. Let X be a tensor of
[[ 1 2 3 4 5]
[ 6 7 8 9 10]].
Split X into 5 same-sized tensors along the second dimension.
In [22]:
Q17. Lex X be a tensor
[[ 1 2 3]
[ 4 5 6].
Create a tensor looking like
[[ 1 2 3 1 2 3 1 2 3 ]
[ 4 5 6 4 5 6 4 5 6 ]].
In [23]:
Q18. Lex X be a tensor
[[ 1 2 3]
[ 4 5 6].
Pad 2 0's before the first dimension, 3 0's after the second dimension.
In [24]:
Q19. Lex X be a tensor
[[ 1 2 3]
[ 4 5 6].
and Y be a tensor
[[ 7 8 9]
[10 11 12]].
Concatenate X and Y so that the new tensor looks like [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]].
In [25]:
Q20. Let x, y, and z be tensors [1, 4], [2, 5], and [3, 6], respectively.
Create a single tensor from these such that it looks [[1, 2, 3], [4, 5, 6]].
In [26]:
Q21. Let X be a tensor [[1, 2, 3], [4, 5, 6]]. Convert X into Y such that Y looks like [[1, 4], [2, 5], [3, 6]].
In [27]:
Q22. Given X below, reverse the sequence along the second axis except the zero-paddings.
In [28]:
X = tf.constant(
[[[0, 0, 1],
[0, 1, 0],
[0, 0, 0]],
[[0, 0, 1],
[0, 1, 0],
[1, 0, 0]]])
Out[28]:
Q23. Given X below, reverse the last dimension.
In [29]:
_X = np.arange(1, 1*2*3*4 + 1).reshape((1, 2, 3, 4))
X = tf.convert_to_tensor(_X)
Q24. Given X below, permute its dimensions such that the new tensor has shape (3, 1, 2).
In [30]:
_X = np.ones((1, 2, 3))
X = tf.convert_to_tensor(_X)
Q25. Given X, below, get the first, and third rows.
In [31]:
_X = np.arange(1, 10).reshape((3, 3))
X = tf.convert_to_tensor(_X)
Q26. Given X below, get the elements 5 and 7.
In [32]:
_X = np.arange(1, 10).reshape((3, 3))
X = tf.convert_to_tensor(_X)
Q27. Let x be a tensor [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get the tensors of unique elements and their counts.
In [33]:
Q28. Let x be a tensor [1, 2, 3, 4, 5]. Divide the elements of x into a list of tensors that looks like [[3, 5], [1], [2, 4]].
In [34]:
Q29. Let X be a tensor [[7, 8], [5, 6]] and Y be a tensor [[1, 2], [3, 4]]. Create a single tensor looking like [[1, 2], [3, 4], [5, 6], [7, 8]].
In [35]:
Q30. Let x be a tensor [0, 1, 2, 3] and y be a tensor [True, False, False, True].
Apply mask y to x.
In [36]:
Q31. Let x be a tensor [[0, 5, 3], [4, 2, 1]]. Convert X into one-hot.
In [37]:
In [ ]: