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') ... print(T1) ... s = T1.get_shape() ... print('Shape of T1 is', s) ... T2 = tf.Variable(tf.random_normal( ... shape=s)) ... ...