Tensor Transformations

``````

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

Casting

Q1. Let X be a tensor of [["1.1", "2.2"], ["3.3", "4.4"]]. Convert the datatype of X to float32.

``````

In [7]:

``````
``````

[[ 1.10000002  2.20000005]
[ 3.29999995  4.4000001 ]]

``````

Q2. Let X be a tensor [[1, 2], [3, 4]] of int32. Convert the data type of X to float64.

``````

In [8]:

``````
``````

[[ 1.  2.]
[ 3.  4.]]

``````

Q3. Let X be a tensor [[1, 2], [3, 4]] of int32. Convert the data type of X to float32.

``````

In [9]:

``````
``````

[[ 1.  2.]
[ 3.  4.]]

``````

Q4. Let X be a tensor [[1, 2], [3, 4]] of float32. Convert the data type of X to int32.

``````

In [10]:

``````
``````

[[1 2]
[3 4]]

``````

Q5. Let X be a tensor [[1, 2], [3, 4]] of float32. Convert the data type of X to int64.

``````

In [11]:

``````
``````

[[1 2]
[3 4]]

``````

Shapes and Shaping

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

``````
``````

[3 2 2]

``````

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

``````
``````

[3 2 2] [2]

``````

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

``````
``````

12

``````

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

``````
``````

3

``````

Q10. Let X be tf.ones([10, 10, 3]). Reshape X so that the size of the second dimension equals 150.

``````

In [16]:

``````
``````

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

``````

Q11. Let X be tf.ones([10, 10, 1, 1]). Remove all the dimensions of size 1 in X.

``````

In [17]:

``````
``````

(10, 10)

``````

Q12. Let X be tf.ones([10, 10, 1, 1]). Remove only the third dimension in X.

``````

In [18]:

``````
``````

(10, 10, 1)

``````

Q13. Let X be tf.ones([10, 10]). Add a dimension of 1 at the end of X.

``````

In [19]:

``````
``````

(10, 10, 1)

``````

Slicing and Joining

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

``````
``````

[[[3 3 3]]

[[5 5 5]]]

``````

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

``````
``````

[[ 1  2]
[ 5  6]
[ 9 10]]

``````

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

``````
``````

[array([[1],
[6]]), array([[2],
[7]]), array([[3],
[8]]), array([[4],
[9]]), array([[ 5],
[10]])]

``````

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

``````
``````

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

``````

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

``````
``````

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

``````

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

``````
``````

[[ 1  2  3  7  8  9]
[ 4  5  6 10 11 12]]

``````

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

``````
``````

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

``````

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

``````
``````

[array([1, 4]), array([2, 5]), array([3, 6])]

``````

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

array([[[0, 1, 0],
[0, 0, 1],
[0, 0, 0]],

[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]])

``````

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)

``````
``````

[[[[ 4  3  2  1]
[ 8  7  6  5]
[12 11 10  9]]

[[16 15 14 13]
[20 19 18 17]
[24 23 22 21]]]]

``````

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)

``````
``````

(3, 1, 2)

``````

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)

``````
``````

[[1 2 3]
[7 8 9]]

``````

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)

``````
``````

[5 7]

``````

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

``````
``````

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

``````

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

``````
``````

[array([3, 5]), array([1]), array([2, 4])]

``````

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

``````
``````

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

``````

Q30. Let x be a tensor [0, 1, 2, 3] and y be a tensor [True, False, False, True].

``````

In [36]:

``````
``````

[0 3]

``````

Q31. Let x be a tensor [[0, 5, 3], [4, 2, 1]]. Convert X into one-hot.

``````

In [37]:

``````
``````

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

``````
``````

In [ ]:

``````