python - Tensorflow 中的超参数优化

标签 python tensorflow deep-learning bayesian hyperparameters

我正在使用 Tensorflow 中的贝叶斯优化为我的卷积神经网络 (CNN) 进行超参数优化。我收到此错误:

ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[4136,1,180,432] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc

我优化了这些超参数:

dim_batch_size = Integer(low=1, high=50, name='batch_size')
dim_kernel_size1 = Integer(low=1, high=75, name='kernel_size1')
dim_kernel_size2 = Integer(low=1, high=50, name='kernel_size2')
dim_depth = Integer(low=1, high=100, name='depth')
dim_num_hidden = Integer(low=5, high=1500, name='num_hidden')
dim_num_dense_layers = Integer(low=1, high=5, name='num_dense_layers')
dim_learning_rate = Real(low=1e-6, high=1e-2, prior='log-uniform',
                         name='learning_rate')
dim_activation = Categorical(categories=['relu', 'sigmoid'],
                             name='activation')
dim_max_pool = Integer(low=1, high=100, name='max_pool')

dimensions = [dim_batch_size,
              dim_kernel_size1,
              dim_kernel_size2,
              dim_depth,
              dim_num_hidden,
              dim_num_dense_layers,
              dim_learning_rate,
              dim_activation,
              dim_max_pool]

它说资源已用完。这是为什么?

是不是优化了太多的超参数?或者有一些维度不匹配?或者我是否分配了超出正确操作允许范围的超参数范围?

最佳答案

OOM 的发生是因为当多个超参数处于范围的高端时,模型变得太大。例如,假设批量大小约为 50dim_num_hidden 约为 1500 等等。超参数的数量无关紧要,只需要几个超参数就足以打垮模型。

错误消息中的具体张量是 [4136,1,180,432]1.2Gb 如果您使用 32 位 float 作为参数。这是很多,它只是进行 NN 训练所需的众多张量之一(例如,前向和后向参数数量加倍,因此内存数量加倍)。难怪 tensorflow 因 OOM 而失败。

用于超参数调整的贝叶斯优化的一个特殊问题是该算法很可能会选择超空间的角点,即值接近范围内最小值的一个点,另一个点其中值接近范围内的最大值。详见 this question .这意味着您必须限制至少一个超参数(通常是批量大小),以便即使其他所有参数都为最大值,模型也能拟合。或者您可以聪明地在每次迭代运行模型之前精确地计算它,但这样算法就不会优化批量大小。

关于python - Tensorflow 中的超参数优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49713848/

相关文章:

python - 多变量线性回归与 scipy linregress

python - PyQt --> addItem --> 类型错误 : arguments did not match any overloaded call:

python - 用 tensorflow 中的条件减少总和

python - Caffe - draw_net_to_file - 'Classifier' 对象没有属性 'name'

python - 使用 python/opencv/深度学习 从图像中删除给定位置的 Logo /水印

dict() 或函数参数中的 Python 多行注释

python - Jupyter 无法启动, "ImportError: This package should not be accessible"

tensorflow - 如何使用Keras中的tile功能?

python - 保存 'fine-tuned' bert模型

machine-learning - HDF5中文件数量小于batch时会发生什么