Kerastuner Randomsearch:TypeError:('关键字参数不理解:','激活')

Posted

技术标签:

【中文标题】Kerastuner Randomsearch:TypeError:(\'关键字参数不理解:\',\'激活\')【英文标题】:Kerastuner Randomsearch: TypeError: ('Keyword argument not understood:', 'activation')Kerastuner Randomsearch:TypeError:('关键字参数不理解:','激活') 【发布时间】:2020-08-12 09:23:45 【问题描述】:

使用 Google Colab,我尝试使用 Kerastuner 的 Randomsearch 来为我的用例找到最佳 CNN。

在我看来,一切都应该正确设置,但由于某种原因我总是得到

TypeError: ('Keyword argument not understood:', 'activation')

每当声明我的 RandomSearch。

声明我的模型的功能:

from tensorflow.keras import datasets, layers, models

def model_declaration(hp):
  cnn = models.Sequential([
    # Filtering & Pooling Layers
    layers.Conv2D(
        filters=hp.Int('filter1', min_value = 16, max_value = 128, step = 16), # Optimizing with filters from 16 to 128 in steps of 16 
        kernel_size = hp.Choice('kernel1', values=[3,6]), # Optimizing kernel size from 3 to 6
        activation ='relu',
        input_shape = (48,48,1) # always the same
        ),
    layers.MaxPooling2D(pool_size=hp.Int('max_pooling_1', min_value = 2, max_value = 4, step = 16), activation = 'relu'),
    layers.Conv2D( 
        filters=hp.Int('filter2', min_value = 16, max_value = 128, step = 16 ), # Optimizing with filters from 16 to 128 in steps of 16 
        kernel_size = hp.Choice('kernel2', values=[3,6]), # Optimizing kernel size from 3 to 6
        activation = 'relu'),
    layers.Conv2D( 
        filters=hp.Int('filter3', min_value = 8, max_value = 256, step = 16 ), # Optimizing with filters from 16 to 128 in steps of 16 
        kernel_size = hp.Choice('kernel3', values=[3,6]), # Optimizing kernel size from 3 to 6
        activation = 'relu'
        ),
    layers.Flatten(), # Flattening
  ])

  for i in range(hp.Int('dense_layers', 2, 10)): 
      cnn.add(layers.Dense(units=hp.Int('dense_parameters'), min_value = 16, max_value = 128, step = 16), activation=hp.Choice(['relu', 'tanh', 'sigmoid']))

  model.compile(optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-1, 1e-2, 1e-3, 1e-4])),
                loss = 'sparse_categorical_crossentropy',
                metrics = ['accuracy'])
  return model

这是我的随机搜索声明:

import kerastuner
from kerastuner import RandomSearch
from kerastuner.engine.hyperparameters import HyperParameter
random_search = RandomSearch(model_declaration, objective='val_accuracy', max_trials=5, directory='output', project_name='CNN best output')

Tensorflow 版本为 2.2.0-rc3 Kerastuner 版本是 1.0.1 Keras 版本是 2.3.0-tf

提前感谢您的帮助,因为我对这个主题相当陌生,所以我真的很挣扎。

【问题讨论】:

【参考方案1】:

MaxPooling2D 层没有activation 参数。检查Keras Documentation 也可以查看层规范。

【讨论】:

【参考方案2】:

MaxPooling2D 没有激活。因为 MaxPooling 是一个最小化数据的层。它没有激活,因为它遵循某种算法,该算法采用池大小并为您选择的每个区域采用最大值。

【讨论】:

以上是关于Kerastuner Randomsearch:TypeError:('关键字参数不理解:','激活')的主要内容,如果未能解决你的问题,请参考以下文章

使用 Keras Tuner RandomSearch 错误进行超参数调优

将 TensorBoard 与 Keras Tuner 一起使用

贝叶斯优化(Bayesian Optimization)深入理解

使用 Keras Tuner 的未知度量 val_accuracy

将参数发送到 Keras Tuner 模型构建器函数

ValueError: need at most 63 handles, got a sequence of length 65