1
00:00:00,240 --> 00:00:01,230
Hi and welcome back.

2
00:00:01,470 --> 00:00:06,510
In this section, we'll take a look at using carrots to build a one or two encoder.

3
00:00:06,540 --> 00:00:09,330
So open Notebook 29 and we'll get started.

4
00:00:09,780 --> 00:00:15,780
So firstly, this lesson was taken from the official Keros blog tutorial on building also incluidas

5
00:00:15,780 --> 00:00:16,500
in Keros.

6
00:00:17,010 --> 00:00:22,320
And here's a nice little diagram to basically show what's happening and also include this here we have

7
00:00:22,320 --> 00:00:23,610
a bottleneck layer here.

8
00:00:24,060 --> 00:00:29,850
And basically what we're doing, we're just trying to encode the input in a smaller dimensionality similar

9
00:00:29,850 --> 00:00:35,640
to principal components analysis and generating an output that looks a lot like the input and try to

10
00:00:36,570 --> 00:00:37,260
try to encode it.

11
00:00:37,950 --> 00:00:40,560
So basically, let's get started with this lesson.

12
00:00:40,560 --> 00:00:44,670
So here are some definitions of what an encoder and decoder is.

13
00:00:45,030 --> 00:00:51,270
Remember an encoded compresses of dung samples, the input image and to a lesser number of bits or lower

14
00:00:51,270 --> 00:00:52,020
dimensionality.

15
00:00:52,560 --> 00:00:56,370
And this last symptom of this is called a living in space or bottleneck.

16
00:00:56,880 --> 00:01:02,310
And what the decoder does, the decoder takes this lesson number of bits or the smaller dimensional

17
00:01:02,310 --> 00:01:09,120
representation of the input and then tries to reconstruct the input using only the including of that

18
00:01:09,120 --> 00:01:09,630
input.

19
00:01:09,840 --> 00:01:13,800
So that's basically a summary of what an auto encode is right here.

20
00:01:14,190 --> 00:01:18,420
So now let's get started with building a very simple, basic auto encoder.

21
00:01:18,720 --> 00:01:22,140
So firstly, let's import some packages that we'll be using.

22
00:01:22,140 --> 00:01:29,040
Those are the Keros TensorFlow packages, as well as MATLAB, and NumPy and knowThis will be using the

23
00:01:29,040 --> 00:01:30,120
amnesty dataset.

24
00:01:30,420 --> 00:01:36,210
So we'll be including we'll be treating auto encoder on the M this handwritten digit dataset.

25
00:01:36,660 --> 00:01:42,000
So the helper functions we're creating here is firstly a pre-processed function that normalizes the

26
00:01:42,000 --> 00:01:47,700
supplied array and then reshapes it into the appropriate format so that we can pass the input to our

27
00:01:47,730 --> 00:01:48,570
neural network.

28
00:01:49,170 --> 00:01:52,410
Then we have a function that adds random noise to an image.

29
00:01:52,950 --> 00:01:57,420
And then lastly, by the way, we'll be using this later and later in this section.

30
00:01:57,420 --> 00:01:58,350
So don't worry about it.

31
00:01:58,800 --> 00:02:00,950
If you're wondering why we're adding noise, don't worry.

32
00:02:00,960 --> 00:02:01,830
I'll explain later.

33
00:02:02,520 --> 00:02:09,570
And then we have a display function that takes these two areas and displays 10 random images from each

34
00:02:09,570 --> 00:02:10,950
one of the supplied arrays.

35
00:02:11,190 --> 00:02:11,940
So that's it.

36
00:02:11,940 --> 00:02:18,810
So let's run this and we'll be importing our functions and declaring our helper functions.

37
00:02:20,400 --> 00:02:21,100
So there we go.

38
00:02:21,120 --> 00:02:22,440
So it's going to be run now.

39
00:02:22,860 --> 00:02:26,340
Next, we have to load and pre process our endless data set.

40
00:02:26,700 --> 00:02:32,370
So we just use the amnestied load function to get only to set up the training data and test it to notice.

41
00:02:32,370 --> 00:02:39,740
We're not using the limo's here because we just assign into this dummy variable, but an auto encoder.

42
00:02:39,750 --> 00:02:41,130
We don't need the variables at all.

43
00:02:41,430 --> 00:02:45,450
We just need to learn the representation, the underlying representation of the data.

44
00:02:45,780 --> 00:02:47,640
That's why we just look at the data itself.

45
00:02:48,240 --> 00:02:53,460
So then we normalize and reshape the data using the preprocessor pre-processed helper function we created

46
00:02:53,460 --> 00:02:53,850
above.

47
00:02:54,570 --> 00:03:00,480
And then we add some noise to the data because what we'll be doing here will be training an auto encoder

