I'm reproducing two versions of what I believe to be the same code, but one of them works, the other doesn't:
from tensorflow.keras.layers import Dense, Input, Lambda, concatenate
from tensorflow.keras.models import Model
inp = Input(shape=(9,))
# Version 1 (works)
out_1 = Dense(1)(Lambda(lambda x: x[:,0:4])(inp))
out_2 = Dense(1)(Lambda(lambda x: x[:,4:9])(inp))
out = concatenate([out_1, out_2])
model = Model(inp, out)
model.compile(...)
model.fit(...) ✓
# Version 2 (doesn't work)
out = concatenate([Dense(1)(Lambda(lambda x: x[:,i:j])(inp)) for i, j in [(0, 4), (4, 9)]]) # concatenating with a list comprehension
model = Model(inp, out)
model.compile(...)
model.fit(...) ✗
The error message is:
ValueError: Input 0 of layer dense_2 is incompatible with the layer: expected axis -1 of input shape to have value 4 but received input with shape (None, 5)
I'm not sure if this is a bug or an error on the code, but it looks like the concatenation is mixing the tensors when using a list comprehension. Helps appreciated :)
As a matter of clearing, the compile and fit functions are the same for both cases:
import numpy as np
X = np.random.uniform(0, 1, (100, 9))
Y = np.random.uniform(0, 1, (100, 2))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, Y)