Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a model into two seperate models?

Tags:

keras

For example I have a model with 3 intermediate layers:

Model1 : Input1 --> L1 --> L2 --> L3,

and want to split it into

Model2 : Input2 --> L1 --> L2

and Model3 : Input3 --> L3.

It is easy to stack these two to get the first one using functional API. But I'm not sure how to do the opposite thing.

The first split model can be obtained by: Model(Input1, L2.output), but the second one is not that easy. What is the simplest way to do this?

Example code:

# the first model
input1 = Input(shape=(784,))
l1 = Dense(64, activation='relu')(inputs)
l2 = Dense(64, activation='relu')(l1)
l3 = Dense(10, activation='softmax')(l2)
model1 = Model(inputs, l3)

I want to build model2 and model3 described above that share weights with model1 while model1 already exists (maybe loaded from disk).

Thanks!

like image 335
Jeff Dong Avatar asked Feb 25 '26 20:02

Jeff Dong


1 Answers

In short, extra Input is needed. Because the input tensor is different from the intermediate tensor.

First define the shared layers: l1 = Dense(64, activation='relu') l2 = Dense(64, activation='relu') l3 = Dense(10, activation='softmax')

Remember that input1 = Input(shape=(784,)) # input1 is a input tensor o1 = l1(input1) # o1 is an intermediate tensor

Model1 can be defined as model1 = Model(input1, l3(l2(l1(input1))) )

To define model2, you have to first define a new input tensor input2=Input(shape=(64,)). Then model2 = Model(input2, l3(l2(input2)).

like image 61
Van Avatar answered Mar 02 '26 14:03

Van