Python Tutorial : Constants and variables

Isaiah Ho covers TensorFlow basics in Python: what tensors are, how to create constants as immutable values, and how variables differ as mutable, persistent tensors. He uses a bread-slice analogy to explain tensor dimensions and shows constructor functions like zeros and ones for quick tensor generation.
Chapters
Hi, my name is Isaiah Ho and this is a course on the Fundamentals of the TensorFlow API in Python. In our first video we will briefly introduce TensorFlow and then discuss its two basic objects of computation, constants and variables. TensorFlow is an open source library for graph based numerical computation. It was developed by the Google Brain team. It has both low and high level APIs. You can use TensorFlow to perform addition, multiplication and differentiation. You can also use it to design and train machine learning models. TensorFlow 2.0 brought with it substantial changes. Eager execution is now enabled by default, which allows users to write simpler and more intuitive code. Additionally, model building is now centered around the Keras and estimators high level APIs. The TensorFlow documentation describes a tensor as a generalization of vectors and matrices to potentially higher dimensions. Now, if you're not familiar with linear algebra, you can simply think of a tensor as a collection of numbers which is arranged into a particular shape. As an example, let's say you have a slice of bread and you cut it into nine pieces. One of those nine pieces is a zero dimensional tensor. This corresponds to a single number. A collection of three pieces that form a row or a column is a one dimensional tensor. All nine pieces together are a two dimensional tensor and the whole loaf, which contains many slices, is a three dimensional tensor. Now that you know what a tensor is, let's define a few. We'll start by importing TensorFlow's TF. We will then define 0, 1, 2 and 3 dimensional tensors. Note that each object will be a tftensor object. If we want to print the array contained in that object, we can apply the numpy method and pass the resulting object to the print function. We next move on to constants, which are the simplest category of tensor and tensorflow. A constant does not change and cannot be trained. It can, however, have any dimension. In the code block we've defined two constants. The constant A is a 2x3 tensor of 3s. The constant B is a 2x2 tensor which is constructed from the one dimensional tensor 1, 2, 3, 4. In the previous slide we worked exclusively with the constant operation. However, in some cases there are more convenient options for defining certain types of special tensors. You can use the 0s or 1s operations to generate a tensor of arbitrary dimension that is populated entirely with zeros or ones. You can use the zeros like or ones like operations to populate tensors with zeros and ones copying the dimension of some input tensor. Finally, you can use the fill operation to populate a tensor of arbitrary dimension with the same scalar value in each element. Unlike a constant, a variable's value can change during computation. The value of a variable is shared, persistent, and modifiable however, its data type and shape are fixed. Let's take a look at how variables are constructed and used in TensorFlow. In the code, we first define a variable A0, which is a one dimensional tensor with six elements. We can set its data type to a 32 bit float or something else such as a 16 bit int as we have for A1. We then define a constant B and define C0 as the product of A0 and B. Note that certain tensorflow operations such as tfmultiply are overloaded, which allows us to use the simpler A 0 times B expression instead. It's now time to put what you've learned to use in some exercises.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







