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, 
                     optimizer='adam', extra_conv=False, padding='valid', 
                     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,
                     activation='relu', padding=padding,
    conv = MaxPooling2D(pool_size=(2, 2), padding=padding,)(conv)

    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)
        flat = Dense(width_dense//4, activation='relu')(dense)

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

    model = Model(inputs=[image_input, entropy_vector], outputs=[output])
    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])


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)]]
     [[{{node dense_114/Sigmoid/_4329}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_68_dense_114/Sigmoid", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
  • $\begingroup$ 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. $\endgroup$ – Antonio Jurić Feb 12 '19 at 8:40
  • $\begingroup$ 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. $\endgroup$ – Totem Feb 12 '19 at 8:49
  • $\begingroup$ 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). $\endgroup$ – Antonio Jurić Feb 12 '19 at 9:17

Use X = [np.ones((1,64,64,1)), np.ones((1, 1))].

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.