Let's now dive into this new section on TensorFlow indexing.
So let's suppose we have this tensor which we'll call tensor indexed.
And we simply define this 1D tensor with some values.
So we have
24904567.
And then let's run this.
And then we say okay we want to get um let's say this value for now.
To obtain this value for we are going to make use of its index.
In general, in programming, the first index is zero.
So this is going to be 012, three, four, five six and then seven.
So if we want to get this foyer we're going to have to specify the index one.
If we want to get two we have to specify the index zero and so on and so forth for the rest.
So let's after running this that's after getting or creating this tensor index, we could simply print
out tensor index and then specify an index.
So let's say we we want to pick out the start.
So ten tensor index three.
We'll put this three in a square bracket.
We run that.
You see that it's going to give us the value zero which matches with this arrangement.
Now if we say we want to get the last element or if we want to get the the first element, then we'll
specify zero.
So the zeroth index we run that.
And you see, we should get two.
See, we have two.
Now, if you want to have the last element.
So let's say we want to have the last element.
We could pick seven or we could say we want to have tensor indexed.
And then we say negative one.
So when you specify negative one then you're specifying the last element.
Um given that we know the, the the exact index for the last element, which in this case is seven,
then we could specify seven.
So let's run this and then see what we get.
There we go.
You see, we have seven and then we have seven.
So that's how easy it is for us to, uh, obtain a value based on its position in the tensor.
Now let's say we want to get a segment or part of this 1D tensor.
Let's say we want to get this segment.
We want to get 4904.
Now this 4904, as we could see here matches with the indices 1 to 4.
Or let's, let's let's pick another one.
Let's say four.
Um 90456.
This matches with 1 to 6 you see.
So let's just um, run this and see what we get.
Tensor indexed.
Um, and here instead of um, selecting each and every one of this, we would do one because remember
one would pick out this elements 490456.
Then we will specify the indices um one right up to six.
But we are not going to say one, two, three, four, five and then six and then try to run that.
You see, we get an error here.
So what we are going to do is we're going to say we want to go from 1 to 6 by specifying or by putting
between one.
That's the start index and the end index a colon.
So we just put that colon, we run that and there we go.
You see we have this part which we wanted.
That's 494490456.
But when you look at the those outputs actually 49045 now what this tells us is actually when we say
one right up to six, we're going from um, one, uh, let's, let's put it out this way.
When we say one up to six, we're actually going from one right up to.
Six minus one.
So we're actually going from one right up to five.
And so if we want to go or if we want to pick out 490456, then we'll need to say here that we want
to go to seven.
So, so such that we'll go from 1 to 7 and or we'll go from 1 to 7 minus one.
That will be 1 to 6.
So let's take that off.
Run that again.
And you can see now that we have 490456.
So in general what we'll do is um, if we want to go from um a given start.
So we'll do a start to the end.
But instead of um, end, we'll do start to end minus one.
And so in general, if you want to go from index start right up to index end, we should instead do
tensor slice or tensor index.
Um, start right up to end plus one so that uh when we do n plus one minus one it falls back at end.
So that's it.
Now you could, um, pause the video at this point and try to, um, do for example, or get this first
tree, you see that you will have two, four, nine.
That's going to be 0 to 2.
Uh, hopefully you got the answer.
Let's just get that done.
So we'll do tensor tensor um index.
And then we'll do because we're going from 0 to 2.
So we'll do 0 to 2 plus one.
You see let's run.
That could take this off so that we could see the the the original tensor indexed.
So let's take that off.
There we go.
You see um 0 to 2 plus one.
You see we have 249.
Now if we're starting from the very first index, if we're starting from the zeroth index then we must
not put this.
So, uh, taking the zero off already tells us that it's actually zero.
So we could have the zero, or you may not have the zero.
So you see, it's the same answer.
Now, if let's say we want to get from zero right up to the end, what we'll do here, we will have
three right up to seven.
So we'll do three um, up to seven remember plus one.
So we run that again.
And you see we, we should have 0456 704 567 exactly as we expect.
Now given that we're going right up to the end, we could also decide not to put anything.
So if you're going or if you're starting from the, the first the zeroth index, you may not put anything.
Um, if you're going right up to the end, you may also decide not to put any value.
So let's run that and see what we get.
You see, we have exact same, um, output 04567, the same as we would have with um, seven plus 104567.
So that's it.
Now if you want to go right up to the last but one value, you could say we're going from three right
up to minus one.
So remember when we specified, um or when we had tensor tensor indexed um and then we specified minus
one.
The value we got was seven which was the last value.
But when we um specify or when we go from a start index to that end index and we specify this minus
one, obviously we'll go from start to end and the end will instead be the the value before the actual
end.
So it's just like what we've seen already where if we're going from let's say um, three let's take
the start off.
If we're going from three to to seven, then what we will actually get by doing this, if we just took
3 to 7, what will actually get will be 3 to 2 six, you see.
So when we do three two negative one is the same as going from 3 to 7.
So that's actually um 3 to 6.
So when let's run this you see we should get 0456.
So that's it.
Now if we want to get to the second to the last, that's 3 to -2.
You see now we have 045.
Then we could do third to the last we have or we should have zero four.
So on and so forth.
So that's it.
Then another thing we could do is let's say we let's get back to say six.
And the thing we could do is we could, um, get these values and steps.
Let's take that from the beginning to the, to the, to the seat, or let's even take the seventh value.
So let's go to seven.
So if we run this we have from 0 to 7 then we have 2490456.
That's fine.
Now we could um get this values in steps of two.
So if we put two see specifying this two would make us get two.
And then we skip this one.
Nine will skip this one four we'll skip this one and then six.
So let's just run this let's print this out here.
Tensor indexed.
Tensor indexed and let's say goes 0 to 7.
Let's write out explicitly 0 to 7.
And then here we have 0 to 7.
Bear with steps of two.
So let's print this out.
Let's print that out.
There we go.
So as you could see we have 2490456.
But now uh when we specify steps of two we'll take two and then we'll skip four.
We get nine.
See that's nine here.
And then we skip zero.
We get four.
Does four year and then we skip, um, six.
All right, I will skip five.
And then we get six and that's six there.
So that's it.
So essentially this is the same as um having one.
So not skipping any value.
So that's it.
We've just looked at indexing with 1D tensors.
So let's take an example of a two dimensional tensor.
So here we have again tensor um indexed tensor indexed.
And then we have this 2D tensor.
We simply fill out those values.
So we have 23456.
And then we have say 90421.
And then we have 001 or well 000017 um, zero.
Okay.
So we have this tensor which turns out to be a five or rather three by five tensor 2D.
So we have to add this square bracket.
There we go.
We add the square bracket here and at this position.
So this is our 2D tensor.
And then we want to get a specific value.
Now let's suppose that we want to get the value or this value nine at this position.
So what we'll do is we'll start by saying um how many rows do we have.
We have one two, three.
And what we'll do is we'll locate the row in which this nine is found and the column.
So this is the first row second row and third row.
While this is the first column that's 290 first um colon second colon, third colon fourth colon and
fifth column.
Now this nine is found on the second row.
But since we start counting from one from zero we have the zeroth row and the first row.
So here we'll say we want to have tensor tensor indexed.
And then we'll specify we want to have one.
So index one because this is zero this is one.
And then this is two.
Now for the colon we have colon zero because this is the first colon or this is the zeroth colon or
colon with index zero, colon index one, index two, index three and then index four.
So since this is index zero we're going to have zero.
But we separate this tool with a comma.
So unlike when the 1D where we didn't separate with a comma because obviously it's one dimensional.
Now we have two dimensions.
So the that to the left of the comma is the first dimension.
That to the right of the comma is the second dimension.
Um essentially this is the rows uh to the left obviously rows and then columns.
So let's run this and then see what we get.
See we have um this tensor and the value in this tensor is nine.
So that's it.
So you see we obtain nine.
Now let's say we want to get this two right here.
So it will be the same row but different column.
So this is 0123.
So here we replace zero with three.
We run that again and we should get two.
So that's fine.
Now let's suppose we want to get um this 042.
Or let's say we want to get 170.
Let's say we want to get this 170.
Now um, unlike here where we had a single value.
So we just pick out a specific index.
Now we have a range.
So just like what we are seeing with the 1D tensor where we could pick out the range.
Now we're also going to pick out this range of values.
So we have 170 which we want to pick out.
What we'll do is we'll go again zeroth row first row second row.
So here we pick second row.
There we go.
So up because all these values fall in the second row.
We just pick the second row.
And then for the columns because the values we want to pick um fall in a range of columns that this
is zero, one, two.
This is where one starts.
So colon two is here we have two.
Remember this is after the comma to the right of the comma.
And then we go from two right up to this colon.
So we have 234.
Now again if we want to get to the fourth column then we will do four plus one just as we had seen with
a 1D.
So let's run this and then see what we get.
You see we have 170.
But if we did um two four we would have only one seven.
So let's, let's have that four plus one and that's fine.
So that's how we obtain this.
Now let's say we want to obtain just six and one.
So we want to obtain six one.
Now in this case.
In this case we would have um six one.
So this six belongs to zero row zero one belongs to row two.
So we want to go from row zero right up to row two.
But because we want to get the specific value we'll say two plus one just as we had seen already.
So two plus one.
And then we have the comma.
So this is the first.
This is for the row.
And then this is now for the columns.
So the one to the right for the columns.
Now we are having this values on the same column.
So we'll just simply say 01234.
So we'll specify four.
Run that and there we go.
You see we have 610 because we want to have only six and one.
We are going to take off this one from here.
Because going from 0 to 2 is simply simply having zero and then two minus one which is one.
So that's exactly what we want to have.
So no need.
Um, adding plus one.
That was actually an error.
We would have added plus one if we wanted to get all these elements.
That's 610.
So let's run this again and see what we obtain.
See we have six one.
Now let's say we want to have 610.
Actually if we wanted to have 610 instead of doing from 0 to 2 plus one, obviously this will give you
610.
We could take this off and just um, put the column.
So now by putting the column, what you're saying is you want to work with all the rows.
So here we work with all the rows, which is obviously the same as going from 0 to 2 plus one.
So we run this and see what we get.
Say we have 610.
We could do the same for the columns.
We could decide to.
Obviously if you say we want to work for work with all the columns and all the rows, then you would
have the original tensor.
But if we pick out a specific row, let's say the row one, um, row one, or let's say we want to go
from 0 to 1, let's run that, see what we get.
You see we have two, three, four, five, six.
We have this because obviously going from 0 to 1, it's like going from 0 to 1 minus one, which is
still uh picking only the zeroth row.
So we have two, three, four, five, six.
Um, that's because we've picked all the columns.
Now, another way of picking all the columns is by working with three dots.
So if we do three dots we should get the exact same output.
You see here we have 23456.
Now again instead of picking or going from 0 to 1 we could just say zero.
We run that again and we have two, three, four, five, six.
But there is a difference.
Let's let's put this side by side.
So let's print out tensor indexed.
And then we say 0 to 1.
And then we have all the columns.
Then yeah let's just print this out.
So we print this out.
And then we also print out the shape.
So well the shape is given.
So let's just run this.
There we go.
You notice that although we have the exact same values peaked, they the output has two different shapes.
So the output of this first one is different from the output of the second one.
And this is simply because when you pick out zero like this you are selecting a specific row.
And so now we since you've picked out a specific role you are dealing with a 1D tensor.
And that's why the output here is 1D.
But when you select a set of rows, you still having to do with a 2D tensor because you this zero one,
although it's picking only this um row.
In this case it would have picked this, um two.
So when you're dealing with a range there we have a 2D tensor because we this dimension still counts.
Whereas when you pick a specific value then you're dealing with a 1D tensor.
Now let's suppose we want to pick 4542.
So we want to pick four five and then four two.
What we'll do in this case is simple.
You could pause the video and try to, to to reflect on that.
But simply what we'll do is we will have tensor indexed.
And then because we want to have four, five, four, two which belong to column zero.
And column one will simply go from zero right up to one, or rather right up to two.
Because we are we want to have zero.
One is actually one plus one.
That's it, which is two.
So we go from zero right up to two.
And then for the columns we go from this is zero, one, two, three.
We're going from two right up to three.
So we'll have 2 to 3 but obviously three plus one which is four.
So let's run that and then see what we get.
You see we have 4542 exactly as we expect.
After exploring the 2D tensor indexing let's look at the 3D tensor indexing.
So let's suppose that let's first of all run this.
And then we suppose that we want to obtain this value nine which we have right here.
Let's rearrange this a little.
So um it's clearer.
There we go.
We want to have that value of nine um, which is found at this position.
So first of all we have tensor tensor indexed.
And then um as with the 2D where we had a single comma separating the two dimensions here we would have
two commas separating the three dimensions.
So we have the first, second and third dimension.
Now if we print out this tensor indexed let's create a new cell right here and print out tensor indexed.
You would find that let's get let's get a shape directly.
You would find that its shape is four by two by three.
So here we have four blocks of 2D tensors.
There's the first there's the second there's the third.
And this is the fourth.
So here we have this four different blocks.
Now nine belongs to the fourth block.
And so if we want to obtain nine all we need to do is let's take this one off.
All we need to do is come right here and specify that we want this, um fourth block.
So this is 0123.
So here we have three.
And then once we pick out this um block that's this fourth block which is three, because we start counting
from zero, we are then going to select a specific row.
So this is the zeroth row.
This is the first row.
Or we could say first row second row knowing that we're counting from index zero um onwards.
So this is zero.
And this is one.
Now nine year belongs to this um row zero.
So here again we will say zero.
And then for the columns this is the zeroth column or this is the first column index zero, second column
index one and third column index two.
So 012.
So nine belongs to two.
So let's um have two right there and then see what we get.
So you see here we have nine.
Which makes sense because um we've actually picked the exact position where nine is located.
Now let's say we want to have um 219.
If we want to have this 219, then it will suffice to maintain this three because 219 belongs to this,
uh, block with index three.
Um, and then we'll say we want to pick, um for the rows.
We want to pick the zeroth row, but for the columns, because we want all the columns because it goes
from zero, one, two or from 0 to 2, we are going to say we could go from 0 to 2 plus one go run that
we should have two, one, nine.
Or we could just simply say one to have all those values.
We run that and that should be fine.
See we have 219.
Now that we have this 219, we could decide to pick out, um, let's say 214 minus three.
In that case, again we're going to have all the rows because 214 minus three contains um, all the
rows um are taken into consideration when picking these values.
And then we're going to go from the zeroth column, zeroth column right up to the second to the last
column.
So we go 012 because obviously this is going to be two minus one.
So let's run that.
And then you see we have 214 minus three which makes sense.
Or we could just simply say um minus one.
So if we do that we should have 214 minus three now.
Now let's suppose that for each and every block we have right here, that's for all these four blocks,
we want to get the elements which lie on the second column.
In that case, what we'll do is we're going to take or we're going to pick out all the different blocks,
and then we'll pick out all the rows, and then we'll say, I want to get the elements in the second
column.
So let's run that.
Are we getting an error?
Yeah, we have multiple ellipses and slice spec not allowed.
So we are not allowed to have multiple ellipses as we've just done.
We just have this like this, run that and there we go.
You see that we're going to pick now all the columns um, or all the columns of index two.
So here we have C zero minus one.
Here we have zero two.
You see that here.
Here we have zero zero.
You can see that here.
Here we have 932.
You could see that right here.
You could also just have only semicolon and run that.
And you should have the exact same output.
So that's it for this section on indexing we are going to move to the next section where we will work
with math operations.