Hello, everyone, and welcome to this new session in which we'll see how to switch between the ego
mode and the graph mode in TensorFlow.
We'll first start by understanding what these two modes mean and also when to use either of them.
Then finally, we'll see how to switch between these modes by simply adding this TF function decorator
right here.
So far in this course we've been building methods like this one in the eager mode.
That is, we've been following a python istic approach in creating this method apart from this ego mode
or those eager way of manipulating data.
We also have the graph mode.
In the graph mode, we have data which is passed into this different nodes right here.
Now this nodes are operations.
And so when we pass in our data, which could be a tensor, this data gets modified and then pass to
the next node and right up to this output right here.
This means that if we consider this line of code where we have X equal Y times z, TensorFlow is capable
of converting this into a graph with a single node where this node will represent this multiplication
operation.
So here we have this multiplication operation and then we have two inputs Y and Z, then our output
X, let's draw it this way, we have y, Z on the multiplication operation.
Then we have X, Now this x, this data we get can be passed into to other operators or to other operations.
So here we could have additions and here we have subtraction and then we could combine this to finally
have say, an addition operation and then get the output.
Now let us come out with some code which will represent this rest of our graph.
Yeah, we've had X and then we could have, for example, R, which is equal X plus a certain constant.
So X plus a constant, let's call that constant key.
We have X plus a constant key, let's just say constant.
So this addition we have here is actually an addition to a fixed constant and that's it.
So here we have our and then we could define SX to be equal y minus the same fixed constant.
There we go, we have SX now we have SX, we could get an output by adding up SX and R, So let's call
this output.
T We have T which is equal.
R plus sx.
Now this addition here is different from this one as this one takes in just one input, since this input
is going to be added to the constant and the subtraction here, it takes in just one input as this input
is going to be subtracted or the constant will be subtracted from the input, which in this case is
meant to be.
X.
Actually, let's take this off.
This is X because here you have after this multiplication, you have X and this is x that gets in this
and the same exact goes this way.
So yes, meant to be X and that's it.
So yeah, we have now sx once we get sx we have SX here, this tensor sx and then here we have our ten,
so our now our plus SX gives us an output.
DT So we see that under the hood what TensorFlow is capable of doing is taking this Python code, you
write and convert it into a graph like this one.
One advantage of working with the graph mode is that this now becomes portable or this code now becomes
portable.
Since in a case where you don't have the python interpreter, you have now this data structure which
could be used in any environment, hence making our code or this method more portable.
Another advantage is that since we're now dealing with this data structure, it can be broken up into
several or separate blocks, hence making it easy for parameterization, leading to faster computations
on devices like GPUs.
The good news here is in order to convert this method, for example, into this graph, all you need
to do is to add this tf function decorator.
And when?
Once.
This is done when TensorFlow gets this block right here.
It does what we call tracing during the tracing process.
This graph right here is generated and once generated, each time you'll be calling this method.
You now have your inputs passed into this data structure and your output generated without you going
through each and every step in this method.
Getting back to the code, all we need to do right here is to include our tier function decorator and
this automatically is turned to graph mode.
That's fine.
Run this and then for this augment we just add tf function and that's it.
So there we go.
Now, right here in this 90 class, what we will do is at this level of this call, that is when we
compute in this root 90, we will have this decorator put out here.
So that's it.
That's fine.
We run this.
So augmented layers.
Okay, then this augmented layer method two, we have to function.
Now, note that previously we had seen this resized rescale layers.
So let's get back.
We have resize.
Okay.
That's the resize scale layers.
But if we were calling instead this resize rescale and that we had already put applied this decorator
right here, then it would have been needless putting this decorator here.
So in fact, what we're saying is if you have a big function and you have this function, say let's
say we have a equal a function call to to small function, there we go.
And we return some value.
We have that.
Let's say we return none.
So we have this function of this method.
We have this method defined, and then we have the small function right here.
Then we could also let's define small functions.
We have small function, and then we have return whatever value, let's say none.
So what we're trying to say is, if you have this decorator, just a function decorator, put out your
it's needless having it in this small function because once you put this, then all the methods which
will be called in this method will automatically be converted to graph mode.
So that's how this works.
Take this off.
Let's explore another interesting effect of working with the graph mode.
So.
Right.
Yeah, we have prints.
Let's say I was here.
Okay?
And then what we're doing here is we're going to call this method.
We're going to call this resize method.
The first time we have resize reskill, we pass in the original image, original image, and the label
which we got from here.
So that's it.
We run this and we see we have this.
I was here.
Now we repeat this several times.
So let's have this.
And repeating this.
We should expect to have this printed out three times since we run this actually three times, let's
run the cell and we see that this is printed out only once.
Okay?
That's what we notice.
Now, let's tick off this t f function decorator and run this again.
We see that this is printed twice.
And the reason why we have this is because in the graph mode, what we have is a data structure, a
graph data structure.
So we have this graph, we have this nodes which have been linked to one another.
And so when we write code input output, so when we write code like this and it's converted into this
graph format, the very first time we make a call on this method, we actually carrying out tracing.
Now tracing permits us to convert this into this graph format and then the next time we make this call,
since in the graph mode, we basically storing this operations and the data that's going to be passed
in between the operations, we are going to focus on only this portion of.
This method right here.
So this print is not going to be taken into consideration for the second time and for any other time.
So that said, we could run this for as many times as we wish and we wouldn't.
Okay.
Let's let's make let's take this back to the graph mode.
So this is the eagle mode.
Taking it back to the graph mode you see is going to be printed only once and then you could convert
all the methods we are using into the EGL mode by making use of this config run functions equally function.
So yeah, we have to have that config that run functions equally.
And then we set this to true.
So we're saying that we're going to run all these functions now eagerly and then let's copy this out.
So let's copy, let's say we take this three and then paste right here, you see once we run this,
now you see it's printed out twice.
This will tell you that even though we have this TF function decorator right here, all those methods
now are run equally.
We could also set this to false.
So here, let's take this.
If we have this false, we run it.
And there we go.
You notice that we have not printed out and this is simply because a tracing has already been done.
The next point we want to make this in the case where you're working with the graph mode, then there
is this specialized print you could use.
So yeah, instead of using the print some sort of this print for the eagle mode, the usual python print,
you could now use this print which is specially made for working in the graph mode.
So here we have this print here and you see that the output of this TF print is different from that
of the print because now this TF print makes this method look like a normal method as it goes through
this print each and every time.
Let's commend this and have this.
So you see how this is printed.
You see it's printed only once.
Whereas if we're having the TF print is going to be printed twice, then from here always ensure that
if you're trying to work in the graph mode, make sure all your operations are TensorFlow operations.
So if you are able to resize, for example, with OpenCV, it's actually a good thing.
But you have to make sure that that resizing all, you have to try as much as possible to have that
resizing done with TensorFlow operations.
And so instead of CV to the resize, for example, is preferable to use this image resize method.
And then in order to keep everything in this function right here based on TensorFlow, we should avoid
passing in Python variables into this method right here.
So you should make this method depend only on TensorFlow variables.
In conclusion, we've looked at the graph and eagle mode with TensorFlow how to leave from the eagle
mode to graph mode and vice versa.
Thank you for getting right up to this point and see you next time.