Tensorflow Text Regression

 In this tutorial, you are going to learn

 

1. How to import the TensorFlow libraries?

2. How to Import the dataset?

3. How to preprocess the dataset?

4. How to normalize the dataset?

5. How to implement a feed-forward neural network TensorFlow Regression model?

6. How to compile a TensorFlow model?

7. How to train a compiled neural network TensorFlow Regression model?

1. Import the Libraries

The first step is to import all the necessary libraries.
import pathlib
import numpy as np
import pandas as pd
import tensorflow as tf
import keras

2. Import the Dataset

 

We are importing the Cpu Performance dataset. We are getting the data in .data format, so we need to convert the data into a pandas data frame.

path = keras.utils.get_file("machine.data", "https://archive.ics.uci.edu/ml/machine-learning-databases/cpu-performance/machine.data")
path
column_names =['vendor','Model_Name','MYCT','MMIN','MMAX','CACH','CHMIN','CHMAX','PRP','ERP']
dataset = pd.read_csv(path, names=column_names,
                      na_values = "?", comment='\t',
                      sep=",", skipinitialspace=True)

dataset.head(5)

3. Data Preprocessing

 

We can see that we have a total of 10 columns in our dataset. We have 8 columns with continuous values. We are going to predict the”ERP” value in this. Since we don’t need the “vendor” and “Model_Name” column, we are going to drop these columns.

tensorflow-dataframe.head(5)
dataset=dataset.drop(columns=['vendor','Model_Name'])
y=dataset.pop("ERP")
X=dataset.copy()

4. Data Normalization

 

Now we have training data (X) and a target variable (y). Now we need to normalize the data before feeding it to the model. We cannot feed the model without normalizing the data because, with so high values in the data, the layers will not be able to learn. We will use Z-Score normalization for the same.

X_stats = X.describe()
X_stats= X_stats.transpose()
X_stats
X_normalized = (X - X_stats['mean']) / X_stats['std']
X_normalized

5. Model Implementation

 

Once we are creating a neural network with 3 layers. Each layer is having 100 hidden units and the last layer is the output layer.  

model = tf.keras.Sequential([
    tf.keras.layers.Dense(100, activation='relu', input_shape=[len(X_normalized.keys())]),
    tf.keras.layers.Dense(100, activation='relu'),
    tf.keras.layers.Dense(1)
  ])

6. Model Compilation

 

In this model, we have taken a few parameters to train the model.

optimizer=’adam’ : To update the model while training.

loss=’mse’ : We are going to tell the accuracy of the model bases on Mean Square Error.

metrics=’mse’ : Monitoring the Mean Square Error steps while the model is being trained.

model.compile(loss='mse',
                optimizer='adam',
                metrics=['mse'])

7. Model Training

 

We will use model.fit( ) method to train the model. The model learns to predict the target value (ERP) based on the training data (X).

model.fit(X_normalized,y,epochs=1000,validation_split=0.1)

 Summary

 

1. drop( ) : To drop columns in the dataset.

2. describe( ) : To find the mean and standard deviation (std) of every column in the dataset.

3. Data Normalization : It is important to normalize the data before feeding it to the model.

4.  tf.keras.Sequential( ) : To implement a neural network model.

5. compile( ) : To compile a model.

6. fit( ) : To train a model.

  
 You can find the Github link here.