Question about KerasLayer

I meet a question when I use qml.qnn.KerasLayer. I want to build a hybrid classical-quantum model. But when I run the code, a question appear. Here is my code.

import os,random
os.environ["KERAS_BACKEND"] = "tensorflow"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as scio
import pickle,sys,h5py
import keras
import keras.backend as K
from keras.callbacks import LearningRateScheduler
import tensorflow as tf
import pennylane as qml
import mltools, dataset2016, dataset_train_alldb_allmods2
import csv
# from tensorflow.keras.layers import Input,Dense,Conv1D,MaxPool1D,ReLU,Dropout,Softmax,concatenate,Flatten,Reshape,Activation

#set Keras data format as channels_first
K.set_image_data_format('channels_last')
print(K.image_data_format())

(mods, snrs, lbl), (X_train, Y_train, Y_train_hot), (X_val, Y_val, Y_val_hot), (X_test, Y_test, Y_test_hot), (train_idx, val_idx, test_idx), file_num = \
    dataset_train_alldb_allmods2.load_data()

print(X_train.shape)
print(X_test.shape)
classes = mods
# Set up some params
nb_epoch = 3     # number of epochs to train on
batch_size = 512  # training batch size

n_qubits = 2
n_layers = 2
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev)
def qnode(inputs, weights):
    qml.RX(inputs[0], wires=0)
    qml.RX(inputs[1], wires=1)

    qml.RY(weights[0,0], wires=0)
    qml.RY(weights[1,1], wires=1)
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))

# model=mcl.DNN3()
dr =0.2

# print(weight_shapes)
weight_shapes = {"weights": (n_layers, n_qubits)}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=2)

input   = tf.keras.layers.Input(shape=(5,))
Dense_1 = tf.keras.layers.Dense(5)
QNN_1   = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=2, name="QNN_1")
output  = tf.keras.layers.Dense(2, activation="softmax")

model = tf.keras.models.Sequential([input, Dense_1, QNN_1, output])

model.compile(loss='categorical_crossentropy',
              metrics=['accuracy'],
              optimizer='adam')
model.summary()

# Train the framework (model)
filepath = 'weights/weights.h5'
history = model.fit(X_train,
    Y_train_hot,
    batch_size=batch_size,
    epochs=nb_epoch,
    verbose=2,
    #validation_data=([X_test,X1_test,X2_test],Y_test),
    validation_data=(X_val, Y_val_hot),
    callbacks = [
                keras.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='auto'),
                keras.callbacks.ReduceLROnPlateau(monitor='val_loss',factor=0.5,verbose=1,patince=5,min_lr=0.0000001),
                keras.callbacks.EarlyStopping(monitor='val_loss', patience=30, verbose=1, mode='auto')
                ]
                    )
mltools.show_history(history)

And the full error message below:

Traceback (most recent call last):
  File "E:\Postgraduate\Projects\AMC\AMR-Benchmark-main\HCQDNN3\pytorch_pennelane.py", line 75, in <module>
    history = model.fit(X_train,
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\engine\training.py", line 1230, in fit
    callbacks.on_epoch_end(epoch, epoch_logs)
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\callbacks.py", line 413, in on_epoch_end
    callback.on_epoch_end(epoch, logs)
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\callbacks.py", line 1368, in on_epoch_end
    self._save_model(epoch=epoch, batch=None, logs=logs)
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\callbacks.py", line 1422, in _save_model
    self.model.save(filepath, overwrite=True, options=self._options)
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\engine\training.py", line 2145, in save
    save.save_model(self, filepath, overwrite, include_optimizer, save_format,
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\saving\save.py", line 145, in save_model
    hdf5_format.save_model_to_hdf5(
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\saving\hdf5_format.py", line 110, in save_model_to_hdf5
    model_metadata = saving_utils.model_metadata(model, include_optimizer)
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\saving\saving_utils.py", line 148, in model_metadata
    raise e
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\saving\saving_utils.py", line 145, in model_metadata
    model_config['config'] = model.get_config()
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\engine\sequential.py", line 411, in get_config
    layer_configs.append(generic_utils.serialize_keras_object(layer))
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\utils\generic_utils.py", line 508, in serialize_keras_object
    raise e
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\utils\generic_utils.py", line 503, in serialize_keras_object
    config = instance.get_config()
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\engine\base_layer.py", line 727, in get_config
    raise NotImplementedError('Layer %s has arguments in `__init__` and '
NotImplementedError: Layer KerasLayer has arguments in `__init__` and therefore must override `get_config`.

Why I can’t build the model? I think I obey the rul when I use ‘qml.qnn.KerasLayer’
If I delete the QNN_1 , let model = tf.keras.models.Sequential([input, Dense_1, output]), the code can run successfully.

OMG! I find out the answer! When I installed pennylane, I install pennylane==0.13.0 instead of 0.23.0. Now I can build the hybird network successfully (o゜▽゜)o

1 Like

Hey @AHHil! Welcome to the forum :rocket:

Glad you were able to solve your issue :grin:. Indeed, upgrading your pennylane version is a good thing to check when you encounter issues. You can see the paper trail of new features, improvements, breaking changes, etc., in the release notes: Release notes — PennyLane 0.31.0 documentation

Also, I’m glad you upgraded to v0.23 and your code now works, but we’re on v0.31 as of writing this. Might be worth upgrading again :smile:. There are a lot of improvements to KerasLayer!

:smile:Thank you@isaacdevlugt! It’s my first time to use pennylane. Now I can successful build a simple hybird classical quantum netwok!
But I met a question about the version of pennylane again. Today I intstall pennylane0.31.0. But my hybird network can’t run under the new environment. I didn’t change my code. I don’t know the reason. I uninstall pennylane0.31.0 and install 0.28.0, then my code work again. Now I run my code under pennylane0.28.0 instead of 0.23. Is it necessary to update it to 0.31.0?

It’s not necessary to upgrade to the latest version of PennyLane. If you’re content with the functionality that v0.28 offers, then by all means keep it that way :slight_smile:

No problem! Thank you for your help! :smiley:

1 Like