1
00:00:00,110 --> 00:00:07,460
In this section, we shall be focusing on common TensorFlow methods like the Expanddims method, like

2
00:00:07,460 --> 00:00:13,730
the squeeze, reshape, concatenate, stack, gather and gather, and the methods.

3
00:00:13,730 --> 00:00:16,850
So we will start with the expanding method.

4
00:00:16,850 --> 00:00:19,100
So here we have Expanddims.

5
00:00:20,030 --> 00:00:22,940
Let's first of all suppose that we have some input.

6
00:00:22,940 --> 00:00:32,300
So let's say we have a we have a um which is TensorFlow 1D uh or this 1D tensor.

7
00:00:32,450 --> 00:00:37,850
Let's say we have 12345676 and seven okay.

8
00:00:37,850 --> 00:00:46,550
So let's suppose that we have this tensor A and we have a method which takes in only 2D um values.

9
00:00:46,550 --> 00:00:51,800
So what will what we could do here is we could add an extra dimension.

10
00:00:51,800 --> 00:00:54,740
That's, uh, the role of our Expanddims method.

11
00:00:54,740 --> 00:01:01,610
So here we have expanddims returns a tensor with a length one axis inserted at index axis.

12
00:01:01,610 --> 00:01:04,550
So you see it takes an input and a specific axis.

13
00:01:04,550 --> 00:01:12,020
So essentially here um, because we do not want to change, um, the values we have in our inputs,

14
00:01:12,020 --> 00:01:18,080
like in this case a we are just going to add an extra axis of length one.

15
00:01:18,080 --> 00:01:23,180
So if we want to have uh, let's just do TensorFlow expand dims.

16
00:01:23,480 --> 00:01:27,830
Um, we have a and then let's say we add this the zeroth axis.

17
00:01:28,250 --> 00:01:29,210
So we have that.

18
00:01:29,210 --> 00:01:34,220
Well for now let's print out let's print out the shape the shape of a.

19
00:01:34,220 --> 00:01:36,110
So print out a shape.

20
00:01:36,110 --> 00:01:42,980
And then we have um this TensorFlow expand dims a axis specified to be zero.

21
00:01:43,220 --> 00:01:44,360
There we go.

22
00:01:44,360 --> 00:01:52,520
And then we run that you would find that we have this output which is 1234567 similar or the same as

23
00:01:52,520 --> 00:01:53,450
what we have here.

24
00:01:53,450 --> 00:01:58,460
But with the difference now that we have this extra dimension, see this?

25
00:01:59,220 --> 00:02:00,420
Of size one.

26
00:02:00,420 --> 00:02:07,950
And so now, um, we've, we'll be able to pass this into that method, which needs a 2D input.

27
00:02:08,100 --> 00:02:14,190
Another example or another modification could be that we may want to add this axis, um, into that

28
00:02:14,190 --> 00:02:15,090
position one.

29
00:02:15,090 --> 00:02:20,910
So if we have an input like this and we want to add this at position one, we let's let's have this

30
00:02:20,910 --> 00:02:21,270
zero.

31
00:02:21,270 --> 00:02:23,370
Let's just copy this and and place out here.

32
00:02:23,370 --> 00:02:26,190
So now we have position one axis one.

33
00:02:26,190 --> 00:02:29,610
You see now that we have this still a 2D um tensor.

34
00:02:29,610 --> 00:02:33,930
But now that extra axis was added at this position one.

35
00:02:33,930 --> 00:02:38,760
So now we have a six by one tensor instead of a one by six tensor.

36
00:02:38,760 --> 00:02:43,650
Again it's still the same values one, two three, 456 up to seven.

37
00:02:44,040 --> 00:02:50,880
Um, obviously if we have something 2D, that's we have a 2D tensor or even a 3D tensor, you could

38
00:02:50,880 --> 00:02:52,830
convert this into a 4D tensor.

39
00:02:52,830 --> 00:03:01,920
So let's let's just paste this out here we have a and then we let's run that tensor flow expand um expand

40
00:03:01,920 --> 00:03:02,940
dimensions.

41
00:03:02,940 --> 00:03:08,130
Well let's print out the shape of a um print out the shape of a.

42
00:03:08,160 --> 00:03:12,210
We have TensorFlow, expand the dimensions and then we specify a.

43
00:03:12,210 --> 00:03:14,640
And then we give the axis.

44
00:03:14,640 --> 00:03:16,710
So let's suppose that we do not put any axis.

45
00:03:16,710 --> 00:03:18,420
Let's just run that and see what we get.

46
00:03:18,720 --> 00:03:21,990
See missing required position or positional argument.

47
00:03:21,990 --> 00:03:24,720
So let's have axis equals zero.

48
00:03:24,750 --> 00:03:25,560
We run that.

49
00:03:25,560 --> 00:03:30,210
And you see now we have a one by two by three by four tensor.

50
00:03:30,210 --> 00:03:34,200
So this is a 4D tensor we have now from this 3D tensor.

51
00:03:34,200 --> 00:03:36,000
So uh that's it.

52
00:03:36,000 --> 00:03:39,570
If we say say we want to have one you see.

53
00:03:39,570 --> 00:03:41,280
Notice how it's one by two by three.

54
00:03:41,280 --> 00:03:45,270
If we say one one half one, it's now two by one by three by four.

55
00:03:45,750 --> 00:03:48,540
Um, it's still the same tensor, still same values.

56
00:03:48,540 --> 00:03:50,940
You see two, six, five, two and so on and so forth.

57
00:03:50,940 --> 00:03:52,230
So it's still the same values.

58
00:03:52,230 --> 00:03:56,430
But now, um, it's two by one by three by four.

59
00:03:56,430 --> 00:04:03,480
So we've added this extra, um, this option right here, here in the documentation we have some uses

60
00:04:03,480 --> 00:04:04,320
of this operation.

61
00:04:04,320 --> 00:04:09,090
So you see when we want to add an outer batch dimension to a single element.

62
00:04:09,090 --> 00:04:14,010
So let's suppose that you've trained a model and then you have some inputs.

63
00:04:14,010 --> 00:04:18,570
So let's say the input was this year um or let's let's take this input.

64
00:04:18,570 --> 00:04:21,810
So let's suppose that we have this input which is two by three by four.

65
00:04:21,810 --> 00:04:24,900
Now uh, the models are generally trained in batches.

66
00:04:24,900 --> 00:04:28,140
And so it wouldn't take this two by three by four.

67
00:04:28,170 --> 00:04:33,510
It would want to take, um, an input of shape batch by two by three by four.

68
00:04:33,510 --> 00:04:38,640
But even though we have this input which is two by three by four, we could convert it to that form

69
00:04:38,640 --> 00:04:43,110
batch by two by three by four, by simply adding that dimension.

70
00:04:43,500 --> 00:04:46,440
Uh, and that's done by this, um, Expanddims.

71
00:04:46,440 --> 00:04:52,050
Another case where we could make use of the Expanddims method is to align axis for broadcasting.

72
00:04:52,050 --> 00:04:58,350
So we could, um, add that extra dimension and then carry out broadcasting.

73
00:04:58,470 --> 00:05:03,660
Then we have to add an inner vector length axis to a tensor of scalars.

74
00:05:03,720 --> 00:05:09,120
It should be noted that if we have or if we set the axis to negative one, then that's simply the last

75
00:05:09,120 --> 00:05:09,780
axis.

76
00:05:09,780 --> 00:05:16,290
So um, here, uh, if we say negative one, you will find that this will fall on the at the last position.

77
00:05:16,290 --> 00:05:20,130
So here we have two by three by four by one.

78
00:05:20,130 --> 00:05:22,440
This operation is related to TensorFlow.

79
00:05:22,440 --> 00:05:27,690
Squeeze um reshape and sparse expand dimensions methods.

80
00:05:27,690 --> 00:05:31,200
Well let's look at this um TensorFlow squeeze here.

81
00:05:31,200 --> 00:05:32,400
Instead we have that.

82
00:05:32,400 --> 00:05:35,760
It removes dimensions of size one from the shape of a tensor.

83
00:05:35,760 --> 00:05:39,960
So it's kind of like the opposite of what we did with the expand dimensions.

84
00:05:39,960 --> 00:05:44,940
So here um let's suppose that we have this um output.

85
00:05:44,940 --> 00:05:47,580
So let's say we have C which is this.

86
00:05:47,730 --> 00:05:49,290
Let's print out the shape of C.

87
00:05:50,400 --> 00:05:55,650
Now if we do TensorFlow squeeze and then we have C.

88
00:05:55,650 --> 00:06:00,150
And then we'll specify that the axis we want to take off is the last axis.

89
00:06:00,150 --> 00:06:02,880
Then you um get a new output.

90
00:06:02,880 --> 00:06:08,550
So here we have let's get back here we have the C which has a shape.

91
00:06:08,550 --> 00:06:16,080
But after squeezing out that um dimension we now have two by three by four, which is what we had originally.

92
00:06:16,080 --> 00:06:22,470
So that's how we could, um, play around with these two methods, depending on your specific use case.

93
00:06:22,470 --> 00:06:31,020
Moving on to the reshape method, we have this, um, tool which permits us to reshape our input tensors

94
00:06:31,020 --> 00:06:35,190
without changing the order in which they were, um, initially.

95
00:06:35,190 --> 00:06:37,200
So let's get back here.

96
00:06:37,500 --> 00:06:49,680
Um, suppose we have a, uh, a constant, and here we say one, two, three, four, um, five, five,

97
00:06:49,680 --> 00:06:52,350
six, seven, eight and nine.

98
00:06:52,560 --> 00:06:54,900
So let's say we want to reshape this.

99
00:06:55,050 --> 00:06:58,380
Um, we could do TensorFlow reshape and then we'll.

100
00:06:58,900 --> 00:07:00,910
A and then we give it a new shape.

101
00:07:00,910 --> 00:07:07,150
So for now we have this um 1D tensor which has nine elements.

102
00:07:07,150 --> 00:07:12,460
And so we could convert this into a 2D tensor where we would have three by three.

103
00:07:12,460 --> 00:07:15,580
So let's um run this and see what we get.

104
00:07:16,690 --> 00:07:17,740
Let's run that.

105
00:07:17,740 --> 00:07:19,660
Let's print this out.

106
00:07:20,320 --> 00:07:21,580
There we go.

107
00:07:21,580 --> 00:07:26,110
We you see we have 123456789.

108
00:07:26,110 --> 00:07:29,590
Notice how the ordering um is, is maintained.

109
00:07:29,590 --> 00:07:37,480
Now you could, you could have three by three by one run that again see order is still maintained.

110
00:07:37,510 --> 00:07:40,930
Now let's get back and say we want to have three by two.

111
00:07:41,680 --> 00:07:42,820
You see we have an error.

112
00:07:42,820 --> 00:07:50,260
And this makes sense because, um, you cannot have as input nine values and then want to reshape in

113
00:07:50,260 --> 00:07:56,410
such a way that, um, you would have expected to have six values because, um, this output matrix

114
00:07:56,410 --> 00:08:02,260
here, which is three by two, has actually six values or takes six values, because obviously it's

115
00:08:02,260 --> 00:08:02,950
three by two.

116
00:08:02,980 --> 00:08:05,740
So that's three rows, two columns, that's six values.

117
00:08:05,740 --> 00:08:11,560
And so given that our input is um above that, this reshaping cannot work.

118
00:08:11,560 --> 00:08:17,710
So you have to be careful to make sure that, um, the shape you pass in actually matches with the,

119
00:08:17,710 --> 00:08:20,410
the input you're, you're sending in here.

120
00:08:20,410 --> 00:08:24,880
And so sometimes when you are not sure, you could say for example, negative one.

121
00:08:24,880 --> 00:08:26,950
So if we put negative one right here.

122
00:08:27,610 --> 00:08:33,130
You'll see that it's going to TensorFlow is going to figure out, um, what is the right value for the,

123
00:08:33,130 --> 00:08:34,690
for the, uh, for this position.

124
00:08:34,690 --> 00:08:40,540
Now, if you put for obviously if there is no number that when you multiply by four it's going to give

125
00:08:40,540 --> 00:08:41,290
um nine.

126
00:08:41,290 --> 00:08:42,730
So it's not going to work.

127
00:08:42,730 --> 00:08:45,400
But let's say we put let's try negative one here.

128
00:08:45,910 --> 00:08:47,080
We try negative one.

129
00:08:47,080 --> 00:08:48,040
We still have an error.

130
00:08:48,040 --> 00:08:52,150
So reshape only one input size maybe negative one not both.

131
00:08:52,150 --> 00:08:54,850
So because we have both here it doesn't work.

132
00:08:54,850 --> 00:08:57,550
So let's say we have that three negative one.

133
00:08:57,550 --> 00:08:59,320
And then let's say one.

134
00:08:59,470 --> 00:09:00,310
Run that again.

135
00:09:00,310 --> 00:09:02,440
And we should have this 331.

136
00:09:02,440 --> 00:09:03,340
So that's it.

137
00:09:03,340 --> 00:09:05,770
That's how we um carry out the reshaping.

138
00:09:05,770 --> 00:09:10,570
Now we could have um, 2D inputs like you have here in the documentation.

139
00:09:10,660 --> 00:09:11,680
Scroll down.

140
00:09:11,680 --> 00:09:13,330
You could pick out some examples.

141
00:09:13,330 --> 00:09:15,700
So let's say we have this T one.

142
00:09:15,700 --> 00:09:20,170
Let's take this t one and then we paste it out here.

143
00:09:20,710 --> 00:09:21,790
There we go.

144
00:09:21,790 --> 00:09:23,200
We have t one.

145
00:09:24,850 --> 00:09:26,680
Spin out the shape of T one.

146
00:09:29,500 --> 00:09:31,270
Um, has an attribute shape.

147
00:09:31,330 --> 00:09:34,150
Well, this is TensorFlow constant.

148
00:09:35,320 --> 00:09:35,740
Um.

149
00:09:35,740 --> 00:09:36,880
That's fine.

150
00:09:38,430 --> 00:09:41,370
Put this way and then we close that.

151
00:09:41,370 --> 00:09:43,950
So we have t one shape now.

152
00:09:43,950 --> 00:09:47,550
And we could reshape this um into a 1D tensor.

153
00:09:47,550 --> 00:09:55,110
So we could, you could do um TensorFlow reshape of t one um with of shape six.

154
00:09:55,110 --> 00:09:56,430
So we'll run that.

155
00:09:56,430 --> 00:09:57,990
And you see that works.

156
00:09:57,990 --> 00:10:00,360
So we now reshape this into a 1D tensor.

157
00:10:00,360 --> 00:10:03,330
Again notice how the order is maintained.

158
00:10:03,330 --> 00:10:08,880
So if we had um four here and then here we had let's say two.

159
00:10:09,630 --> 00:10:14,190
You run that and run this, we should have 1432656.

160
00:10:14,190 --> 00:10:16,830
So the order is, um respected.

161
00:10:16,830 --> 00:10:21,120
We now move to the next method, which is going to be the concat method.

162
00:10:21,690 --> 00:10:23,310
Um, let's get back to see.

163
00:10:23,340 --> 00:10:27,360
Here we have c, um, concat.

164
00:10:27,390 --> 00:10:28,080
There we go.

165
00:10:28,080 --> 00:10:30,210
We have this concat method.

166
00:10:30,210 --> 00:10:38,520
And what this concat method does is it concatenates two tensors along a given dimension.

167
00:10:38,850 --> 00:10:41,490
And so that's why here you have to specify the values.

168
00:10:41,490 --> 00:10:45,060
That's the two the two tensors or the different tensors.

169
00:10:45,060 --> 00:10:48,060
And then you also specify the axis.

170
00:10:48,240 --> 00:10:52,710
Let's copy this example and show how the Concatenate method works.

171
00:10:52,710 --> 00:10:55,590
So here we have T1T2.

172
00:10:55,620 --> 00:10:56,700
There we go.

173
00:10:56,910 --> 00:11:03,000
Um then let's let's run this and then let's do concatenate that's concat.

174
00:11:03,000 --> 00:11:06,570
And then we'll specify t1 and t2.

175
00:11:06,600 --> 00:11:10,140
Then we have the axis which is set to zero.

176
00:11:10,140 --> 00:11:16,020
Now let's first of all start by printing out the shapes of T1 and out of T2.

177
00:11:16,050 --> 00:11:19,950
As you could see the both um two by three tensors.

178
00:11:20,040 --> 00:11:27,330
And so what the Concatenate method does is uh, when you specify the axis, it's going to concatenate

179
00:11:27,330 --> 00:11:31,380
the two tensors along this specified axis.

180
00:11:31,380 --> 00:11:40,860
So two, three um together with two three is going to produce uh four three because you, you specify

181
00:11:40,860 --> 00:11:42,540
that the axis is zero.

182
00:11:42,540 --> 00:11:47,280
So along this zero axis we carry out the concatenation.

183
00:11:47,280 --> 00:11:50,070
And then we're now left with an output which is four by three.

184
00:11:50,070 --> 00:11:52,560
And that's exactly what we see right here.

185
00:11:52,650 --> 00:11:59,220
Now notice how um this 123456 is this first T1.

186
00:11:59,220 --> 00:12:04,980
And then 789 ten 1112 is the next T two.

187
00:12:05,040 --> 00:12:07,860
So what if we modify this axis.

188
00:12:07,860 --> 00:12:09,960
So let's let's take this to be axis one.

189
00:12:09,960 --> 00:12:17,430
If you have axis one then we'll go from 2 to 3 23226.

190
00:12:17,640 --> 00:12:21,870
You see we now concatenating along this um axis one.

191
00:12:21,870 --> 00:12:23,400
So now we have two six.

192
00:12:23,400 --> 00:12:25,560
So let's run this and see what we get.

193
00:12:26,250 --> 00:12:28,470
You see now we have this two by six.

194
00:12:28,470 --> 00:12:33,900
So again we have 123456123456.

195
00:12:33,900 --> 00:12:34,830
There we go.

196
00:12:34,920 --> 00:12:37,680
And for this first part see this first part.

197
00:12:37,680 --> 00:12:41,640
And then we have 789 ten um 1112.

198
00:12:41,640 --> 00:12:48,150
So depending on whether you want to concatenate along the rows or along the columns, you just need

199
00:12:48,150 --> 00:12:51,540
to specify that where at the level of this axis.

200
00:12:51,570 --> 00:12:55,680
Now let's take some other example from the documentation.

201
00:12:55,680 --> 00:12:59,160
Scroll down and let's copy this example here.

202
00:12:59,160 --> 00:13:01,290
We could start by printing out the shape.

203
00:13:01,290 --> 00:13:04,200
So we have t1 and t2 shape.

204
00:13:04,440 --> 00:13:05,160
There we go.

205
00:13:05,160 --> 00:13:07,650
We have 222 and 222.

206
00:13:07,650 --> 00:13:15,420
And so if we concatenate along the last axis as the example was given in the documentation, then we

207
00:13:15,420 --> 00:13:18,690
would have um let's add this code cell and show that.

208
00:13:18,690 --> 00:13:20,370
So let's just copy this.

209
00:13:20,400 --> 00:13:26,430
We have 222 concatenated with 222 along the last axis.

210
00:13:26,430 --> 00:13:28,590
That's because we specify negative one.

211
00:13:28,590 --> 00:13:33,660
Then our output should be two two and then four.

212
00:13:33,660 --> 00:13:38,070
So we'll still have our 3D tensor as output.

213
00:13:38,070 --> 00:13:46,950
But the 2D tensors or the matrices we have in this um 3D blocks or in this 3D tensor.

214
00:13:46,950 --> 00:13:55,530
But this 2D blocks will be concatenated such that we now have um, two by four shapes.

215
00:13:55,530 --> 00:14:00,720
So instead of this two by two we take this concatenate with this, and then from two by four.

216
00:14:00,750 --> 00:14:03,930
Then we take this, concatenate with this, and we form two by four.

217
00:14:03,930 --> 00:14:06,810
So let's go ahead and run that and see what we get.

218
00:14:07,850 --> 00:14:12,800
You see, we have one 2 or 1, two, two, three.

219
00:14:13,100 --> 00:14:14,180
Let's get back here.

220
00:14:14,210 --> 00:14:15,830
Scroll back up.

221
00:14:15,860 --> 00:14:19,340
We have 122, three.

222
00:14:19,370 --> 00:14:20,030
You see that?

223
00:14:20,030 --> 00:14:21,080
That block here.

224
00:14:21,080 --> 00:14:22,250
Is it right here.

225
00:14:22,250 --> 00:14:23,960
1223.

226
00:14:23,960 --> 00:14:27,920
And then we have seven four, eight for this block.

227
00:14:28,100 --> 00:14:30,320
See this block right here.

228
00:14:30,320 --> 00:14:34,010
Well it's actually this with this.

229
00:14:34,010 --> 00:14:39,950
And then for the next one we have 4453 which is this block.

230
00:14:40,800 --> 00:14:42,180
4453.

231
00:14:42,180 --> 00:14:44,760
And then we have two, ten, 15, 11.

232
00:14:44,760 --> 00:14:53,190
Now if this was along the z axis, as if we had zero here, then we should have four, two, two.

233
00:14:53,400 --> 00:14:53,940
You see.

234
00:14:53,940 --> 00:15:00,180
So now we're going to have um, instead of having blocks of two we're going to have blocks of four.

235
00:15:00,180 --> 00:15:02,670
So let's run that and see what we obtain.

236
00:15:03,990 --> 00:15:04,920
There we go.

237
00:15:04,920 --> 00:15:11,580
You see, now, instead of having, um, a block like here where we had, um, just two, like here,

238
00:15:11,580 --> 00:15:15,180
we just have two blocks of, um, two by two matrices.

239
00:15:15,210 --> 00:15:19,530
Now we have four blocks, um, of two by two matrices.

240
00:15:19,530 --> 00:15:25,680
And obviously we have one, two, two, three one, 223, four, 453.

241
00:15:25,680 --> 00:15:29,760
And then we have 74842 ten 1511.

242
00:15:29,760 --> 00:15:30,840
So that's it.

243
00:15:30,840 --> 00:15:35,700
Now another common and similar method is the stack method.

244
00:15:35,700 --> 00:15:43,050
What the stack does is it stacks a list of uh rank R tensors into one of rank R plus one.

245
00:15:43,050 --> 00:15:46,350
So getting back is similar with the concat.

246
00:15:46,350 --> 00:15:47,280
So we're taking the values.

247
00:15:47,280 --> 00:15:48,660
And then we specify the axis.

248
00:15:48,660 --> 00:15:52,470
So let's get back and see what we get when we carry out stacking.

249
00:15:52,740 --> 00:15:54,450
Um for this first example.

250
00:15:54,450 --> 00:15:57,000
So here we have t1 and t2.

251
00:15:57,180 --> 00:15:58,860
Now let's take this off.

252
00:15:58,860 --> 00:16:00,990
Let's do TensorFlow stack.

253
00:16:01,020 --> 00:16:02,160
TensorFlow stack.

254
00:16:02,160 --> 00:16:06,570
We have um t1 t2.

255
00:16:06,570 --> 00:16:08,400
And then we specify the axis.

256
00:16:08,400 --> 00:16:09,900
Let's take axis zero.

257
00:16:09,900 --> 00:16:11,430
We run that and see what we get.

258
00:16:11,430 --> 00:16:12,660
Well let's take this.

259
00:16:12,660 --> 00:16:16,410
Let's run this again because we had um created a new T1 t2.

260
00:16:16,410 --> 00:16:22,320
So uh one thing you will notice is that with a concat let's get, let's take the axis to, let's say

261
00:16:22,320 --> 00:16:24,540
the axis to zero with the concat.

262
00:16:24,570 --> 00:16:27,720
There is no, um, extra dimension created.

263
00:16:27,720 --> 00:16:34,470
So um, if our inputs are four by three or if our inputs are 2D, then our output will be 2D.

264
00:16:34,500 --> 00:16:38,850
Whereas with the concat, if our inputs are 2D, then our output will be 3D.

265
00:16:38,850 --> 00:16:42,060
And if our inputs are 3D, our output will be 4D.

266
00:16:42,060 --> 00:16:47,070
So here we have this um tool 123456.

267
00:16:47,070 --> 00:16:47,940
There we go.

268
00:16:47,940 --> 00:16:50,250
And then we have 789 ten 1112.

269
00:16:50,250 --> 00:16:57,120
You see, they have been stacked in such a way that they now form, um, a 3D tensor where we have this

270
00:16:57,120 --> 00:16:59,040
extra dimension created.

271
00:16:59,040 --> 00:17:05,730
So if you look carefully at the start of the stack method, it's similar to um, carrying out, um,

272
00:17:06,090 --> 00:17:07,140
expanddims.

273
00:17:07,140 --> 00:17:11,940
So making use of the Expanddims, um, let's, let's have the axis set to zero.

274
00:17:11,940 --> 00:17:18,750
So we add an extra dimension to T1, and we also add another extra dimension um, to T2.

275
00:17:18,750 --> 00:17:23,370
So there we have this extra dimension added to both T1 and T2.

276
00:17:23,400 --> 00:17:24,540
Let's reduce this.

277
00:17:24,990 --> 00:17:30,420
Um, and then uh we run that so well this is concat actually.

278
00:17:30,420 --> 00:17:32,040
So let's get back.

279
00:17:33,510 --> 00:17:36,360
Um, let's just have, let's just have this.

280
00:17:37,040 --> 00:17:38,060
Copy that.

281
00:17:38,060 --> 00:17:42,470
And then we let's, let's run it just above here.

282
00:17:42,470 --> 00:17:46,550
So here is concat concat.

283
00:17:46,940 --> 00:17:50,030
And let's take this back to um original stack.

284
00:17:50,030 --> 00:17:53,030
So when we stacked um t1 t2 okay.

285
00:17:53,030 --> 00:17:56,840
So that's for stack run this again to be sure.

286
00:17:56,840 --> 00:17:59,510
So this is for stack two by two by three.

287
00:17:59,510 --> 00:18:06,800
Now what the relationship between concat and start is uh, for the concat you, you can or you can get

288
00:18:06,800 --> 00:18:10,580
the stack from concat by adding this extra dimension.

289
00:18:10,580 --> 00:18:13,160
Remember we said we're going to add an extra dimension.

290
00:18:13,160 --> 00:18:18,680
And here we we just simply add that extra dimension for T1 and add it for T2.

291
00:18:18,680 --> 00:18:20,750
And then carry out the concat normally.

292
00:18:20,750 --> 00:18:26,090
So you will notice that now we have exact same output with a with a stack and with a concat.

293
00:18:26,090 --> 00:18:28,640
So you see clearly the difference between the two.

294
00:18:28,790 --> 00:18:33,560
With a concat no extra dimension added, or the stack will have that extra dimension added.

