11

mobilenet 中的通道数为什么要使用 _make_divisible 函数确保为 8 的整数倍?

 3 years ago
source link: https://blog.popkx.com/why-using-_make_divisible-to-make-sure-mobilenet-channels-is-divisible-by-8/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

mobilenet 中的通道数为什么要使用 _make_divisible 函数确保为 8 的整数倍?

发表于 2020-09-28 16:09:00   |   已被 访问: 196 次   |   分类于:   深度学习   |   暂无评论

今天阅读一段 PyTorch 版的 mobilenet 时,发现了下面这个函数:

def _make_divisible(v, divisor, min_value=None):
    """
    This function is taken from the original tf repo.
    It ensures that all layers have a channel number that is divisible by 8
    It can be seen here:
    https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
    :param v:
    :param divisor:
    :param min_value:
    :return:
    """
    if min_value is None:
        min_value = divisor
    new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
    # Make sure that round down does not go down by more than 10%.
    if new_v < 0.9 * v:
        new_v += divisor
    return new_v

_make_divisible() 函数保证输出的数可以整除 divisor,在我拿到的 mobilenet 代码里,注释清楚的说明了使用该函数使用来保证 mobilenet 所有层的通道数都可以被 8 整除,这是为什么呢?

谷歌,百度了一些答案,很多都是直接讨论了一下 mobilenet 的原理,但这并没有回答上述问题,直到我发现了这个:

答案

如果仅从数学角度来考虑,是得不到答案的,“为什么保证 mobilenet 所有层的通道数都可以被 8 整除”这个问题要从计算机处理器单元的架构上考虑,按照上述文章中的说法:

在大多数硬件中,size 可以被 d = 8, 16, ... 整除的矩阵乘法比较块,因为这些 size 符合处理器单元的对齐位宽。

其实,总结起来就一句话:为了快。至于“对齐”为什么可以加快计算,我之前做结合实例写过一篇文章,可参考:为什么逐字节拷贝没有memcpy函数快

不错的一本书

这段解答来自于下面这个 pdf,我也是无意中发现的,粗略的看了下,似乎是一篇博士论文,讨论深度学习中设计网络的一些基本思想的,说的比较细。可以点击下面的链接下载:

支付宝支付
价格: 0.50 元 (已有3人付款) 温馨提示:感谢支持!
输入邮箱或者手机号码,付款后可永久阅读隐藏的内容,请勿未经本站许可,擅自分享付费内容。
如果您已购买本页内容,输入购买时的手机号码或者邮箱,点击支付,即可查看内容。
电子商品,一经购买,不支持退款,特殊原因,请联系客服。 付费可读

阅读更多:   深度学习


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK