0

我想创建一个可以保存图层不同设置的列表,以便以后可以合并,这里是原始代码

_conv = Conv2D(64, kernel_size=[32,1])(_input)
_norm = BatchNormalization()(_conv)
_activ = Activation("relu")(_norm)
_maxpool_1 = MaxPooling2D()(_activ)

_conv = Conv2D(64, kernel_size=[32,2])(_input)
_norm = BatchNormalization()(_conv)
_activ = Activation("relu")(_norm)
_maxpool_2 = MaxPooling2D()(_activ)

_conv = Conv2D(64, kernel_size=[32,3])(_input)
_norm = BatchNormalization()(_conv)
_activ = Activation("relu")(_norm)
_maxpool_3 = MaxPooling2D()(_activ)

_conv = Conv2D(64, kernel_size=[32,4])(_input)
_norm = BatchNormalization()(_conv)
_activ = Activation("relu")(_norm)
_maxpool_4 = MaxPooling2D()(_activ)

merged_tensor = concatenate([_maxpool_1, _maxpool_2, _maxpool_3, _maxpool_4])

正如您所看到的,除了内核大小之外它们都是相同的,所以为了简化代码,我可以创建这样的东西吗?(基本上是一个循环和一个列表)

_maxpool_list=[]
for i in range(1,5):
    _conv = Conv2D(64, kernel_size=[32,i])(_input)
    _norm = BatchNormalization()(_conv)
    _activ = Activation("relu")(_norm)
    _maxpool_list.append((MaxPooling2D()(_activ))

merged_tensor = concatenate(_maxpool_list)

或者,我的问题可能是,创建 keras 图层列表的最佳方法是什么,以便我以后可以加载所有图层

4

1 回答 1

2
def applyLayerGroup(kernelSize, _input):
    _conv = Conv2D(64, kernel_size=[32,kenelSize], padding='same')(_input)
    _norm = BatchNormalization()(_conv)
    _activ = Activation("relu")(_norm)
    _maxpool = MaxPooling2D()(_activ)

    return _maxpool

sizes = [1,2,3,4]
_maxpool_list= [getLayerGroup(size,_input) for size in sizes]
merged_tensor = Concatenate()(_maxpool_list)

添加padding='same'以避免错误。(归功于@user36624)

于 2018-05-11T01:09:23.600 回答