Here is a sample code including dummy input data.
It returns error.
Pennylane : Version: 0.16.0
Tensorflow : Version: 2.4.1
import tensorflow as tf
import keras_metrics
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
import pennylane as qml
from pennylane import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
num_of_data = 64
X = np.random.normal((num_of_data,1))
Y = np.sin(X)
n_qubits = 1
layers = 2
data_dimension = 1 # output
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev, diff_method='adjoint')
def qnode(inputs, weights):
for i in range(layers):
qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
qml.templates.StronglyEntanglingLayers(weights[i,:,:], wires=range(n_qubits))
return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]
weight_shapes = {"weights": (layers, n_qubits,3)}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)
clayer1 = tf.keras.layers.Dense(n_qubits, activation='linear')
clayer2 = tf.keras.layers.Dense(data_dimension, activation="linear")
model = tf.keras.models.Sequential([clayer1,qlayer,clayer2])
opt = tf.keras.optimizers.Adam(learning_rate=0.01)
model.compile(opt, loss='mse')
hist = model.fit(X, Y, epochs=30, validation_split=0.1, verbose=1, shuffle='True', batch_size=32)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-01ae07b47770> in <module>
23 model.compile(opt, loss='mse')
24
---> 25 hist = model.fit(X, Y, epochs=30, validation_split=0.1, verbose=1, shuffle='True', batch_size=32)
~~~~~
ValueError: Weights tensor must be 3-dimensional; got shape (1, 3)
weights[i,:,:] was treated as a 2d-array.
I tried to fix the array dimenstion by introducing np_split.
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev, diff_method='adjoint')
def qnode(inputs, weights):
weights_each_layer = np.split(weights,layers,axis=0)
for i in range(layers):
qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
qml.templates.StronglyEntanglingLayers(weights_each_layer[i], wires=range(n_qubits))
return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]
weight_shapes = {"weights": (layers, n_qubits,3)}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)
clayer1 = tf.keras.layers.Dense(n_qubits, activation='linear')
clayer2 = tf.keras.layers.Dense(data_dimension, activation="linear")
model = tf.keras.models.Sequential([clayer1,qlayer,clayer2])
opt = tf.keras.optimizers.Adam(learning_rate=0.01)
model.compile(opt, loss='mse')
hist = model.fit(X, Y, epochs=30, validation_split=0.1, verbose=1, shuffle='True', batch_size=32)
This revised code can run but I got another warning.
Epoch 1/30
WARNING:tensorflow:Gradients do not exist for variables ['sequential_9/keras_layer_9/weights:0'] when minimizing the loss.
WARNING:tensorflow:Gradients do not exist for variables ['sequential_9/keras_layer_9/weights:0'] when minimizing the loss.
1/1 [==============================] - 0s 155ms/step - loss: 0.1568 - val_loss: 0.0025
Epoch 2/30
WARNING:tensorflow:Gradients do not exist for variables ['sequential_9/keras_layer_9/weights:0'] when minimizing the loss.
WARNING:tensorflow:Gradients do not exist for variables ['sequential_9/keras_layer_9/weights:0'] when minimizing the loss.
1/1 [==============================] - 0s 75ms/step - loss: 0.1231 - val_loss: 0.0030
The error seems critical.
How can I fix it?