1
00:00:00,360 --> 00:00:05,820
Hello, everyone, and welcome to this new session in which we'll see how to switch between the ego

2
00:00:05,820 --> 00:00:08,610
mode and the graph mode in TensorFlow.

3
00:00:08,670 --> 00:00:15,660
We'll first start by understanding what these two modes mean and also when to use either of them.

4
00:00:15,690 --> 00:00:23,820
Then finally, we'll see how to switch between these modes by simply adding this TF function decorator

5
00:00:23,820 --> 00:00:24,560
right here.

6
00:00:24,570 --> 00:00:30,840
So far in this course we've been building methods like this one in the eager mode.

7
00:00:30,840 --> 00:00:39,900
That is, we've been following a python istic approach in creating this method apart from this ego mode

8
00:00:39,900 --> 00:00:43,800
or those eager way of manipulating data.

9
00:00:43,800 --> 00:00:45,960
We also have the graph mode.

10
00:00:46,380 --> 00:00:53,010
In the graph mode, we have data which is passed into this different nodes right here.

11
00:00:53,250 --> 00:00:56,100
Now this nodes are operations.

12
00:00:56,100 --> 00:01:03,240
And so when we pass in our data, which could be a tensor, this data gets modified and then pass to

13
00:01:03,240 --> 00:01:08,000
the next node and right up to this output right here.

14
00:01:08,010 --> 00:01:16,080
This means that if we consider this line of code where we have X equal Y times z, TensorFlow is capable

15
00:01:16,080 --> 00:01:24,330
of converting this into a graph with a single node where this node will represent this multiplication

16
00:01:24,330 --> 00:01:24,960
operation.

17
00:01:24,960 --> 00:01:37,080
So here we have this multiplication operation and then we have two inputs Y and Z, then our output

18
00:01:37,080 --> 00:01:43,800
X, let's draw it this way, we have y, Z on the multiplication operation.

19
00:01:43,800 --> 00:01:52,500
Then we have X, Now this x, this data we get can be passed into to other operators or to other operations.

20
00:01:52,500 --> 00:02:00,360
So here we could have additions and here we have subtraction and then we could combine this to finally

21
00:02:00,360 --> 00:02:04,860
have say, an addition operation and then get the output.

22
00:02:05,010 --> 00:02:11,880
Now let us come out with some code which will represent this rest of our graph.

23
00:02:11,910 --> 00:02:23,580
Yeah, we've had X and then we could have, for example, R, which is equal X plus a certain constant.

24
00:02:23,940 --> 00:02:27,690
So X plus a constant, let's call that constant key.

25
00:02:28,140 --> 00:02:31,440
We have X plus a constant key, let's just say constant.

26
00:02:32,190 --> 00:02:37,650
So this addition we have here is actually an addition to a fixed constant and that's it.

27
00:02:37,650 --> 00:02:46,650
So here we have our and then we could define SX to be equal y minus the same fixed constant.

28
00:02:47,280 --> 00:02:55,800
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

29
00:02:55,800 --> 00:02:56,610
this output.

30
00:02:56,640 --> 00:02:59,670
T We have T which is equal.

31
00:02:59,940 --> 00:03:03,360
R plus sx.

32
00:03:03,750 --> 00:03:09,900
Now this addition here is different from this one as this one takes in just one input, since this input

33
00:03:09,900 --> 00:03:14,910
is going to be added to the constant and the subtraction here, it takes in just one input as this input

34
00:03:14,910 --> 00:03:21,600
is going to be subtracted or the constant will be subtracted from the input, which in this case is

35
00:03:21,600 --> 00:03:22,470
meant to be.

36
00:03:22,470 --> 00:03:22,830
X.

37
00:03:22,830 --> 00:03:24,810
Actually, let's take this off.

38
00:03:25,170 --> 00:03:31,740
This is X because here you have after this multiplication, you have X and this is x that gets in this

39
00:03:31,740 --> 00:03:33,780
and the same exact goes this way.

40
00:03:33,780 --> 00:03:37,380
So yes, meant to be X and that's it.

41
00:03:37,380 --> 00:03:46,140
So yeah, we have now sx once we get sx we have SX here, this tensor sx and then here we have our ten,

42
00:03:46,140 --> 00:03:49,050
so our now our plus SX gives us an output.

43
00:03:49,080 --> 00:03:57,120
DT So we see that under the hood what TensorFlow is capable of doing is taking this Python code, you

44
00:03:57,120 --> 00:04:00,510
write and convert it into a graph like this one.

45
00:04:00,510 --> 00:04:09,630
One advantage of working with the graph mode is that this now becomes portable or this code now becomes

46
00:04:09,630 --> 00:04:10,410
portable.

47
00:04:10,410 --> 00:04:19,290
Since in a case where you don't have the python interpreter, you have now this data structure which

48
00:04:19,290 --> 00:04:27,210
could be used in any environment, hence making our code or this method more portable.

49
00:04:27,210 --> 00:04:34,200
Another advantage is that since we're now dealing with this data structure, it can be broken up into

50
00:04:34,200 --> 00:04:42,360
several or separate blocks, hence making it easy for parameterization, leading to faster computations

51
00:04:42,360 --> 00:04:44,480
on devices like GPUs.

52
00:04:44,490 --> 00:04:53,340
The good news here is in order to convert this method, for example, into this graph, all you need

53
00:04:53,340 --> 00:04:58,320
to do is to add this tf function decorator.

54
00:04:58,740 --> 00:04:59,520
And when?

55
00:04:59,520 --> 00:04:59,940
Once.

56
00:05:00,040 --> 00:05:05,330
This is done when TensorFlow gets this block right here.

57
00:05:05,350 --> 00:05:09,620
It does what we call tracing during the tracing process.

58
00:05:09,640 --> 00:05:16,330
This graph right here is generated and once generated, each time you'll be calling this method.

59
00:05:16,360 --> 00:05:25,780
You now have your inputs passed into this data structure and your output generated without you going

60
00:05:25,780 --> 00:05:28,300
through each and every step in this method.

61
00:05:28,330 --> 00:05:34,840
Getting back to the code, all we need to do right here is to include our tier function decorator and

62
00:05:34,840 --> 00:05:37,630
this automatically is turned to graph mode.

63
00:05:38,020 --> 00:05:38,920
That's fine.

64
00:05:39,520 --> 00:05:47,050
Run this and then for this augment we just add tf function and that's it.

65
00:05:47,200 --> 00:05:48,580
So there we go.

66
00:05:48,610 --> 00:05:56,770
Now, right here in this 90 class, what we will do is at this level of this call, that is when we

67
00:05:57,730 --> 00:06:03,560
compute in this root 90, we will have this decorator put out here.

68
00:06:03,580 --> 00:06:04,450
So that's it.

69
00:06:05,680 --> 00:06:06,310
That's fine.

70
00:06:06,310 --> 00:06:06,880
We run this.

71
00:06:06,880 --> 00:06:09,160
So augmented layers.

72
00:06:09,190 --> 00:06:14,410
Okay, then this augmented layer method two, we have to function.

73
00:06:14,680 --> 00:06:19,390
Now, note that previously we had seen this resized rescale layers.

74
00:06:19,390 --> 00:06:21,280
So let's get back.

75
00:06:21,280 --> 00:06:22,650
We have resize.

76
00:06:22,660 --> 00:06:22,960
Okay.

77
00:06:22,990 --> 00:06:24,490
That's the resize scale layers.

78
00:06:24,490 --> 00:06:33,280
But if we were calling instead this resize rescale and that we had already put applied this decorator

79
00:06:33,280 --> 00:06:37,920
right here, then it would have been needless putting this decorator here.

80
00:06:37,930 --> 00:06:44,950
So in fact, what we're saying is if you have a big function and you have this function, say let's

81
00:06:44,950 --> 00:06:51,910
say we have a equal a function call to to small function, there we go.

