QONN loss converges, but training acc stucks

import tensorflow as tf
import pennylane as qml

import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import make_moons

# Set random seeds
np.random.seed(42)
tf.random.set_seed(42)

from tensorflow import keras
from tensorflow.keras import layers

from sklearn import datasets
from sklearn.decomposition import PCA

# import some data to play with
iris = datasets.load_iris()
X = iris.data  # we only take the first two features.
y = iris.target

from sklearn.preprocessing import normalize

X=normalize(X,axis=0)

randomize = np.arange(len(X))
np.random.shuffle(randomize)
X = X[randomize]
y = y[randomize]


X_train=X[:120]
X_test=X[120:]
y_train=y[:120]
y_test=y[120:]


n_qubits = 4

dev = qml.device("strawberryfields.fock", wires=4, cutoff_dim=4) 

def layer(theta, phi, wires):
    M = len(wires)
    phi_nonlinear = np.pi / 2

    qml.templates.Interferometer(
        theta, phi, np.zeros(M), wires=wires, mesh="triangular", beamsplitter="pennylane"
    )

    for i in wires:
        qml.Kerr(phi_nonlinear, wires=i)

@qml.qnode(dev)
def quantum_neural_net(inputs, weights):
    wires = list(range(len(inputs)))
 

    for i in wires:
        qml.FockState(inputs[i], wires=i)

    # "layer" subcircuits
    for i, v in enumerate(weights):
        layer(v[: len(v) // 2], v[len(v) // 2 :], wires)

    return [qml.expval(qml.NumberOperator(w)) for w in wires]

num_layers =4
M = len(X[0])
print(M)
num_variables_per_layer = M * (M - 1)

weight_shapes = {"weights": (num_layers, num_variables_per_layer)}
weight_shapes

qlayer = qml.qnn.KerasLayer(quantum_neural_net, weight_shapes, output_dim=4)

clayer_2 = tf.keras.layers.Dense(3, activation="softmax")
model = tf.keras.models.Sequential([qlayer, clayer_2])

opt = keras.optimizers.SGD()
model.compile(opt, loss = 'categorical_crossentropy', metrics =[tf.keras.metrics.CategoricalAccuracy()])

fitting = model.fit(X_train,y_train,validation_data=(X_test,y_test),batch_size=8,epochs=10,verbose=1)

hi, i tried different hyper parameters, optimizer. but accuracy remains same. Not able to make progress in it. can you please help in this.

Hey @Amandeep!

I think this is probably an issue with the structure of your model in the context of the data you want to learn.

out1 = qlayer(X[0])
print(out1.reshape(1, -1))
out2 = clayer_2(out1.reshape(1, -1))
print(out2, y)

'''
tf.Tensor([[0. 0. 0. 0.]], shape=(1, 4), dtype=float64)
tf.Tensor([[0.33333334 0.33333334 0.33333334]], shape=(1, 3), dtype=float32) [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
 0 0 0 2 1 1 0 0 1 2 2 1 2 1 2 1 0 2 1 0 0 0 1 2 0 0 0 1 0 1 2 0 1 2 0 2 2
 1 1 2 1 0 1 2 0 0 1 1 0 2 0 0 1 1 2 1 2 2 1 0 0 2 2 0 0 0 1 2 0 2 2 0 1 1
 2 1 2 0 2 1 2 1 1 1 0 1 1 0 1 2 2 0 1 2 2 0 2 0 1 2 2 1 2 1 1 2 2 0 1 2 0
 1 2]
'''

I think your model isn’t learning well because it simply can’t!

@isaacdevlugt
can you please suggest what changes should i make in model.

Trying to work with qonn with qnn node.

Answering that question is part of doing research! You are in the best position to know what your research needs are, but we’re here to help if you have a bug or questions about pennylane technicalities :smile: