Guidance required in saving model

I was trying to save this model

https://pennylane.readthedocs.io/en/user-docs-refactor/tutorials/pennylane_quantum_neural_net.html

I have no idea how to do. Any inputs on saving will be appreciated.

Looking forward to your support

Thanks

Hi @JEEVARATHINAM, some people use Keras which allows you to save and load models. Maybe the guide here can help.

In this demo you can learn how to turn PennyLane quantum nodes into Keras layers.

I hope this helps!

Hi,
Since the code which I m asking is looks like something related to forecasting.
But the demo code which you sent is related to binary classification

Can you help me by saying the change I need to do for running this code and saving the model by creating hybrid model

https://pennylane.readthedocs.io/en/user-docs-refactor/tutorials/pennylane_quantum_neural_net.html

I m looking forward to what changes I need to incorporate in keras layers

Thanks

Hi @JEEVARATHINAM, in our community demos page you will find several demos which use Keras. The one on fraud detection might be particularly helpful.

Please let me know if this helps!

Hi,
i was trying to save the model as you said.

When i tried

  1. tf.keras.models.save_model(model,filepath=“dbfs:/FileStore/cc.h5”)
  2. model.save(“dbfs:/FileStore/cc.h5”)

It shows error like

Layer KerasLayer has arguments ['self', 'qnode', 'weight_shapes', 'output_dim', 'weight_specs']
in `__init__` and therefore must override `get_config()`.

Example:

class CustomLayer(keras.layers.Layer):
    def __init__(self, arg1, arg2):
        super().__init__()
        self.arg1 = arg1
        self.arg2 = arg2

    def get_config(self):
        config = super().get_config()
        config.update({
            "arg1": self.arg1,
            "arg2": self.arg2,
        })
        return config

You can this community version databricks link

https://databricks-prod-cloudfront.cloud.databricks.com/public/4027ec902e239c93eaaa8714f173bcfc/4958755231243508/1086808892329495/7267292840714169/latest.html

looking forward to your support

Hi @JEEVARATHINAM, if you run the following code does it work for you?

def get_model():
    # Create a simple model.
    inputs = keras.Input(shape=(32,))
    outputs = keras.layers.Dense(1)(inputs)
    model = keras.Model(inputs, outputs)
    model.compile(optimizer="adam", loss="mean_squared_error")
    return model


model = get_model()

# Train the model.
test_input = np.random.random((128, 32))
test_target = np.random.random((128, 1))
model.fit(test_input, test_target)

# Calling `save('my_model')` creates a SavedModel folder `my_model`.
model.save("my_model")

# It can be used to reconstruct the model identically.
reconstructed_model = keras.models.load_model("my_model")

# Let's check:
np.testing.assert_allclose(
    model.predict(test_input), reconstructed_model.predict(test_input)
)

# The reconstructed model is already compiled and has retained the optimizer
# state, so training can resume:
reconstructed_model.fit(test_input, test_target) 

The code before is an example found here. It works for me. I wasn’t able to reproduce your problem.

I hope this helps.