Adding Regularizer in binary classification problem

Hello, I am trying to build a binary classification network using a quantum circuit. While I train the model, it seems to stop getting better accuracy after some iterations (at around 70% accuracy). I suspect it needs a regularizer, but I cannot find any in pennylane. Is there any build-in regularization that I am missing or should I do it from scratch? Additionally, if you know why the accuracy for both train and validation becomes constant, I would appreciate to explain.

Hi @George_LA, welcome to the Forum!

You can create your own regularizer if you want to, but you could also try using PennyLane together with Keras if you want to use one of the Keras regularizers.

You can learn more about qml.qnn.KerasLayer in the PennyLane docs. You can also learn how to use Keras together with PennyLane by taking a look at our demo on Turning quantum nodes into Keras Layers.

The following code shows you an example of how you could specify the regularizer.

qlayer = KerasLayer(
    weight_shapes={"theta": 3},
    weight_specs={"theta": {"regularizer": tf.keras.regularizers.L2(1e-2)}},

Please let me know if this helps :smiley:

Hi @CatalinaAlbornoz , thank you for your useful response! If I understand it correct, by using keras, I don’t have to change my circuit architecture and the simulation method (I use lightning.gpu, since I run experiments with 21 qubits). So I guess the only part that will change is the training part of the code. If I come up with other questions, I will late you know. Thanks again!

Hi @George_LA ,

I’m glad I could help!
Yes that’s right. The Keras regularizers are classical so this would only change the training part of the code and not the circuit itself. And yes, please let us know if you have any further questions. :slight_smile: