I already made a model without residual connection which compile and fit without any errors [using Keras Sequential API]
I wish to test a modified version just adding a residual connection like in SPEECH ENHANCEMENT BASED ON DEEP NEURAL NETWORKS WITH SKIP CONNECTIONS. So, I need to use Functional API instead.
My problem is extract a piece in the middle of inputs. I tried that.
INPUT_SIZE = N*OUTPUT_SIZE # N must be odd
HIDDEN_SIZE = N*OUTPUT_SIZE # N must be odd
modelInputs = Input(shape=(INPUT_SIZE,))
x = Dense(HIDDEN_SIZE, activation='relu', kernel_initializer=INPUT_KERNEL_INITIALIZER)(modelInputs)
for _ in np.arange(1,N_HIDDEN):
x = Dense(HIDDEN_SIZE, activation='relu', kernel_initializer=INPUT_KERNEL_INITIALIZER)(x)
Y = Dense(OUTPUT_SIZE, activation='relu', kernel_initializer=INPUT_KERNEL_INITIALIZER)(x)
# --------------------------------------------------------
# Here, 4 options I tried to get "modelInputs_selected"
# --------------------------------------------------------
# Try 1
modelInputs_selected = Lambda(lambda x: x[int(N/2)*OUTPUT_SIZE:(int(N/2)+1)*OUTPUT_SIZE])(modelInputs)
# Try 2 [Try 1 with 'output_shape' filled]
modelInputs_selected = Lambda(lambda x: x[int(N/2)*OUTPUT_SIZE:(int(N/2)+1)*OUTPUT_SIZE, :], output_shape=(OUTPUT_SIZE,))(modelInputs)
# Try 3
modelInputs_selected = K.transpose(K.gather(K.transpose(modelInputs), K.arange(int(N/2)*OUTPUT_SIZE, (int(N/2)+1)*OUTPUT_SIZE)))
# Try 4 [Try 3 unwrapped]
toto1 = K.transpose(modelInputs)
toto2 = K.gather(toto1, K.arange(int(N/2)*OUTPUT_SIZE, (int(N/2)+1)*OUTPUT_SIZE))
modelInputs_selected = K.transpose(toto2)
# --------------------------------------------------------
# End of option tried
# --------------------------------------------------------
predictions = add([modelInputs_selected, Y])
model = Model(inputs=modelInputs, outputs=predictions)
Results are:
- Try 1 & Try 2:
- Error = Incoherent shape during add()
- Try 3 & Try 4:
- Good shapes for add()
- Error with Model(...)
- I gone into Model() step by step. We go backward starting with the last layer
- Output add() OK
- Previous one K.transpose(): Error AttributeError: 'Tensor' object has no attribute '_keras_history' in "build_map_of_graph"
The model construction failed because I use a function from the backend (TensorFlow, for me)?
Anyone can help, please?
Maybe if I use a multiply()?
- modelInputs is (m, N*OUTPUT_SIZE) and modelInputs_selected is (m,OUTPUT_SIZE)
- With the good matrix A (N*OUTPUT_SIZE, OUTPUT_SIZE): modelInputs_selected = multiply(modelInputs, A)