# QONN loss converges, but training acc stucks

**URL:** <https://discuss.pennylane.ai/t/qonn-loss-converges-but-training-acc-stucks/2990>\
**Category:** PennyLane Strawberry Fields\
**Created:** [May 30, 2023, 6:11pm UTC](https://discuss.pennylane.ai/t/qonn-loss-converges-but-training-acc-stucks/2990 "2023-05-30T18:11:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amandeep](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/amandeep/32/385_2.png) [@Amandeep](https://discuss.pennylane.ai/u/Amandeep)\
**Post date:** [May 30, 2023, 6:11pm UTC](https://discuss.pennylane.ai/t/qonn-loss-converges-but-training-acc-stucks/2990/1 "2023-05-30T18:11:15Z")

</div>

```auto
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.

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [May 30, 2023, 8:35pm UTC](https://discuss.pennylane.ai/t/qonn-loss-converges-but-training-acc-stucks/2990/2 "2023-05-30T20:35:04Z")

</div>

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.

```auto
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!

---

<div class="post-metadata">

**Author:** ![Amandeep](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/amandeep/32/385_2.png) [@Amandeep](https://discuss.pennylane.ai/u/Amandeep)\
**Post date:** [May 31, 2023, 2:22pm UTC](https://discuss.pennylane.ai/t/qonn-loss-converges-but-training-acc-stucks/2990/3 "2023-05-31T14:22:14Z")

</div>

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

Trying to work with qonn with qnn node.

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [May 31, 2023, 5:17pm UTC](https://discuss.pennylane.ai/t/qonn-loss-converges-but-training-acc-stucks/2990/4 "2023-05-31T17:17:32Z")

</div>

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 😄
