【发布时间】:2020-12-29 11:13:26
【问题描述】:
我知道scikit-optimize 包,我对贝叶斯优化比较陌生,我想在我当前的卷积神经网络中使用它。但是,我尝试使用Bayesian-optimization 找到卷积神经网络的最佳超参数,但我目前的尝试无法正常工作。
到目前为止,我试图为此目的提出实现,但我的代码无法正常工作,我不知道我的代码的哪一部分仍然存在问题。谁能指出我如何做到这一点?为了找到最佳超参数,在卷积神经网络上使用贝叶斯优化是否有任何有效的实现?有什么可能的想法吗?
更新
我为我的卷积神经网络尝试了GridSearchCV、RandomSearchCV,它的层非常深,使用GridSearchCV 花费了太多时间来完成,即使 2-3 天也无法完成优化。我想使用新的优化框架,如贝叶斯优化(即skopt、optuna)来寻找卷积神经网络的最佳参数和超参数。任何人都可以为my current attempt 1 in colab 和my attempt 2 in colab 提供可能的补救措施和有效方法吗?有什么想法吗?
我目前的尝试:
这是我目前使用scikit-optimize 包进行贝叶斯优化的尝试。这是my attempt in this colab,在那里我进行了所有在卷积神经网络上实施贝叶斯优化的实验,以找到它的最佳超参数:
### function returned to Bayesian Optimization
@use_named_args(dimensions=dimensions)
def bayes_opt(cnn_num_steps, cnn_init_epoch, cnn_max_epoch,
cnn_learning_rate_decay, cnn_batch_size, cnn_dropout_rate, cnn_init_learning_rate):
global iteration, num_steps, init_epoch, max_epoch, learning_rate_decay, dropout_rate, init_learning_rate, batch_size
num_steps = np.int32(cnn_num_steps)
batch_size = np.int32(cnn_batch_size)
learning_rate_decay = np.float32(cnn_learning_rate_decay)
init_epoch = np.int32(cnn_init_epoch)
max_epoch = np.int32(cnn_max_epoch)
dropout_rate = np.float32(cnn_dropout_rate)
init_learning_rate = np.float32(cnn_init_learning_rate)
tf.reset_default_graph()
tf.set_random_seed(randomState)
sess = tf.Session()
(train_X, train_y), (test_X, test_y) = cifar10.load_data()
train_X = train_X.astype('float32') / 255.0
test_X = test_X.astype('float32') / 255.0
targets = tf.placeholder(tf.float32, [None, input_size], name="targets")
model_learning_rate = tf.placeholder(tf.float32, None, name="learning_rate")
model_dropout_rate = tf.placeholder_with_default(0.0, shape=())
global_step = tf.Variable(0, trainable=False)
prediction = cnn(model_dropout_rate, model_learning_rate)
model_learning_rate = tf.train.exponential_decay(learning_rate=model_learning_rate, global_step=global_step, decay_rate=learning_rate_decay,
decay_steps=init_epoch, staircase=False)
with tf.name_scope('loss'):
model_loss = tf.losses.mean_squared_error(targets, prediction)
with tf.name_scope('adam_optimizer'):
train_step = tf.train.AdamOptimizer(model_learning_rate).minimize(model_loss,global_step=global_step)
sess.run(tf.global_variables_initializer())
for epoch_step in range(max_epoch):
for batch_X, batch_y in generate_batches(train_X, train_y, batch_size):
train_data_feed = {
inputs: batch_X,
targets: batch_y,
model_learning_rate: init_learning_rate,
model_dropout_rate: dropout_rate
}
sess.run(train_step, train_data_feed)
## how to return validation error, any idea?
## return validation error
## return val_error
my current attempt in colab 仍然存在各种问题,尚未解决。任何人都可以通过使用贝叶斯优化来寻找非常深的卷积神经网络的最佳超参数来提供可能的可行方法吗?有什么想法吗?谢谢!
【问题讨论】:
-
你能先优化一个更简单的问题吗?然后应用到 cnn 上?
-
@Justas 是的,我使用
GridSearchCV来查找超参数,但这对我的问题不是很有效,我的意思是对于非常深的 NN 来说真的很耗时。在这篇文章中,我只使用了非常简单的 CNN,并尝试使用贝叶斯优化找到它的超参数,但它不起作用。你介意提供可能的补救措施来解决我的问题吗?谢谢! -
试试optuna.readthedocs.io/en/stable之类的东西?
-
@AKX 这是我在this colab 中使用
optuna的尝试,你能提供你可能的尝试作为答案吗?谢谢! -
另一个工具是hyperopt,尽管它目前没有实现贝叶斯优化(尽管根据作者的说法,它“被设计为适应贝叶斯优化算法”)。几年前我发布了this answer 列出了一些工具。
标签: python tensorflow optimization conv-neural-network