如何在两个 keras 层之间共享权重? [英] How to share weight between two keras layers?

查看:94
本文介绍了如何在两个 keras 层之间共享权重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在两个 Keras 层之间共享权重,例如out1out2?

How can I share weight between two Keras layers, e.g. out1 and out2?

inp1 = tf.keras.Input(shape=(100, 200, 3))
inp2 = tf.keras.Input(shape=(400, 800, 3))
out1 = tf.keras.layers.Conv2D(32, 3, strides=(2,2), padding='same', activation='relu', name='1')(inp1)
out2 = tf.keras.layers.Conv2D(32, 3, strides=(2,2), padding='same', activation='relu', name='2')(inp2)

推荐答案

如果你想在 inp1inp2 张量上应用相同的卷积层,那么你只需要首先创建层,然后在 inp1inp2 上调用它:

If you want to apply the same convolution layer on inp1 and inp2 tensors, then you just need to first create the layer and then call it on inp1 and inp2:

shared_conv = tf.keras.layers.Conv2D(32, 3, strides=(2,2), padding='same', activation='relu')
out1 = shared_conv(inp1)
out2 = shared_conv(inp2)

请参阅 Keras 文档中的共享层部分更多信息.

See shared layers section in Keras documentation for more information.

这篇关于如何在两个 keras 层之间共享权重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