【问题标题】:Tuning neural network hyperparameters when using Keras functional API使用 Keras 功能 API 时调整神经网络超参数
【发布时间】:2019-12-12 21:24:33
【问题描述】:

我有一个包含两个分支的神经网络。一个分支将输入输入到卷积神经网络。其他分支是全连接层。我合并这两个分支,然后使用 softmax 得到输出。我不能使用顺序模型,因为它已被弃用,因此必须使用功能 API。 我想调整卷积神经网络分支的超参数。例如,我想弄清楚我应该使用多少个卷积层。如果它是一个顺序模型,我会使用 for 循环,但由于我使用的是功能性 API,所以我不能真正做到这一点。我附上了我的代码。谁能告诉我如何以一种智能的方式优化我的神经网络的卷积数量,而不是使用不同数量的卷积层制作许多不同的脚本。

建议将不胜感激。


i1 = Input(shape=(xtest.shape[1], xtest.shape[2]))

###Convolution branch
c1 = Conv1D(128*2, kernel_size=ksize,activation='relu',kernel_regularizer=keras.regularizers.l2(l2_lambda))(i1)
c1 = Conv1D(128*2, kernel_size=ksize, activation='relu',kernel_regularizer=keras.regularizers.l2(l2_lambda))(c1)
c1 = AveragePooling1D(pool_size=ksize)(c1)
c1 = Dropout(0.2)(c1)

c1 = Conv1D(128*2, kernel_size=ksize, activation='relu',kernel_regularizer=keras.regularizers.l2(l2_lambda))(c1)
c1 = AveragePooling1D(pool_size=ksize)(c1)
c1 = Dropout(0.2)(c1)

c1 = Flatten()(c1)

###fully connected branch
i2 = Input(shape=(5000, ))
c2 = Dense(64,  activation='relu',kernel_regularizer=keras.regularizers.l2(l2_lambda))(i2)
c2 = Dropout(0.1)(c2)


###concatenating the two branches
c = concatenate([c1, c2])

x = Dense(256, activation='relu', kernel_initializer='normal',kernel_regularizer=keras.regularizers.l2(l2_lambda))(c)
x = Dropout(0.25)(x)

###Output branch 
output = Dense(num_classes, activation='softmax')(x)

model = Model([i1, i2], [output])

model.summary()

对于顺序模型,我可以使用 for 循环,例如:


layers = [1,2,3,4,5]

b1 = Sequential()
b1.add(Conv1D(128*2, kernel_size=ksize,
                 activation='relu',
                 input_shape=( xtest.shape[1], xtest.shape[2]),
                 kernel_regularizer=keras.regularizers.l2(l2_lambda)))

for layer in layers:
    count = layer
    while count > 0:
        b1.add(Conv1D(128*2, kernel_size=ksize, activation='relu',kernel_regularizer=keras.regularizers.l2(l2_lambda)))
        count -= 1

b1.add(MaxPooling1D(pool_size=ksize))
b1.add(Dropout(0.2))

b1.add(Flatten())
b2 = Sequential()

b2.add(Dense(64, input_shape = (5000,), activation='relu',kernel_regularizer=keras.regularizers.l2(l2_lambda)))

for layer in layers:
    count = layer
    while count > 0:
    b2.add(Dense(64,, activation='relu',kernel_regularizer=keras.regularizers.l2(l2_lambda)))


model = Sequential()
model.add(Merge([b1, b2], mode = 'concat'))
model.add(Dense(256, activation='relu', kernel_initializer='normal',kernel_regularizer=keras.regularizers.l2(l2_lambda)))
model.add(Dropout(0.25))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=keras.optimizers.Adam(),
                  metrics=['accuracy'])


【问题讨论】:

  • 你能展示一下你会用顺序模型做什么吗?
  • 我刚刚添加了一个代码,如果它不被弃用,我会怎么做。

标签: python-3.x keras conv-neural-network


【解决方案1】:

这是使用 Keras 功能 API 的具有可变层数的模型的最小示例:

from keras.layers import Input, Conv2D, Dense, Dropout, Flatten, MaxPool2D
from keras.models import Model

def build_model(num_layers, input_shape, num_classes): 
  input = Input(shape=input_shape)
  x = Conv2D(32, (3, 3), activation='relu')(input)

  # Suppose you want to find out how many additional convolutional 
  # layers to add here.
  for _ in num_layers:
    x = Conv2D(32, (3, 3), activation='relu')(x)

  x = MaxPool2D((2, 2))(x)
  x = Flatten()(x)
  x = Dense(64, activation='relu')(x)
  x = Dropout(0.5)(x)
  x = Dense(num_classes, activation='softmax')(x)

  return Model(inputs=input, outputs=x)

model = build_model(num_layers=2, input_shape=(128, 128), num_classes=3)

这些是我将遵循的步骤来找出要使用多少个“中间”卷积层:

  1. num_layers 参数设置为不同的值来训练几个模型。构建所有这些模型的代码完全相同,只是 num_layers 参数的值在不同的训练运行中会发生变化。
  2. 选择您关心的指标值最高的一项。

就是这样!

旁注:据我所知,Keras Sequential 模型并未被弃用。

【讨论】:

  • 谢谢。你拯救了我的一天。我也是这么想的,但是如果我明显地制作顺序模型,我就不能再使用连接/合并功能了。
【解决方案2】:

您也可以使用函数式 API 动态设置模型结构。对于卷积分支,您可以使用以下内容:

layer_shapes = (64, 64, 32)

for _ in layers:
    b1 = Conv1D(128*2, kernel_size=ksize, activation='relu', kernel_regularizer=keras.regularizers.l2(l2_lambda))(b1)

您只需要将Sequential.add 替换为对应的变量赋值即可。

【讨论】:

    猜你喜欢
    • 2020-09-29
    • 2022-01-23
    • 2020-06-29
    • 1970-01-01
    • 2018-10-08
    • 2022-01-10
    • 2018-05-22
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多