Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Keras RepeatVector repetition be specified dynamically?

Keras's RepeatVector layer allows me to repeat a given vector/Tensor n times: https://www.tensorflow.org/api_docs/python/tf/keras/layers/RepeatVector.

However, it appears n has to be a constant. Is there any way to specify this parameter dynamically?

Use case: I'm generating variable length sequences, and RepeatVector will be followed by an LSTM layer.

like image 235
flow2k Avatar asked Oct 25 '25 20:10

flow2k


1 Answers

It can be a symbolic tensor. Therefore, you can use the backend function shape() (or alternatively tf.shape()) to dynamically find the number of timesteps from input tensor of LSTM layer:

from keras import backend as K

reps = RepeatVector(K.shape(lstm_input)[1])(lstm_out)
like image 159
today Avatar answered Oct 27 '25 23:10

today



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!