In this set of exercises, we will go through the basics of Regression Analysis Using [Tensorflow](https://www.tensorflow.org/). By the end of this post, you will be able to perform regression analysis with linearly separable data. It is recommended to check out the (tutorial)[Click here] before starting the exercises.

We will use the ‘mtcars’ built-in data-set.

Before proceeding, it might be helpful to look over the help pages for the `tf$placeholder`

, `tf$multiply`

, `tf$add`

, `tf$global_variables_initializer`

, `tf$Variable`

, `tf$square`

, `tf$train$GradientDescentOptimizer`

.

Answers to the exercises are available here. If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

In order to make things simpler, we will create a new object called ‘data’ where we will assign the two fields from ‘mtcars’ that will be used in this tutorial. Moreover, we will standardize the data-set.

`data = mtcars[c('mpg','hp')]`

`data = scale(data)`

`data = data.frame(data)`

Exercise 1

The first thing we should do is define the independent and dependent variables. Create two placeholders: the input and the label for our regression model.

Hint: The model should be Y = W*X + b.

Exercise 2

Having created the placeholders, we should create the parameters (initialize them at 0) that will be updated during the training. Also, create an initialization operation for the parameters.

Exercise 3

Create the linear regression model using the formula we mentioned before.

Exercise 4

Define a loss function that calculates the mean squared error.

Exercise 5

Use the gradient descent algorithm to train the regression model.

Exercise 6

Now that we have defined all the components of the model, run a session to train it for 1000 epochs. Then, save the parameters to the objects ‘w_value’ and ‘b_value’. To make sure that your training session works, print out the ‘w_value’ and ‘b_value’.

Exercise 7

Plot the data and draw a line that illustrates the model’s formula.

Exercise 8

Do you think that it fits well? I don’t really think so. Let’s create a second degree polynomial regression.

Create a variable named ‘U'(initializes at 0) which will be the parameter of the squared value of the independent variable. Having done that, create an initialization op; then create the new predicting formula.

Hint: it is Y = U * X^2 + W * X + b.

Exercise 9

Train the new model.

Exercise 10

Plot the new model . Is it better?

**Disclaimer:** Plotting is not the best practice to test the wellness of fitness, but we do it because it is a very simple way to see the difference. It also helps you understand the difference between the models.

## Leave a Reply