Transforming Tensors as multidimensional data arrays

In this section, we explore a selection of operators that can be used to transform tensors. Note that some of these operators work very similar to NumPy array transformations. However, when we are dealing with tensors with ranks higher than 2, we need to be careful in using such transformations, for example, the transpose of a tensor.

First, as in NumPy, we can use the attribute arr.shape to get the shape of a NumPy array. In TensorFlow, we use the tf.get_shape function instead:

>>> import tensorflow as tf >>> import numpy as np >>> >>> g = tf.Graph() >>> with g.as_default(): ... arr = np.array([[1., 2., 3., 3.5], ... [4., 5., 6., 6.5], ... [7., 8., 9., 9.5]]) ... T1 = tf.constant(arr, name='T1') ...

Get Python Machine Learning - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.