Search code examples
pythontensorflowkerasdeep-learningkeras-layer

How to handle repeated input for a Keras layer?


I have a Keras model which has two input layers.

  1. a tweet of shape (20,300).
  2. five other tweets of shape (5,20,300). however this input is same for all training examples.

In other word, for each training step, there will be a different tweet (first input) and the same five tweets (second input). My second input that has a shape of (5,20,300) is very big to be repeated num_samples times and then used as an input layer to Keras model. I need a way to make the second input used inside the keras models but without repeated num_samples times.

Is there any way to handle this type of input?


Solution

  • Create a tensor with that constant input:

    fixed_tweets = keras.backend.constant(the_tweets_as_numpy)
    

    Use a regular input and a tensor input:

    input1 = Input((20,300))
    input2 = Input(tensor=fixed_tweets)
    

    Go have fun!!

    You will probably need custom layers to handle the difference between the batch size of input1 (any) and input2 (5).