【keras模型查看】(卷积层、池化层、全连接层、Batchnorm层)参数个数、乘法次数

【keras模型查看】(卷积层、池化层、全连接层、Batchnorm层)参数个数、
乘法次数
⽂章⽬录
1. 卷积层
1.1 输⼊参数
卷积的输⼊参数:指需要做卷积的输⼊图像/⾳频等,它要求是⼀个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体图⽚的含义是[训练时⼀个batch的图⽚数量, 图⽚⾼度, 图⽚宽度, 图像通道数],注意这是⼀个4维的Tensor,要求类型为float32和float64其中之⼀
源码说明:
Arguments:
filters: Integer, the dimensionality of the output space
中国达人秀海派清口(i.e. the number of output filters in the convolution).
kernel_size: An integer or tuple/list of 2 integers, specifying the
沦陷区的女人
height and width of the 2D convolution window.
Can be a single integer to specify the same value for
all spatial dimensions.
酿酒酵母strides: An integer or tuple/list of 2 integers,
specifying the strides of the convolution along the height and width.
Can be a single integer to specify the same value for
all spatial dimensions.
Specifying any stride value !=1is incompatible with specifying
any `dilation_rate` value !=1.
padding: one of `"valid"` or `"same"` (case-insensitive).
第⼀个参数filters:卷积核个数,也是输出通道数。Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution).
第⼆个参数kernel_size: 卷积核⼤⼩,指定⼆维卷积窗⼝的⾼和宽,(如果kernel_size只有⼀个整数,代表宽和⾼相等):An integer or tuple/list of 2 integers, specifying the height and width of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions.
第三个参数strides: 卷积步长,指定卷积窗沿⾼和宽⽅向的每次移动步长,An integer or tuple/list of 2 integers, (如果strides只有⼀个整数,代表沿着宽和⾼⽅向的步长相等) specifying the strides of the convolution along the height and width. Can be
a single integer to specify the same value for all spatial dimensions. Specifying any stride value != 1 is incompatible with
specifying any dilation_rate value != 1.
第四个参数padding: 为valid或same中⼀种, one of "valid" or "same" (case-insensitive). 两种padding⽅式的区别如下:same mode
当filter的中⼼(K)与image的边⾓重合时,开始做卷积运算。注意:这⾥的same还有⼀个意思,卷积之
后输出的feature map尺⼨保持不变(相对于输⼊图⽚)。当然,same模式不代表完全输⼊输出尺⼨⼀样,也跟卷积核的步长有关系。same模式也是最常见的模式,因为这种模式可以在前向传播的过程中让特征图的⼤⼩保持不变,调参师不需要精准计算其尺⼨变化(因为尺⼨根本就没变化)。
valid mode
当filter全部在image⾥⾯的时候,进⾏卷积运算,可见filter的移动范围较same更⼩了。
1.2 输出维数
由上⼀⼩节可知,卷积层的padding⽅式不同,其输出维数也会不同。在此,分为padding=valid和padding=same两种情况进⾏说明:1.2.1 padding=valid吉林大学法学院
给定输⼊参数:
inputs=[batch_size, in_height, in_width, in_channels],
filters,
kernel_size=[k_h, k_w],
stride_size=[s_h, s_w]
padding=‘valid’
则输出参数为:
output =[batch_size, out_height, out_width, out_channels]
# 其中:
out_channels = filters
out_height = ceil((in_height - k_h +1)/ s_h)
out_width = ceil((in_width - k_w +1)/ s_w)
⽰例1:
给定输⼊参数:
inputs=[batch_size, in_height, in_width, in_channels]=[1, 34, 13, 1] filters=128
kernel_size=[k_h, k_w]=[10, 4]
stride_size=[s_h, s_w]=[3, 2]
padding=‘valid’
则输出参数为:
output =[batch_size, out_height, out_width, out_channels]
# 其中:
out_channels = filters =128
out_height = ceil((in_height - k_h +1)/ s_h)= ceil(34-10+1/3)=ceil(8.3)=9
本溪四高中out_width = ceil((in_width - k_w +1)/ s_w)= ceil(13-4+1/2)= ceil(5)=5
# 即
output =[batch_size, out_height, out_width, out_channels]=[1,9,5,128]
实际⽰例:
⽰例2:
给定输⼊参数:
stride_size=[s_h, s_w]=[1, 1]
padding=‘valid’
则输出参数为:
output =[batch_size, out_height, out_width, out_channels]
# 其中:
out_channels = filters =128
out_height = ceil((in_height - k_h +1)/ s_h)= ceil(9-3+1/1)=ceil(7)=7 out_width = ceil((in_width - k_w +1)/ s_w)= ceil(7-3+1/1)= ceil(5)=5
# 即
output =[batch_size, out_height, out_width, out_channels]=[1,7,5,128]实际⽰例:
1.2.2 padding=same
给定输⼊参数:
inputs=[batch_size, in_height, in_width, in_channels],
filters,
kernel_size=[k_h, k_w],
stride_size=[s_h, s_w]
padding=‘same’
则输出参数为:
output =[batch_size, out_height, out_width, out_channels]
# 其中:
out_channels = filters
out_height = ceil(in_height / s_h)
out_width = ceil(in_width / s_w)
⽰例3:
给定输⼊参数:
氧化苦参碱stride_size=[s_h, s_w]=[3, 2]
padding=‘valid’
则输出参数为:
output =[batch_size, out_height, out_width, out_channels]
# 其中:
out_channels = filters =128
out_height = ceil(in_height / s_h)= ceil(34/3)= ceil(11.3)=12
out_width = ceil(in_width / s_w)= ceil(13/2)= ceil(6.5)=7
# 即
output =[batch_size, out_height, out_width, out_channels]=[1,12,7,128]
1.3 参数个数
# 查看模型层及参数
model.summary()
可查看到每层模型的参数个数, 如最后⼀层的全连接层有3999个参数,模型总参数个数为85023:
⽰例4.0:
普通⼆维卷积Conv2D,给定输⼊参数(同⽰例1):
inputs=[batch_size, in_height, in_width, in_channels]=[1, 34, 13, 1]
filters=128
kernel_size=[k_h, k_w]=[10, 4]
stride_size=[s_h, s_w]=[3, 2]
padding=‘valid’

本文发布于:2024-09-22 16:38:42,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/475146.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:卷积   参数   输出   模型
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议