# How to use K.function with two inputs and a concatenate layer?

In Keras, I try to compute use the K.function between some layers. But I get an error when I use a Concatenate layer.

Here is a minimal code you can try yourself:

import numpy as np
from keras.layers import *
from keras.models import Model
def test_model(concat, conv2_dim=32, kernel=(3,3), dropout=0,
width_dense=1024, batch_norm=False):
img_shape = (64,64,1)

image_input = Input(shape=img_shape)
entropy_vector = Input(shape=(1,))

conv = Conv2D(conv2_dim, kernel_size=kernel,
input_shape=img_shape)(image_input)

final_conv = Flatten()(conv)
dense = Dense(width_dense, activation='relu')(final_conv)

concat_layer = Concatenate()([dense, entropy_vector])

if(concat): #Skipping
flat = Dense(width_dense//4, activation='relu')(concat_layer)
else:
flat = Dense(width_dense//4, activation='relu')(dense)

output = Dense(1, activation='sigmoid')(flat)

model = Model(inputs=[image_input, entropy_vector], outputs=[output])
model.compile(loss='binary_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
return model

X = [np.ones((1,64,64,1)), np.ones(1,)]
m = test_model(concat=False) # you can try with False
fn = K.function(m.input, [m.output])

m.predict(X)
fn(X)


But I get this error if concat=True in the test_model function:

InvalidArgumentError: ConcatOp : Ranks of all input tensors should match: shape[0] = [1,1024] vs. shape[1] = [1]
[[{{node concatenate_18/concat}} = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](dense_112/Relu, _arg_input_44_0_1/_4327, concatenate_18/concat/axis)]]

• If I get it right, you are trying to concatenate the layers of shape [1, 1024] (that's the dense layer variable) and [1] (that's the entropy_vector layer variable). Also, the error trace says that? The layers you concatenate should have the same rank, as the error says. – Antonio Jurić Feb 12 '19 at 8:40
• If I understand you well, it's not what I meant. If you try to execute the code you'll see that with or without the concatenate layer model.predict(X) (or even fit, ...) works well the problem comes when K.function is involved. – Totem Feb 12 '19 at 8:49
• Your sentence ("But I get this error if concat=True in the test_model function:") is misleading. That's why I wrote the previous comment. I got a different error when running your code: ---> 41 fn(X) ValueError: Cannot feed value of shape (1,) for Tensor 'input_36:0', which has shape '(?, 1)', no matter what concat variable was (True or False). – Antonio Jurić Feb 12 '19 at 9:17
Use X = [np.ones((1,64,64,1)), np.ones((1, 1))].