Broadcast Keras layer to sequence

Hi,

I want to apply a KerasLayer object to an input sequence with shape (batch_size, seq_len, n_features). If I use a TF2/Keras Dense(output_dim) layer, the same weights are applied to each element of the sequence. However, if the layer is a PennyLane’s KerasLayer, I get an error :

ValueError: 'features' must be of shape (4,) or smaller; got (64, 4).

where 4=number of qubits, 64=sequence length.

I can fix this temporarily by doing something like:

_, seq_len, _ = tf.shape(x)
output = tf.convert_to_tensor([self.dense(x[:, t, :]) for t in range(seq_len)])

…but I wonder if it’s a problem from my side, or if broadcasting is not implemented yet.

Hi @rdisipio!

Thanks, this is good feedback for how people want to use KerasLayer. So far, we’ve yet to implement broadcasting in this sense - but it shouldn’t be too much of an overhead to do and is a great first issue for anyone interested.

1 Like

Thanks for the reply. I wanted to make sure I was not doing anything stupid in the first place. I’ll keep an eye on future developments on this front.

1 Like

Thanks @rdisipio, and please let us know if you have any additional thoughts from your experience of using PennyLane.

I also added a feature request on PennyLane’s GitHub here.