82
00:06:51,910 --> 00:06:54,130
And we return some value.

83
00:06:54,940 --> 00:06:55,930
We have that.

84
00:06:55,930 --> 00:06:57,280
Let's say we return none.

85
00:06:57,730 --> 00:07:02,440
So we have this function of this method.

86
00:07:02,440 --> 00:07:07,030
We have this method defined, and then we have the small function right here.

87
00:07:07,150 --> 00:07:10,570
Then we could also let's define small functions.

88
00:07:10,570 --> 00:07:16,980
We have small function, and then we have return whatever value, let's say none.

89
00:07:16,990 --> 00:07:24,790
So what we're trying to say is, if you have this decorator, just a function decorator, put out your

90
00:07:24,820 --> 00:07:34,450
it's needless having it in this small function because once you put this, then all the methods which

91
00:07:34,450 --> 00:07:39,580
will be called in this method will automatically be converted to graph mode.

92
00:07:39,580 --> 00:07:41,980
So that's how this works.

93
00:07:42,160 --> 00:07:43,240
Take this off.

94
00:07:43,420 --> 00:07:47,710
Let's explore another interesting effect of working with the graph mode.

95
00:07:47,710 --> 00:07:48,280
So.

96
00:07:48,280 --> 00:07:48,540
Right.

97
00:07:48,550 --> 00:07:49,930
Yeah, we have prints.

98
00:07:50,260 --> 00:07:51,790
Let's say I was here.

99
00:07:52,390 --> 00:07:52,940
Okay?

100
00:07:53,050 --> 00:07:57,100
And then what we're doing here is we're going to call this method.

101
00:07:57,100 --> 00:07:59,140
We're going to call this resize method.

102
00:07:59,920 --> 00:08:08,770
The first time we have resize reskill, we pass in the original image, original image, and the label

103
00:08:08,770 --> 00:08:10,150
which we got from here.

104
00:08:11,160 --> 00:08:11,940
So that's it.

105
00:08:11,940 --> 00:08:15,450
We run this and we see we have this.

106
00:08:15,450 --> 00:08:16,510
I was here.

107
00:08:16,530 --> 00:08:19,740
Now we repeat this several times.

108
00:08:19,740 --> 00:08:21,030
So let's have this.

109
00:08:21,210 --> 00:08:22,350
And repeating this.

110
00:08:22,380 --> 00:08:30,540
We should expect to have this printed out three times since we run this actually three times, let's

111
00:08:30,540 --> 00:08:34,710
run the cell and we see that this is printed out only once.

112
00:08:35,280 --> 00:08:35,720
Okay?

113
00:08:35,790 --> 00:08:36,830
That's what we notice.

114
00:08:36,840 --> 00:08:41,850
Now, let's tick off this t f function decorator and run this again.

115
00:08:42,510 --> 00:08:44,550
We see that this is printed twice.

116
00:08:44,550 --> 00:08:51,630
And the reason why we have this is because in the graph mode, what we have is a data structure, a

117
00:08:51,630 --> 00:08:53,010
graph data structure.

118
00:08:53,010 --> 00:09:00,450
So we have this graph, we have this nodes which have been linked to one another.

119
00:09:00,450 --> 00:09:07,680
And so when we write code input output, so when we write code like this and it's converted into this

120
00:09:07,680 --> 00:09:16,310
graph format, the very first time we make a call on this method, we actually carrying out tracing.

121
00:09:16,320 --> 00:09:24,120
Now tracing permits us to convert this into this graph format and then the next time we make this call,

122
00:09:24,540 --> 00:09:30,930
since in the graph mode, we basically storing this operations and the data that's going to be passed

123
00:09:30,930 --> 00:09:38,010
in between the operations, we are going to focus on only this portion of.

124
00:09:39,160 --> 00:09:40,650
This method right here.

125
00:09:40,660 --> 00:09:47,200
So this print is not going to be taken into consideration for the second time and for any other time.

126
00:09:47,770 --> 00:09:54,790
So that said, we could run this for as many times as we wish and we wouldn't.

