1 00:00:00,300 --> 00:00:08,220 All right so in this lesson the goal is to make predictions based on what it is that we've been drawing 2 00:00:08,310 --> 00:00:09,830 on our canvas. 3 00:00:10,200 --> 00:00:16,170 So we're now getting to the point where we're moving away from the data processing and getting to the 4 00:00:16,260 --> 00:00:23,280 exciting part which is using the process data to make predictions using the tenth floor light model 5 00:00:23,430 --> 00:00:25,380 that we've trained. 6 00:00:25,380 --> 00:00:33,530 And that means all we need to do is create a tensor and then make predictions on our data. 7 00:00:33,570 --> 00:00:36,830 So how do we create a tensor. 8 00:00:36,880 --> 00:00:44,190 Do you remember if we look at this page on the documentation then we can scroll down and we get a little 9 00:00:44,190 --> 00:00:49,750 code snippet here and what we see is that creating a tensor is very very simple. 10 00:00:49,770 --> 00:00:57,690 We just use the tensor flow module so TAF put a doctorate and then called us tensor method and inside 11 00:00:57,690 --> 00:01:06,780 the parentheses we supply our data and this data here is in the form of an array and we've got two ways 12 00:01:06,780 --> 00:01:14,400 that we can create a tensor either using the square bracket notation like so or providing a shape of 13 00:01:14,400 --> 00:01:20,850 the tens of first and then providing the data in form of an array like so but also providing the shape 14 00:01:21,570 --> 00:01:28,230 when we tested our tensor flow J.S. model in the very beginning of this module just after converting 15 00:01:28,230 --> 00:01:33,150 it to tensor flow jazz we use the double square bracket notation. 16 00:01:33,240 --> 00:01:39,450 So at the very top of our processing jazz file we had some sample data and we had the double square 17 00:01:39,450 --> 00:01:43,440 brackets and then we used that to create our tensor. 18 00:01:43,440 --> 00:01:50,220 So we're gonna stick with this notation but what we're going to do next is we're gonna use the pixel 19 00:01:50,220 --> 00:01:55,920 values that we're grabbing from the canvas instead of the sample data. 20 00:01:56,670 --> 00:02:04,590 So let's create our tensor down here and I want to stored a constant called Capital X to go with the 21 00:02:04,590 --> 00:02:13,330 convention for naming feature vectors and I'm going to use tensor flow with adult and then tensor and 22 00:02:13,380 --> 00:02:21,200 in the parentheses going to supply the pixel values within a pair of square brackets. 23 00:02:21,210 --> 00:02:24,660 That's because we want our tensor to be two dimensional. 24 00:02:24,660 --> 00:02:25,270 Right. 25 00:02:25,320 --> 00:02:32,130 So I've got two pairs of square brackets the one inside pixel values and then one just outside of it 26 00:02:32,280 --> 00:02:34,050 like so. 27 00:02:34,050 --> 00:02:40,980 So now that we have our tensor why don't we log the shape and the data type for this tensor for let's 28 00:02:40,980 --> 00:02:51,580 add two lock statements and the first one is gonna read shape of tensor and that's going to be X dot 29 00:02:51,910 --> 00:02:55,780 shape which is a method from tensor flow jars. 30 00:02:55,930 --> 00:03:02,020 And we're also going to log the data type just so we see what kind of data is actually stored inside 31 00:03:02,200 --> 00:03:03,730 our tensor. 32 00:03:03,790 --> 00:03:10,120 Now if you remember we're hoping that it's gonna be a floating point number but we should verify this 33 00:03:10,210 --> 00:03:13,070 in the logs like so. 34 00:03:13,120 --> 00:03:13,720 All right. 35 00:03:13,780 --> 00:03:19,660 So now that we have our two log statements and we are logging this property here the shape of property 36 00:03:19,780 --> 00:03:29,220 of our tensor and the data type property of our tensor let's save and refresh and head back into our 37 00:03:29,230 --> 00:03:33,910 Web site which should be running on your local host server. 38 00:03:33,940 --> 00:03:40,450 So when I refresh this and I'm going to have my logs here up on the right hand side and when I draw 39 00:03:40,450 --> 00:03:48,490 something in here and then just click check and so now I get a lot of logs but I don't actually see 40 00:03:48,550 --> 00:03:52,850 the log statements that I've just added so I've saved this file. 41 00:03:52,900 --> 00:03:55,290 This is saved but I still don't see it. 42 00:03:55,300 --> 00:04:03,870 So you might have to once again clear browsing data just to be sure that that's not the reason why I 43 00:04:03,880 --> 00:04:06,120 refresh again draw something in here. 44 00:04:07,470 --> 00:04:10,980 Click check answer then I see my two log statements here. 45 00:04:10,980 --> 00:04:17,190 So this is the shape that I'm expecting to see one by seven hundred and eighty four and my tensor is 46 00:04:17,190 --> 00:04:20,730 indeed of data type float 32. 47 00:04:20,730 --> 00:04:22,750 Fantastic. 48 00:04:22,770 --> 00:04:26,560 So now we can use our tensor to make a prediction. 49 00:04:26,640 --> 00:04:31,130 We've actually already seen how to make predictions at the very top of this file. 50 00:04:31,200 --> 00:04:35,970 So the line that I'm talking about is this one right here. 51 00:04:35,970 --> 00:04:41,190 Model don't predict and then the tensor in the parentheses. 52 00:04:41,190 --> 00:04:46,390 That's how we made a prediction based on our sample data at this point. 53 00:04:46,410 --> 00:04:53,350 What I'd like you to do is try and make a prediction based on the data coming from the canvas as a challenge. 54 00:04:53,400 --> 00:05:02,910 What I'd like you to do is reuse this model here and make the prediction inside are predict image method. 55 00:05:02,910 --> 00:05:11,100 I'll give you a few seconds to pause the video before I show you the solution. 56 00:05:11,110 --> 00:05:11,450 All right. 57 00:05:11,470 --> 00:05:12,240 Ready. 58 00:05:12,280 --> 00:05:22,180 So we've loaded our model inside the load model function but we can't use this model inside our predict 59 00:05:22,180 --> 00:05:27,680 image function because of the scope of this variable. 60 00:05:27,700 --> 00:05:33,730 So at the moment it has a local scope it only exists inside load model. 61 00:05:33,730 --> 00:05:40,840 If we wanted to access the model variable inside predict image we would only be able to do that if our 62 00:05:40,840 --> 00:05:43,230 model had a global scope. 63 00:05:43,240 --> 00:05:50,440 So what we need to do is change this variable scope and we can do that simply by defining or declaring 64 00:05:50,440 --> 00:05:51,600 our variable appear. 65 00:05:51,670 --> 00:05:59,440 So VaR model and instead of creating the model down here on line 7 I'm instead assigning it a value 66 00:05:59,920 --> 00:06:01,100 at this point. 67 00:06:01,120 --> 00:06:09,030 So I've created it up here and it's getting the value inside Load model and that promotes it to a global 68 00:06:09,030 --> 00:06:14,200 variable allowing me to use it down here to make my prediction. 69 00:06:14,220 --> 00:06:22,490 So now I can see model not predict and I can supply my capital X feature vector as an input. 70 00:06:22,590 --> 00:06:28,990 I'm actually going to store the result of this prediction in a constant called result. 71 00:06:29,060 --> 00:06:36,240 So I'll set result equal to model not predict and then what I'll do is I'm going to call it result dot 72 00:06:36,270 --> 00:06:43,860 print in order to show what the result is in the console and that completes the challenge. 73 00:06:43,920 --> 00:06:48,350 Let's say the changes come back into chrome and see this works. 74 00:06:48,540 --> 00:06:50,080 So I'm going to refresh. 75 00:06:50,130 --> 00:06:51,700 I'm going to draw something in here. 76 00:06:51,730 --> 00:06:55,340 Click check answer and there at the very bottom. 77 00:06:55,440 --> 00:06:59,540 I should see a log output which gives the prediction of what I drew. 78 00:06:59,580 --> 00:07:00,920 So I draw three. 79 00:07:01,000 --> 00:07:02,700 Let's try again. 80 00:07:02,700 --> 00:07:06,660 So it seems to be working which is great news for us right. 81 00:07:06,780 --> 00:07:08,070 Let's try a few more numbers. 82 00:07:08,160 --> 00:07:12,760 Let's try a 1 Let's try a 4. 83 00:07:12,950 --> 00:07:20,150 Like so let's try a 4 maybe drawn like so that's not bad. 84 00:07:20,150 --> 00:07:21,410 That's pretty encouraging right. 85 00:07:21,800 --> 00:07:26,810 So it seems to be correctly interpreting what it is that we're drawing into our canvas. 86 00:07:26,810 --> 00:07:32,810 Now do you remember the cleanup code that we added with open C.V. at the bottom of our predict image 87 00:07:32,810 --> 00:07:33,530 function. 88 00:07:33,530 --> 00:07:40,070 We had these lines here that deleted all of our open CV objects. 89 00:07:40,070 --> 00:07:46,400 Let's investigate if there's something similar that we have to do with tensor flow light just below 90 00:07:46,400 --> 00:07:47,990 where we're printing our results. 91 00:07:47,990 --> 00:07:58,900 I'm going to add a log statement and this one is going to simply log P.F. dot memory and that's it. 92 00:07:58,910 --> 00:08:04,310 So going to save my changes and then what I'll do is I'll coming out a few of the things that are clogging 93 00:08:04,310 --> 00:08:12,350 up my console someone coming out this line right here where we're logging our scaled array and and then 94 00:08:12,380 --> 00:08:15,770 coming out the pixel values right here. 95 00:08:16,460 --> 00:08:20,400 And I'm comin out these two lines right here. 96 00:08:20,400 --> 00:08:26,540 Now if you're wondering how I'm doing this so quickly it's because I'm using a keyboard shortcut I'm 97 00:08:26,810 --> 00:08:29,570 passing command and then the forward slash. 98 00:08:29,660 --> 00:08:36,770 But if you're working on Windows it'll be the control key and the forward slash on your keyboard. 99 00:08:36,770 --> 00:08:37,040 All right. 100 00:08:37,310 --> 00:08:43,150 So I'm going to save my changes head back into him and refresh. 101 00:08:43,280 --> 00:08:50,710 Now let's see what happens as we are using this Web site and predicting our answer over and over again. 102 00:08:50,720 --> 00:08:58,310 So I want to write a little two here click check and then here's the log coming from TFT memory at the 103 00:08:58,310 --> 00:08:58,790 moment. 104 00:08:58,790 --> 00:09:04,680 I've got 13 tenses and I've got this many bytes in my memory. 105 00:09:04,700 --> 00:09:06,160 Let's try another number. 106 00:09:06,240 --> 00:09:14,060 Quick check answer again and what we see is that I've got two more tenses and the number of bytes has 107 00:09:14,150 --> 00:09:22,400 increased so I'm using more and more memory as I'm checking my answer again and again and hitting that 108 00:09:22,400 --> 00:09:25,700 predict image function in my code. 109 00:09:25,700 --> 00:09:34,070 So that's a problem actually we are bleeding memory as the site is being used more and more and this 110 00:09:34,070 --> 00:09:40,190 suggests that we should manage our memory just like we did with open CV. 111 00:09:40,190 --> 00:09:46,340 If we take a look at this documentation here on tensor flow and we scroll down a little bit you've actually 112 00:09:46,340 --> 00:09:55,010 got this section here on memory and here what it says is that we must manage our memory explicitly and 113 00:09:55,010 --> 00:09:59,870 we can do that by calling this disposed method on our tenses. 114 00:10:00,110 --> 00:10:03,920 In other words calling dispose will free up our memory. 115 00:10:03,920 --> 00:10:09,030 Looking back at our code let's investigate how many tenses we've actually got. 116 00:10:09,080 --> 00:10:16,310 We've got this one right here which we've just created but result actually is also a tensor. 117 00:10:17,300 --> 00:10:22,580 So one of the things we should do is maybe we're we're adding our cleanup code at the end is called 118 00:10:22,640 --> 00:10:31,460 dispose on capital X which is our tensor holding onto our features and we should also call dispose on 119 00:10:31,460 --> 00:10:38,930 the other tensor that we've got like result but this isn't the only place that we've got tense it's 120 00:10:38,930 --> 00:10:39,860 right. 121 00:10:39,860 --> 00:10:46,670 We also have tenses up here namely this one right here and this one right here. 122 00:10:46,670 --> 00:10:53,220 Now given that this is sample data we are actually not going to call this pose on these tenses. 123 00:10:53,270 --> 00:10:58,670 Instead what we're gonna do is we're just gonna delete this code here and we're just going to be left 124 00:10:58,700 --> 00:11:03,040 with loading the model inside the load model function. 125 00:11:03,200 --> 00:11:04,550 So that solves that problem. 126 00:11:05,640 --> 00:11:12,810 Let's save the changes in processing got Jesus and head back into chrome and refresh. 127 00:11:12,810 --> 00:11:20,360 Now let's check what happens as we draw on canvas and check the answers so the first time I've got 11 128 00:11:20,360 --> 00:11:23,740 tenses and I've got this many bytes in memory. 129 00:11:23,960 --> 00:11:30,430 The second time round I still have 11 tenses and I'll have the same number of bytes in memory. 130 00:11:30,500 --> 00:11:33,830 So this is really really promising news right. 131 00:11:33,860 --> 00:11:39,320 We're no longer bleeding memory and we're using tensor flow Jesus as intended. 132 00:11:39,320 --> 00:11:43,010 In other words we're being good citizens and we're cleaning up after ourselves. 133 00:11:43,130 --> 00:11:44,760 So what's left to do now. 134 00:11:44,780 --> 00:11:48,830 Well we still have to write the logic for checking our equation. 135 00:11:48,830 --> 00:11:49,260 Right. 136 00:11:49,280 --> 00:11:53,380 Three plus two is not equal to one as far as I know. 137 00:11:53,450 --> 00:12:01,250 So let's head into this code and make sure we add the logic to actually check that what's being drawn 138 00:12:01,250 --> 00:12:04,160 into the canvas is correct or not. 139 00:12:04,250 --> 00:12:09,810 And that's exactly what we're going to be doing in the next lesson. 140 00:12:09,890 --> 00:12:15,050 But before we finish off this lesson let's do a little bit more cleaning up. 141 00:12:15,050 --> 00:12:20,900 Let's stop creating a new canvas every time we push the button and also let's clean up what's going 142 00:12:20,900 --> 00:12:23,320 on in our console here on the right. 143 00:12:23,330 --> 00:12:27,960 It's getting very very busy and starting to be a little bit distracting. 144 00:12:27,980 --> 00:12:34,430 So what we're going to do is we're simply going to comment out all the long statements that we've previously 145 00:12:34,520 --> 00:12:35,380 added. 146 00:12:35,690 --> 00:12:43,790 And we're also going to delete the testing code that we had at the bottom in order to sort of visualize 147 00:12:44,000 --> 00:12:46,730 what we're doing with the data pre processing. 148 00:12:46,730 --> 00:12:54,470 So I'm just going to comment everything out both in the processing not J.S. and inside the drawing not 149 00:12:54,570 --> 00:13:01,970 J.S. when a comment on all the log statements I encourage you to do the same so that in the next lesson 150 00:13:02,120 --> 00:13:09,560 we can look at the console and just see very very few log statements in there and focus on what's important 151 00:13:09,620 --> 00:13:12,230 and relevant for that lesson. 152 00:13:12,230 --> 00:13:12,770 All right. 153 00:13:12,830 --> 00:13:15,010 I think that's all of them. 154 00:13:15,020 --> 00:13:21,050 Let me just save the changes here and both of these files and when I refresh in hand just make sure 155 00:13:21,050 --> 00:13:27,640 that up looks like I missed this one and this one or did I. 156 00:13:27,680 --> 00:13:29,840 Let me just go to more tools. 157 00:13:29,840 --> 00:13:31,150 Clear the browsing data. 158 00:13:31,790 --> 00:13:40,970 I'm not really sure why I have to do this so often and refresh again and see if I've got a busy console 159 00:13:41,120 --> 00:13:43,780 or a clear console. 160 00:13:43,850 --> 00:13:49,630 So now all I'm seeing here are my predictions from my tensor. 161 00:13:49,790 --> 00:13:51,790 So this is all I want to be left with at this point. 162 00:13:51,950 --> 00:13:57,530 I just want to be left with the predictions from printing out the result and that's it. 163 00:13:57,800 --> 00:14:03,410 So I'll see you in the next lesson where we're gonna be using our prediction and writing our gaming 164 00:14:03,410 --> 00:14:03,830 logic.