How to Create a Neural Network Regression Model with TensorFlow



Original Source Here

How to Create a Neural Network Regression Model with TensorFlow

Photographer Pixabay

TensorFlow is one of the trending keywords in deep learning. In this article, I will tell you how to create a regression model using TensorFlow. Here I used Google Colab. You can use any IDE according to your preferences. You can follow this article to get an idea about how to create a neural network model using TensorFlow.

In the creation of this model, I used the medical cost personal dataset in Kaggle.In that dataset, it contains 7 columns. 6 of them are the attributes and the charges column is the target.

Dataset Information
Check for the null values

This dataset contains region, sex, smoking attributes with string values. So we know that we cannot directly apply string values to the machine learning models. So we need to convert them into numbers. Here I used the one-hot encoding technique. Then I was able to convert the string values into numerical values.

#one-hot encode the dataframe
insurance_one_hot=pd.get_dummies(insurance)
insurance_one_hot
Data set after one-hot encode

Then I need to select X and y values. So I drop the credit attribute and set other attributes as X . Then I select the credit attribute as y.

#Create X and Y valuesX=insurance_one_hot.drop("charges",axis=1)
y=insurance_one_hot["charges"]

Then I need to create a train and test dataset. Here I used 0.2 as the test size. I used a random state as 42 to get the same results every time.

#creating training and test data setfrom sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,
random_state=42)
Data set after splitting

In this tutorial at the start, I used two layers. So I created the model with two layers. I used the SGD as the optimizer as the start. I used the MAE as the metric. At the start, I used the 100 epochs. Because sometimes providing a high number of epochs can be time-consuming.

#Build the neural network
tf.random.set_seed(42)
#create a model
insurance_model=tf.keras.Sequential([
tf.keras.layers.Dense(10),
tf.keras.layers.Dense(1)
])
#compile the model
insurance_model.compile(loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.SGD(),
metrics=["mae"]
)
#fit the model
insurance_model.fit(X_train,y_train,epochs=100)
#check the results with insurnce model test data
insurance_model.evaluate(X_test,y_test)

After creating the model I evaluate the model. I got the MAE as 7023.3291 as the value. But when considering the dataset I cannot accept that value. Because that much of difference cannot accept. So I need to improve the performance of the Model.

Evaluate the model with 1 st time Parameters

Then I try to create the model with 3 layers. This time I used Adam as the optimizer because I got Nan as the loss and MAE values in SGD Optimizer. Then I used the 300 as the epochs.

#Build the neural network
tf.random.set_seed(42)
#create a model
insurance_model_2=tf.keras.Sequential([
tf.keras.layers.Dense(100),
tf.keras.layers.Dense(10),
tf.keras.layers.Dense(1)
])
#compile the model
insurance_model_2.compile(loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.Adam(),
metrics=["mae"]
)
#fit the model
history=insurance_model_2.fit(X_train,y_train,epochs=300)

By using these new parameters I was able to get the 3215.8601 as the MAE value. This can be acceptable when considering the before value. For Improving the Model performance I had to run many trials.

Evaluation after doing hyperparameter Tuning to the Model

Let’s talk briefly about some parameters I used in this model.

Optimizers

Optimizers mainly used to speed up and improve the training performance of the training model. SGD and Adam are the most common used optimizers. But there are more optimizers that you can use according to your problem. You need to have an idea and do trials with different optimizers to select the most suitable optimizer.

Epochs

This called the number of times that model goes through the data set to find patterns. Here you need to find a suitable number according to your problem. Sometimes you think that giving a higher number is Good. But that is not Suitable. Because Running many times through the dataset can be time-consuming and high usage of the hardware came into the play. You can use an Early stopping call back to stop the iterations when it comes to suitable value without running more times without improving the Model.

#plot history (Also known as loss  curve or a training curve)
pd.DataFrame(history.history).plot()
plt.ylabel("Loss")
plt.xlabel("Epochs")
Loss curve with No of Epochs

After getting those MSE values I tried to improve the model performance. I tried to use Normalization in the data. Because normalized data will help to identify the patterns in the data more easily and quickly. Because of normalized data in the same range as Actual data.

In this dataset I identify that age, BMI and children can apply standardization. So in this case I used min mac scaler for the features. There are min-max scalar, Robust Scaler, standard scaler and Normalizer. You can use a suitable scaler according to your dataset and usage of those scalers. Here I need to convert values between 0 and 1.So I used a min-max scaler here.

from sklearn.compose import make_column_transformer
from sklearn.preprocessing import MinMaxScaler,OneHotEncoder
from sklearn.model_selection import train_test_split
#create column transformer
ct=make_column_transformer(
(MinMaxScaler(),["age","bmi","children"]),#turn the values betweeen range of 0 and 1
(OneHotEncoder(handle_unknown="ignore"), ["sex","smoker","region"])
)

In here you need to fit the X_train data into the scaler. Otherwise provide both train and test data the model will perform well in test data. Then You will get into problems when the model works in the real world. So you need to ensure that train data to put in to fit. Then you can transform train and test data.

#create X and y
X =insurance.drop("charges",axis=1)
y=insurance["charges"]
#build train and test dataX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,
random_state=42)
#fit the column transformer to our training data
ct.fit(X_train)
#tranfome training and test data with normailization(minmax scaler and normalization)X_train_normal=ct.transform(X_train)
X_test_normal=ct.transform(X_test)
Data row and Shape after Normalization

Then I used the previous hyperparameters in the model. I provide the normalized data to the model. In that case, I was able to get 3161.5601 as the MAE. I was able to improve the performance of the model.

#Build the neural network
tf.random.set_seed(42)
#create a model
insurance_model_4=tf.keras.Sequential([
tf.keras.layers.Dense(100),
tf.keras.layers.Dense(10),
tf.keras.layers.Dense(1)
])
#compile the modelinsurance_model_4.compile(
loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.Adam(),
metrics=["mae"]
)
#fit the model
history=insurance_model_4.fit(X_train_normal,y_train,epochs=300)

Conclusion

In this article, I tell you how to create a neural network model using TensorFlow.I think this will help you get some idea. Here at the start I just follow the string conversion and directly applied data into the model. But you need to Follow the pre-processing steps in the Model Creation. In this data set, there are no Null values. Then you need to add logarithm and exponential transformations to skewed attributes. You need to consider the outliers. Then you need to apply Standardization and Normalization as Suitable. Here I just try to give a simple idea about model creation. I can tell that you need to spend more time on the data preprocessing steps. Because it is the most important step in model creation. Then you can improve the performance by using more hyperparameter tuning and cross-validation techniques.

I also learn these things by reading articles and watching videos and following videos on Udemy. So I can say that you need to do self-study and practice to improve your knowledge. In the beginning, this can be hard. But with time and practice, you can improve. So you need to keep your patience and discipline.

I did not go deep into topics like hyperparameter tuning, Optimization, Data Visualization, and data preprocessing in this article. Because adding those things to this article can increase the size and become difficult to understand for beginners.

This article has explored ways to work with a Neural network with TensorFlow I hope will assist you in completing your work more accurately. I’d like to thank you for reading my article, I hope to write more articles on new trending topics in the future to keep an eye on my account if you liked what you read today!

References :

More content at plainenglish.io

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: