1
$\begingroup$

I have two well trained neural networks, shown as:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 42)                1806      
                                                                 
 dense_1 (Dense)             (None, 128)               5504      
                                                                 
 dense_2 (Dense)             (None, 256)               33024     
                                                                 
 dense_3 (Dense)             (None, 512)               131584    
                                                                 
 dense_4 (Dense)             (None, 256)               131328    
                                                                 
 dense_5 (Dense)             (None, 128)               32896     
                                                                 
 dense_6 (Dense)             (None, 64)                8256      
                                                                 
 dense_7 (Dense)             (None, 32)                2080      
                                                                 
 dense_8 (Dense)             (None, 28)                924       
                                                                 
=================================================================
Total params: 347,402
Trainable params: 347,402
Non-trainable params: 0
_________________________________________________________________
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 28)                812       
                                                                 
 dense_1 (Dense)             (None, 128)               3712      
                                                                 
 dense_2 (Dense)             (None, 256)               33024     
                                                                 
 dense_3 (Dense)             (None, 512)               131584    
                                                                 
 dense_4 (Dense)             (None, 256)               131328    
                                                                 
 dense_5 (Dense)             (None, 128)               32896     
                                                                 
 dense_6 (Dense)             (None, 64)                8256      
                                                                 
 dense_7 (Dense)             (None, 32)                2080      
                                                                 
 dense_8 (Dense)             (None, 4)                 132       
                                                                 
=================================================================
Total params: 343,824
Trainable params: 343,824
Non-trainable params: 0

And I want to combine them such that I replace the input layer of the second network with output layer of the first network. How can I do this in tensorflow?

$\endgroup$

1 Answer 1

2
$\begingroup$

The concatenate function can connect 2 neural networks. You just have to be careful with the dimensions between them.

Here is a pseudo-code:

from tensorflow.keras.layers import Concatenate, Dense, Input
from tensorflow.keras.models import Model

import numpy as np

input_a = ...
layer_a_1 = ...
layer_a_n = ...
output_a = Dense(1)(layer_a_n)
input_b = Input(shape=(28,))
concat_2 = Concatenate()([output_a, input_b])
layer_b_1 = Dense(1)(concat_2)
...
layer_b_n = ...
output_b = Dense(1)(layer_b_n)
model = Model(inputs=[input_a], outputs=[output_b])

model.compile(loss='mse', optimizer='sgd')
hist = model.fit([input_a], output_b, epochs=500)
$\endgroup$

Your Answer

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

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