Reputation: 61
I wanted to multiply(find dot product) the output from 2 cnn layers. Unfortunately both have different dimensions. Can any one help with resizing of the tensors?
My base model is
model_base = Sequential()
# Conv Layer 1
model_base.add(layers.SeparableConv2D(32, (9, 9), activation='relu', input_shape=input_shape))
model_base.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))
# Conv Layer 2
model_base.add(layers.SeparableConv2D(64, (9, 9), activation='relu'))
model_base.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))
# Conv Layer 3
model_base.add(layers.SeparableConv2D(128, (9, 9), activation='relu'))
model_base.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))
model_base.add(layers.Conv2D(256, (9, 9), activation='relu'))
# model.add(layers.MaxPooling2D(2, 2))
# Flatten the data for upcoming dense layer
#model_base.add(layers.Flatten())
#model_base.add(layers.Dropout(0.5))
#model_base.add(layers.Dense(512, activation='relu'))
print(model_base.summary())
output from layer 2 and layer 6 are taken and tried multiplication
c1 = model_base.layers[2].output
c1 = GlobalAveragePooling2D()(c1)
p=np.shape(c1)
c3 = model_base.layers[6].output
c3 = GlobalAveragePooling2D()(c3)
x = keras.layers.multiply([c1, c3])
Getting error since both are of different dimensions. How will I multiply ?
Upvotes: 1
Views: 230
Reputation: 22031
in order to compute multiplication, u have to have two tensors with the same dimensionality. here a possibility (following your model_base structure):
c1 = model_base.layers[2].output
c1 = GlobalAveragePooling2D()(c1)
c3 = model_base.layers[6].output
c3 = GlobalAveragePooling2D()(c3)
c3 = Dense(c1.shape[-1])(c3)
x = Multiply()([c1, c3])
Upvotes: 1