深度学习中的fan_in与fan_out

Understanding the difficulty of training deep feedforward neural network 中,fan_in指第i层神经元个数,fan_out指第i+1层神经元个数。通常卷积网络不是全连接的,fan_in与fan_out的计算方式有所不同。
pytorch中:
f a n i n = c h a n n e l s i n × k e r n e r w i d t h × k e r n e r h e i g h t fan_{in}=channels_{in}\times kerner_{width} \times kerner_{height} fanin=channelsin×kernerwidth×kernerheight

f a n o u t = c h a n n e l s o u t × k e r n e r w i d t h × k e r n e r h e i g h t fan_{out}=channels_{out}\times kerner_{width} \times kerner_{height} fanout=channelsout×kernerwidth×kernerheight

根据对http://deeplearning.net/tutorial/lenet.html中的理解,以及https://stackoverflow.com/questions/42670274/how-to-calculate-fan-in-and-fan-out-in-xavier-initialization-for-neural-networks中的描述,另外一种更加精确的描述为:
f a n i n = c h a n n e l s i n × r e c e p t i v e f i e l d h e i g h t × r e c e p t i v e f i e l d w i d t h fan_in = channels_{in}\times receptivefield_{height}\times receptivefield_{width} fanin=channelsin×receptivefieldheight×receptivefieldwidth

f a n i n = c h a n n e l s i n × r e c e p t i v e f i e l d h e i g h t × r e c e p t i v e f i e l d w i d t h m a x p o o l a r e a fan_in = \frac{channels_{in}\times receptivefield_{height}\times receptivefield_{width}}{maxpool_{area}} fanin=maxpoolareachannelsin×receptivefieldheight×receptivefieldwidth

按照第二种说法,空洞卷积(dilated convolution)的感受野要比普通卷积大, r e c e p t i v e f i e l d receptivefield receptivefield k e r n e l kernel kernel的大小不同,计算结果也不同。

  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: _calculate_fan_in_and_fan_out函数在PaddlePaddle.nn.init模块用于计算参数的输入和输出fan数量。该函数的源码如下: def _calculate_fan_in_and_fan_out(tensor): dims = tensor.ndimension() if dims < 2: raise ValueError("Fan in and fan out can not be computed for tensor with fewer than 2 dimensions") if dims == 2: # Linear fan_in = tensor.size(1) fan_out = tensor.size(0) else: num_input_fmaps = tensor.size(1) num_output_fmaps = tensor.size(0) receptive_field_size = 1. if tensor.dim() > 2: receptive_field_size = tensor[0][0].numel() fan_in = num_input_fmaps * receptive_field_size fan_out = num_output_fmaps * receptive_field_size return fan_in, fan_out ### 回答2: paddle.nn.init模块的_calculate_fan_in_and_fan_out函数是用来计算张量的fan_in和fan_out值的。该函数是用于权重初始化的过程,用来确定权重的随机初始化的范围。 根据源码,_calculate_fan_in_and_fan_out函数接受一个张量作为输入,然后根据其维度来计算fan_in和fan_out的值。张量的维度可以是1维、2维或者4维。 当输入张量的维度为1维时,fan_in和fan_out都等于张量的长度。 当输入张量的维度为2维时,fan_in等于输入张量的列数,fan_out等于输入张量的行数。 当输入张量的维度为4维时,fan_in等于输入张量的通道数乘以输入张量的高度和宽度的乘积,fan_out等于输出张量的通道数乘以输出张量的高度和宽度的乘积。 最后,_calculate_fan_in_and_fan_out函数返回计算得到的fan_in和fan_out的值。 这个函数的作用是在进行权重初始化时,根据输入张量的维度,确定了权重初始化的随机范围。fan_in和fan_out的值决定了权重的初始化的合理范围,帮助模型更好地进行训练和收敛。 ### 回答3: paddle.nn.init模块是基于PaddlePaddle深度学习框架的初始化函数库。其,_calculate_fan_in_and_fan_out函数用于计算线性层的输入和输出通道数。 该函数的源码如下: ```python def _calculate_fan_in_and_fan_out(tensor): dimensions = tensor.dim() if dimensions < 2: raise ValueError("Fan in and fan out can not be computed for tensor with less than 2 dimensions") if dimensions == 2: # Linear layer or Convolutional layer fan_in = tensor.size(1) fan_out = tensor.size(0) else: # Convolutional layer with multiple input channels num_input_fmaps = tensor.size(1) num_output_fmaps = tensor.size(0) receptive_field_size = operator.mul(*tensor.shape[2:]) fan_in = num_input_fmaps * receptive_field_size fan_out = num_output_fmaps * receptive_field_size return fan_in, fan_out ``` 根据源码解析,该函数通过判断输入张量的维度来计算线性层(Linear layer)或卷积层(Convolutional layer)的输入通道数和输出通道数。 当输入张量的维度为2时,代表是线性层或卷积层,此时fan_in表示输入通道数,fan_out则表示输出通道数。例如,在全连接层fan_in表示输入特征的维度,即输入通道数,fan_out表示输出特征的维度,即输出通道数。 当输入张量的维度大于2时,代表是具有多个输入通道的卷积层,此时需要计算输入通道数和输出通道数。其,num_input_fmaps表示输入通道数,num_output_fmaps表示输出通道数,receptive_field_size表示感受野的大小。通过将输入通道数、输出通道数以及感受野的大小相乘,可以得到fan_in和fan_out的值。 综上所述,_calculate_fan_in_and_fan_out函数根据输入张量的维度和类型,来计算线性层或卷积层的输入通道数和输出通道数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值