From 37661080a111768e565ae53299c4796ebe711a71 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Thu, 21 Mar 2024 14:29:50 -0600 Subject: fix mathjax stuff --- docs/feed.rss | 66 +++++++++------------- ...3-21-Polynomial-Regression-in-TensorFlow-2.html | 62 +++++++++----------- 2 files changed, 54 insertions(+), 74 deletions(-) (limited to 'docs') diff --git a/docs/feed.rss b/docs/feed.rss index df334a3..12e9f8d 100644 --- a/docs/feed.rss +++ b/docs/feed.rss @@ -4,8 +4,8 @@ Navan's Archive Rare Tips, Tricks and Posts https://web.navan.dev/en - Thu, 21 Mar 2024 13:54:34 -0000 - Thu, 21 Mar 2024 13:54:34 -0000 + Thu, 21 Mar 2024 14:27:28 -0000 + Thu, 21 Mar 2024 14:27:28 -0000 250 @@ -553,18 +553,17 @@ creating a DOS Which is equivalent to the general cubic equation:

-

- + -$$ +

$$ y = ax^3 + bx^2 + cx + d -$$ +$$

-### Optimizer Selection & Training -
+

Optimizer Selection & Training

+
optimizer = tf.keras.optimizers.Adam(learning_rate=0.3)
 num_epochs = 10_000
 
@@ -577,25 +576,23 @@ $$
     if (epoch+1) % 1000 == 0:
         print(f"Epoch: {epoch+1}, Loss: {loss.numpy()}"
 
-
+

In TensorFlow 1, we would have been using tf.Session instead.

-In TensorFlow 1, we would have been using `tf.Session` instead. +

Here we are using GradientTape() instead, to keep track of the loss evaluation and coefficients. This is crucial, as our optimizer needs these gradients to be able to optimize our coefficients.

-Here we are using `GradientTape()` instead, to keep track of the loss evaluation and coefficients. This is crucial, as our optimizer needs these gradients to be able to optimize our coefficients. +

Our loss function is Mean Squared Error (MSE):

-Our loss function is Mean Squared Error (MSE) +

$$ += \frac{1}{n} \sum_{i=1}^{n}{(Y_i - \hat{Y_i})^2} +$$

-$$ -= \frac{1}{n}\sum_{i=1}^{n} (Y_i - \^{Y_i}) -$$ +

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

-Where $\^{Y_i}$ is the predicted value and $Y_i$ is the actual value +

Plotting Final Coefficients

-### Plotting Final Coefficients
-
final_coefficients = [c.numpy() for c in coefficients]
 print("Final Coefficients:", final_coefficients)
 
@@ -606,18 +603,15 @@ Where $\^{Y_i}$ is the predicted value and $Y_i$ is the actual value
 plt.title("Salary vs Position")
 plt.show()
 
-
+

Code Snippet for a Polynomial of Degree N

+

Using Gradient Tape

-## Code Snippet for a Polynomial of Degree N - -### Using Gradient Tape +

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

-This should work regardless of the Keras backend version (2 or 3)
-
import tensorflow as tf
 import numpy as np
 import pandas as pd
@@ -666,17 +660,15 @@ This should work regardless of the Keras backend version (2 or 3)
 plt.legend()
 plt.show()
 
-
+

Without Gradient Tape

-### Without Gradient Tape +

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

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

This will not work with Keras 3 backend in TF 2.16.0 and above unless you switch to the legacy backend.

-This will not work with Keras 3 backend in TF 2.16.0 and above unless you switch to the legacy backend.
-
import tensorflow as tf
 import numpy as np
 import pandas as pd
@@ -726,26 +718,24 @@ This will not work with Keras 3 backend in TF 2.16.0 and above unless you switch
 plt.title(f"{x_column} vs {y_column}")
 plt.show()
 
-
+

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

-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. +

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

-## Further Programming +

$$ y = ab^x $$

-How would you modify this code to use another type of nonlinear regression? Say, $ y = ab^x $ +

Hint: Your loss calculation would be similar to:

-Hint: Your loss calculation would be similar to:
-
bx = tf.pow(coefficients[1], X)
 pred_y = tf.math.multiply(coefficients[0], bx)
 loss = tf.reduce_mean(tf.square(pred_y - Y))
 
- -

+
]]> 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 c1a4ae4..7a25daf 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 @@ -103,18 +103,17 @@

Which is equivalent to the general cubic equation:

-

- + -$$ +

$$ y = ax^3 + bx^2 + cx + d -$$ +$$

-### Optimizer Selection & Training -
+

Optimizer Selection & Training

+
optimizer = tf.keras.optimizers.Adam(learning_rate=0.3)
 num_epochs = 10_000
 
@@ -127,25 +126,23 @@ $$
     if (epoch+1) % 1000 == 0:
         print(f"Epoch: {epoch+1}, Loss: {loss.numpy()}"
 
-
+

In TensorFlow 1, we would have been using tf.Session instead.

-In TensorFlow 1, we would have been using `tf.Session` instead. +

Here we are using GradientTape() instead, to keep track of the loss evaluation and coefficients. This is crucial, as our optimizer needs these gradients to be able to optimize our coefficients.

-Here we are using `GradientTape()` instead, to keep track of the loss evaluation and coefficients. This is crucial, as our optimizer needs these gradients to be able to optimize our coefficients. +

Our loss function is Mean Squared Error (MSE):

-Our loss function is Mean Squared Error (MSE) +

$$ += \frac{1}{n} \sum_{i=1}^{n}{(Y_i - \hat{Y_i})^2} +$$

-$$ -= \frac{1}{n}\sum_{i=1}^{n} (Y_i - \^{Y_i}) -$$ +

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

-Where $\^{Y_i}$ is the predicted value and $Y_i$ is the actual value +

Plotting Final Coefficients

-### Plotting Final Coefficients
-
final_coefficients = [c.numpy() for c in coefficients]
 print("Final Coefficients:", final_coefficients)
 
@@ -156,18 +153,15 @@ Where $\^{Y_i}$ is the predicted value and $Y_i$ is the actual value
 plt.title("Salary vs Position")
 plt.show()
 
-
+

Code Snippet for a Polynomial of Degree N

+

Using Gradient Tape

-## Code Snippet for a Polynomial of Degree N - -### Using Gradient Tape +

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

-This should work regardless of the Keras backend version (2 or 3)
-
import tensorflow as tf
 import numpy as np
 import pandas as pd
@@ -216,17 +210,15 @@ This should work regardless of the Keras backend version (2 or 3)
 plt.legend()
 plt.show()
 
-
+

Without Gradient Tape

-### Without Gradient Tape +

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

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

This will not work with Keras 3 backend in TF 2.16.0 and above unless you switch to the legacy backend.

-This will not work with Keras 3 backend in TF 2.16.0 and above unless you switch to the legacy backend.
-
import tensorflow as tf
 import numpy as np
 import pandas as pd
@@ -276,26 +268,24 @@ This will not work with Keras 3 backend in TF 2.16.0 and above unless you switch
 plt.title(f"{x_column} vs {y_column}")
 plt.show()
 
-
+

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

-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. +

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

-## Further Programming +

$$ y = ab^x $$

-How would you modify this code to use another type of nonlinear regression? Say, $ y = ab^x $ +

Hint: Your loss calculation would be similar to:

-Hint: Your loss calculation would be similar to:
-
bx = tf.pow(coefficients[1], X)
 pred_y = tf.math.multiply(coefficients[0], bx)
 loss = tf.reduce_mean(tf.square(pred_y - Y))
 
- -

+
If you have scrolled this far, consider subscribing to my mailing list here. You can subscribe to either a specific type of post you are interested in, or subscribe to everything with the "Everything" list.