127
00:09:54,850 --> 00:09:55,120
Okay.

128
00:09:55,120 --> 00:09:57,980
Let's let's make let's take this back to the graph mode.

129
00:09:58,000 --> 00:10:00,400
So this is the eagle mode.

130
00:10:00,400 --> 00:10:05,230
Taking it back to the graph mode you see is going to be printed only once and then you could convert

131
00:10:05,230 --> 00:10:14,620
all the methods we are using into the EGL mode by making use of this config run functions equally function.

132
00:10:14,980 --> 00:10:20,410
So yeah, we have to have that config that run functions equally.

133
00:10:20,410 --> 00:10:22,150
And then we set this to true.

134
00:10:22,150 --> 00:10:26,950
So we're saying that we're going to run all these functions now eagerly and then let's copy this out.

135
00:10:26,950 --> 00:10:32,110
So let's copy, let's say we take this three and then paste right here, you see once we run this,

136
00:10:32,110 --> 00:10:34,330
now you see it's printed out twice.

137
00:10:34,330 --> 00:10:41,800
This will tell you that even though we have this TF function decorator right here, all those methods

138
00:10:41,800 --> 00:10:43,690
now are run equally.

139
00:10:43,870 --> 00:10:45,490
We could also set this to false.

140
00:10:45,490 --> 00:10:47,440
So here, let's take this.

141
00:10:47,440 --> 00:10:50,260
If we have this false, we run it.

142
00:10:50,260 --> 00:10:52,330
And there we go.

143
00:10:52,390 --> 00:10:57,190
You notice that we have not printed out and this is simply because a tracing has already been done.

144
00:10:57,220 --> 00:11:02,950
The next point we want to make this in the case where you're working with the graph mode, then there

145
00:11:02,950 --> 00:11:05,080
is this specialized print you could use.

146
00:11:05,080 --> 00:11:12,550
So yeah, instead of using the print some sort of this print for the eagle mode, the usual python print,

147
00:11:12,550 --> 00:11:18,010
you could now use this print which is specially made for working in the graph mode.

148
00:11:18,010 --> 00:11:24,880
So here we have this print here and you see that the output of this TF print is different from that

149
00:11:24,880 --> 00:11:33,460
of the print because now this TF print makes this method look like a normal method as it goes through

150
00:11:33,460 --> 00:11:36,040
this print each and every time.

151
00:11:36,040 --> 00:11:38,590
Let's commend this and have this.

152
00:11:38,590 --> 00:11:39,880
So you see how this is printed.

153
00:11:39,880 --> 00:11:41,200
You see it's printed only once.

154
00:11:41,200 --> 00:11:46,960
Whereas if we're having the TF print is going to be printed twice, then from here always ensure that

155
00:11:46,960 --> 00:11:53,830
if you're trying to work in the graph mode, make sure all your operations are TensorFlow operations.

156
00:11:53,830 --> 00:12:00,820
So if you are able to resize, for example, with OpenCV, it's actually a good thing.

157
00:12:00,820 --> 00:12:08,080
But you have to make sure that that resizing all, you have to try as much as possible to have that

158
00:12:08,080 --> 00:12:11,500
resizing done with TensorFlow operations.

159
00:12:11,500 --> 00:12:19,090
And so instead of CV to the resize, for example, is preferable to use this image resize method.

160
00:12:19,090 --> 00:12:26,920
And then in order to keep everything in this function right here based on TensorFlow, we should avoid

161
00:12:26,920 --> 00:12:31,720
passing in Python variables into this method right here.

162
00:12:31,720 --> 00:12:37,090
So you should make this method depend only on TensorFlow variables.

163
00:12:37,120 --> 00:12:43,750
In conclusion, we've looked at the graph and eagle mode with TensorFlow how to leave from the eagle

164
00:12:43,750 --> 00:12:46,540
mode to graph mode and vice versa.

165
00:12:46,570 --> 00:12:49,840
Thank you for getting right up to this point and see you next time.
