This error has been addressed here in numerous questions but it seems mine is different than all of them.
I am building a (quantum) convolutional neural network. In order to do that, I created a custom layer which acts as the (quantum) convolution and outputs 1 float for each image patch sent into it.
This is my test code for the (Q)CNN model:
stride = 2 # Length of stride in convolution operationbatch_size = 64 # Number of training samples used in each iteration# QCNN (convolves the input image with many applications of the same quantum circuit)def HybridModel(data): # Define the input layer inputs = tf.keras.Input(shape=list(data[0].shape), batch_size=batch_size, name='Input') # Extract image patches patches = tf.image.extract_patches(images=inputs, sizes=[1, 2, 2, 1], strides=[1, stride, stride, 1], rates=[1, 1, 1, 1], padding='VALID', name='ExtractPatches') print(1, patches) # Reshape patches (because otherwise I get an error about the shape) reshaped = tf.keras.layers.Reshape([-1], name='Reshape')(patches) print(2, reshaped) # Loop over the coordinates of the top-left pixel of 2x2 squares q_results = [] n_rows = (image_height - 1) // stride + 1 n_cols = (image_width - 1) // stride + 1 for j in range(0, n_rows): for k in range(0, n_cols): # Process a squared 2x2 region of the image with a quantum circuit q_results.append(QuantumConv(reshaped[:, 4*(j*n_cols+k):4*(j*n_cols+k+1)])) print(3, len(q_results), q_results[0].shape) # Transpose & flatten the output from quantum convolution quantum_conv = tf.concat(q_results, axis=-1, name='Transpose&Flatten') print(4, quantum_conv) # Apply dense layer with 2 outputs and softmax activation outputs = tf.keras.layers.Dense(2, activation='softmax', name='Dense')(flatten) print(5, outputs) # Create the model model = tf.keras.Model(inputs=inputs, outputs=outputs, name='QCCNN') model.compile(optimizer='adam', # stochastic gradient descent loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.run_eagerly = True return model# Train hybrid modelh_model = HybridModel(train_images)# print(h_model.summary())with tf.device('/CPU:0'): # otherwise it gives the error "JIT compilation failed." h_history = h_model.fit(train_images, train_labels, validation_data=(test_images, test_labels), batch_size=batch_size, epochs=1, verbose=2)The model compiles and returns its summary without problems. Here is the output of the print functions along the code:
1 KerasTensor(type_spec=TensorSpec(shape=(64, 9, 6, 4), dtype=tf.float32, name=None), name='tf.image.extract_patches_87/ExtractImagePatches:0', description="created by layer 'tf.image.extract_patches_87'")2 KerasTensor(type_spec=TensorSpec(shape=(64, 216), dtype=tf.float32, name=None), name='Reshape/Reshape:0', description="created by layer 'Reshape'")3 54 (64, 1)4 KerasTensor(type_spec=TensorSpec(shape=(64, 54), dtype=tf.float32, name=None), name='tf.concat_6/concat:0', description="created by layer 'tf.concat_6'")5 KerasTensor(type_spec=TensorSpec(shape=(64, 2), dtype=tf.float32, name=None), name='Dense/Softmax:0', description="created by layer 'Dense'")The shapes of the tensors seem correct in theory. However, when I try to train it with real data, it gives the following error:
---------------------------------------------------------------------------InvalidArgumentError Traceback (most recent call last)Cell In[123], line 67 64 # h_model = HybridModel(a) 65 # print(h_model.summary()) 66 with tf.device('/CPU:0'):---> 67 h_history = h_model.fit(train_images, train_labels, validation_data=(test_images, test_labels), batch_size=batch_size, epochs=1, verbose=2) 68 # h_history = h_model.fit(a, train_labels, validation_data=(b, test_labels), batch_size=batch_size, epochs=1, verbose=2)File c:\Users\bravo\AppData\Local\anaconda3\envs\qml\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs) 67 filtered_tb = _process_traceback_frames(e.__traceback__) 68 # To get the full stack trace, call: 69 # `tf.debugging.disable_traceback_filtering()`---> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tbFile c:\Users\bravo\AppData\Local\anaconda3\envs\qml\lib\site-packages\pennylane\qnn\keras.py:388, in KerasLayer.call(self, inputs) 386 if has_batch_dim: 387 new_shape = tf.concat([batch_dims, tf.shape(results)[1:]], axis=0)--> 388 results = tf.reshape(results, new_shape) 390 return resultsInvalidArgumentError: Exception encountered when calling layer "QuantumConv" " f"(type KerasLayer).{{function_node __wrapped__Reshape_device_/job:localhost/replica:0/task:0/device:CPU:0}} Input to reshape is a tensor with 4 values, but the requested shape has 64 [Op:Reshape]Call arguments received by layer "QuantumConv" " f"(type KerasLayer):• inputs=tf.Tensor(shape=(64, 4), dtype=float32)"Input to reshape is a tensor with 4 values, but the requested shape has 64 [Op:Reshape]"
This makes it seem like I set the custom layer to receive 64 samples of 4 values but it is only receiving 1 sample of 4 values and assumes those values as the first dimension.
How do I solve this?