295
00:18:33,800 --> 00:18:41,720
Now if we change this axis to the last one negative one, we're going to have two by three because our

296
00:18:41,720 --> 00:18:42,800
inputs are two by three.

297
00:18:42,800 --> 00:18:45,890
So our inputs are two by three and now by two.

298
00:18:45,920 --> 00:18:49,700
So the stacking is done on the last axis.

299
00:18:49,850 --> 00:18:53,240
So here we have um one seven or.

300
00:18:53,240 --> 00:18:55,940
Well we have 123456.

301
00:18:55,940 --> 00:18:56,300
You see.

302
00:18:56,300 --> 00:18:59,120
Notice this here this arrangement.

303
00:18:59,420 --> 00:19:01,460
Uh 123456.

304
00:19:01,850 --> 00:19:04,850
And then here we have seven, eight 910, 11, 12.

305
00:19:04,850 --> 00:19:10,910
So, um, the arrangement now is different from what we had before when the axis was set to zero.

306
00:19:10,940 --> 00:19:16,910
Again, you just need to specify a negative one year on that we're getting something different.

307
00:19:16,910 --> 00:19:22,160
But we just needed to have this expand, uh, this extra dimension added at the end.

308
00:19:22,160 --> 00:19:23,390
So let's run that again.

309
00:19:23,390 --> 00:19:29,960
And you see, now that we have exact same output with a stack, our next method is the pad method.

310
00:19:29,960 --> 00:19:33,830
And essentially it pads a tensor with given values.

311
00:19:33,830 --> 00:19:41,060
So here we have TensorFlow pad takes in a tensor um the patterns the mode and the constant values which

312
00:19:41,060 --> 00:19:41,870
will pad with.

313
00:19:41,870 --> 00:19:46,820
So let's take this example and illustrate or show exactly how it works.

314
00:19:47,060 --> 00:19:49,220
Let's get back to our notebook.

315
00:19:49,220 --> 00:19:50,900
We paste this out here.

316
00:19:50,900 --> 00:19:52,760
You see we have this t.

317
00:19:53,090 --> 00:19:54,620
So that's our T.

318
00:19:54,740 --> 00:19:56,630
Then we have the patterns.

319
00:19:56,870 --> 00:19:59,210
We're going to explain exactly how this works.

320
00:19:59,210 --> 00:20:00,680
Then let's print this out.

321
00:20:00,680 --> 00:20:05,600
So let's print this out and see what we get.

322
00:20:05,600 --> 00:20:08,450
You see we have 123456.

323
00:20:08,450 --> 00:20:11,630
And then all this is padded with this zeros.

324
00:20:11,630 --> 00:20:18,680
Now to understand how this is, uh, how we get this kind of output, you should note that this value

325
00:20:18,680 --> 00:20:24,800
for the or this values we have here for the patterns, permit us to know, um, how many values are

326
00:20:24,800 --> 00:20:25,490
for at the top.

327
00:20:25,490 --> 00:20:26,750
We're going to add how many values.

328
00:20:26,750 --> 00:20:30,290
At the bottom we're going to add how many values to the left we're going to add and how many values

329
00:20:30,290 --> 00:20:31,460
to the right we're going to add.

330
00:20:31,460 --> 00:20:34,280
So if you notice here we have 123.

331
00:20:34,280 --> 00:20:36,290
And then we have 456.

332
00:20:36,290 --> 00:20:40,400
At this top is padded with zeros just one.

333
00:20:40,400 --> 00:20:43,640
And at the bottom we spider with zeros just one layer.

334
00:20:43,640 --> 00:20:46,250
So we have just one layer up and one layer down.

335
00:20:46,250 --> 00:20:48,770
But to the left we have two layers.

336
00:20:48,770 --> 00:20:50,180
Notice how we have two layers.

337
00:20:50,180 --> 00:20:51,590
And to the right we have two layers.

338
00:20:51,590 --> 00:20:54,380
Let's say to the right we want to add um two other layers.

339
00:20:54,380 --> 00:20:56,240
So let's just change this to four.

340
00:20:56,270 --> 00:20:57,350
We run that again.

341
00:20:57,350 --> 00:21:00,890
You see now that to the right we have this four layers.

342
00:21:00,890 --> 00:21:05,480
If we want to the top to be four, we run, we change that.

343
00:21:05,480 --> 00:21:06,440
Run that again.

344
00:21:06,440 --> 00:21:09,320
You see now that to the top we have four layers.

345
00:21:09,320 --> 00:21:12,980
To the right we have four layers to the left we still have two layers.

346
00:21:12,980 --> 00:21:15,410
And then to uh the bottom we have one layer.

347
00:21:15,410 --> 00:21:18,380
That's the role of this patterns we have here.

348
00:21:18,740 --> 00:21:22,880
Um, we also have different, uh, this coming different formats.

349
00:21:22,970 --> 00:21:28,730
So we could change this like you see in this examples, uh, we have here.

350
00:21:28,730 --> 00:21:30,140
Well, this is stack.

351
00:21:30,140 --> 00:21:31,310
Let's get back to pattern.

352
00:21:31,310 --> 00:21:31,760
Okay.

353
00:21:31,760 --> 00:21:35,990
So um, if it's constant and then we specify the constant values.

354
00:21:35,990 --> 00:21:40,520
So we could, we could change this here um constant values.

355
00:21:40,520 --> 00:21:49,040
So let's say we, we have constant values and we set that to let's say um 20 or let's say let's just

356
00:21:49,040 --> 00:21:53,360
say uh nine value that's not found in the original tensor.

357
00:21:53,360 --> 00:22:01,370
So here we have nine, you see, all padded by nines getting back apart from padding with constant values,

358
00:22:01,370 --> 00:22:07,250
we could decide to reflect or use reflect or use um, symmetry.

359
00:22:07,250 --> 00:22:14,570
And what this means is that the patterns or the padded values we have surrounding our main tensor,

360
00:22:14,570 --> 00:22:18,980
like here, are going to be gotten from the tensor itself.

361
00:22:19,700 --> 00:22:25,760
And the way these patterns are gotten will be will be dependent on whether it's a reflection or whether

362
00:22:25,760 --> 00:22:27,500
we're making use of symmetry.

363
00:22:27,530 --> 00:22:30,230
The next method we'll look at is the getter method.

364
00:22:30,260 --> 00:22:38,270
So what this gather does is it gathers slices from the tensor axis according to the indices.

365
00:22:38,270 --> 00:22:46,400
So remember when we're um working with um indexing we had for example, we could build we could say

366
00:22:46,400 --> 00:22:49,460
is this um constant.

367
00:22:49,460 --> 00:22:52,760
And here we had say zero one.

368
00:22:52,910 --> 00:22:55,850
Well let's just say 4567.

369
00:22:55,850 --> 00:23:02,180
So if we had this input and then we wanted to have, um, the elements five and six, in that case we

370
00:23:02,180 --> 00:23:10,190
would do a uh, one, two, you know, A13 actually, because it's um, three minus one.

371
00:23:10,490 --> 00:23:13,070
So there we have, um, five and six.

372
00:23:13,070 --> 00:23:20,210
So we go one and then oh, this is zero, this is zero, this is one, this is two.

373
00:23:20,210 --> 00:23:22,760
And that's exactly why we have this five, six.

374
00:23:22,760 --> 00:23:28,610
Now what if we wanted to have four seven and then five.

375
00:23:28,610 --> 00:23:32,300
So what if we do not want um, this to be in a specific order.

376
00:23:32,630 --> 00:23:35,300
Um, that's where the gather method comes in.

377
00:23:35,300 --> 00:23:42,650
So if we want to have, um, this four, seven, five, then we'll say gather, we'll specify a, we

378
00:23:42,650 --> 00:23:46,190
specify that um, four which is zero.

379
00:23:46,550 --> 00:23:47,960
Um, seven is the last.

380
00:23:48,080 --> 00:23:50,960
Um that's zero, one, two, three.

381
00:23:50,960 --> 00:23:53,390
So three and then five is one.

382
00:23:53,390 --> 00:24:01,760
So when you specify this way and it set the axis to zero, you see now that we have 475.

383
00:24:01,760 --> 00:24:09,260
So it's capable of gathering all those elements um based on this order we had set, let's suppose we

384
00:24:09,260 --> 00:24:15,290
have this b we have this B TensorFlow gather and we specify b.

385
00:24:15,290 --> 00:24:19,130
And let's suppose we want to have this this right here.

386
00:24:19,130 --> 00:24:20,390
We want to have this.

387
00:24:20,390 --> 00:24:22,430
And then we want to have this.

388
00:24:22,430 --> 00:24:25,760
But we want to have this first before having this.

389
00:24:25,880 --> 00:24:28,850
So in that case we will have b.

390
00:24:28,880 --> 00:24:33,950
We'll specify that we we want to have the last because this is in row wise.

391
00:24:33,950 --> 00:24:35,150
This is zero.

392
00:24:35,180 --> 00:24:37,760
This is one this is two and this is three.

393
00:24:37,760 --> 00:24:41,000
So one to have three and then zero.

394
00:24:41,000 --> 00:24:45,350
And the axis we specify that's the row the the row.

395
00:24:45,350 --> 00:24:46,880
So it's row wise.

396
00:24:46,880 --> 00:24:54,680
So when we run this now you see we have 33020530205.

397
00:24:54,680 --> 00:24:58,520
And then zero which is 29030.

398
00:24:58,520 --> 00:25:03,110
So it permits us to gather elements from our tensor um, very easily.

399
00:25:03,110 --> 00:25:08,420
Now imagine we wanted to do this same process, but column wise we'll specify the axis to be one.

400
00:25:08,420 --> 00:25:15,710
But here we have um, let's say we want to have this the, this column that's 9630.

401
00:25:15,710 --> 00:25:19,130
And we wanted to have three negative two zero zero.

402
00:25:19,130 --> 00:25:24,950
In that case we will do um one because this is this is zero.

403
00:25:24,980 --> 00:25:28,520
This is one, this is two and this is three.

404
00:25:28,520 --> 00:25:30,470
So we'll have one and three.

405
00:25:30,470 --> 00:25:31,790
And then the axis is set to one.

406
00:25:31,790 --> 00:25:32,810
So we run that again.

407
00:25:32,810 --> 00:25:34,670
You see we have 9630.

408
00:25:34,670 --> 00:25:37,190
And then we have three negative two zero zero.

409
00:25:37,190 --> 00:25:40,670
Now what happens when this indices is 2D.

410
00:25:40,670 --> 00:25:45,320
So let's simply copy this again and paste this out here.

411
00:25:45,320 --> 00:25:49,370
So let's suppose that instead of one three we have one three.

412
00:25:49,370 --> 00:25:54,770
And then let's say um one zero or let's let's say zero two.

413
00:25:54,800 --> 00:25:57,230
Well let's, let's change the order to zero.

