For the section on math operations, we shall head over to the documentation.
And then just right here to the left we shall look for TensorFlow math.
So let's scroll down and get this.
Here we go.
We have TensorFlow math.
You could check out this very first method we have here out of all these different methods.
So here are different math functions which you could make use of very easily.
And anytime you have any doubt with how a specific method is being used, you could head over to documentation,
select one like this.
Like this is absolute um function.
Select that.
And then to the right you would have that function definition.
You would have some example.
And then you would also have um a more detailed explanation of what um, the different arguments the
function takes in mean and also what the function returns.
That said, um, the absolute function takes an x.
Um, so we'll get back to the code right here.
So let's say we have um TensorFlow absolute function which takes in x.
In this case let's say x is let's define x.
So here we have x uh which is simply a constant.
And this constant is c 0.2.
Now if we have 0.2 and we pass this into the absolute value function, then what we get as output,
what we get as output is the same or the exact same 0.2.
Now let's change this to -0.2 and see what we get.
You see here we have still 0.2.
Now what this tells us is um, the absolute value, what the absolute value function does is it takes
in an input x and if so it takes in x.
If x is less than zero what happens is this x uh becomes negative x you see.
And then um if x is greater than if x is greater than zero then that x remains the same.
So you simply say remains the same.
So that's um, exactly what the absolute value function does.
Now you could, you could also take in a 1D array.
So let's say this is 1D.
We have two minus one 30.1 and zero.
Let's run that and see what we obtain.
You see we have two one.
So the negative one has tends to has turned to one.
The three is remains the same 0.1 remains the same and zero remains the same.
Now let's say we want to have your -0.1.
Run that again.
So we have the exact same number but with a with the difference that whenever we have a negative value
that negative sign is taken off.
Now getting back to the documentation, you would see that we um apart from real numbers we could also
take in complex numbers.
So you see you see very simple examples here.
Um, you could take in a complex number.
And essentially what um, this does is like, uh, when it gets a complex number, a plus BG.
The absolute value of that complex number a plus b is a squared plus b squared all of that square root.
So you see the simple example here.
Now let's let's just go ahead and and test this out.
So let's say we have um x or let's just copy this um copy that and then paste it out here.
So let's say we have x equal two x equal two plus um 3g.
There we go.
So we have x equal two plus three j.
And we want to get the absolute value of that x.
You see we have 3.6.
Now to verify that this matches with our formula.
Let's do two or let's say TensorFlow square root.
So this is another TensorFlow method TensorFlow square root of two to the power of two.
That's two square plus um two square plus three square.
That's three three.
Here we have three square or to the power of two that's fine.
So we have two square plus three square.
And then we get a square root.
So let's run that and see what we get.
We get an error.
We're told here that the value for the attribute of int32 is not in the list of allowed values.
So let's let's take this to 2.03.0.
Run that again and see what we get.
There we go.
So as you could see we have 3.6055512.
And this is that same as this.
So that's it.
You you you see that this when we get a complex number suffices to do a squared plus b squared and square
root.
And that's fine.
Now we'll move on to the next method.
We'll look at very simple methods.
So let's say we have the addition method.
So obviously you should um know how this addition method works.
Um because it's just simple addition.
So it returns x plus y and it's element wise addition.
So obviously we would expect x and y to be of the same shape.
Um so yeah let's get back let's get back.
And then let's take an example.
Let's say we have x which is equal um TensorFlow constant.
And then we say um 235 and seven.
And then we have y TensorFlow constant TensorFlow constant.
And then we have 77217721.
There we go okay.
So here we would have TensorFlow add x y.
So we should let's print that out.
There we go.
Let's print this out.
We have um TensorFlow add x y.
Now you see that we have two plus seven.
That's nine three plus seven.
That's ten five plus two.
That's seven seven plus one that's eight.
And then um as we said this two are expected to be of the same shape because obviously it's an element
wise addition.
Now what if instead of having this, we, we, we say we want to have one.
Let's say what if we do one.
We run that and see what we get.
You see, it still works, although we don't have exact same shape.
And the reason why this works is because just before adding up this two, three, five, seven with
this one, this one is stretched out to match the shape of this two, three, five, seven.
So this one now becomes one one, one one and one.
So that's it.
So if you run this again you'll see you have 3468.
You see.
Yeah.
We have three, four, six, eight.
That's it.
So we get back to one, run that again.
We still have the exact same output.
So, um, although we said they should have the same shape, um, in some cases where we could broadcast
the tensor with a smaller shape, then we could have the addition operation carried out without any
errors.
So that's it for our tensor flow math.
So this tensor flow math add um we could also do the same with subtraction.
So if you check below you should have subtraction multiplication.
Remember it's element wise multiplication and element wise subtraction.
So let's go to em.
Here we have multiply um s we have subtract.
Let's scroll down.
We should have subtract.
And then we also have divide.
So let's get back to D.
We have divide.
So that's it.
And you notice here that you have this divide and divide.
No Nan.
So let's check out what this divide Nolan does.
Let's get back to the code.
Well, let's just let's just check this out.
You see, compute is saved.
Divide which returns zero if the denominator is zero.
So let's get back here.
Let's say we have 2357 and then we have zero.
And then we decide to have divide divide instead of add.
You see here we have infinity infinity infinity infinity because we're dividing by zero.
And so what we could do is if we say divide no, no, um no Nan run that.
Well, this is let's let's get the exact name.
So here we have um divide.
No Nan.
That's TensorFlow math.
Well so far we had been using um this divide from TensorFlow itself.
But we need to specify uh we're using from TensorFlow math.
And TensorFlow math has divide no Nan.
So let's run that.
And you see we now have zeros because whenever we have an um an infinite output we turn this into zeros.
Let's, let's let's change this and say we have 024 and five.
Run that again and see what we get.
You see this is zero, 1.51.25 and 1.5 uh, 1.4.
If you take the divide off, run that again.
You see we have infinity 1.5, 1.25 and 1.4.
So that's it for this basic math operations.
Before we move on to other math methods, let's consider the following example.
So supposing we have this um tensor which we want to multiply by this other tensor.
This is obviously uh one by four tensor.
And here we have uh two by one tensor.
In that case we would still apply the broadcasting similar with what we had seen already.
So before we had two by three or rather two three.
So this was a tensor two three um four and then five.
And then when we when we multiply this by one, this one became this one year became one, one, one
and then one such that when we multiply this two, three, four, five by one, we're actually multiplying
two, three, four five by 1111.
Now that said, um, for this example here, we would still carry out this broadcasting.
So we would um, repeat this two, three, four, five twice because here we have two rows.
So here we have only one row.
We're just going to repeat this so that such that we have 234.
Five.
There we go.
So we now have two, three, four, five repeated twice.
That's row wise.
And then this one three is also going to be repeated but column wise.
So let's um let's take all this off.
Let's take this once off.
There we go.
We take this off, we take this off, and then we now repeat this one three, four times.
So we have one three, one three and then one three.
So you see now that we could multiply this two um element wise.
And after multiplying this two obviously this should give us something like um this is two.
So all of this on the top remains the same.
So we have 234, five.
And then what we have down is um two times three.
That's six.
Three times three.
That's nine.
Four times three, that's 12.
Um five times three, that's 15.
So this is now our output.
Now let's get back to the code and see if, um, this is the output we're going to have.
So we'll get back here and we have x.
Let's take that off x equal um our constant.
Well let's say we have let's get back here.
We have 2345 and then one three.
So we have 2345.
And then 132345.
And then we have y which is um another TensorFlow tensor.
And then here is instead one three.
So because we're going row wise we will have one.
So this is actually 2D.
So we have one.
And then we have three.
Whereas here this 2D is column wise.
So we have one two or we have 2345.
So that's it.
Now let's get back and do TensorFlow.
Let's print that out TensorFlow multi or math multiply.
And then we specify x and y.
So let's run that and see what we get.
Increase the cells.
There we go.
You see we have 234569 1215.
Exactly as we had gotten already from this board.
Let's continue with other methods.
We have.
For example maximum.
So let's click on maximum.
You will find that with maximum um it's simple.
We're going to compare two values.
Again element wise we compare two values.
If x is greater than y then the output is going to be x.
And if x is less than y um the output will be y.
We just comparing and seeing the the one which is maximum is the one which um, takes that position.
So let's take an example like here we have 0000.
And then negative two 025 at this zero position the maximum here is zero.
So it's normal that the output is zero.
At this um position the maximum is zero obviously is the same.
So um zero.
Yeah the maximum is two.
So we have two.
Last position we have zero and five.
The max is five.
So here we have five.
It also supports the broadcast semantics.
So saying you want to compare negative five 000 with negative three is the same as comparing negative
five 000 with negative three negative three negative three and negative three.
So obviously the maximum um the value the highest value will be negative three.
But for the rest will be zeros because zero is greater than negative three.
So that's it.
And that's it for the maximum we also have the minimum.
So minimum obviously is going to be uh just the opposite of maximum.
So here instead we want to get the value with a or the smaller value.
So when comparing um element wise we just want to get the smaller value.
That's it.
Getting back to the code.
You will try this with our previous example.
You find that we have 23453345.
Now let's get back to our board and try to justify this answer.
So after broadcasting we have this output for X and we have this output for y.
Now if it's a maximum then um at this position we have two because comparing two and one the max is
two.
Then the next is three.
Well it's going to be 2345.
Obviously because all this are greater than one which is what we have.
Um right here.
And then for the next we have two and three.
The max there is three, um, three and three we have three.
Next we have four.
And then finally we have five.
So this makes sense that this is the output we are getting.
We are now going to move to the next method which is the arg max method.
So this arg max method is similar to the max method, but with the difference that here we aren't tracking
the value which has the or the maximum value where instead tracking the position of the maximum value.
So let's take a simple example.
Um, you have this here, this a let's copy this you and paste our here so we could get the ARG Max.
There we go.
We have um TensorFlow arg max or math arg max.
We have a and then we run that you see from here that the value or the output is two.
Now the reason why we having two is because the value or the, the max value we have here is found at
position two.
Remember, let's, um, get back here.
If we want to specify each and every index, we will have zero.
We have one.
Um, let's get back here.
We have two, we have three, and then we have four.
Now the maximum value is 30.
And it turns out that is found at position two.
So that's why we have that if we just um, if we have mean then because the smallest value we have two
is two, then we should have an output of zero.
So let's take this off.
Run that again.
And you see we have an output of zero.
So essentially the arg max arg min um help you trace the position or the index uh, on which we have
the max or the minimum value.
Now, if instead we have a 2D tensor, then doing math.
Arg max and then passing in our our tensor, then specifying the axis to be zero, we would get an output
of 32030.
Now let's explain why we have this output.
Because we've set the axis to zero.
It simply means that we are going to be carrying out the comparisons across the rows.
So um, that will be across the rows.
Now if the axis is set to one, then it's going to be across the columns.
That said, for each and every row that is um, for this row, remember we have the zeroth, first,
second and third row or we have this for different rows.
We are going to compare values at each and every position.
So um, for this position right here for the zeroth position, we're going to compare this value with
this value with this value and with this value.
So this is essentially comparing two with zero with zero and with seven.
And we're comparing across um these rows uh, because we've specified axis to be zero.
And so that said the max value we have here is seven but seven that's the seven year but seven is found
at the position three.
That's why we have three.
And then um, for the next we have one one, five and three.
The max is five which is found at position two.
And then for the next we have 9710.
Max is nine at position zero.
And then we have 6329 max is nine position three.
And finally we have 5200.
Max is five at position zero.
So that's it.
That's how we obtain um 32030 as we got here.
Now you could pause at this level, change the axis to one and try to figure out what the output would
be and compare with the solution we're going to have.
So let's run this um, while okay we have 2213.
So let's figure out how we have 2213.
The axis has been sent to one.
So we carry out comparison across the different columns.
So um, we have this here, we have this and we have this and we have this.
So we instead compare here compare here, compare across the different columns.
Compare across the different columns.
So 21965.
Then the max is nine and is found at position two.
Then we have 01732.
Max is seven found at position two.
Then again we have 05120.
Max is five found at position one.
Finally we have 73090.
Max is nine found at position three.
So that's exactly how we have 2213.
Now obviously you could change this to mean and you would have something similar.
So that's how we we make use of this arg max um function.
Well this should be argmin arg min.
And then here we should have arg min.
There we go.
You have one zero for two, so that's fine.
The next method we'll look at will be this equal method.
Essentially what this operation does is it compares each and every element based on its position.
And then um returns um true or false.
So looking at this example here, you see that when we compare two four that's this 1D um tensor with
this um tensor two, we first of all start by broadcasting.
So we would have two four compared with two two.
And then since two is the same as two like um let's get back to our drawing board.
Since we're comparing two four with two two.
And since two is the same as two, we would have.
True.
But since four is different from two, we would have false.
So we would have an output, which is true.
And then uh, right here which, which is true and then false.
Now when they are exactly the same we have true true.
The next will be the power method.
Let's check our power.
Um, it should be this here.
Now feel free to check out the others we're just going through.
So you have an idea of of the important methods.
Well, this power does is it, um, computes the power of one value to another.
So it's again element wise.
So if you have this example you see here you have two.
Let's copy this.
And um let's take this from here.
There we go.
Let's get back here.
Paste out that we have x y.
And then let's reduce this so that it could uh might be much clearer.
So that's fine.
Okay.
So now we have two to the power of zero.
Let's run this.
We would have two to the power of zero which is one, two to the power of one, which is, um, two,
three to the power of two, which is nine, three to the power of three, which is 27.
So you see, that's it.
So that's essentially what this power method does.
Now the next will be the reduce methods.
So here we have reduce or reduce any write up to reduce variance.
Now let's just pick out this reduce max which is um one of the most commonly used um among this reduce
methods.
Um here what we have is it computes the maximum of elements across dimensions of a tensor.
So let's let's just copy this and then go ahead and test it out.
So here we have an example like this 15124.
So let's first of all paste out the code.
We copy it.
And then we have as input tensor input um input tensor our tensor flow constant.
And then we have 5124 okay.
So what this does essentially is it simply sums up all the or rather it simply looks for the max of
all these elements.
Now the max here is five.
So we have five.
Now if we change this to sum if we change it to sum, you will find that it's going to reduce all this
into the sum.
So five plus one is um six six plus 288 plus 412.
Let's run that.
You see we have the value of 12.
Now um let's specify like here the axis is set to none and keep dims is set to false.
We take out name.
Now let's say we set this keep dims to true.
Well let's print this out separately.
So let's print um let's print this out and then let's print.
Well let's just copy that piece out here we have keep dream or keep dims set to false.
So we run that.
There we go.
You will notice that we have two um different outputs here.
We have still this tensor with value 12.
And here we have the value 12.
But with the difference that this is a 1D array.
Whereas here we have um A0D array.
Now this the or simple um scalar.
Now the reason why we are having this for the first output is because we've decided to keep the dimensions.
So um, we are going to carry out the reduce sum that's we're going to sum up all these elements, but
we are going to maintain the original dimensions which here in this case was 1D.
So that's why we have this 1D array, uh this 1D tensor.
And then here we are not we don't necessarily or we don't keep these dimensions.
And so that's why we have this output scalar.
Now if we have um 2D.
So let's change this to 2D.
Let's just copy out an output we had already previously.
So let's take this one.
There we go.
We have that and then take this off.
So let's say input tensor.
Peace out.
Um get back take this off.
That's fine.
So let's run this again and see what we get.
Now.
You see, we we we let's first of all, um, comment this part, this comment that run that again,
we have this output of 65.
Now if you had if you had to sum up all this, let's just take out the max since it's easy to to see
the output clearly.
So you see the max here, the max value we have um, of all these different values is nine.
So that's it.
And then if we were to take the sum, let's get back to sum.
And you have to add all this up.
Three plus one plus nine plus six plus five.
All this up is going to give us 65.
Now if we set this skip James to to true.
Let's just take all this off.
If we set the kingdoms to true you'll see that because we have a 2D input, we're going to we're still
going to have a 2D output.
So the dimensions are maintained.
And let's get back to false.
And then now for the axis uh, which is set to known.
So we don't specify any axis.
But if we had to specify this axis and set it to zero for example, then similar to what we had seen
in previous examples like the, uh, um, Max, I mean, here again, we are going to simply go through
each and every row at every position and then sum up the values.
So we have three plus zero plus one plus seven.
That's um 11.
So that's that's it right here.
One plus one.
That's two 710.
So that's it.
And so on and so forth.
So that's how we have all the different values.
Now if you specify um one then we'll sum all these values um for the different columns.
So let's run that.
And you see we have three plus one.
That's four plus nine.
That's um 13 plus 11 that's 24 and so on and so forth.
Now clearly if you just add all this up, you should see that this is um 65 here we have 20 that's 18,
18 plus, um, 24 plus 13.
That gives us 65.
So here we see already that with a reduced sum we could reduce across specific axes.
Now let's set this to true and see what we get.
There we go.
You see because we want to maintain or we want to keep the dimensions.
We still have.
Uh, we, we have the, the same outputs, but now with a different shape.
Again, as we've said, we have different methods, um, or different reduce methods.
We have reduce mean, reduce um prod, reduce std, reduce variance, reduce log sum exponential, reduce
Euclidean norm, reduce any, reduce all.
So you could feel free to check this out.
The next method we'll be looking at will be the top key.
So let's click here on top K um with the top k if we have an input let's say we have um a 1D input like
here.
You see we have this 1D input.
Let's just copy this.
If we have this 1D input you would find that it's going to produce the top k values just as the the
the the method name goes.
So let's print out from here.
Let's print out results.
Um the top k already.
Clearly you see them.
You have 9098, 99.
All right.
At the top three.
The top three.
Uh we have 98, 99, 96.
So that makes sense.
Now if you scroll here you will find that apart from the the top values we also have the positions.
So 99 is found under the feet um index.
So we have 012345.
So that's why we have five.
So we're not or we don't only output the top three values, we also output the top um the positions
of the top three values.
If we change this to top five you see we would have more values now.
Yeah.
Here we have 99989643.
If we change it to top one that essentially just the the max value we will have 99.
And obviously we will have this position.
So that's um, essentially what um, or how we could make use of the top k um method.
The last method we'll be looking at in this section will be the sigmoid, um, function.
So let's check out sigmoid.
Um there we go.
We have sigmoid.
So essentially this is um a very common method.
And wish we could make use of or wish you could call very easily.
You could actually write this um, by yourself.
But, um, TensorFlow already has this, so you could uh you don't need to always write that.
So essentially what the sigmoid does is it takes in an input and then um, that input is uh negated.
So we have minus x.
We take the exponential of that value, we add plus one.
And then we take one divided by whatever output we have.
So um the sigmoid takes values from negative infinity to positive infinity and then keeps them or maintains
them in the range zero one.
So let's get back here and just take this off.
So let's pull this up.
This way.
Get back.
Then plot out what the sigmoid function does.
Exactly.
So with a sigmoid function, as we're saying, we have this function which takes values of x between
negative infinity and positive infinity, and then maintains them um, between the range zero and one.
So it will never cross one as well as it will never cross, um, it will never go below zero.
So x goes from negative infinity to positive infinity.
But the output or the result always falls in the range zero negative one.
And we have one divided by one plus e to the negative x.
Now again we could um call on this method with a 1D array.
So like this for example you find that when you take a zero obviously the output should be 0.5, when
if you look carefully at the plot you find that zero.
Our output here is 0.5.
So this makes sense that, um, at the level of the plot, or rather at the, at the level of this,
um, code, we have output 0.5.
When we take one, we have 0.73 50 is one, 100 is one.
Now you notice that as the numbers become very large, we um, converge towards one.
So that's it.
Um, and obviously as we as the numbers become very small, we're going to also converge towards, um,
zero.
So if we let's just copy this out here.
Um, I'll get back to the code.
Scroll up.
There we go.
Let's run that.
You see we have 0.50.7311.
Now let's take very small numbers.
So obviously very small negative numbers.
So we have -50 run that you see we have these numbers now converging towards zero.
So we have very small numbers.
Now if we take negative um five let's increase that or five e let's say e um 100.
Run that.
Um, out of range for double precision float.
Let's make that smaller.
So we just take one zero off, run that again, and you could see that we have now zero.
So basically as we go towards very small or very large negative numbers, we converge to a zero.
And when we go towards very large positive numbers we converge towards one.
Exactly as we've seen in the plot.
You see we're getting towards zero or we are approaching zero.
And for this we are approaching one which is right here.