Fusing two keras layers with different shape in python

629 views
Skip to first unread message

chra...@gmail.com

unread,
Sep 10, 2018, 9:16:30 AM9/10/18
to Keras-users
I have a layer called e5in my keras model with shape(?, 512, 1, 1). I have got also an input layer called input_conditional with shape shape=(?, 6). The code for my layers is the following:

e5 = Convolution2D(512, 4, 4, subsample=(2,2),  activation='linear',init='uniform', border_mode='same')(e4)
e5
= LeakyReLU(alpha=0.1)(e5)
e5
= BatchNormGAN()(e5)
model
= Model(input=[inputs, input_conditional], output=e5)
model
.summary()
input_conditional
= Input(shape=(6,))  
# merge = Concatenate()([e5, input_conditional])

Since the layers have different size I cannot merge them or concatenate them directly. Which operation could help  in fusing those two layers?

Sergey O.

unread,
Sep 10, 2018, 10:06:32 AM9/10/18
to chra...@gmail.com, Keras-users
The problem is the two tensors have different number of dimensions. You first have to make the dimensions the same. Luckily two of the dimensions are size=1, meaning you can easily expand the dimensions of your input_cond, or squeeze the dimensions of e5.

What if you first use a Reshape layer on input_conditional to reshape to (?,6,1,1).

Or use Flatten layer on e5 to reshape to (?,512)

It really depends what final shape you want... But flatten, followed by concatenating, might be easiest.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/21147b68-13b3-4fee-9f28-4bea23a02cee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

chra...@gmail.com

unread,
Sep 11, 2018, 10:43:42 AM9/11/18
to Keras-users
I tried to do something like: 

input_conditional  = Reshape((6, 1, 1))(input_conditional)
e5 = Concatenate([e5, input_conditional])

Firstly, reshape the input and then try to concatenate the input with the previous layer. It worked however, the e5 is not tensor anymore, If i pass it in a model it does not work anymore:

model = Model(input=[inputs], output=e5)


Sergey O.

unread,
Sep 11, 2018, 10:49:22 AM9/11/18
to Christos Athanasiadis, Keras-users
Be careful not to overwrite your input variable when you reshape. 

If you do input= some_layer(input)

Your input != Original input. So when you define model it doesn't work.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.

chra...@gmail.com

unread,
Sep 11, 2018, 11:02:46 AM9/11/18
to Keras-users
Indeed you are right. However, I am still struggling to find a way to horizontal concatenate the two tensors the e5 and the 

input_conditional1  = Reshape((6, 1, 1))(input_conditional)
e5 = Concatenate()([e5, input_conditional1])

That way does not work. 

Sergey O.

unread,
Sep 11, 2018, 11:04:42 AM9/11/18
to Christos Athanasiadis, Keras-users
I think you'd be better off using flatten on both then concate

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages