1

I wanted to extract and apply independently a Conv2D layer on the columns of my input tensors, but after adding the code:

accelerometer_input = Input(shape=(1400, 3))

for i in range(3):
    out = Lambda(lambda x: x[:,:, i:i+1])(accelerometer_input) # Extracting the ith channel
    out = K.expand_dims(out, axis=1)
    out = Conv2D(64, (30, 1), data_format="channels_first")(out)  
    branch_outputs.append(out)
out_put = K.concatenate(branch_outputs)

it gives me the error in the title. I think it is due to the Lambda layer or the extraction which is not differentiable.

But how can I do without it?

1 Answer 1

3

That's because you are directly applying a backend function (i.e. K.expand_dims()) on a Keras Tensor (i.e. out) and therefore the result would be a Tensor (and not a Keras Tensor). Actually, a Keras Tensor is an augmented version of Tensor and have additional attributes (e.g. _keras_history) which helps Keras to build the model. Now, to resolve the issue, you just need to put the backend function in a Lambda layer to have a Keras Tensor as output:

out = Lambda(lambda x: K.expand_dims(x, axis=1))(out)

The same thing applies to using K.concatenate(). However in this case, there is a specific layer for it in Keras:

from keras.layers import concatenate, Concatenate

# use functional interface
out_put = concatenate(branch_outputs)

# or use layer class
out_put = Concatenate()(branch_outputs)
Sign up to request clarification or add additional context in comments.

2 Comments

it was: out_put = K.concatenate(branch_outputs), I removed the K.and worked. Your answer helped me understand it, so if you want you can add it to your answer and i'll accept it
@FrancescoPegoraro Oh! I did not even see that line. Yeah, it is wrong as well for the same reason. You need to use the Concatenate layer instead. I have updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.