0

I'm trying to concatenate a tensor with shape (None, 11, 1, 1) with another tensor with shape (None,1,1,1). Reading the keras.layers.Concatenate() docs I understood that the shape should be the same except for the concatenation axis which in this case would be axis 1. However when running the following code

import keras

mainInputShape = (11,1,1)
weightInputShape = (1,1,1)
mIn = keras.layers.Input(shape=mainInputShape, name='mainInput')
wIn = keras.layers.Input(shape=weightInputShape, name='weightInput') 
x = keras.layers.Concatenate(axis=0)([mIn, wIn])

I get the following error

  Cell In[10], line 7
    x = keras.layers.Concatenate(axis=1)([mIn, wIn])

  File ~\anaconda3\Lib\site-packages\keras\src\utils\traceback_utils.py:122 in error_handler
    raise e.with_traceback(filtered_tb) from None

  File ~\anaconda3\Lib\site-packages\keras\src\layers\merging\concatenate.py:70 in build
    del reduced_inputs_shapes[i][axis]

IndexError: list assignment index out of range

Could someone please explain what's happening and how to fix this? I need an output tensor of shape (None, 12,1,1)?

2
  • Hello! I can't reproduce your error. Could you specify your keras version and your tensorflow version? Thanks Commented Jun 20, 2024 at 12:21
  • tensorflow - 2.16.1 keras - 3.3.3 thanks! Commented Jun 20, 2024 at 20:42

1 Answer 1

0

Tensorflow is buggy when handling this case (report here).

>>> import numpy as np
>>> x = np.random.random((32, 11, 1, 1))
>>> y = np.random.random((32, 1, 1, 1))
>>> np.concatenate([x, y], axis=1).shape
(32, 12, 1, 1)

Sign up to request clarification or add additional context in comments.

Comments

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.