Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate keras layers beyond the last axis

I tried to concatenate keras layers beyond the last axis.

concat_layer = keras.layers.concatenate([layer1,layer2],axis=3);

The shapes of layer1 and layer2 are both (?,7,7),for now I want it become (?,7,7,2) rather than (?,7,14). If I wrote like axis=3, it returns "IndexError: list assignment index out of range"...

What should I do? Thank you so much!

like image 538
This is bill Avatar asked May 06 '18 09:05

This is bill


People also ask

What is the difference between concatenate () and concatenate () layers in keras?

Keras has two basic organizational modes: "Sequential" and "Functional". concatenate is the functional version, and really just wraps the Concatenate layer.

What is Axis in keras concatenate?

Concatenate(axis=-1, **kwargs) Layer that concatenates a list of inputs. It takes as input a list of tensors, all of the same shape except for the concatenation axis, and returns a single tensor that is the concatenation of all inputs.


1 Answers

Keras backend has an expand_dim operation, which you can you use with the Lambda layer. Try:

import keras.backend as K
from keras.layers import Lambda, concatenate

layer1 = Lambda(lambda x: K.expand_dims(x, axis=3))(layer1)
layer2 = Lambda(lambda x: K.expand_dims(x, axis=3))(layer2)
concat_layer = concatenate([layer1, layer2], axis=3)
like image 116
Mark Loyman Avatar answered Oct 02 '22 11:10

Mark Loyman