Get Started With TensorFlow 2.0 and Linear Regression | by Shubham Panchal | Towards Data Science

TensorFlow 2.0 has been a major breakthrough in the TensorFlow family. It’s completely new and refurbished and also less creepy! We’ll create a simple Linear Regression model in TensorFlow 2.0 to explore some new changes. So, open up your code editors and let’s get started!
Also, open up this notebook for an interactive learning experience.
Attention! TensorFlow 2.0 is now available in the stable channel!
For viewing some basic concepts and their simpler explanations see,
Machine Learning Glossary | Google
ML Cheatsheet
Human-In-The-Loop Systems — All You Need To Know
Deploying TF Models on Heroku for Android Apps
Accessing App Usage History In Android
No, Kernels & Filters Are Not The Same
Deploying Pretrained TF Object Detection Models on Android
Realtime Selfie Segmentation In Android With MLKit
Grad-CAM: A Camera For Your Model’s Decision
MLP Mixer Is All You Need?
Age + Gender Estimation in Android with TensorFlow
Using FaceNet For On-Device Face Recognition With Android
Coding Feed-Forward Neural Networks in Kotlin (or Android)
Q-Learning With The Frozen Lake Environment In Android
Hyperparameter Optimization With Genetic Algorithms In Kotlin
Image Classification With TensorFlow 2.0 ( Without Keras )
Get Started With TensorFlow 2.0 and Linear Regression
Cityscape Image Segmentation With TensorFlow 2.0
4 Awesome Ways Of Loading ML Data In Google Colab
Neural Implanting With AutoEncoders and TensorFlow
Colorizing B/W Images With GANs in TensorFlow
Exploring Random Forests In Light Of Kotlin
Gaussian Naive Bayes ( for Iris Classification ) in Android
Sarcasm Detection using Word Embeddings in Android
Hands-on With Multiple Linear Regression on Android
Bayes Text Classification in Kotlin for Android without TensorFlow
How I made Skinly for Melanoma Detection in Android
Text Classification in Android with TensorFlow
Designing Decision Trees From Scratch on Android
Introducing TensorFlow Lite Android Support Library
Introducing Text2Summary: Text Summarization on Android
We’ll get some information on what Linear Regression at the first instance. In Linear Regression, we tend to find a best-fit line for your data.
The line could be defined in its slope-intercept form as:
m and c are the slope and y-intercept respectively. Where W and b are the weights and biases respectively for the 2nd equation.
To optimize our parameters w and b, we require a loss function. That’s where Mean Squared Error ( L1 / MSE ) comes in the picture.
Where N is the number of samples in the batch/dataset, y is the predicted outcome whereas y− ( y-hat ) is the target outcome.
Also, we will need the derivative of the Mean Squared Error function which is as follows:
Where N is the number of samples in the batch/dataset, y is the predicted outcome whereas y− ( y-hat ) is the target outcome.
Now, enters Gradient Descent through which we will update our parameters θ.
Where θ is our parameter, α is the learning rate or step size and loss is our loss function.
We optimize both w and b by obtaining their partial derivatives with respect to the loss function ( MSE ).
Where w and b are the parameters of optimization, h is the hypothesis function, loss is the loss function and MSE’ is the derivative of the Mean Squared Error loss function.
We are going to use the data from Graduate Admissions on Kaggle.com. It contains 6 continuous and 1 binary feature making a total of 7 features. The label or the expected outcome is the chance of admission for a student. This is our target variable.
We’ll download and parse the dataset into something which we really love train and validations datasets!
We define 3 methods with TensorFlow’s low-level APIs for :
Mean Squared Error functionThe derivative of Mean Squared Error functionHypothesis function/ Regression function
Mean Squared Error function
The derivative of Mean Squared Error function
Hypothesis function/ Regression function
which we have discussed earlier in raw Math.
Then, we initialize some hyperparameters for training and also create tf.data.Datasetobject to store and batch our data efficiently.
Noticed a TF 2.0 change? Earlier for TF 1.x versions, we used tf.data.Dataset.make_one_shot_iterator() method for creating an iterator. That’s changed and now we use tf.data.Dataset.__iter__()
Finally, we train the model for num_epochs epochs with a batch size of batch_size which makes a step size of num_samples/batch_size .
Changes: We do not need to run the ops and tensors via a tf.Session() object. TensorFlow 2.0 has Eager Execution enabled by default. To get the value of a tf.Tensor we only use the tf.Tensor.numpy() method.
Also, we can get a plot of epoch-loss using matplotlib.pyplt using,
import matplotlib.pyplot as pltplt.plot( epochs_plot , loss_plot ) plt.show()
And now for evaluating our model’s accuracy, we measure the Mean Absolute Error.
Changes: The tf.metrics now return an op ( operation ) instead of a Tensor. That’s good though!
ope that was a good introduction to TensorFlow 2.0 and linear regression. Thank You and happy Machine Learning!