TensorFlow Linear Regression: A Layman’s Step by Step Guide

An Easy Way To Understand TensorFlow Linear Regression

Machine learning might sound like a concept ripped from the pages of science fiction, but it’s actually a commonly used application. More specifically, it’s the ability of a system to perform actions without specific instructions from a user through the use of algorithms and statistical models. It’s a subset of AI (artificial intelligence) that’s becoming quite commonplace in business environments. This is because machine learning has proven incredibly useful for marketers due to the massive data sets that even smaller to mid-sized companies have access to.

If you want to take advantage of machine learning in order to make sense of your data and to improve your business decision making, learn how to use TensorFlow. TensorFlow is an open-source software library originally developed by Google. It was developed to conduct machine learning and deep neural networks research.

If you’re interested in learning about machine learning and how you can apply it to your business, a good place to begin is by learning about TensorFlow linear regression. Here  is an overview of TensorFlow and how to use bumpy and matplotlib.pyplot.

A Brief Summary Of Linear Regression

Linear regression is a statistical model used for predictive analysis and modeling.  It’s a form of supervised learning in which existing data is used to train a machine.

The purpose of linear regression is to determine the existing relationship between two variables, allowing you to make a prediction based on that relationship. This is achieved by fitting a linear equation to the data being observed. The two variables that are used include a dependent variable and an independent variable (also referred to as an explanatory or predictor variable).

For example, a company might compare the amount they spent on marketing to their total sales on a yearly basis. The amount spent on marketing would be the independent variable. The linear progression model might show that as their marketing costs went up, so did their sales. Using these numbers, you would be able to predict whether sales will go up or down in the future based on how much you spend on marketing.

What Makes An Equation

To create a linear regression equation, first plot your dependent variable on the y-axis against the independent variable on the x-axis. Using the marketing and sales example, sales would go on the y-axis while marketing costs would go on the x-axis. You would then plot a straight line to measure the correlation between these variables.

Once you’ve plotted your data, create the actual linear regression equation so that you can make your prediction. The equation for linear regression is:

y = Wx + b

This translates as:

Dependent variable (y) = Weights (W) x Independent Variable (x) + Bias (b)

The weights are the vector, the bias is the scalar, and the parameters of the model are the weights and bias. Finally, the hypothesis refers to the relationship that you identify between the x and y data points.

Implementation

Now that you have a better idea of what linear regression is and how to plot a linear regression and form a hypothesis using the linear regression equation, it’s time to learn how to actually implement the linear regression model. The first thing to do is to download and install Python 3, a general purpose, high-level programming language. TensorFlow is Python-friendly and Python has the modules you’ll need to execute linear regression models.

Linear Regression Equation

Generating Dataset

To generate your dataset, import two Python modules — Numpy and Matplotlib. Numpy is a general-purpose array processing package, while Matplotlib is a plotting library. Follow lines 1 through 3 for reference:

Once you’ve imported the required libraries, prepare the dataset for linear regression model training by defining some methods outlined in lines 6 and 7. In line 6, you’ll notice the numbers (0, 2, 100). 100 refers to the number of points you want Numpy to generate, while 0 and 2 refer to the value that you want those points to be generated between. The result will be stored in x_batch.

In line 7, you will set your y-intercept b to 0.5. This will make the data interesting and it will generate for y with a gradient of 1.5 (w) and some form of randomness using np.random.randn(). With this given set of your Numpy, you should be able to generate a dataset (see line 8). Here is an example of how your training data will be visualized:

Constructing Your Graph

Once you’ve imported the required modules and generated your datasets, you can construct a computational graph. A computational graph is a series of TensorFlow operations that are arranged into a graph of nodes (operations are just nodes that represent every piece of data in TensorFlow). At this point, you’re just designing the graph and not actually running it. This essentially means that you’re defining the nodes. Refer to the following for reference:

When constructing your graph, be sure to include the function linear_regression() as it appears in line 1. This will allow you to compute W and b. In the above formula, the x and y nodes are represented in lines 2 and 3 as tf.placeholder (tf being TensorFlow). By declaring the x and y nodes as placeholders, you will need to input values later. Remember, at the moment you’re just designing the graph, not running it.

You will notice that in lines 2 and 3, the data type is defined as float32. Float32 is a commonly used data type for placeholders. You will also notice that the shape of the placeholder in those same lines is set to None. This is because this won’t be determined until it’s time to train the model.

In line 5, the variable scope for the variables named in lines 6 and 7 is defined. Variable scope lets you name the variable in a hierarchical way. This allows variables to be shared throughout different parts of your graph without having to worry about references to the variable being passed around. This function works more smoothly if you name your variables appropriately.

In line 6, you’ll define W as tf.Variable in which the value will change as the model is trained. The variable is set using np.random.normal to obtain a sample from the normal distribution. The function of W right now is to find the gradient of your best line of best fit. You will also want to train your b (as shown in line 7) to prevent the best line of best fit from cutting through the origin and not learning the y-intercept.

Once x, y, and W have been individually defined, they need to be put together. Line 9 is how you implement the formula y = Wx + b. The tensor y_pred represents the predicted value of y; however, it will be far off from the generated y. To determine how far the predicted y is from your generated y, you’ll need to calculate the loss function, which represents the gap between the two. This is done in line 11.

Computing The Graph

Once you’ve constructed your graph using generate_dataset() and linear_regression(), you can run the graph so that you can compute for W and b. Refer to this example to do so:

To begin, call the generate_dataset() in line 2 in order to get x_batch and y_batch. Call the linear_regression() in line 3 to get y, y, y_pred, and loss. In line 5, define the optimizer and request that it minimize the loss in the graph. Here, the Gradient Descent algorithm is chosen and the learning rate is set to 0.1; however, there are a number of different optimizers you can choose from. The purpose of the optimizer is to minimize or maximize your loss function.

To initiate the first session, initialize all of the values that you want the variables to hold. This is done in line 9. Then input a feed_dict argument, which is a session.run () argument that allows the caller to override the tensors’ value in the graph. This is done in line 10, in which the x_batch and y_batch are the values generated and which are ready to fill the placeholders during session.run(). The x and y are the placeholders here.

Once the variables have been initialized and the values for your placeholders have been prepared, define how often you want to adjust and/or train the W and b. This is shown in line 12.  Every full cycle of going through the x and y training data is known as an epoch or a training step. Each epoch/training step is also defined as a one feedforward and one backpropagation. It’s during feedforward that the values of x, W, and b are passed through in order to obtain the predicted y. This will generate the loss. The optimizer will then execute a backpropagation to adjust W and b to help lower the loss the next time feedforward is performed.

Call session.run() with fetches and feed_dict to run your first epoch as shown in line 13. Each tensor will be evaluated in fetches by Session.run(). The values that are in feed_dict will then be substituted for the corresponding input values.

Line 14 will print out the loss at each epoch. Using loss.eval() and feed_dict as argument will calculate the loss value. Following 30 epochs (defined in line 12), W and b will have been trained to perform inference. Inference can be executed using session.run() with the same graph. However, instead of the fetches being train_op, they will be y_pred, which means you’ll only have to feed in x (see lines 16 and 17). Because W and b are already trained, you only need x to compute predicted y.

In lines 19 through 23, you can finally plot your chart using the x_batch and y_batch that were generated along with the predicted line.

The Basic Concept

Linear regression is a basic form of machine learning that can be incredibly helpful in terms of predicting future events and helping to improve business decision making. This was a basic overview of how to implement a linear regression model using TensorFlow. Once you have a better grasp of TensorFlow linear regression, you can begin delving deeper into the TensorFlow capabilities and implement more advanced machine learning models.

Working on a Tensorflow project? Speak with an Expert Today