Predicting Data with TensorFlow
![Vadim Karpusenko](https://static.frontendmasters.com/assets/teachers/karpusenko/thumb.webp)
![A Practical Guide to Machine Learning with TensorFlow 2.0 & Keras](https://static.frontendmasters.com/assets/courses/2020-03-03-practical-machine-learning/thumb.webp)
Check out a free preview of the full A Practical Guide to Machine Learning with TensorFlow 2.0 & Keras course
The "Predicting Data with TensorFlow" Lesson is part of the full, A Practical Guide to Machine Learning with TensorFlow 2.0 & Keras course featured in this preview video. Here's what you'd learn in this lesson:
Vadim demonstrates how to use TensorFlow to calculate the error between two different plot lines: one representing the solution, and the other being a prediction of the solution. The loss function calculates the difference between the solution and the prediction. This section introduces different types of helper functions used to predict data using TensorFlow.
Transcript from the "Predicting Data with TensorFlow" Lesson
[00:00:00]
>> Basically, that's the line we're trying to fit our new line to. So that's the ideal solution, right? So let's say we don't know w and b, and we're just trying to guess, so w_guess. And we should start from somewhere. So let's just assign 0.0 to our w_guess, and b_guess can also be 0.0.
[00:00:29]
So if I will plot new line with those values, So w_guess and b_guess. Our line, yes, I should probably change, let's say, red color and, Yeah. So without specifying anything, it will just use the line. So you can see that's the problem I'm trying to solve. So I had the original data, right?
[00:01:13]
And it was kinda randomly distributed. Let's say I just measured something. There should be some sort of dependency. I'm trying to figure out the kinda physical problem so we can associate it easily. So let's say I'm trying to find the correlation between height and shoe size. There might be some correlation, right?
[00:01:42]
But the data will be still noisy. So some tall people might have smaller feet. [LAUGH] Some people, the opposite. And still, that can be the data we just created. And the line, the green line will show me the true dependency between those. So I can actually, to better meaning to this problem, let's say our w is equal to, So I want height in inches, right?
[00:02:14]
So for instance, Let's play around a little bit with those parameters. So b=10, Our random numbers distributed from 0 to 1. So it means that at some point, we will have people with height equal to 0, or [LAUGH] shoe size equal to 0. All right, probably height and shoe size is the bad example.
[00:02:52]
We need better example. Or maybe just skip the example completely. It's just numbers and just the dependency between one value, those xs and some ys, right? And what we will try to do, we'll try to get this red line to get to the optimal solution, but kinda to get to the green line points.
[00:03:16]
And we will do it by playing around with ws and bs. So we need to somehow figure out the error, how far away we are from the true solution. And we can just simply measure the distance between our points and our line and just add them all together, right?
[00:03:40]
And that's gonna correspond how far we are from the line. So let's do the predict function. So predict function will simply take x, right? And it's not the whole array. It's just, let's say, one number. And what it will do, it will just return y, which is equal to our w_guess multiplied by x and + b_guess.
[00:04:19]
And we will return y, okay? So that's our simple function of prediction. And now I also want to define loss. So I'm already using terminology which we will rely on when we will talk about machine learning examples. So with the loss function, I need to simply figure out the distance between my line and my points.
[00:04:52]
So before I do that, let's introduce additional helper function. So define mean_squared_error, yeah. And what it will do, it will just take all my y predictions and true Ys. Now define mean_squared_error. With mean_squared_error, what I'm doing is just return, I can rely on tensor flow, reduce_mean. So reduce_mean of tensor flow function will just calculate the mean value for all the arguments provided to it.
[00:05:44]
But I want to also use squared. So do we have squared here? That's a lot of it, square, yes. So square will just simply, well, squared our inputs. And I want to just use y_pred-Y. So what's happening here, I will just find the distance between my prediction and true value of Y.
[00:06:20]
I will square it because I don't want it to be negative, right? So squaring it means that I will always get the positive number. And then I will find the mean value of all of those kinda squared distances. So if we, for instance, right now print out, Mean.
[00:06:46]
Actually, let's execute this function first. So printing mean_squared_error for my, I can do something like this, predict, Of X and true Ys. So let's see if it's gonna work. It does, and it basically telling me that right now with my guess of w equal to 0 and b_guess equal to 0 as well, we have pretty huge error.
[00:07:30]
So it's gonna square the distance between my line and those points. For instance, if I modify w, _guess and let's actually put it to exactly to where our original data was. Remember, we set w, I changed it. Let's go to w=0.1 and b=0.5. We create our points, we plot them.
[00:08:07]
Yeah, everything looks good. All those, yeah, error reduced significantly just because I modified those ws and bs, but yeah, it's still kinda some error. If we change w_guess and b_guess to whatever values I've used in the original distribution and then try to calculate this new error, You see that it's dropped significantly.
[00:08:39]
So basically our mean_square_error will be our loss function. It will simply calculate kinda, what is our mistake? How far are we from ideal solution? So if we modify w_guess and b from anywhere from those perfect values, for instance, to 1 and, I don't know, -5, you see that error is only increasing.
[00:09:12]
What I'm trying to do right now is to tell you that for machine learning and for our particular problem where we're trying to hit our red line into this distribution, so kinda to put it into the position of the green line. I need to specify the loss function, which will show me how far I am from the real solution.
[00:09:37]
And in my case, I just use the distance in ys between my red line and corresponding ys of those blue points, right? And I squared this distance to avoid of negatives and just found the average, although I could have easily just sum those distances together as well. It's just gonna lead to a pretty large number.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops