简体   繁体   English

使用 Keras Sequential 模型实现快捷方式

[英]Implement shortcut with Keras Sequential model

I have implemented shortcut with the Keras functional model this way:我以这种方式使用 Keras 功能模型实现了快捷方式:

inputs = ...

# shortcut path
shortcut = ShortcutLayer()(inputs)

# main path
outputs = MainLayer()(inputs)

# add main and shortcut together
outputs = Add()([outputs, shortcut])

Is it possible to convert this to a Sequential model, so that I don't need to know inputs in advance?是否可以将其转换为顺序模型,以便我不需要提前知道inputs

Basically, what I want to achieve looks like:基本上,我想要实现的目标是:

def my_model_with_shortcut():
    # returns a Sequential model equivalent to the functional one above

model = my_model_with_shortcut()

inputs = ...
outputs = model(inputs)

I would try the following;我会尝试以下方法;

def my_model_with_shortcut():
    def _create_shortcut(inputs):
         # here create model as in case you know inputs, e.g.:
         aux = Dense(10, activation='relu')(inputs)
         output = Dense(10, activation='relu')(aux)
         return output  
    return _create_shortcut

Now you should have your scenario possible.现在你应该可以实现你的场景了。

Usually I create a callable object that maintains the model.通常我会创建一个可调用的对象来维护模型。 I do this when I want this segment to be reused many times with the same weights .当我希望以相同的权重多次重复使用该段时,我会这样做。

You can try Marcin's approach, but you will create a new model every time you call the function.您可以尝试 Marcin 的方法,但每次调用该函数时都会创建一个新模型。 It may be enough for you, depending on your purposes.根据您的目的,这对您来说可能就足够了。 My answer has these differences:我的回答有以下区别:

  • Advantage: you reuse the same model with the same weights as many times as you want优点:您可以根据需要多次重复使用具有相同权重的相同模型
  • Disadvantage: after the first input, all new inputs cannot have different shapes (a consequence of sharing the weights)缺点:在第一次输入之后,所有新输入不能有不同的形状(共享权重的结果)

Code:代码:

from keras.models import Model
from keras.layers import *
import numpy as np

class ModelSegment():
    def __init__(self):

        self.shortcutLayer = Dense(10,activation='sigmoid')
        self.mainLayer = Dense(10,activation='sigmoid')
        self.addLayer = Add()

        self.model = None

    def __call__(self,inputs):

        if self.model is None:
            shortcut = self.shortcutLayer(inputs)
            outputs = self.mainLayer(inputs)
            outputs = self.addLayer([outputs,shortcut])

            #creating this model is not really necessary
            #you can skip the following code line and discard the if clause
            #but since you stated you wanted a "model" I added it here
            self.model = Model(inputs,outputs)

            return outputs
        else:
            return self.model(inputs)



segment = ModelSegment()

inputTensor = Input((13,))
inputTensor2 = Input((13,))
out = segment(inputTensor)
out2 = segment(inputTensor2)

m1 = Model(inputTensor,out)
m2 = Model(inputTensor2,out2)

m1.summary()
m2.summary()

w1 = m1.get_weights()
w2 = m2.get_weights()

for arr1,arr2 in zip(w1,w2):
    print((arr1==arr2).all())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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