Learn TensorFlow: Creating the Linear Regression Model in TensorFlow 2
In this article, take a look at how to create the linear regression model in TensorFlow 2.
Join the DZone community and get the full member experience.Join For Free
Introduction to TensorFlow 2 (TF 2)
From TensorFlow Guide, there are major changes in TF 2:
- API Cleanup: Removing redundant APIs such as
- Eager Execution: Executing eagerly like Python
- No more globals: Keeping track of your variables
- Functions, not sessions: Can decorate a Python function using
tf.function()to mark it for JIT compilation so that TensorFlow runs it as a single graph.
Building Linear Regression in TF 2
In one of my older articles, I introduced the linear regression algorithm and how to create a simple linear regression model using TensorFlow 1.X. In this post, I will rebuild that model using TensorFlow 2.x as follows:
The result will look like this:
We note some changes:
- declare variables (
tf.Variable) but don't need to use
tf.global_variables_initializer, that means, TensorFlow 2.0 doesn’t make it mandatory to initialize variables.
- train our model using t
f.GradientTapeand we will use
assign_subfor weight variables.
- not require the session execution.
TensorFlow is a great platform for deep learning and machine learning and TF 2.0 focuses on simplicity and ease of use. In this post, I introduced some new changes in TF 2.0 by building simple linear regression models from scratch (don't use APIs such as Keras), and I hope you feel excited about it.
Opinions expressed by DZone contributors are their own.