@isaacdevlugt Thank you for your clear explanation and reminder. I forgot to convert the label into a binary matrix.
Here I made some slight changes to the code, but I am currently trying to solve a problem “Gradients do not exist for variables [‘weights:0’] when minimizing the loss.” I have referred to the method in the Quantum convolution neural network using Keras, and it still couldn’t solve this problem.
WARNING:tensorflow:Gradients do not exist for variables ['weights:0'] when minimizing the loss. If you're using `model.compile()`, did you forget to provide a `loss` argument?
qnode_weights output:
<tf.Variable 'weights:0' shape=(5,) dtype=float64, numpy=array([ 0.72691341, -0.59184832, 0.73965137, 0.66283934, 0.39719912])>
Traceback while using lightning_gpu :
File ~/anaconda3/envs/pennylane/lib/python3.9/site-packages/pennylane_lightning/lightning_gpu/lightning_gpu.py:347, in LightningGPU.reset(self)
345 super().reset()
346 # init the state vector to |00..0>
--> 347 self._gpu_state.resetGPU(False)
default.qubit:
File ~/anaconda3/envs/pennylane/lib/python3.9/site-packages/tensorflow/python/framework/ops.py:5979, in is_auto_dtype_conversion_enabled()
5976 def is_auto_dtype_conversion_enabled():
5977 return (
5978 _dtype_conversion_mode == PromoMode.ALL
-> 5979 or _dtype_conversion_mode == PromoMode.SAFE
5980 )
Full code:
import tensorflow as tf
# tf.keras.backend.set_floatx('float64')
from tensorflow.keras.datasets import cifar10
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers.experimental import preprocessing
(x_train_full, y_train_full), (x_test, y_test) = cifar10.load_data()
x_train_full=x_train_full[:10]
y_train_full=y_train_full[:10]
x_test=x_test[:10]
y_test=y_test[:10]
split = 0.2
x_train, x_val, y_train, y_val = train_test_split(
x_train_full, y_train_full, test_size=0.2, random_state=42)
x_train = preprocessing.Rescaling(scale=1.0/255)(x_train)
x_val = preprocessing.Rescaling(scale=1.0/255)(x_val)
y_train = y_train.reshape(y_train.shape[0],)
y_val = y_val.reshape(y_val.shape[0],)
import pennylane as qml
from pennylane import numpy as np
n_qubits = 5
# qiskit.aer, default.qubit, lightning.gpu
dev = qml.device("lightning.gpu", wires=n_qubits)
@qml.qnode(dev)
def circuit(inputs, weights):
# weights = tf.split(weights, num_or_size_splits=n_qubits, axis=0)
qml.Hadamard(wires=0)
qml.AngleEmbedding(inputs[0:4], wires=range(0,4), rotation='X')
qml.AngleEmbedding(inputs[4:8], wires=range(0,4), rotation='Y')
qml.AngleEmbedding(inputs[8:12], wires=range(0,4), rotation='Z')
qml.CRX(weights[0], wires=[0,1])
qml.CRX(weights[1], wires=[1,2])
qml.CRX(weights[2], wires=[2,3])
qml.CRX(weights[3], wires=[3,1])
qml.CPhase(weights[4], wires=[0,4])
return qml.expval(qml.PauliX(wires=0))
class MyDenseLayer(qml.qnn.KerasLayer):
def _quanv(self, image, weights):
output_array = []
r = image[:,:,0]
g = image[:,:,1]
b = image[:,:,2]
for j in range(0, 31, 1):
for k in range(0, 31, 1):
_inputs = [
r[j, k],
r[j, k + 1],
r[j + 1, k],
r[j + 1, k + 1],
g[j, k],
g[j, k + 1],
g[j + 1, k],
g[j + 1, k + 1],
b[j, k],
b[j, k + 1],
b[j + 1, k],
b[j + 1, k + 1]
]
result_qn = self.qnode(_inputs, weights)
output_array.append(result_qn)
output_array = np.array(output_array, requires_grad=False).reshape(1,31,31) # dtype='float32'
# output_array = tf.dtypes.cast(output_array, tf.float32)
output_array = tf.constant(output_array)
# print(output_array)
return output_array
def _evaluate_qnode(self, x):
batch_round = x.shape[0]
weights_qn = self.qnode_weights['weights']
# weights_qn = tf.split(weights_qn, num_or_size_splits=n_qubits, axis=0)
res_list = self._quanv(x[0], weights_qn)
# res_list = tf.constant(res_list)
if batch_round > 1:
for i in range(1, batch_round):
temp = self._quanv(x[i], weights_qn)
# temp = tf.constant(temp)
res_list = tf.concat([res_list, temp], 0)
print(res_list[0][0])
return res_list
def call(self, inputs):
results = self._evaluate_qnode(inputs)
return results
weight_shapes = {"weights": (5)}
class MyModel(tf.keras.Model):
def __init__(self):
super().__init__()
self.qlayer = MyDenseLayer(circuit, weight_shapes, output_dim=None)
self.flatten1 = tf.keras.layers.Flatten()
self.dense1 = tf.keras.layers.Dense(100, activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(10, activation=tf.nn.softmax)
self.dropout = tf.keras.layers.Dropout(0.2)
def call(self, inputs):
# all EagerTensor
x = self.qlayer(inputs)
x = self.flatten1(x)
x = self.dense1(x)
x = self.dropout(x, training=True)
print(x)
return self.dense2(x)
y_trains = tf.keras.utils.to_categorical(y_train)
y_vals = tf.keras.utils.to_categorical(y_val)
y_trains = tf.convert_to_tensor(y_trains)
y_vals = tf.convert_to_tensor(y_vals)
model = MyModel()
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
loss="categorical_crossentropy",
metrics=["accuracy"],
)
history = model.fit(
x_train,
y_trains,
validation_data=(x_val, y_vals),
batch_size=2,
epochs=5,
verbose=2,
)
tensorflow - 2.15.0
qml.about()
Name: PennyLane
Version: 0.33.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: /home/xx1/anaconda3/envs/pennylane/lib/python3.9/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning, PennyLane-Lightning-GPU, PennyLane-qiskit
Platform info: Linux-5.15.133.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Python version: 3.9.18
Numpy version: 1.26.2
Scipy version: 1.11.4
Installed devices:
- default.gaussian (PennyLane-0.33.1)
- default.mixed (PennyLane-0.33.1)
- default.qubit (PennyLane-0.33.1)
- default.qubit.autograd (PennyLane-0.33.1)
- default.qubit.jax (PennyLane-0.33.1)
- default.qubit.legacy (PennyLane-0.33.1)
- default.qubit.tf (PennyLane-0.33.1)
- default.qubit.torch (PennyLane-0.33.1)
- default.qutrit (PennyLane-0.33.1)
- null.qubit (PennyLane-0.33.1)
- lightning.qubit (PennyLane-Lightning-0.33.1)
- lightning.gpu (PennyLane-Lightning-GPU-0.33.1)
- qiskit.aer (PennyLane-qiskit-0.33.1)
- qiskit.basicaer (PennyLane-qiskit-0.33.1)
- qiskit.ibmq (PennyLane-qiskit-0.33.1)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.33.1)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.33.1)
- qiskit.remote (PennyLane-qiskit-0.33.1)
The dtype format should be fine… Could you please advise me on how to resolve this warning?
Thanks!5