{"id":7009,"date":"2020-11-24T14:15:12","date_gmt":"2020-11-24T08:45:12","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=7009"},"modified":"2022-06-29T11:39:48","modified_gmt":"2022-06-29T06:09:48","slug":"linear-regression-using-tensorflow-with-examples","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/linear-regression-using-tensorflow-with-examples\/","title":{"rendered":"Linear Regression Using TensorFlow with Examples"},"content":{"rendered":"\n<p>TensorFlow is a popular open-source library used for high-end numerical computations in machine learning and deep learning. The major reason for its wide acceptance is on accounts of the library\u2019s support for APIs in various languages. APIs are used to ease the building of models and enhance the performance of project execution. With TensorFlow APIs, you can fully control your computations and execute many machine learning models much faster. The Tensorflow APIs can be broadly classified into two, low-level API and high-level APIs.&nbsp;<\/p>\n\n\n\n<p>Low-level APIs are generally more elementary and detailed, allowing you to have full control of your functions and computations. Low levels APIs allow you to build and optimize your model from scratch. High-level APIs on the other hand are relatively simpler with predefined functions. You can easily execute computations that would require long lines on codes with low-level APIs, in one statement. Simply put, high-level APIs are easier to use.&nbsp;<\/p>\n\n\n\n<p>Some common APIs include Keras and the estimator toolbox. In this tutorial, we will be using the estimator toolbox to build, train, and evaluate a machine learning model. We will streamline our focus to the Linear Regression algorithm and see the various methods that can be used to build the model.&nbsp;<\/p>\n\n\n\n<p>By the end of this tutorial, you will learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What linear regression is<\/li><li>How to build a line of best fit with python<\/li><li>How to build a linear regression model with Tensorflow<\/li><li>What feature columns are<\/li><li>What an input function is and why it is necessary<\/li><li>Understanding what batch sizes, epoch, steps are<\/li><li>How to feed data to your TensorFlow model using Pandas Dataframe<\/li><li>How to feed data to your TensorFlow model using Numpy arrays and dictionaries.&nbsp;<\/li><\/ul>\n\n\n\n<p>Let\u2019s begin with understanding what linear regression is.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Introduction to Linear Regression<\/strong><\/h2>\n\n\n\n<p>A linear regression model is a model that is used to show how two variables are related. The linear regression algorithm seeks to find a line that best fits the two variables and can be used to predict the output of one variable given the other variable. The variable that is being predicted is called the dependent variable whereas the variable that is needed for the prediction is called the independent variable. This kind of model with two variables is called a simple linear regression model.&nbsp;<\/p>\n\n\n\n<p>A system can have more than two variables nevertheless. When only more than two variables are involved, the model is called a multiple linear regression problem.&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s say we are dealing with a simple linear regression model. The independent variable is conventionally denoted by x while the dependent variable is denoted by y. The line that best describes how x is related to y is given by the formula,<\/p>\n\n\n\n<p class=\"has-text-align-center\"><em><strong>y = mx + b<\/strong><\/em><\/p>\n\n\n\n<p>Where y is the dependent variable&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">x is the independent variable\nm is the weight of the regression line\nAnd b is the bias of the regression line\nThe slope m can be found using the formula&nbsp;\nm=(y<sub>2<\/sub>-y<sub>1<\/sub>)\/(x<sub>2<\/sub>-x<sub>1<\/sub>)<\/pre>\n\n\n\n<p>Sometimes, an error term \u03b2 is added to the formula to make up for the fact that x and y cannot always have a linear relationship. The equation of the regression line now becomes&nbsp;<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><em>y = mx + b+ \u03b2<\/em><\/strong><\/p>\n\n\n\n<p>Where it is not added, it implies that knowing x and b are sufficient enough to ascertain the value of y.&nbsp;<\/p>\n\n\n\n<p>The slope of the regression indicates whether the relationship between the dependent and independent variables are positive, negative, or non-existent.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the regression line is flat such that the slope of the line is zero, it means there is no relationship whatsoever between the dependent and independent variables. In other words, an increase in one variable does not affect the other variable.&nbsp;<\/li><li>If the regression line slopes downwards, with the upper end of the line pointing towards the y-axis and the lower end pointing towards the x-axis, it implies that there exists a negative relationship between the dependent and independent variables. In other words, as one variable increases, the other decreases.&nbsp;<\/li><li>If the regression line slopes upwards, such that the upper end of the line points away from the graph and the lower end pointing towards the x or y-intercept, it implies that there exists a positive relationship between the dependent and independent variables. In other words, as one variable increases, the other increases as well.&nbsp;<\/li><\/ul>\n\n\n\n<p>Now, we have a good understanding of what Linear Regression is about, let\u2019s see how to build one in Python.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating a Linear Regression Line in Python<\/strong><\/h2>\n\n\n\n<p>We can create the line of best fit between two variables using python. First, we would need to create some random data for the x and y-axis. To do this we would be needing the NumPy and <a href=\"https:\/\/www.h2kinfosys.com\/blog\/introduction-to-data-visualization-using-matplot\/\">matplotlib libraries<\/a>.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#import the necessary libraries<\/em>\n<strong>import<\/strong> <strong>numpy<\/strong> <strong>as<\/strong> <strong>np<\/strong>\n<strong>from<\/strong> <strong>matplotlib<\/strong> <strong>import<\/strong> pyplot <strong>as<\/strong> plt<\/pre>\n\n\n\n<p>We will define a random seed to ensure the randomly generated numbers remain the same even if the program is run again. This is a good practice to ensure homogeneity in our program.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#define a random seed<\/em>\nnp.random.seed(42)<\/pre>\n\n\n\n<p>At this point, we can now create our randomly generated data for both the x and y axis alongside some noise.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#create random points for the x and y axis from 0 to 20<\/em>\nxs = np.linspace(0, 20, 50)\nys = np.linspace(20, 0, 50)\n\n<em># add some positive and negative noise on the y axis<\/em>\nys += np.random.uniform(-2, 2, 50)\nys += np.random.uniform(-2, 2, 50)<\/pre>\n\n\n\n<p>Plotting the graph, we have<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#plot the graph<\/em>\nplt.plot(xs, ys, '.')<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/GQ7SvUsR04GaSuuLYngqtj38sSYrNK4o6HoTJXVcEUZ2rcGhglqxD3PKIRoFjLfBEtoNLlpGr4kOBqr8Nh-ay9nMzP9eTdU84Bydpe_zcH08eu-PW2hf865ik3ZyYB1Q9jmEsLYIQq2QWintnw\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>Using the formula earline defined, we can calculate the weight and bias of the graph and plot the line of best fit.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#define the weight of the regression line<\/em>\nm = (((np.mean(xs) * np.mean(ys)) - np.mean(xs * ys)) \/\n&nbsp;&nbsp;&nbsp;&nbsp;((np.mean(xs) ** 2) - np.mean(xs ** 2)))\n\n<em>#define the bias of the regression line<\/em>\nb = np.mean(ys) - (m * np.mean(xs))\n\n<em>#define the equation of the regression line<\/em>\nregression_line = [(m * x) + b <strong>for<\/strong> x <strong>in<\/strong> xs]\n\n<em>#plot the graph<\/em>\nplt.plot(xs, ys, '.')\nplt.plot(xs, regression_line)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/H5xw6FfmvD3HpdvbMbKQC9WndZe_LrZWUGFUdkn-5aK6BehfgCvi2aE_yx3-XoRQ9J52gaa-2TXL1hVRl2NdKJ8eCrrMy19o_Jq_jCZofVx3AMjEJaFRQziQzl6kFeAPcm4Is2A9sRmyWh8Kcw\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>And there you have it \u2013 the regression line. The red line represents the line of best fits for the randomly generated data.<\/p>\n\n\n\n<p>But this is a simple scenario where you have just one dependent and independent variable each. In many real-life situations, you will be dealing with more than one independent variable and even more than more dependent variables. For instance, the popular iris dataset has 4 independent variables (sepal length, petal length, sepal width, and sepal width) to determine the dependent variable (the species of the flower).&nbsp;<\/p>\n\n\n\n<p>In such cases, to determine the line of best fit will be difficult using the above method. It would be impossible to even visualize the data since it has 5 variables in total (we can at most, visualize in 3 dimensions). Thus, a more sophisticated approach that involves training and evaluating the model is employed to determine the regression line. Let\u2019s understand how this works.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How Training a Linear Regression Model Works<\/strong><\/h2>\n\n\n\n<p>Let\u2019s say we are dealing with the iris dataset. The independent variables are the sepal length, petal length, sepal width, and petal width, while the dependent variable is the species of the flower. In other words, to predict the species of the flower, you will need to define the petal length, sepal length, petal width, and petal width. The equation for the linear regression line is therefore&nbsp;<\/p>\n\n\n\n<p>y = m1(sepal length) +m2(petal length) +m3(sepal width) +m4(petal width) + b+ \u03b2<\/p>\n\n\n\n<p>Where m1, m2, m3, and m4 are the weights and b is the bias<\/p>\n\n\n\n<p>When training the linear regression algorithm initializes a random number for the weight and bias equation and computes the predicted value for all the observations in the data. After this is done, the error in the prediction is calculated by subtracting the predicted values from the actual values.&nbsp;<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Error = <sub>yactual <\/sub>&#8211; y<sub>pred<\/sub><\/strong><\/p>\n\n\n\n<p>The goal is to attempt to make the error as minimal as possible. This error is technically called the cost function. For linear regression problems, the cost function fondly used is the mean of the sum of the errors. The value is called mean squared error and is mathematically represented as&nbsp;<\/p>\n\n\n\n<p>MSE= 1mi=1nTxi-yi2&nbsp;<\/p>\n\n\n\n<p>Txi is the predicted value while y is the actual value. T is the weight which is altered continuously until the MSE is as minimal as possible. But how is the weight altered?<\/p>\n\n\n\n<p>After the mean squared error has been computed, the weights are calculatedly corrected using an optimizer. There are a plethora of optimizers but the common is the Gradient Descent optimizer. The gradient descent finds the derivative or the gradient by measuring how a change in the weight will affect the error. If the gradient descent is positive, then the weight needs to be reduced. If however, the gradient descent is negative, it implies that the weight must be increased. The process keeps on happening for different weights until the derivative is very close to zero. Each of the processes is called an iteration and the point where the derivative is approximately zero is called the local minimum.&nbsp;<\/p>\n\n\n\n<p>But there\u2019s one thing to note in this process. What informs how much the weight should be changed after each iteration? The idea of gradient descent is better explained with the analogy of a man going down the hill. With him taking giant strides, he will most likely not get to the steepest part of the hill because he will take giant strides when he is just close already. On the other hand, taking baby steps will take a longer time to get to the lowest part of the hill. The best bet is to take giant strides in the starting and reduce it as he goes down the hill.&nbsp;<\/p>\n\n\n\n<p>Bringing it back to the machine learning algorithm, the difference in weight changes is determined by the learning rate. The learning rate determines how large or small the weights should be changed to get to the local minimum quickly. If the learning rate is large, the gradient descent would not get to the local minimum. If it is too small, it will take a lot of time to get there.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/aEl29WCeCeEOdLQLzFb2e4rvL4nLNz0b5pCJ7fCJ1fBzK89IBk1J3hTPr0YSN6xGrY8MHjJQ_SU5wyWSHb-5NqpRNy-iDLxxxMKKq64iqW6jY-f1_bPFgykeDPsMhNWHRpNimTZQ208RYDRfUg\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>&nbsp;Source: <a href=\"https:\/\/builtin.com\/data-science\/gradient-descent\" rel=\"nofollow noopener\" target=\"_blank\">Builtin<\/a><\/p>\n\n\n\n<p>Your learning rate must be carefully defined such that the cost function decreases very rapidly in the first few iterations and stabilizes at some point as seen in the figure below.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/Ju8kqUAeTi2klu_gipjp9JxE3fzzth0Z2z7XepXf604g_Q2xgUqpEE99ypVqAy3JiTZs-Ej_LkNUnmSbdiYHg1MNcM29vnHMlkBzkzw6v03XxULTXo8JdQjl_WiofudZFppsKzABGx8OIfMUWg\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>In the figure above, we can see that the loss stabilizes after the 600<sup>th<\/sup> iteration. That means the algorithm found the local minimum after tweaking the weights 600 times. The model has learned the data and is ready to make predictions for completely new data.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Training a Linear Regression Model with TensorFlow (Example)<\/strong><\/h2>\n\n\n\n<p>In this session, we will go ahead to train a linear regression model using the Tensorflow API, TensorFlow.estimator. We will be using the popular Boston housing dataset for this example. The dataset will be imported from Scikit learn dataset repository.&nbsp;<\/p>\n\n\n\n<p>We will start by importing the necessary libraries<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#import necessary libraries<\/em>\n<strong>import<\/strong> <strong>pandas<\/strong> <strong>as<\/strong> <strong>pd<\/strong>\n<strong>import<\/strong> <strong>numpy<\/strong> <strong>as<\/strong> <strong>np<\/strong>\n<strong>import<\/strong> <strong>tensorflow<\/strong> <strong>as<\/strong> <strong>tf<\/strong>\n<strong>from<\/strong> <strong>sklearn.datasets<\/strong> <strong>import<\/strong> load_boston<\/pre>\n\n\n\n<p>Then we\u2019d go-ahead to load the dataset<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#load the dataset<\/em>\nboston = load_boston()<\/pre>\n\n\n\n<p>A dataset is a form of a dictionary where the keys are a list of information that can be extracted from the data. To check what the dataset is about, we use the DESCR method.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#check the description of the dataset<\/em>\n<strong>print<\/strong>(boston.DESCR)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Boston house prices dataset\n---------------------------\n\n**Data Set Characteristics:**&nbsp;&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp;:Number of Instances: 506&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp;:Number of Attributes: 13 numeric\/categorical predictive. Median Value (attribute 14) <strong>is<\/strong> usually the target.\n\n&nbsp;&nbsp;&nbsp;&nbsp;:Attribute Information (<strong>in<\/strong> order):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- CRIM &nbsp; &nbsp; per capita crime rate by town\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ZN &nbsp; &nbsp; &nbsp; proportion of residential land zoned <strong>for<\/strong> lots over 25,000 sq.ft.\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- INDUS&nbsp; &nbsp; proportion of non-retail business acres per town\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- CHAS &nbsp; &nbsp; Charles River dummy variable (= 1 <strong>if<\/strong> tract bounds river; 0 otherwise)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- NOX&nbsp; &nbsp; &nbsp; nitric oxides concentration (parts per 10 million)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- RM &nbsp; &nbsp; &nbsp; average number of rooms per dwelling\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- AGE&nbsp; &nbsp; &nbsp; proportion of owner-occupied units built prior to 1940\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- DIS&nbsp; &nbsp; &nbsp; weighted distances to five Boston employment centres\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- RAD&nbsp; &nbsp; &nbsp; index of accessibility to radial highways\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- TAX&nbsp; &nbsp; &nbsp; full-value property-tax rate per $10,000\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- PTRATIO&nbsp; pupil-teacher ratio by town\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- B&nbsp; &nbsp; &nbsp; &nbsp; 1000(Bk - 0.63)^2 where Bk <strong>is<\/strong> the proportion of blacks by town\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- LSTAT&nbsp; &nbsp; % lower status of the population\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- MEDV &nbsp; &nbsp; Median value of owner-occupied homes <strong>in<\/strong> $1000's\n\n&nbsp;&nbsp;&nbsp;&nbsp;:Missing Attribute Values: None\n\n&nbsp;&nbsp;&nbsp;&nbsp;:Creator: Harrison, D. <strong>and<\/strong> Rubinfeld, D.L.\n\nThis <strong>is<\/strong> a copy of UCI ML housing dataset.\nhttps:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/housing\/<\/pre>\n\n\n\n<p>As seen above, the dataset shows the median values of owner-occupied homes in $1000\u2019s given various attributes such as per capita crime rate by town, the proportion of residential land zoned for lots over 25000 square feet, and the others. Let\u2019s see what the dataset looks like<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#convert the dataset into a dataframe<\/em>\ndf = pd.DataFrame(boston.data, columns=boston.feature_names)\n<em>#print the first 5 rows of the dataframe<\/em>\n<strong>print<\/strong>(df.head())\n<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CRIM&nbsp; &nbsp; ZN&nbsp; INDUS&nbsp; CHAS&nbsp; &nbsp; NOX &nbsp; &nbsp; RM &nbsp; AGE &nbsp; &nbsp; DIS&nbsp; RAD&nbsp; &nbsp; TAX&nbsp; \\\n0&nbsp; 0.00632&nbsp; 18.0 &nbsp; 2.31 &nbsp; 0.0&nbsp; 0.538&nbsp; 6.575&nbsp; 65.2&nbsp; 4.0900&nbsp; 1.0&nbsp; 296.0&nbsp;&nbsp;&nbsp;\n1&nbsp; 0.02731 &nbsp; 0.0 &nbsp; 7.07 &nbsp; 0.0&nbsp; 0.469&nbsp; 6.421&nbsp; 78.9&nbsp; 4.9671&nbsp; 2.0&nbsp; 242.0&nbsp;&nbsp;&nbsp;\n2&nbsp; 0.02729 &nbsp; 0.0 &nbsp; 7.07 &nbsp; 0.0&nbsp; 0.469&nbsp; 7.185&nbsp; 61.1&nbsp; 4.9671&nbsp; 2.0&nbsp; 242.0&nbsp;&nbsp;&nbsp;\n3&nbsp; 0.03237 &nbsp; 0.0 &nbsp; 2.18 &nbsp; 0.0&nbsp; 0.458&nbsp; 6.998&nbsp; 45.8&nbsp; 6.0622&nbsp; 3.0&nbsp; 222.0&nbsp;&nbsp;&nbsp;\n4&nbsp; 0.06905 &nbsp; 0.0 &nbsp; 2.18 &nbsp; 0.0&nbsp; 0.458&nbsp; 7.147&nbsp; 54.2&nbsp; 6.0622&nbsp; 3.0&nbsp; 222.0&nbsp;&nbsp;&nbsp;\n\n&nbsp;&nbsp;&nbsp;PTRATIO &nbsp; &nbsp; &nbsp; B&nbsp; LSTAT&nbsp;&nbsp;\n0 &nbsp; &nbsp; 15.3&nbsp; 396.90 &nbsp; 4.98&nbsp;&nbsp;\n1 &nbsp; &nbsp; 17.8&nbsp; 396.90 &nbsp; 9.14&nbsp;&nbsp;\n2 &nbsp; &nbsp; 17.8&nbsp; 392.83 &nbsp; 4.03&nbsp;&nbsp;\n3 &nbsp; &nbsp; 18.7&nbsp; 394.63 &nbsp; 2.94&nbsp;&nbsp;\n4 &nbsp; &nbsp; 18.7&nbsp; 396.90 &nbsp; 5.33&nbsp;<\/pre>\n\n\n\n<p>The target column is separated and needs to be added to the dataframe. This is done using fancy indexing,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#add the target column to the dataframe<\/em>\ndf['MEDV'] = boston.target&nbsp;\n<em>#print the first 5 rows of the data frame<\/em>\n<strong>print<\/strong>(df.head())\n<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CRIM&nbsp; &nbsp; ZN&nbsp; INDUS&nbsp; CHAS&nbsp; &nbsp; NOX &nbsp; &nbsp; RM &nbsp; AGE &nbsp; &nbsp; DIS&nbsp; RAD&nbsp; &nbsp; TAX&nbsp; \\\n0&nbsp; 0.00632&nbsp; 18.0 &nbsp; 2.31 &nbsp; 0.0&nbsp; 0.538&nbsp; 6.575&nbsp; 65.2&nbsp; 4.0900&nbsp; 1.0&nbsp; 296.0&nbsp;&nbsp;&nbsp;\n1&nbsp; 0.02731 &nbsp; 0.0 &nbsp; 7.07 &nbsp; 0.0&nbsp; 0.469&nbsp; 6.421&nbsp; 78.9&nbsp; 4.9671&nbsp; 2.0&nbsp; 242.0&nbsp;&nbsp;&nbsp;\n2&nbsp; 0.02729 &nbsp; 0.0 &nbsp; 7.07 &nbsp; 0.0&nbsp; 0.469&nbsp; 7.185&nbsp; 61.1&nbsp; 4.9671&nbsp; 2.0&nbsp; 242.0&nbsp;&nbsp;&nbsp;\n3&nbsp; 0.03237 &nbsp; 0.0 &nbsp; 2.18 &nbsp; 0.0&nbsp; 0.458&nbsp; 6.998&nbsp; 45.8&nbsp; 6.0622&nbsp; 3.0&nbsp; 222.0&nbsp;&nbsp;&nbsp;\n4&nbsp; 0.06905 &nbsp; 0.0 &nbsp; 2.18 &nbsp; 0.0&nbsp; 0.458&nbsp; 7.147&nbsp; 54.2&nbsp; 6.0622&nbsp; 3.0&nbsp; 222.0&nbsp;&nbsp;&nbsp;\n\n&nbsp;&nbsp;&nbsp;PTRATIO &nbsp; &nbsp; &nbsp; B&nbsp; LSTAT&nbsp; MEDV&nbsp;&nbsp;\n0 &nbsp; &nbsp; 15.3&nbsp; 396.90 &nbsp; 4.98&nbsp; 24.0&nbsp;&nbsp;\n1 &nbsp; &nbsp; 17.8&nbsp; 396.90 &nbsp; 9.14&nbsp; 21.6&nbsp;&nbsp;\n2 &nbsp; &nbsp; 17.8&nbsp; 392.83 &nbsp; 4.03&nbsp; 34.7&nbsp;&nbsp;\n3 &nbsp; &nbsp; 18.7&nbsp; 394.63 &nbsp; 2.94&nbsp; 33.4&nbsp;\n4 &nbsp; &nbsp; 18.7&nbsp; 396.90 &nbsp; 5.33&nbsp; 36.2&nbsp;<\/pre>\n\n\n\n<p>You can input your data to the tf.estimator method using various means. In this tutorial, we will use 2 different methods to pass the data into the tensorflow.estimator method: using pandas dataframe, using numpy arrays. There are other ways to feed the data into your tensorflow model but we will limit this tutorial to these two methods. Let\u2019s begin with using pandas.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Using Pandas<\/strong><\/h4>\n\n\n\n<p>Step 1: Specify the feature and target columns<\/p>\n\n\n\n<p>For easy accessibility, the dataset will be split into columns containing the features and the column containing the target.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#define the feature columns and target columns<\/em>\nfeatures = df[boston.feature_names]\ntarget = 'MEDV'\n<\/pre>\n\n\n\n<p>Step 2: Define the estimator<\/p>\n\n\n\n<p>Just before you define your model or estimator, TensorFlow requires you to define what is called feature columns. Feature columns is a data preprocessing step that transforms raw data into a form that can be understood by the TensorFlow estimator. You may see the feature_columns as a bridge between the raw data and the TensorFlow estimator. Note that only the features need to be passed and not the target column.&nbsp;<\/p>\n\n\n\n<p>You can transform the feature columns with tensorflow using tf.feature_columns(). Since all the columns contain continuous numbers, the numeric_column() method will be used. To transform all the columns in one line of code, you can use a list comprehension.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#convert the feature columns into Tensorflow numeric column<\/em>\nfeature_columns = [tf.feature_column.numeric_column(i) <strong>for<\/strong> i <strong>in<\/strong> features]<\/pre>\n\n\n\n<p>If however, the dataset contains categorical features, you will need to convert that using other methods such as the categorical)column_with_vocabulary_list() or the indicator_column().&nbsp;<\/p>\n\n\n\n<p>After defining the feature columns, you can define your estimator. The estimator would require 2 arguments, the feature columns (which we just defined) and the model directory where the model parameters and graph will be stored. We will name the model directory \u2018LinRegTrain\u2019<\/p>\n\n\n\n<p>There are 6 estimators in the tensorflow.estimator method &#8211; 3 each for regression and classification problems.&nbsp;<\/p>\n\n\n\n<p>For regression problems, you may select<\/p>\n\n\n\n<p>1. LinearRegressor<\/p>\n\n\n\n<p>2. DNNRegressor<\/p>\n\n\n\n<p>3. DNNLineaCombinedRegressor<\/p>\n\n\n\n<p>For classification problems, you may select<\/p>\n\n\n\n<p>1. LinearClassifier<\/p>\n\n\n\n<p>2. DNNClassifier<\/p>\n\n\n\n<p>3. DNNLineaCombinedClassifier<\/p>\n\n\n\n<p>For this tutorial, we shall be using the LinearRegressor estimator. We can call the method using the tf.estimator.LinearRegressor method.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#define the linear regression estimator<\/em>\n\nestimator = tf.estimator.LinearRegressor(feature_columns=feature_columns, model_dir='LinRegTrain')\n<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INFO:tensorflow:Using default config.\nINFO:tensorflow:Using config: {'_model_dir': 'LinRegTrain', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true\ngraph_options {\n&nbsp;&nbsp;rewrite_options {\n&nbsp;&nbsp;&nbsp;&nbsp;meta_optimizer_iterations: ONE\n&nbsp;&nbsp;}\n}\n, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_service': None, '_cluster_spec': &lt;tensorflow.python.training.server_lib.ClusterSpec object at 0x00000146D467B908&gt;, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}<\/pre>\n\n\n\n<p>Step 3: Split the data into train and test data<\/p>\n\n\n\n<p>We will need to split the data into train data and test data. The model will be trained with the training dataset while it would be evaluated using the test dataset. First, you\u2019d need to create a training size which is set to 75% of the entire observation. The train and test data were then specified using the iloc attribute.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;<em>#define the training size of the dataset to be 75%<\/em>\ntraining_size = int(len(df) * 0.75)\n\n<em>#define the train and test data<\/em>\ntrain = df.iloc[:training_size, :]\ntest = df.iloc[training_size:, :]<\/pre>\n\n\n\n<p>Step 4: Train the model.&nbsp;<\/p>\n\n\n\n<p>The next step is to go ahead to train the model. It can be done using the estimator.train()<\/p>\n\n\n\n<p>The method takes arguments that include the input function, the X data (features), y data (labels), batch size, number of epochs, shuffling, etc. These terms may sound alien but let&#8217;s take out time to demystify them one at a time.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Input function: Tensorflow estimators take in data through what is called an input function. Tensorflow deals with tensors and so, all forms of data (it could be streaming data, in-memory data, custom data, etc) must be converted into tensors. The input function generates tensors from the raw data and supplies them to the TensorFlow estimator. The input function also configures how the model trains or evaluates the data. This is why you&#8217;d also need to define the batch size, the number of epochs, shuffling, etc. We&#8217;d discuss these terms momentarily.&nbsp;<\/li><\/ol>\n\n\n\n<p>The input data needed for the input function can either be a NumPy or pandas. In this method, the input data will be created as a pandas dataframe. We&#8217;d discuss how to use NumPy in the next method.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Batch size: Tensorflow was designed to accommodate large datasets and parallel computing. When working with large datasets, you can train the data on various computers (parallel computing) or decide to use one computer if you don\u2019t have the resources. If you&#8217;re using one computer to train a large dataset, it is impossible to expose your model to all the data at once. Your computer\u2019s memory will run out of space. This is where defining your batch size comes into play.&nbsp;<\/li><\/ol>\n\n\n\n<p>The batch size helps to feed your data to the model in batches. If you have data with 5000 observations and you define a batch size of 100, the data will be split into 100 places. That means 50 observations will be fed to your model per iteration.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Epoch: An epoch is the term used when the model has been exposed to all the data. If the epoch is set to 2, it means the model will be exposed to the data twice. The second time, it uses different weights with the aim of reducing the loss.&nbsp;<\/li><\/ol>\n\n\n\n<p>By default, the epoch is set to None. If you leave it this way, the model will see the data just once. That is, it will end after all the batches of the data have been fed to the model. You could define a parameter called steps. The number of steps is simply the number of iterations you want.<\/p>\n\n\n\n<p>If you have 5000 observations and you set the batch size to be 100. It means it will take 50 iterations to see all the data. Setting the epoch to 4 will require 4 \u00d7 50 iterations. Another way of doing this is to set the steps argument to 200 and leave the number of epochs as None. It will run, 200 iterations.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Shuffling: It is always a good idea to shuffle your data during training. This will ensure your model does not learn specific patterns in your data hook, line, and sinker. When it does learn patterns as it is, your model would not make good predictions even though it learns the data pretty well during training. This is called overfitting your model and it must be avoided.&nbsp;<\/li><\/ol>\n\n\n\n<p>Now we understand the useful parameters when creating an input function, let&#8217;s create an input function. Remember we are using the pandas_input function here.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>'''This function defines the input function for the dataset'''<\/em>\n<strong>def<\/strong> input_fn(dataset, batch_size=128, num_epochs=None, shuffle=True):\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong> tf.estimator.inputs.pandas_input_fn(\n&nbsp;&nbsp;&nbsp;&nbsp;x = dataset[boston.feature_names],\n&nbsp;&nbsp;&nbsp;&nbsp;y = dataset['MEDV'],\n&nbsp;&nbsp;&nbsp;&nbsp;batch_size=batch_size,\n&nbsp;&nbsp;&nbsp;&nbsp;num_epochs=num_epochs,\n&nbsp;&nbsp;&nbsp;&nbsp;shuffle=shuffle\n&nbsp;&nbsp;&nbsp;&nbsp;)\n<\/pre>\n\n\n\n<p>And then, we can finally train the model.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#train the model with 2000 steps<\/em>\nestimator.train(input_fn=input_fn(train, num_epochs=None), steps=2000)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INFO:tensorflow:Calling model_fn.\nINFO:tensorflow:Done calling model_fn.\nINFO:tensorflow:Create CheckpointSaverHook.\nINFO:tensorflow:Graph was finalized.\nINFO:tensorflow:Restoring parameters <strong>from<\/strong> <strong>LinRegTrain<\/strong>\\model.ckpt-19081\nINFO:tensorflow:Running local_init_op.\nINFO:tensorflow:Done running local_init_op.\nINFO:tensorflow:Saving checkpoints <strong>for<\/strong> 19081 into LinRegTrain\\model.ckpt.\nINFO:tensorflow:loss = 2068.8728, step = 19082\nINFO:tensorflow:global_step\/sec: 159.792\nINFO:tensorflow:loss = 3538.3533, step = 19182 (0.631 sec)\nINFO:tensorflow:global_step\/sec: 200.115\nINFO:tensorflow:loss = 1659.8722, step = 19282 (0.500 sec)\nINFO:tensorflow:global_step\/sec: 203.782\nINFO:tensorflow:loss = 3306.3003, step = 19382 (0.491 sec)\nINFO:tensorflow:global_step\/sec: 207.158\nINFO:tensorflow:loss = 3048.774, step = 19482 (0.483 sec)\nINFO:tensorflow:global_step\/sec: 206.305\nINFO:tensorflow:loss = 3164.751, step = 19582 (0.485 sec)\nINFO:tensorflow:global_step\/sec: 208.453\nINFO:tensorflow:loss = 2899.857, step = 19682 (0.481 sec)\nINFO:tensorflow:global_step\/sec: 206.728\nINFO:tensorflow:loss = 3106.3613, step = 19782 (0.482 sec)\nINFO:tensorflow:global_step\/sec: 200.517\nINFO:tensorflow:loss = 2640.4854, step = 19882 (0.499 sec)\nINFO:tensorflow:global_step\/sec: 202.545\nINFO:tensorflow:loss = 2857.7683, step = 19982 (0.494 sec)\nINFO:tensorflow:global_step\/sec: 204.616\nINFO:tensorflow:loss = 2167.958, step = 20082 (0.489 sec)\nINFO:tensorflow:global_step\/sec: 204.198\nINFO:tensorflow:loss = 2442.528, step = 20182 (0.491 sec)\nINFO:tensorflow:global_step\/sec: 207.588\nINFO:tensorflow:loss = 2945.9646, step = 20282 (0.482 sec)\nINFO:tensorflow:global_step\/sec: 210.646\nINFO:tensorflow:loss = 3567.1733, step = 20382 (0.477 sec)\nINFO:tensorflow:global_step\/sec: 211.093\nINFO:tensorflow:loss = 3195.5977, step = 20482 (0.472 sec)\nINFO:tensorflow:global_step\/sec: 205.035\nINFO:tensorflow:loss = 2235.641, step = 20582 (0.488 sec)\nINFO:tensorflow:global_step\/sec: 208.453\nINFO:tensorflow:loss = 2183.0503, step = 20682 (0.480 sec)\nINFO:tensorflow:global_step\/sec: 206.729\nINFO:tensorflow:loss = 3767.7236, step = 20782 (0.484 sec)\nINFO:tensorflow:global_step\/sec: 198.135\nINFO:tensorflow:loss = 3152.8857, step = 20882 (0.507 sec)\nINFO:tensorflow:global_step\/sec: 182.586\nINFO:tensorflow:loss = 2599.474, step = 20982 (0.546 sec)\nINFO:tensorflow:Saving checkpoints <strong>for<\/strong> 21081 into LinRegTrain\\model.ckpt.\nINFO:tensorflow:Loss <strong>for<\/strong> final step: 2783.6982.<\/pre>\n\n\n\n<p>Step 5: Evaluate the model<\/p>\n\n\n\n<p>Evaluating the models enables you to check how well the model can make predictions. Just as it is when training the model, you&#8217;d require an input function when evaluating the model as well.&nbsp;<\/p>\n\n\n\n<p>It&#8217;s good practice to use the same input function you used when training the model, to evaluate the model. You&#8217;d however change the data passed in the input function. This time, the test data.&nbsp;<\/p>\n\n\n\n<p>&nbsp;Let&#8217;s evaluate the model&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#evaluate the model<\/em>\nevaluation = estimator.evaluate(input_fn=input_fn(test, num_epochs=10, shuffle=True))<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INFO:tensorflow:Calling model_fn.\nINFO:tensorflow:Done calling model_fn.\nINFO:tensorflow:Starting evaluation at 2020-11-09T23:06:48Z\nINFO:tensorflow:Graph was finalized.\nINFO:tensorflow:Restoring parameters <strong>from<\/strong> <strong>LinRegTrain<\/strong>\\model.ckpt-21081\nINFO:tensorflow:Running local_init_op.\nINFO:tensorflow:Done running local_init_op.\nINFO:tensorflow:Finished evaluation at 2020-11-09-23:06:49\nINFO:tensorflow:Saving dict <strong>for<\/strong> <strong>global<\/strong> step 21081: average_loss = 43.917557, global_step = 21081, label\/mean = 14.948031, loss = 5577.53, prediction\/mean = 18.892693\nINFO:tensorflow:Saving 'checkpoint_path' summary <strong>for<\/strong> <strong>global<\/strong> step 21081: LinRegTrain\\model.ckpt-21081<\/pre>\n\n\n\n<p>The model has a loss of $5577. To put this in perspective, let&#8217;s see the average price for a house according to the data.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">train['MEDV'].describe()<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">count&nbsp; &nbsp; 379.000000\nmean&nbsp; &nbsp; &nbsp; 25.074406\nstd&nbsp; &nbsp; &nbsp; &nbsp; 8.801969\nmin &nbsp; &nbsp; &nbsp; 11.800000\n25% &nbsp; &nbsp; &nbsp; 19.400000\n50% &nbsp; &nbsp; &nbsp; 22.800000\n75% &nbsp; &nbsp; &nbsp; 28.700000\nmax &nbsp; &nbsp; &nbsp; 50.000000\nName: MEDV, dtype: float64\n<\/pre>\n\n\n\n<p>As seen above, the average price for a house is $25,000. The model\u2019s performance can however still be tweaked by changing the training parameters such as the number of epochs, batch size, etc.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Using Numpy Arrays<\/strong><\/h4>\n\n\n\n<p>We can also feed the data into the tensorflow estimator using numpy arrays. We start by splitting the data into train and test data.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#define the training size of the dataset to be 75%<\/em>\ntraining_size = int(len(df) * 0.75)\n\n<em># #define the train and test data<\/em>\ntrain = df[:training_size].values\ntest = df[training_size:].values<\/pre>\n\n\n\n<p>The values attribute converts the data frame into a numpy array. The train and test data is then split into X_train, X_test, y_test, y_split<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>def<\/strong> prepare_data(df):\n&nbsp;&nbsp;&nbsp;&nbsp;<em>\"\"\"This function splits the data frame into X and y data\"\"\"<\/em>\n&nbsp;&nbsp;&nbsp;&nbsp;X = df[:, :-1]&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;y = df[:,-1]\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong> X, y\n\n<em>#define the X_train, y_train, X_test, y_test data<\/em>\nX_train, y_train = prepare_data(train)\nX_test, y_test = prepare_data(test)<\/pre>\n\n\n\n<p>The feature columns can now be defined using the code below.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#convert the feature columns into Tensorflow numeric column<\/em>\nfeature_columns = [tf.feature_column.numeric_column('x', shape=X_train.shape[1:])]<\/pre>\n\n\n\n<p>And now, the model can be trained using the feature columns defined above.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#define the linear regression estimator<\/em>\nestimator = tf.estimator.LinearRegressor(feature_columns=feature_columns, model_dir='LinRegTrain1')<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INFO:tensorflow:Using default config.\nINFO:tensorflow:Using config: {'_model_dir': 'LinRegTrain1', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true\ngraph_options {\n&nbsp;&nbsp;rewrite_options {\n&nbsp;&nbsp;&nbsp;&nbsp;meta_optimizer_iterations: ONE\n&nbsp;&nbsp;}\n}\n, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_service': None, '_cluster_spec': &lt;tensorflow.python.training.server_lib.ClusterSpec object at 0x000001B878B46278&gt;, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}\n<\/pre>\n\n\n\n<p>Afterward, the input function for the train dataset is defined.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#define the train input function<\/em>\ntrain_input_fn = tf.estimator.inputs.numpy_input_fn(\n&nbsp;&nbsp;&nbsp;&nbsp;x = {'x': X_train},\n&nbsp;&nbsp;&nbsp;&nbsp;y = y_train,\n&nbsp;&nbsp;&nbsp;&nbsp;batch_size=128,\n&nbsp;&nbsp;&nbsp;&nbsp;num_epochs=None,\n&nbsp;&nbsp;&nbsp;&nbsp;shuffle=True,\n&nbsp;&nbsp;&nbsp;&nbsp;)\n<\/pre>\n\n\n\n<p>Now, the model can be trained with the input function defined above.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#train the model<\/em>\nestimator.train(input_fn=train_input_fn, steps=5000)\n<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INFO:tensorflow:Calling model_fn.\nINFO:tensorflow:Done calling model_fn.\nINFO:tensorflow:Create CheckpointSaverHook.\nINFO:tensorflow:Graph was finalized.\nINFO:tensorflow:Restoring parameters <strong>from<\/strong> <strong>LinRegTrain1<\/strong>\\model.ckpt-31500\nINFO:tensorflow:Running local_init_op.\nINFO:tensorflow:Done running local_init_op.\nINFO:tensorflow:Saving checkpoints <strong>for<\/strong> 31500 into LinRegTrain1\\model.ckpt.\nINFO:tensorflow:loss = 2298.775, step = 31501\nINFO:tensorflow:global_step\/sec: 529.409\nINFO:tensorflow:loss = 3787.5515, step = 31601 (0.192 sec)\nINFO:tensorflow:global_step\/sec: 637.311\nINFO:tensorflow:loss = 4150.9785, step = 31701 (0.157 sec)\nINFO:tensorflow:global_step\/sec: 515.754\nINFO:tensorflow:loss = 2896.7476, step = 31801 (0.193 sec)\nINFO:tensorflow:global_step\/sec: 592.061\nINFO:tensorflow:loss = 2679.3975, step = 31901 (0.169 sec)\nINFO:tensorflow:global_step\/sec: 621.468\nINFO:tensorflow:loss = 2945.3008, step = 32001 (0.162 sec)\nINFO:tensorflow:global_step\/sec: 671.53\nINFO:tensorflow:loss = 3135.522, step = 32101 (0.148 sec)\nINFO:tensorflow:global_step\/sec: 645.523\nINFO:tensorflow:loss = 1737.3533, step = 32201 (0.155 sec)\nINFO:tensorflow:global_step\/sec: 667.063\nINFO:tensorflow:loss = 2122.1785, step = 32301 (0.150 sec)\nINFO:tensorflow:global_step\/sec: 625.357\nINFO:tensorflow:loss = 2111.141, step = 32401 (0.160 sec)\nINFO:tensorflow:global_step\/sec: 667.048\nINFO:tensorflow:loss = 3291.2444, step = 32501 (0.151 sec)\nINFO:tensorflow:global_step\/sec: 667.05\nINFO:tensorflow:loss = 4172.6313, step = 32601 (0.150 sec)\nINFO:tensorflow:global_step\/sec: 667.041\nINFO:tensorflow:loss = 3981.296, step = 32701 (0.149 sec)\nINFO:tensorflow:global_step\/sec: 662.638\nINFO:tensorflow:loss = 3058.733, step = 32801 (0.151 sec)\nINFO:tensorflow:global_step\/sec: 690.052\nINFO:tensorflow:loss = 2693.0422, step = 32901 (0.146 sec)\nINFO:tensorflow:global_step\/sec: 667.048\nINFO:tensorflow:loss = 3583.536, step = 33001 (0.151 sec)\nINFO:tensorflow:global_step\/sec: 667.046\nINFO:tensorflow:loss = 2732.2446, step = 33101 (0.150 sec)\nINFO:tensorflow:global_step\/sec: 694.842\nINFO:tensorflow:loss = 1517.0491, step = 33201 (0.143 sec)\nINFO:tensorflow:global_step\/sec: 699.704\nINFO:tensorflow:loss = 3136.3606, step = 33301 (0.142 sec)\nINFO:tensorflow:global_step\/sec: 637.307\nINFO:tensorflow:loss = 2074.9668, step = 33401 (0.157 sec)\nINFO:tensorflow:global_step\/sec: 690.049\nINFO:tensorflow:loss = 2896.9585, step = 33501 (0.145 sec)\nINFO:tensorflow:global_step\/sec: 645.534\nINFO:tensorflow:loss = 3446.4941, step = 33601 (0.156 sec)\nINFO:tensorflow:global_step\/sec: 595.577\nINFO:tensorflow:loss = 3641.3157, step = 33701 (0.168 sec)\nINFO:tensorflow:global_step\/sec: 602.755\nINFO:tensorflow:loss = 2726.9165, step = 33801 (0.166 sec)\nINFO:tensorflow:global_step\/sec: 625.359\nINFO:tensorflow:loss = 2953.2432, step = 33901 (0.163 sec)\nINFO:tensorflow:global_step\/sec: 617.635\nINFO:tensorflow:loss = 1818.0906, step = 34001 (0.158 sec)\nINFO:tensorflow:global_step\/sec: 671.529\nINFO:tensorflow:loss = 3186.2725, step = 34101 (0.149 sec)\nINFO:tensorflow:global_step\/sec: 667.047\nINFO:tensorflow:loss = 3824.3748, step = 34201 (0.150 sec)\nINFO:tensorflow:global_step\/sec: 461.072\nINFO:tensorflow:loss = 2467.5938, step = 34301 (0.221 sec)\nINFO:tensorflow:global_step\/sec: 446.707\nINFO:tensorflow:loss = 4125.1543, step = 34401 (0.220 sec)\nINFO:tensorflow:global_step\/sec: 483.367\nINFO:tensorflow:loss = 2444.977, step = 34501 (0.212 sec)\nINFO:tensorflow:global_step\/sec: 555.873\nINFO:tensorflow:loss = 2906.3044, step = 34601 (0.176 sec)\nINFO:tensorflow:global_step\/sec: 581.73\nINFO:tensorflow:loss = 2992.641, step = 34701 (0.171 sec)\nINFO:tensorflow:global_step\/sec: 456.846\nINFO:tensorflow:loss = 2374.501, step = 34801 (0.222 sec)\nINFO:tensorflow:global_step\/sec: 529.453\nINFO:tensorflow:loss = 2264.888, step = 34901 (0.187 sec)\nINFO:tensorflow:global_step\/sec: 481.045\nINFO:tensorflow:loss = 2630.542, step = 35001 (0.208 sec)\nINFO:tensorflow:global_step\/sec: 529.405\nINFO:tensorflow:loss = 3225.4219, step = 35101 (0.190 sec)\nINFO:tensorflow:global_step\/sec: 492.893\nINFO:tensorflow:loss = 1567.6238, step = 35201 (0.202 sec)\nINFO:tensorflow:global_step\/sec: 431.279\nINFO:tensorflow:loss = 3448.526, step = 35301 (0.234 sec)\nINFO:tensorflow:global_step\/sec: 629.291\nINFO:tensorflow:loss = 2485.2834, step = 35401 (0.157 sec)\nINFO:tensorflow:global_step\/sec: 653.975\nINFO:tensorflow:loss = 2805.2188, step = 35501 (0.152 sec)\nINFO:tensorflow:global_step\/sec: 667.048\nINFO:tensorflow:loss = 2969.5796, step = 35601 (0.151 sec)\nINFO:tensorflow:global_step\/sec: 676.062\nINFO:tensorflow:loss = 2702.0142, step = 35701 (0.147 sec)\nINFO:tensorflow:global_step\/sec: 641.384\nINFO:tensorflow:loss = 2972.1235, step = 35801 (0.157 sec)\nINFO:tensorflow:global_step\/sec: 575.043\nINFO:tensorflow:loss = 3671.458, step = 35901 (0.175 sec)\nINFO:tensorflow:global_step\/sec: 662.639\nINFO:tensorflow:loss = 2267.0298, step = 36001 (0.150 sec)\nINFO:tensorflow:global_step\/sec: 704.633\nINFO:tensorflow:loss = 2972.4639, step = 36101 (0.141 sec)\nINFO:tensorflow:global_step\/sec: 694.84\nINFO:tensorflow:loss = 2400.421, step = 36201 (0.146 sec)\nINFO:tensorflow:global_step\/sec: 667.038\nINFO:tensorflow:loss = 2839.9067, step = 36301 (0.148 sec)\nINFO:tensorflow:global_step\/sec: 680.673\nINFO:tensorflow:loss = 1892.6975, step = 36401 (0.147 sec)\nINFO:tensorflow:Saving checkpoints <strong>for<\/strong> 36500 into LinRegTrain1\\model.ckpt.\nINFO:tensorflow:Loss <strong>for<\/strong> final step: 2119.7617.<\/pre>\n\n\n\n<p>To evaluate the data, you need to define another input function. This time, using the test dataset.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#define the test input function<\/em>\ntest_input_fn = tf.estimator.inputs.numpy_input_fn(\n&nbsp;&nbsp;&nbsp;&nbsp;x = {'x': X_test},\n&nbsp;&nbsp;&nbsp;&nbsp;y = y_test,\n&nbsp;&nbsp;&nbsp;&nbsp;batch_size=128,\n&nbsp;&nbsp;&nbsp;&nbsp;num_epochs=10,\n&nbsp;&nbsp;&nbsp;&nbsp;shuffle=True\n&nbsp;&nbsp;&nbsp;&nbsp;)\n<\/pre>\n\n\n\n<p>Finally, we can evaluate the model to see how it performs.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>#evaluate the model&nbsp;<\/em>\nestimator.evaluate(input_fn=test_input_fn)\n<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INFO:tensorflow:Calling model_fn.\nINFO:tensorflow:Done calling model_fn.\nINFO:tensorflow:Starting evaluation at 2020-11-10T01:12:56Z\nINFO:tensorflow:Graph was finalized.\nINFO:tensorflow:Restoring parameters <strong>from<\/strong> <strong>LinRegTrain1<\/strong>\\model.ckpt-36500\nINFO:tensorflow:Running local_init_op.\nINFO:tensorflow:Done running local_init_op.\nINFO:tensorflow:Finished evaluation at 2020-11-10-01:12:56\nINFO:tensorflow:Saving dict <strong>for<\/strong> <strong>global<\/strong> step 36500: average_loss = 46.48516, global_step = 36500, label\/mean = 14.948031, loss = 5903.6157, prediction\/mean = 19.212402\nINFO:tensorflow:Saving 'checkpoint_path' summary <strong>for<\/strong> <strong>global<\/strong> step 36500: LinRegTrain1\\model.ckpt-36500\nOut[70]:\n{'average_loss': 46.48516,\n&nbsp;'label\/mean': 14.948031,\n&nbsp;'loss': 5903.6157,\n&nbsp;'prediction\/mean': 19.212402,\n&nbsp;'global_step': 36500}<\/pre>\n\n\n\n<p>Rounding off, we have discussed the pathway to building a TensorFlow model using a TensorFlow High-level API, tensorflow.estimator. We started by explaining the theory behind linear regression and afterward created a linear regression algorithm with python.&nbsp;<\/p>\n\n\n\n<p>We took a step to build a linear regression model. The model was trained using the Boston housing dataset. In the example, we outlined the steps necessary to train and evaluate your model and how to tweak the performance of the model accordingly. In the next tutorial, we shall introduce you to the data preprocessing techniques and how to improve a model built on another TensorFlow high-end API, Keras.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TensorFlow is a popular open-source library used for high-end numerical computations in machine learning and deep learning. The major reason for its wide acceptance is on accounts of the library\u2019s support for APIs in various languages. APIs are used to ease the building of models and enhance the performance of project execution. With TensorFlow APIs, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7065,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[498],"tags":[],"class_list":["post-7009","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/7009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=7009"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/7009\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/7065"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=7009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=7009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=7009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}