【问题标题】:Why do Keras Tuners' Fixed hyperparameters produce different results from static values?为什么 Keras Tuners 的固定超参数会产生与静态值不同的结果?
【发布时间】:2021-12-06 19:57:51
【问题描述】:

尽管我的模型应该有效地使用相同的参数,但我的超调结果却完全不同,这取决于我是使用 hp.Fixed(key, value) 还是仅使用 value(其中 value 是一个 Int)。我已经验证了每个测试的重复运行在instructions for reproducibility 之后产生了相同的结果,并为所有适用的层/初始化器/等设置了种子。即使说明书上说没有必要。

使用hp.Fixed(key, value)

使用value

查看所有超参数表,hp.Fixed 似乎根本没有做任何事情。正在测试所有超参数。

编辑:HyperbandTuner 忽略了我的自定义超模型的超参数,无论其状态如何。


这是有问题的代码:

class MyModel(kt.HyperModel):
    def __init__(self, **config):
        self.config = config
        self.seed = config.get('seed')
    
    def build_model(self):
        model = Sequential(name=self.name)
        model.add(LSTM(self.units, name='LSTM'))
        model.add(Dense(1, name='Output', kernel_initializer=GlorotUniform(seed=self.seed)))
        model.compile(loss='mean_squared_error', metrics='mean_squared_error', sample_weight_mode='temporal')
        return model
    
    # If the user has supplied the parameter manually, use hp.Fixed()
    # Otherwise, use the provided hyperparameter (default)
    def _param(self, key, default=None):
        value = self.config.get(key)
        if value is not None:
            return self.hp.Fixed(key, value)
        else:
            return default

    def build(self, hp):
        self.hp = hp
        self.units = self._param('units', hp.Int('units', 1, 200, step=5))
        return self.build_model()

【问题讨论】:

    标签: tensorflow keras hyperparameters


    【解决方案1】:

    好吧,在进行更多挖掘之后,我发现(特别是通过this tutorial 声明超参数的方式)当您编写hp.[Fixed|Choice|etc.] 时,您会立即声明这些超参数在搜索空间中的存在,无论该代码在哪里出现。

    将超参数声明视为一种特征类方法,而不是 HyperTuner 从模型中提取的常规 Python 对象。

    基本上,每个固定/选择/等。超参数方法同时在 HyperTuner 类的后台某处设置全局超参数,同时返回一个常规变量(Int/Float/String/Range/List/etc.),这样您仍然可以在 HyperTuner 最终覆盖之前无错误地构建模型在搜索阶段。

    我对此感到困惑,因为通常hp 显示为build_model() 方法或kt.HyperModel 类中的参数,其中调用分配给局部变量,然后传递给模型声明。


    这里是有问题的代码的修复:

    class MyModel(kt.HyperModel):
        def __init__(self, **config):
            self.config = config
            self.seed = config.get('seed')
        
        def build_model(self):
            model = Sequential(name=self.name)
            model.add(LSTM(self.units, name='LSTM'))
            model.add(Dense(1, name='Output', kernel_initializer=GlorotUniform(seed=self.seed)))
            model.compile(loss='mean_squared_error', metrics='mean_squared_error', sample_weight_mode='temporal')
            return model
        
        # If the user has supplied the parameter manually, use hp.Fixed()
        # Otherwise, use the provided hyperparameter (default)
        def _param(self, key, default=None):
            value = self.config.get(key)
            if value is not None:
                return self.hp.Fixed(key, value)
            else:
                return default()
    
        def build(self, hp):
            self.hp = hp
            self.units = self._param('units', lambda: hp.Int('units', 1, 200, step=5))
            return self.build_model()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      相关资源
      最近更新 更多