Use 1D array from Keras Input as index for a list

I have the following list of Encoders:

fam_specific_enc = [
    Encoder(num_layers=1, 
            num_heads=num_heads_enc, 
            hidden_dim=dense_dim_enc/2,
            dropout_rate=drop_enc,
            name=f"enc_{fam}")
    for fam in range(vocab_size_fam_lang)
]

I have the following input layer in my model:

lang_fam_input = keras.Input(
    shape=(1), dtype="int64", name="fam_lang"
)

I want to call a specific encoder from the list above by using a value in lang_fam_input, as it’s basically a list of integers, so it should ideally look something like this: fam_specific_enc[lang_fam_input](x). But this obviously does not work and I can’t transform KerasTensor to integer value either.

Hi @acraev,

Sorry for the delay in response.
To call a specific encoder from the fam_specific_enc list based on the value in the lang_fam_input, I would suggest to use a Lambda layer in your Keras model where it allows you to index into the fam_specific_enc list using lang_fam_input and apply the corresponding encoder to the input data x. Please refer to this official documentation for more information about lambda layer.

Hope this helps.Thank You.