From f6d2141a480dd6b5b8ee0e48d43bb64773232791 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Tue, 26 Mar 2024 23:38:14 -0600 Subject: add header ids --- ...3-21-Polynomial-Regression-in-TensorFlow-2.html | 30 +++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'docs/posts/2024-03-21-Polynomial-Regression-in-TensorFlow-2.html') diff --git a/docs/posts/2024-03-21-Polynomial-Regression-in-TensorFlow-2.html b/docs/posts/2024-03-21-Polynomial-Regression-in-TensorFlow-2.html index ab46ec7..0d958b2 100644 --- a/docs/posts/2024-03-21-Polynomial-Regression-in-TensorFlow-2.html +++ b/docs/posts/2024-03-21-Polynomial-Regression-in-TensorFlow-2.html @@ -6,13 +6,13 @@ - Polynomial Regression Using TensorFlow 2.x + id="polynomial-regression-using-tensorflow-2x">Polynomial Regression Using TensorFlow 2.x - - + Polynomial Regression Using TensorFlow 2.x" /> + Polynomial Regression Using TensorFlow 2.x" /> @@ -44,13 +44,13 @@
-

Polynomial Regression Using TensorFlow 2.x

+

Polynomial Regression Using TensorFlow 2.x

I have a similar post titled Polynomial Regression Using Tensorflow that used tensorflow.compat.v1 (Which still works as of TF 2.16). But, I thought it would be nicer to redo it with newer TF versions.

I will be skipping all the introductions about polynomial regression and jumping straight to the code. Personally, I prefer using scikit-learn for this task.

-

Position vs Salary Dataset

+

Position vs Salary Dataset

Again, we will be using https://drive.google.com/file/d/1tNL4jxZEfpaP4oflfSn6pIHJX7Pachm9/view (Salary vs Position Dataset)

@@ -61,11 +61,11 @@ -

Code

+

Code

If you just want to copy-paste the code, scroll to the bottom for the entire snippet. Here I will try and walk through setting up code for a 3rd-degree (cubic) polynomial

-

Imports

+

Imports

import pandas as pd
@@ -75,14 +75,14 @@
 
-

Reading the Dataset

+

Reading the Dataset

df = pd.read_csv("data.csv")
 
-

Variables and Constants

+

Variables and Constants

Here, we initialize the X and Y values as constants, since they are not going to change. The coefficients are defined as variables.

@@ -109,7 +109,7 @@ y=ax3+bx2+cx+d -

Optimizer Selection & Training

+

Optimizer Selection & Training

optimizer = tf.keras.optimizers.Adam(learning_rate=0.3)
@@ -136,7 +136,7 @@
 
 

Where Yi^ is the predicted value and Yi is the actual value

-

Plotting Final Coefficients

+

Plotting Final Coefficients

final_coefficients = [c.numpy() for c in coefficients]
@@ -151,9 +151,9 @@
 
-

Code Snippet for a Polynomial of Degree N

+

Code Snippet for a Polynomial of Degree N

-

Using Gradient Tape

+

Using Gradient Tape

This should work regardless of the Keras backend version (2 or 3)

@@ -208,7 +208,7 @@
-

Without Gradient Tape

+

Without Gradient Tape

This relies on the Optimizer's minimize function and uses the var_list parameter to update the variables.

@@ -268,7 +268,7 @@

As always, remember to tweak the parameters and choose the correct model for the job. A polynomial regression model might not even be the best model for this particular dataset.

-

Further Programming

+

Further Programming

How would you modify this code to use another type of nonlinear regression? Say,

-- cgit v1.2.3