414
00:25:57,380 --> 00:26:01,400
So we have one three and two zero.

415
00:26:01,640 --> 00:26:03,410
Close that and that's fine.

416
00:26:03,410 --> 00:26:03,920
Okay.

417
00:26:03,920 --> 00:26:07,280
So instead of one three we have 1320.

418
00:26:07,280 --> 00:26:08,750
So let's see what we have here.

419
00:26:08,750 --> 00:26:10,640
Let's change the axis to zero.

420
00:26:10,640 --> 00:26:11,420
Run that.

421
00:26:12,200 --> 00:26:13,730
And axis zero.

422
00:26:13,730 --> 00:26:18,080
So you would see that, um, it's going to be similar to what we've seen already.

423
00:26:18,080 --> 00:26:19,490
So here we have one three.

424
00:26:19,490 --> 00:26:24,590
So and and because we specify the axis to be zero we are going to pick along the rows.

425
00:26:24,590 --> 00:26:31,310
So here because we've selected axis to be zero we would have one because this is zero.

426
00:26:31,370 --> 00:26:32,510
This is one.

427
00:26:32,510 --> 00:26:34,430
We have this one we have two.

428
00:26:34,460 --> 00:26:35,540
Then we have three.

429
00:26:35,540 --> 00:26:38,660
So you see that we have this here one.

430
00:26:38,660 --> 00:26:42,980
And then we have three which is gotten from um our input.

431
00:26:42,980 --> 00:26:47,330
Now when we have one three, two zero you see that it's similar.

432
00:26:47,330 --> 00:26:52,640
We have the one, we have the three and then two.

433
00:26:52,670 --> 00:26:55,190
You see this is two because this is 012.

434
00:26:55,220 --> 00:26:57,620
This is two one, three five.

435
00:26:57,620 --> 00:27:00,500
So here we have 13501.

436
00:27:00,500 --> 00:27:03,380
And then zero is 29030.

437
00:27:03,380 --> 00:27:07,250
So 29030 exactly as we would expect.

438
00:27:07,250 --> 00:27:10,460
And that's how the gather method works.

439
00:27:10,460 --> 00:27:15,590
Now another method which is similar to the gather method is a gather NDI method.

440
00:27:15,590 --> 00:27:24,860
And the main difference here is that, um, the gather can do this across multiple or rather the gather

441
00:27:24,860 --> 00:27:32,360
ndi um gathers across multiple axes so it can gather elements of a matrix instead of a row or column.

442
00:27:32,360 --> 00:27:36,230
So let's take a simple example to illustrate how the gather NDI works.

443
00:27:36,230 --> 00:27:38,780
Here we have TensorFlow gather.

444
00:27:38,930 --> 00:27:41,720
Um, and well, let's just copy this.

445
00:27:41,720 --> 00:27:45,770
One thing you should note is the fact that it doesn't have, um, an axis.

446
00:27:45,770 --> 00:27:47,210
You don't need to put an axis.

447
00:27:47,210 --> 00:27:54,140
So because as, as has been described here, um, it gathers across multiple axes.

448
00:27:54,140 --> 00:27:56,870
So you don't need to specify a, um a given axis.

449
00:27:56,870 --> 00:27:59,480
So now we have one three, two zero.

450
00:27:59,510 --> 00:28:00,500
You see that.

451
00:28:00,500 --> 00:28:01,820
Well let's, let's run this.

452
00:28:01,820 --> 00:28:05,600
Let's have here gather and run that and see what we get.

453
00:28:05,600 --> 00:28:07,460
You see we have negative two one.

454
00:28:07,460 --> 00:28:10,520
Now the reason why we have negative two one here is simple.

455
00:28:10,520 --> 00:28:19,550
What this gather does is it gets like here we have well let's put this up so it's closer to our be okay.

456
00:28:19,550 --> 00:28:22,730
So here we have let's rearrange this.

457
00:28:22,760 --> 00:28:24,830
We here we have one three.

458
00:28:25,400 --> 00:28:33,230
So unlike the gather which is going to say for example we gathering along all the rows then it means

459
00:28:33,230 --> 00:28:37,190
that we could pick the the first and then we pick the third.

460
00:28:37,310 --> 00:28:41,030
Here we are going to pick the elements directly.

461
00:28:41,030 --> 00:28:44,780
So when you specify 123 it's going to go zero one.

462
00:28:44,780 --> 00:28:48,050
And then 0123.

463
00:28:48,050 --> 00:28:49,310
That's how we have negative two.

464
00:28:49,310 --> 00:28:54,890
And then when you specify two zero it's going to go 012 and then zero which is one.

465
00:28:54,890 --> 00:28:56,630
So that's how we get this output.

466
00:28:56,750 --> 00:29:01,820
Now if you want to get something similar to this one three we had here, what you would need to do is

467
00:29:01,820 --> 00:29:02,720
take this off.

468
00:29:02,720 --> 00:29:05,780
So now it's one it's going to pick all the row.

469
00:29:06,230 --> 00:29:10,730
Um and then it will take or you take your three put your three.

470
00:29:10,730 --> 00:29:12,230
So you're also going to pick the third row.

471
00:29:12,230 --> 00:29:19,100
We run that and you see we get um, exactly what we got with the scatter while specifying your one three.

472
00:29:19,100 --> 00:29:27,290
So you see that while the two methods are used in gathering, um, some elements from the inputs, they

473
00:29:27,290 --> 00:29:29,810
have different ways of functioning.
