In this new section, we shall be working with ragged tensors, and so we shall understand the reason
why we need rag tensors, and also how to create them very easily.
So let's get back.
Let's suppose we have an a um tensor flow constant.
And then here we have another, another um array.
So we have 123.
Well let's just end like that.
And then here we have 456.
Let's close this.
We have 456.
And then we have 789 okay.
So here's our input.
We could print out.
We could print out a and then print out.
Well let's just print out a.
Now once we print out a here's the output we get.
Uh we could suppose that we modify this.
So let's let's change that and change this okay.
So now uh our row has two columns.
And the second the second row has three columns, while the last row has only a single column.
Let's run this again.
You see that obviously we get an error.
Can convert a non rectangular Python sequence to a tensor.
So, um, the number of columns has to be constant across the rows.
And number of rows have to be constant across the different columns.
So such that we have a rectangular array or rectangular tensor.
Nonetheless in some cases you would need this kind of data.
That is, you would need data where some rows will contain shorter columns as compared to the others.
And so the way we deal with this is we convert this to, um, racked tensors, or we just simply create
a racked tensor.
So here we have racked instead of the TensorFlow constant.
Now we have TensorFlow racked constant.
So we run that again and you will see that we have a racked tensor.
Now this racked tensors have different functions.
Like we've just looked at the constant which constructs a constant rank tensor from a nested Python
list.
We could also have the boolean mask.
So let's just check out the boolean mask.
And you would uh feel free to check out the others later on.
So for the boolean mask let's take let's take out this example.
From here we have the data and then we have the mask.
Um there we go.
Let's copy this.
Uh, let's copy this other one because we here we have um this non rectangular list.
So let's copy that.
And then we could simply paste this out here okay.
So now here we have this um racked tensor right here.
Or we have this two racked tensors.
Let's see.
This is the this is the input rank tensor.
But this is the mask.
So what this mask does is simple.
We are going to um keep all the values.
So let's run this.
So you see the output.
Um f is not defined.
Well f is um false and t is true.
So let's get back and then let's say here we have false false true false true and then true okay.
Run that again.
There we go.
So you see that each and for every position where we have false is taken off.
So here we have one.
It's no more.
It's no longer there.
Two is no longer there because we have false to three.
Um remains because we have true.
So you see we are left with this three and then for the next this for false.
So it's just taken off.
We have this empty list and then we have the next true true.
So five six are maintained.
So that's it for this um boolean mask method which we have seen from the documentation.
You could check out other methods like this cross Crosshatched range and so on and so forth.
So we could check out those different methods.
Um, the simple way we could check this out is by uh going through this, different examples.
So right here we have our values.
You see 31415926.
So when you have this um rank tensors from row splits method and you specify the row splits.
The way this is done is simple.
We start from zero and then we go to four.
So you see when we have row splits start from zero we go to four.
So we have 0123.
See we have 3141.
See that.
And then let's, let's copy this and um run the code here where we specify the row splits and we go
from 0 to 4.
This simply means just like with a range we're going to have um zero.
So we will take zero.
Then we will take one, then we will take two.
They will take three.
So we go from 0 to 4 minus one.
And that's why we have three one, four one which we have here.
And then for the next we're going to have from 4 to 4 minus one is uh, basically telling us that we
are not going to have any elements at this position.
And that's why, you see, we have an empty list.
Then we go from 4 to 7, 4 to 7.
We would have four, five, four, five and then six because we're going from 4 to 7 minus one.
So here we have 592592.
Then we have 7 to 8.
So we go from seven to to eight minus one.
So we we are at seven and that's six.
That's a year.
Then finally we have um 8 to 8.
So 8 to 8 minus one is um empty.
So that's why we have this empty list.
So that's it.
That's how we read this row splits.
Now for the row lengths.
We'll take this off.
For the row lengths.
We simply, um, look at this four and say, okay, we're going um, one, two, three, four.
That's it.
The length.
And then the next is zero.
So we have an empty list.
The next is three.
So we take the next three.
That's it.
The next is one.
We take this one and finally we have zero empty list.
So we see that where the row splits or row length, we're able to generate the same output like well
let's just let this be.
That's fine.
Then we go to value row IDs.
So for value row ids we're going to say this one.
This three we have right here is going to be part of the zeroth um row.
So that's that's that's where this row ID comes from.
This this three is going to be part from part of the zeroth row.
So that's why we have three.
One is going to be part of the zeroth row.
That way we have one.
And uh for the in the zeroth row um, four is going to be part and one is going to be part.
So that's how we have this 314.
And then if you notice we skip one.
What this means is we have no element belonging to the um row one.
And that's why we have an empty list.
And then we now look at two this next three elements.
This next three elements belong to row two.
And then we have three meaning that this next element belongs to row three.
And then we have no four.
So um, there is no elements in the row four.
Now specify the number of rows here is important because if we just if we just, um, take this off
or let's just say if we have considering that we have four rows.
Then you see that there.
We do not have this this last, uh, empty list.
But when you say we have five rows and that you don't see the four and then you don't see this four,
it means that that's an empty list.
So that's it for the from row stats.
It shows us where the row starts from.
So this this row this row starts from position zero.
And then the next row starts from position four.
So this is your start from position for the other row starts again from position four.
So because these two start from the same position it means that the there is a row which is empty.
And that's that's how we have this four this 592.
Because the the next starts from position seven.
So the six and then the other one starts from position eight.
So here we have 01234567.
But there's no position eight.
So we have an empty list.
Now we also have this row limits here.
Instead we're looking at where the row ends.
So the the first row is going to end at position four.
It's going to end here 0123.
Well when we say position four um it's actually four minus one.
So it's 0123 ends right here.
And then the next one is going to end at that same position.
Four.
And because it ends at that position four um we are going to make it an empty list.
So that's why we have the empty list since it ends at the same position where the previous one ended.
So, um, that's why we have the empty list now for the next one we have seven.
So 01234567.
Obviously seven minus one.
Remember when we when we we generally have start start to end minus one actually.
So when you say start end it's start it's actually start end minus one.
That's why when you delete with this limits that's with the ends we take instead of minus one.
So we get right up to this um position because this is 0123456 right up to position six.
We stop there and we have this 592.
Then we have eight.
So we go zero one um 2345678 minus one.
Obviously we have six.
And then the, the finally we have the empty list because um, this tool uh match.
So that's it.
The next we have this uniform row length.
So we want if we want to have a uniform row length then it simply means that all this will have the
exact same length.
So we have 314159 and two six.
Well this is 2468.
So let's say we had seven here.
You see the well let's take the start end from here.
Run that again.
We have an error because the row splits here doesn't make sense.
Without taking into consideration the fact that we have added an extra element.
Run that again.
We for that.
That was fine.
But we we still, um, have some errors here anyway.
We don't want to, um, check that out for now.
We just wanted to test out this last one.
So let's comment all this.
Let's just cut that out.
Run that.
And we're getting an error.
All the uniform rolling has to be specified.
So if you specify that you see now that we have the output as expected.
So let's get back and take this off.
Run that again.
And that should be fine.
So that's it for this section on Rack Tensors.
See you in the next section where we will deal with the string tensors and variables.