Keras:在具有不同名称的图层之间共享权重

时间:2019-06-04 13:39:26

标签: python tensorflow keras deep-learning

Keras的标准配方:

# This layer can take as input a matrix
# and will return a vector of size 64
shared_lstm = LSTM(64)

# When we reuse the same layer instance
# multiple times, the weights of the layer
# are also being reused
# (it is effectively *the same* layer)
encoded_a = shared_lstm(tweet_a)
encoded_b = shared_lstm(tweet_b)

为我们提供了一个共享的层,即采用不同的张量作为输入和输出,但在它们上均等地工作。层名称将类似于“ lstm_1”。

有没有办法创建两个具有不同名称的不同(卷积)层,但共享其内核权重和偏差权重?

更新

让我澄清一下目的。我有一个带有特征图金字塔的对象检测模型(单发检测器)。这些功能图像这样相互叠置:

det_ctx1 (32x32, Conv+ReLU+MaxPool) -> det_ctx2 (16x16, Conv+ReLU+MaxPool) -> ...

检测预测在技术上被实现为卷积层,这些卷积层从这些特征图产生偏移量和类置信度:

det_ctx1 -> det_ctx1_conf
         -> det_ctx1_loc

   |
   \/
det_ctx2 -> det_ctx2_conf
         -> det_ctx2_loc

   |
   \/
det_ctx3 -> det_ctx3_conf
         -> det_ctx3_loc

det_ctx1_confdet_ctx2_conf在语义上是相同的,我想利用权重共享来更好地训练那些图层(在正常情况下,训练样本被分配给某些金字塔等级,因此某些等级训练样本太少)。

0 个答案:

没有答案
相关问题