How to concatenate an input and a matrix in Keras

Viewed 2895

I am building a model in Keras. I have an input

X = Input(shape=(input_size, ), name='input_feature')

and a fixed pre-given numpy matrix D which is input_size by n.

I want to concatenate X and D before input them to the next layer. In other word, I need to concatenate each slice of X and D to generate a new inpt whose expected size should be (none, input_size, n+1). So what should I do to concatenate them? In my understanding, the batch size is none since it will adaptive to the batch size of input X when we fit data to the model.

1 Answers

Answer if D has shape (batch, input_size,n)

Provided that D is a tensor (it's a tensor if it's an output from some layer):

X = Reshape((input_size,1))(X)
concat = Concatenate()([D,X])

If D is not a tensor:

import keras.backend as K

#create a tensor:
Dval = K.variable(numpyArrayForD)

#create an input for D:
D = Input(tensor=Dval)

#do as in the top of this answer. 

If you want to avoid the additional Input (it will not affect the way you train, because of the tensor parameter), you can use a lambda layer:

def concatenation(x):
    D = K.variable(D_df)

    return K.concatenate([x,D])

XD = Lambda(concatenation,output_shape=(input_size,n+1))(X)

Answer if D has shape (input_size,n)

In this case, it's probably better replicate D many times. You can do this outside of the model, using numpy functions before creating the K.variable (see the other answer), like this:

D_df = D_df.reshape((1,input_size,n))
D_df = numpy.repeat(D_df,batch,axis=0)

But this approach requires you to adapt to the size of x beforehand. If you want something that adapts to any size of X without having to change D before, then it's more complicated....

Related