简体   繁体   中英

Load two model's weights into one on Keras

What I'm trying to do is loading Keras layers' weights from model A and model B , both with same architecture, to model C . Let me explain:

I know that typical way of loading weights is:

modelC.load_weights('name.h5')

But by doing this you can only load from one model and, as I've said before, I do not want to load all of it. Is there any way you can partially load these weights? If not, how could I solve this?

What I really want would be something like this:

modelC_weights = 0.2 * modelA_weights + 0.8 * modelB_weights

Something ugly but that shoud work:

modelC.load_weights('name.h5')
weights1 = np.array(modelC.get_weights())

modelC.load_weights('name2.h5')
weights2 = np.array(modelC.get_weights())

modelC.set_weights(0.2 * weights1 + 0.8 * weights2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM