Unknown regularizer: L2 in JS Environment

I have Converted my Python trained model (used l2 kernel_regularizer) tfjs, while loading in the JS application encountered below exception

ERROR Error: Unknown regularizer: L2. This may be due to one of the following reasons:
1. The regularizer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom regularizer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().

  • at deserializeKerasObject (generic_utils.js:227:19)*
  • at deserializeRegularizer (regularizers.js:80:34)*
  • at getRegularizer (regularizers.js:97:16)*
  • at new Conv (convolutional.js:332:48)*
  • at new Conv2D (convolutional.js:424:9)*
  • at fromConfig (serialization.js:69:16)*
  • at deserializeKerasObject (generic_utils.js:258:31)*
  • at deserialize (serialization.js:25:34)*
  • at fromConfig (models.js:874:38)*
  • at deserializeKerasObject (generic_utils.js:258:31)*

Struck with the above how to register custom regularizer in JS and how to port regularizer is defined in Python to JS. Any thoughts here would be helpfull.

1 Like

Hello @Kesavan_Ramalingam,

is this related to Course 1 week 3 assignment?

and you got this error while submission or running a cell?

Check if your codes for below cell is mentioned correctly
Run the TensorFlow.js Converter on The Saved Keras Model

Use the below image to cross reference if you defined it as per shown in the image
![Screenshot 1945-10-14 at 1.30.15 PM|690x292]
(upload://tTwoSAXm1aPz0wDc7TVr3cdMMKB.png)

The change you need to do from the image shared is instead of
tmp/my_keras_model.h5
/tmp/my_tfjs_model

Use your saved model path (if you coded it correctly) with
{saved_model_path}
“./”

Regards
DP

No, this is not related to Assignments or Submission, Curious on trying to convert my own model.

1 Like

:slight_smile: own model??

Then you need to share information about what you are doing, what data are you using and what are you trying to achieve.

Regards
DP

Here is the topology, it’s a slightly modified LeNet architecture. I have converted it into JSON model, trying to use this in JS Environment

1 Like

can you share more information about what you are trying to achieve. like about the data, if you used any generator and/or other necessary features you need mention.

L2 regularization distributes the impact of correlated features more evenly among the coefficients, preventing any one feature from dominating the model’s predictions.
Generalization performance: L2 regularization is known to improve the generalization performance of models by reducing overfitting.

I have a found a link related to your issue where it shows how to register L2, you can try once.

Regards
DP

Thank you for your response, yes, I have come across this Stack overflow, the exception clearly mentions the regularizer used in python not supported by tfjs, I struck in how to create and register Custom regularizer in JS Code.

This Model is trained for multi class classification scenario and no issues with the training data, the l2 regularization used on conv layer causing the issue, and it works well in Python environment, I’m trying to convert that which needs to work in JS platform aswell

Sidenote : If no regularization used during model training, it works fine

1 Like

what are the results if you do not use regularization?

Kesavan,

Did you go through the below

https://www.tensorflow.org/api_docs/python/tf/keras/regularizers/L2

Also after noticing your image again, I was wondering if you could apply it outside your model where you could conversion to JS environment at the end. Did you think about this?

Regards
DP

If no regularization used during model training, i could successfully be able to load the JSON model in JS Environment

Regarding above context, do you want me to add the regularization post the model training, since I’m a newbie to this world, couldn’t get that.

1 Like

Did you check this out

https://www.tensorflow.org/api_docs/python/tf/keras/regularizers/L2

1 Like

Did it help @Kesavan_Ramalingam?

so, I removed l2 regularizer in model training and able do inference after conversion to tfjs, yet to try adding regularizer in a particular layer from tfjs model

will keep you posted here

Thanks

1 Like