How to create a Keras model with switchable input layers?

Viewed 41

Current simple model:

from tensorflow.keras import Model
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.optimizers import Adam 


def model():
    input_A = Input(shape=(6, ))
    out = Dense(64, activation="relu")(input_A)
    out = Dense(32, activation="relu")(out)
    outputs = Dense(1, activation="tanh")(out)  
    model = Model(
        inputs=input_A,
        outputs=outputs,
        name="switchable_inputs_model")
    model.compile(loss="mse", optimizer=Adam(), metrics=["accuracy"])
    return model

I want to have another input layer input_B which will not be active all the time during learning. Let us say we have two input layers: input A, input B. However, at a given time, only one input layer can be active. This selection of input layer is decided by a binary combination of information available at the execution time(learning stage). For instance, if it is 1 0, then input layer A will be used. Similarly, if it is 0 1, input layer B will be used.

How can I do this?

1 Answers

It's hard to guess from your question what you are trying to accomplish in detail, but you should carefully consider if that is necessary.

It's common practice to have an input layer of a fixed size that matches the structure of your data. You preprocess your data to match that shape.

In the domain of e.g. images this might mean: If you have images of different resolutions, you could consider cropping, padding or resizing your inputs to a fixed size.

If there is a rationale behind this please clarify.

Related