hi everybody welcome back in this video we're going to cover some of the essential tensorflow operations that we'll be using throughout the course tensorflow has many analogous functions to numpy and so the usage is very similar to numpy and straightforward in many cases however there are a few things in tensorflow that are unique to tensorflow and we're going to cover some of the most common use cases in this video first let's talk about tensorflow constants and variables tensorflow constants are immutable objects that must be created with the constant method in tensorflow they can be created
for any rank tensor and data type and once they're created their value cannot be changed as shown below we're creating three rank zero constants and notice that the data type is inferred in the first two cases since it isn't specified and then in the third case we're specifying a data type of float 64 to retain some additional precision in the next example we're showing that you can create tensor constants of any rank in the first example the data type of the rank 1 constant tensor is inferred since it wasn't specified and in the second case
we're specifying the data type explicitly as an n32 for the rank 2 constant tensor next let's take a look at how to concatenate tensors as an example how tensorflow parallels the use of numpy in many cases here we're creating two two by three tensors and concatenating them along axis zero and axis one and as you can see below the syntax is very straightforward as you might expect but as we'll see in just a bit there are some things in tensorflow that are conceptually the same as numpy but have different syntax worth mentioning but first let's
talk about tensorflow variables tensorflow variables of any type must be created with the tensorflow variable method in the same way that constants are created in tensorflow tensorflow variables can be reassigned but it's not possible to do this with the assignment operator as you might expect so that's what this first code cell demonstrates here we're creating a one by two tensorflow variable that contains two elements and then we're attempting to reassign the first element of the tensor to the number 11. but as you can see this produces an error so in order to assign a new
value to a tensorflow variable we must use the assign method as shown here and in the output below you can see that we're able to reassign the value of the first element to 42. as we mentioned previously there's many operations in tensorflow that parallel the syntax and numpy such as the concatenate method that we demonstrated above but there's also some differences in tensorflow for example there's a class of reduced functions that will compute various quantities based on the numeric values within a tensor for example min max mean and sum are all commonly used methods in
numpy and tensorflow but the tensorflow versions are preceded with the name reduce conceptually the use is the same as in numpy where you specify any particular axis for the operation or if you don't specify an axis and the computation is made for all the elements in the data structure so we're simply pointing out here that the names of these methods in tensorflow are different but as you might expect the usage is really the same as you can see in the examples below so let's now talk about indexing and tensorflow and this is another example where
the concept is exactly the same as numpy but the syntax is different as we'll see below in order to index a tensor with another tensor we're going to need to use the gather method but let's first review how this is done in numpy in this example we begin by creating a numpy array of 24 random numbers and then next we're going to select five random indices and then use that array of indices to select the corresponding elements in the data array and as you can see we get the expected result below next let's perform the
same operation in tensorflow and again there's no conceptual difference it's simply the syntax that's different so here we're going to create a rank 1 tensor with 24 data elements and then we're going to randomly specify five elements as indices and in order to extract the data from the tensor associated with those indices we need to use the gather method as shown below where we pass in the tensor that contains our data as the first argument and then the tensor that contains the indices as the second argument and then here's another example that shows if we
have a rank two tensor that we can easily select specific rows or specific columns using the same approach and notice that this time we need to specify the axis that the indices should be applied to we're now going to conclude this notebook with a section on numpy tensorflow interoperability that introduces two specialized functions that allow you to convert back and forth between numpy objects and tensorflow tensors these are especially useful when you're writing code some of which may be in numpy and some of which may be in tensorflow these two specialized methods are called convert
to tensor and numpy convert to tensor allows you to convert a python object such as a scalar python list or numpy array to tensor objects and numpy allows you to convert a tensor to a numpy array in the code below we're creating a python list and then we're also converting that list to a numpy array so that we can demonstrate how both of these object types can be converted to tensors using the convert to tensor method in the last example we're then converting one of those tensors back to a numpy array using the numpy method
in tensorflow so the usage is straightforward and very convenient so that's all we wanted to cover in this video in future videos we'll start introducing the use of tensorflow objects and we'll be making use of many of the techniques discussed in this video thanks so much and we'll see you next time