-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom Layer output shape isn't working with model.summary() #20746
Comments
The results I got when trying to recreate the issue were quite different from what is stated in the description. In fact, it seems to behave in the opposite way. Here's the code I used to reproduce the issue: import tensorflow as tf
import pennylane as qml
import numpy as np
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev)
def circuit(inputs, weights):
qml.RX(inputs[0], wires=0)
qml.RY(inputs[1], wires=1)
qml.CNOT(wires=[0, 1])
for w in weights:
qml.RZ(w, wires=0)
return [qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))]
class DenseQKan(tf.keras.layers.Layer):
def __init__(self, units: int, circuit: qml.QNode, layers: int, **kwargs):
super().__init__(**kwargs)
self.circuit = circuit
self.qubits = len(circuit.device.wires)
self.units = units
self.qbatches = None
self.layers = layers
def build(self, input_shape):
if input_shape[-1] > self.qubits:
self.qbatches = np.ceil(input_shape[-1] / self.qubits).astype(np.int32)
else:
self.qbatches = 1
self.layer_weights = []
for u in range(self.units):
self.layer_weights.append(
self.add_weight(
shape=(self.qbatches, input_shape[-1] // self.qbatches, self.layers),
initializer=tf.keras.initializers.RandomUniform(minval=-np.pi, maxval=np.pi, seed=None),
trainable=True
)
)
self.built = True
def compute_output_shape(self, input_shape):
print("Build Input Shape", input_shape)
return (input_shape[0], self.units)
def call(self, inputs):
assert self.qbatches is not None
splits = tf.split(inputs, self.qbatches, -1)
out = []
for u in range(self.units):
unit_out = 0
for qb in range(self.qbatches):
qb_out = tf.reduce_sum(
tf.stack(self.circuit(splits[qb], self.layer_weights[u][qb]), axis=-1),
axis=-1
)
unit_out = unit_out + qb_out
out.append(unit_out)
out = tf.stack(out, axis=-1)
return out
class Rescale(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def call(self, inputs):
return inputs * np.pi
def model_with_issue(input_shape=(2,), units=10, layers=3):
inp = tf.keras.layers.Input(shape=input_shape)
out = DenseQKan(units, circuit, layers, name="DenseKAN")(inp)
out = Rescale(name="RescalePi")(out)
model = tf.keras.Model(inputs=inp, outputs=out, name="Q_KAN")
return model
def model_without_issue(input_shape=(2,), units=10, layers=3):
inp = tf.keras.layers.Input(shape=input_shape)
out = DenseQKan(units, circuit, layers, name="DenseKAN")(inp)
# the apparent fix to reshape the output as suggested
out = tf.reshape(out, (tf.shape(inp)[0], units))
out = Rescale(name="RescalePi")(out)
model = tf.keras.Model(inputs=inp, outputs=out, name="Q_KAN")
return model
print("Model with the issue:")
model_issue = model_with_issue()
model_issue.summary(show_trainable=True)
print("Model without the issue:")
model_issue = model_without_issue()
model_issue.summary(show_trainable=True) When I run this, the output for
However, when I introduce the fix in
It seems that the fix causes issues due to attempting to use a Do respond in this thread if there are any additional details, as I feel like there's a piece missing from this. Environment:
|
Hi @vinayak19th @harshaljanjani - Model summary without reshape:
Model summary with reshape:
The error And instead of defining a custom |
The model.summary api doesn't seem to properly access the layer.compute_output_shapes() call.
Current Behavior
Expected Behavior
Standalone code to reproduce the issue
Custom layer code:
Appararent fix:
For some reason adding the line below as the final layer fixes the issue.
Model definition code:
The text was updated successfully, but these errors were encountered: