A Simple Deep Learning model to Convert Celcius to Fahrenheit using Tensorflow + Python with Google Colab

Sushant Kumar
Analytics Vidhya
Published in
4 min readAug 15, 2020

Introduction

The objective is to predict the value of one variable Y based on another variable X using simple deep learning model. X in this will be an independent variable and Y will be a dependent variable which will be linearly related to X.

Challenge

This challenge is based on converting different temperature values from Celsius to Fahrenheit using Deep learning method.

Deep Learning complete project

Importing the datasets

In this case, a very basic self-created dataset name Celsius_Fahrenheit_dataset_1000.csv of 1000 values is used. This dataset can be downloaded from the link https://drive.google.com/file/d/19Laj9jreNnMWLpqR1tMrnyscnsQUUONF/view?usp=sharing. Or the complete dataset can also be seen from https://github.com/sushantkumar-estech/Celsius-to-Fahrenheit-using-ANN/blob/master/Celsius_Fahrenheit_dataset_1000.csv

This is how the top rows of dataset look like:

Top 5 rows of an imported dataset

and the last five rows of dataset as well:

Last 5 rows of the same dataset

also some more information about the dataset

Details about the dataset

And some more.

Details about the type of data in the dataset

Here below shows the curve between Celsius and Fahrenheit. Fahrenheit is linearly related to Celsius by relation Temperature in Fahrenheit = 9/5 * Temperature in Celsius + 32” with the slope (m) = 9/5.

Celsius v/s Fahreneheit curve

For this project, the entire dataset used for training the model whereas testing of the model is done by passing random value and checking the correctness by comparing model output with actual output obtained from Celsius to Fahrenheit conversion formula.

Creating the model

As mentioned, the model is deep neural networks which consist of one input layer, two hidden layers and one output layer in total. As there is one value i.e. X which is Celsius in this project, to be converted to another value i.e. Y which is Fahrenheit, that’s why the model has only one neuron in the input layer and output layer. Each hidden layer contains 32 neurons. All the layers are fully connected with each other.

Model structure

The created Model summary is as follows.

Model summary

As mentioned there are 1,153 parameters in the model which needs to be trained.

Training the model

For training the model, the formatted dataset needs to be pass or in other terms, the model needs to be fitted to the dataset. While training, parameters like optimization function, number of epochs, validation split etc. needs to be decided. There is an impact of these values on the training of the model. A model must always be protected from over-fitting or under-fitting.

For this project ‘Adam’ optimizer, 30 epochs & validation split = 0.2 been chosen. The model gets trained on 800 values from the dataset and simultaneously validated for 200 values.

As the model started getting trained, epochs by epochs, the model gets better and the losses started getting reduced. The performance summary of model while training on training and validation data during 30 epochs is as follows:

Model summary performance during training

The model loss v/s Epochs graph shows how the Training loss get reduced with increase in the number of Epochs.

Training loss v/s Epochs curve

The same kind of behaviour is shown by Validation losses as well. As that can be seen in the following graph, they also get reduced as the number of epochs increases. The green line shows Training loss and red line representing Validation loss.

Model loss (training and validation loss) v/s Epochs curve

Testing the model

For testing, the Temperature value in Celsius = 12° passed in the model, for which the model predicted Temperature value in Fahrenheit = 53.60002°. From the Celsius to Fahrenheit formula (Temperature in Fahrenheit = 9/5 * Temperature in Celsius + 32), the actual value supposed to be 53.6°. The neural network model’s output is 0.00002° deviated from an actual value, which can be neglected.

Conclusion

Thus the Neural networks are capable of making very close predictions. This is a strong field in machine learning.

Reference

The full code the dataset can be downloaded from the following Git

Also if you are a beginner in machine learning and enthusiast to learn more, then you can search for GitHub account sushantkumar-estech or can use the link https://github.com/sushantkumar-estech for interesting projects

Select any project from your wish for practice and in case of any question, you can write to me. I would be happy to help.

Enjoy reading and happy learning!!

--

--

Sushant Kumar
Analytics Vidhya

Researcher | Engineer | Programmer | Reader | Interested in learning AI, Edge Computing | ASICs, FPGA enthusiast |