48
00:03:00,870 --> 00:03:03,840
that basically can remove noise from images.

49
00:03:04,320 --> 00:03:06,210
So let's take a look and see how we do that.

50
00:03:06,210 --> 00:03:12,600
So we just use a noise helper function to add random noise to the training data and test data, and

51
00:03:12,600 --> 00:03:13,830
then we can display it here.

52
00:03:13,860 --> 00:03:16,400
Let's display those sample images here.

53
00:03:16,420 --> 00:03:18,100
So it's downloaded data set first.

54
00:03:18,120 --> 00:03:19,260
That's what it's doing here.

55
00:03:20,370 --> 00:03:21,210
And there we go.

56
00:03:21,480 --> 00:03:25,620
So we have the the clean images on top and then the noisy ones below.

57
00:03:27,480 --> 00:03:30,450
So now we're ready to build or to include a model.

58
00:03:30,450 --> 00:03:35,450
So let's take a look at firstly, the encoder model you can see it's quite simple.

59
00:03:35,460 --> 00:03:39,510
We just have to convolutional is and each one followed by a maximal.

60
00:03:39,870 --> 00:03:45,060
So what you can imagine as we build this model, as the input goes through the network, it gets smaller.

61
00:03:45,370 --> 00:03:47,640
In fact, yes, it's getting a lot smaller right now.

62
00:03:48,120 --> 00:03:51,720
And then that's what we used to convert 2D transport layers.

63
00:03:52,080 --> 00:03:55,740
These are basically upsampling layers, but they're smart upsampling layers.

64
00:03:55,740 --> 00:04:00,930
And because they learn the feature representation of the image of this, their previous input here.

65
00:04:01,890 --> 00:04:09,870
So we have the two clung to the upscaling layers to produce basically back the output right here, and

66
00:04:09,870 --> 00:04:10,610
that's what we get.

67
00:04:10,620 --> 00:04:17,610
So that's how we can create or to encode a model, which is basically the input here and X, which basically

68
00:04:17,610 --> 00:04:21,600
strings along all of these sequential operations here.

69
00:04:22,140 --> 00:04:28,380
And that gives us the final or to encode a model where we use Adam as the optimizer and binary cross

70
00:04:28,380 --> 00:04:30,000
entropy as the lost function.

71
00:04:30,420 --> 00:04:36,510
So let's run this and we can take a look at the architecture here so you can see how it gets bottlenecked

72
00:04:36,720 --> 00:04:40,290
as we go down here and then suddenly it gets back bigger.

73
00:04:40,680 --> 00:04:44,430
That's because of the continuity transport layers here.

74
00:04:45,150 --> 00:04:46,110
So that's it.

75
00:04:46,620 --> 00:04:48,710
And then we are now ready to trino quarter on quarter.

76
00:04:49,080 --> 00:04:51,660
So notice labels?

77
00:04:51,870 --> 00:04:52,800
No, the training data.

78
00:04:53,160 --> 00:04:59,900
That's because the output of this network is a 28 by 28 dimension effective and.

79
00:04:59,970 --> 00:05:04,790
No, previously it used to be at one by 10, and those were the outputs for probabilities of which classes.

80
00:05:05,150 --> 00:05:07,040
That's when we were building classifiers.

81
00:05:07,430 --> 00:05:11,690
Now we're building a neural net to learn the image and output the image back again.

82
00:05:11,900 --> 00:05:17,210
So and that's why we have separate kind of separate encoder and decoder models.

83
00:05:18,260 --> 00:05:19,870
So we're not ready to try this.

84
00:05:19,880 --> 00:05:25,430
And also notice to test data is what we'll be evaluating one after to see how well it performs.

85
00:05:26,000 --> 00:05:30,020
So let's train this and let's see how long it takes to train.

86
00:05:30,260 --> 00:05:35,000
I actually don't remember we're training for 50 epochs with a bad set of 128.

87
00:05:36,500 --> 00:05:40,220
OK, it's pretty quick, but we'll have to wait a bit still.

88
00:05:40,400 --> 00:05:41,420
So let's wait it out.

89
00:05:51,190 --> 00:05:57,610
OK, so we're done training now, and as you can see, we trained 50 bucks and examining the validation

90
00:05:57,610 --> 00:05:58,300
laws here.

91
00:05:58,840 --> 00:06:01,770
You can see 50 folks was probably a good choice.

92
00:06:01,780 --> 00:06:02,740
It seemed to have.

93
00:06:02,950 --> 00:06:06,700
It seemed to actually keep decreasing here, so we could have actually trained us for a bit longer.

94
00:06:07,180 --> 00:06:10,630
But nevertheless, this is a fairly low validation loss here.

95
00:06:11,080 --> 00:06:13,780
So let's take a look at our predictions now.

96
00:06:14,260 --> 00:06:19,960
So to get to predictions here, what we're doing, we're going to get all of the predictions out by

97
00:06:19,960 --> 00:06:23,590
just feeding into test data here into the quarter to include a model.

98
00:06:23,950 --> 00:06:29,950
Don't predict and we get all the predictions here and then we just display some random turnaround and

99
00:06:29,950 --> 00:06:30,600
predictions here.

100
00:06:30,610 --> 00:06:35,170
So this is this display as the input data here and this display as the output.

101
00:06:35,560 --> 00:06:42,940
So remember what we're doing here, but the output is it's going to be the input that was reduced in

102
00:06:42,940 --> 00:06:43,720
dimensionality.

103
00:06:44,260 --> 00:06:50,260
And then we're going to we going to blow that back up to the full representation of the full decoding

104
00:06:50,260 --> 00:06:52,840
model so we can compare how well it did.

105
00:06:52,990 --> 00:06:59,950
So let's take a look at that and you can see this is actually quite good, actually really good.

106
00:07:00,190 --> 00:07:01,360
You can see the snow.

107
00:07:01,810 --> 00:07:05,170
Remember, let's take a look at the auto included here.

108
00:07:05,860 --> 00:07:12,310
We basically shrunk this data down all the way to a seven by seven by 282 representation at a time.

109
00:07:12,640 --> 00:07:20,410
And now we can use that input to blow back using the decoder, and you can see it gives us almost the

110
00:07:20,410 --> 00:07:21,300
exact image.

111
00:07:21,370 --> 00:07:26,970
Looking at it carefully, it seems like a disk encoded the exact image itself, so that is actually

112
00:07:26,980 --> 00:07:27,700
pretty -- good.

113
00:07:28,510 --> 00:07:30,050
So now let's take a look at this.

114
00:07:30,070 --> 00:07:35,740
Remember, we also created a noisy version of our dataset and we said we're going to use our auto encoder

115
00:07:35,750 --> 00:07:36,460
to clean that up.

116
00:07:36,850 --> 00:07:38,320
Well, let's take a look at how we do that.

117
00:07:38,320 --> 00:07:41,770
So now we have the noisy data here as input.

118
00:07:42,040 --> 00:07:46,930
However, notice what's different here, we don't use a noisy version of the training data here.

119
00:07:47,260 --> 00:07:53,500
We used a clean vision because what we want to auto included to do is to clean that image data set up.

120
00:07:53,890 --> 00:07:58,930
So we are taking noisy inputs and we're going to feed it into the network and then we're going to learn

121
00:07:59,050 --> 00:08:01,510
how to decode a clean vision out of it.

122
00:08:02,080 --> 00:08:03,130
So let's run that.

123
00:08:03,130 --> 00:08:05,650
No, let's put this for 50 bucks here.

124
00:08:05,650 --> 00:08:09,760
So it won't take that long, and let's wait for our results.

125
00:08:19,190 --> 00:08:23,210
OK, so we're done training, that's about three minutes of training this network.

126
00:08:23,630 --> 00:08:30,110
And you can firstly notice that a loss function here isn't as low as a previous one, and that's understandable

127
00:08:30,110 --> 00:08:33,320
because given how noisy our input images were.

128
00:08:33,890 --> 00:08:38,990
So let's go back to this again, and you can see we're training with noisy test data.

129
00:08:39,080 --> 00:08:40,880
So this is a validation data set here.

130
00:08:41,240 --> 00:08:44,210
And this is a target data here says, remember that.

131
00:08:44,930 --> 00:08:49,800
So now let's evaluate its performance on some noisy data.

132
00:08:49,820 --> 00:08:57,750
So let's see how well our auto encoder can clean up images and it's remarkably well, it gives it.

133
00:08:57,770 --> 00:09:03,140
We can take these inputs that these noisy inputs and clean them up and get these almost perfect images

134
00:09:03,140 --> 00:09:03,440
here.

135
00:09:03,860 --> 00:09:09,140
The only one that doesn't look too great is perhaps maybe this tree, because it's a bit of a streak

136
00:09:09,140 --> 00:09:13,910
right here and the five has some noise added to it.

137
00:09:14,720 --> 00:09:17,450
Maybe this eight as well, but I mean, this is quite good.

138
00:09:17,930 --> 00:09:21,500
So I hope you enjoy this lesson and know what we're going to do.

139
00:09:21,860 --> 00:09:25,730
We're going to build an auto encoder in PyTorch, so stay tuned for that.

140
00:09:25,880 --> 00:09:26,270
Thank you.
