From the course: TensorFlow: Practical Skills in Constructing, Training, and Optimizing Models

Define and manipulate tensors

- [Instructor] In TensorFlow, tensors are essential for constructing and deploying powerful machine learning solutions. They're the primary way of representing structured data, which is what your machine learning models will take as input. In this lesson, I'll cover the basics of tensors to help you understand their fundamental operations in TensorFlow. First, I'll define what a tensor is. Just like a vector is a list of numbers in one dimension, and a matrix is a grid of numbers in two dimensions, a tensor is a more generalized array of numbers that can have multiple dimensions. You can think of vectors and matrices as limiting cases of tensors. The rows and columns of our arrays may represent variables and values, pixels of a digital image, frequencies of ngrams in a corpus of text documents, or many other things. Go ahead and open up 02_01_Define.ipynb. You can create a tensor simply by using the constant function. It looks like this. Start with your variable equals tf.constant. Then enter your numbers. Here, I'm just putting in random numbers. I'm not worried about any particular data yet. As usual, I'll hit Shift + Return to execute the code in the cell. You can access tensor properties by taking advantage of Python's object structure with the dot notation. Every tensor will have a shape attribute, which shows the dimensions of the tensor and a dtype attribute, which shows the data type of the objects in the tensor. So here I'm simply executing the code that says x.shape, and then executing the code that says x.dtype. There are many techniques of machine learning that require you to manipulate the shapes of your data tensors. Perhaps you're computing a singular value decomposition as a way to reduce the dimensionality of your problem. Perhaps you're using a pre-trained neural network that requires your data to have a certain input shape. Whatever the case may be, TensorFlow makes it easy to compute basic manipulations on your tensors. Let me show you a couple simple two-dimensional examples. First, you can transpose any array, that is swap its rows and columns, simply by calling the transpose function. So here my code is tf.transpose of x, and when I execute it, you can see that the rows have become columns and vice versa. Finally, I'll reshape a tensor using the reshape function. Does your data have two rows and three columns when you need it to have three rows and two columns? This is easily solved with the reshape function. So here I'm executing the code that says tf.reshape of x three, comma, two. The three, comma, two bit tells TensorFlow how to arrange the data in the tensor. Here I'm asking for three rows and two columns. Tensors and their attributes are the nuts and bolts of TensorFlow. Once you've taken a look at a few more features of tensors, you'll be ready to start building machine learning models.

Contents