小编Ker*_*ren的帖子

尝试在 tensorflow 2.0 中使用 tensorflow.plugins.hparams 来创建一堆不同的优化器

我正在尝试使用 tensorflow.plugins.hparams 调整神经网络的超参数。

在此链接中,它提供了有关如何使用该函数调整超参数的建议代码。

如提供的链接所示,可以使用以下内容:

HP_NUM_UNITS = hp.HParam('num_units', hp.Discrete([16, 32]))
HP_DROPOUT = hp.HParam('dropout', hp.RealInterval(0.1, 0.2))
HP_OPTIMIZER = hp.HParam('optimizer', hp.Discrete(['adam', 'sgd']))

METRIC_ACCURACY = 'accuracy'

with tf.summary.create_file_writer('logs/hparam_tuning').as_default():
  hp.hparams_config(
    hparams=[HP_NUM_UNITS, HP_DROPOUT, HP_OPTIMIZER],
    metrics=[hp.Metric(METRIC_ACCURACY, display_name='Accuracy')],
  )

def train_test_model(hparams):
  model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(hparams[HP_NUM_UNITS], activation=tf.nn.relu),
    tf.keras.layers.Dropout(hparams[HP_DROPOUT]),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax),
  ])
  model.compile(
      optimizer=hparams[HP_OPTIMIZER],
      loss='sparse_categorical_crossentropy',
      metrics=['accuracy'],
  )

  model.fit(x_train, y_train, epochs=1) # Run with 1 epoch to speed things up for demo purposes
  _, accuracy = model.evaluate(x_test, y_test)
  return accuracy
Run Code Online (Sandbox Code Playgroud)

我将重点放在下一行,因为我将它作为我想做的事情的参考。线路:

tf.keras.layers.Dense(hparams[HP_NUM_UNITS], activation=tf.nn.relu),
Run Code Online (Sandbox Code Playgroud)

我最终想要做的是创建一堆不同类型的优化器,它们具有不同的学习率、衰减率、beta_1 和 beta_2 值等。这就是我试图做的: …

deep-learning hyperparameters keras tensorflow

5
推荐指数
1
解决办法
1307
查看次数