1
00:00:00,210 --> 00:00:04,060
So now let's take a look at
convolutions and pooling in code.

2
00:00:04,060 --> 00:00:07,020
We don't have to do all the math for
filtering and compressing,

3
00:00:07,020 --> 00:00:11,370
we simply define convolutional and
pooling layers to do the job for us.

4
00:00:12,760 --> 00:00:16,500
So here's our code from the earlier
example, where we defined out a neural

5
00:00:16,500 --> 00:00:19,610
network to have an input layer
in the shape of our data, and

6
00:00:19,610 --> 00:00:22,970
output layer in the shape of the number
of categories we're trying to define, and

7
00:00:22,970 --> 00:00:24,750
a hidden layer in the middle.

8
00:00:24,750 --> 00:00:27,690
The Flatten takes our
square 28 by 28 images and

9
00:00:27,690 --> 00:00:29,260
turns them into a one dimensional array.

10
00:00:30,300 --> 00:00:33,660
To add convolutions to this,
you use code like this.

11
00:00:33,660 --> 00:00:37,130
You'll see that the last three lines are
the same, the Flatten, the Dense hidden

12
00:00:37,130 --> 00:00:41,800
layer with 128 neurons, and
the Dense output layer with 10 neurons.

13
00:00:41,800 --> 00:00:44,857
What's different is what has
been added on top of this.

14
00:00:44,857 --> 00:00:47,000
Let's take a look at this, line by line.

15
00:00:48,150 --> 00:00:50,870
Here we're specifying
the first convolution.

16
00:00:50,870 --> 00:00:54,194
We're asking keras to
generate 64 filters for us.

17
00:00:54,194 --> 00:00:59,086
These filters are 3 by 3, their activation
is relu, which means the negative values

18
00:00:59,086 --> 00:01:03,589
will be thrown way, and finally the input
shape is as before, the 28 by 28.

19
00:01:03,589 --> 00:01:07,677
That extra 1 just means that we
are tallying using a single byte for

20
00:01:07,677 --> 00:01:09,300
color depth.

21
00:01:09,300 --> 00:01:12,510
As we saw before our image is our
gray scale, so we just use one byte.

22
00:01:13,570 --> 00:01:16,550
Now, of course,
you might wonder what the 64 filters are.

23
00:01:16,550 --> 00:01:19,740
It's a little beyond the scope of
this class to define them, but

24
00:01:19,740 --> 00:01:20,960
they aren't random.

25
00:01:20,960 --> 00:01:24,390
They start with a set
of known good filters

26
00:01:24,390 --> 00:01:27,370
in a similar way to the pattern
fitting that you saw earlier, and

27
00:01:27,370 --> 00:01:30,260
the ones that work from that
set are learned over time.

28
00:01:31,560 --> 00:01:34,159
For more details on convolutions and
how they work,

29
00:01:34,159 --> 00:01:36,175
there's a great set of resources here.