CNN中的卷积操作的参数数计算
之前一直以為卷積是二維的操作,而到今天才發(fā)現(xiàn)卷積其實是在volume上的卷積。比如輸入的數(shù)據(jù)是channels*height*width(3*10*10),我們定義一個核函數(shù)大小為3*3,則輸出是8*8。實際核函數(shù)的參數(shù)量是3*3*channels,在本例子中就是3*3*3。
舉例:
假設(shè)輸入的tensor是3*10*10,定義一個大小為3*3的kernel,如果進(jìn)行一個conv2d操作,輸出的feature map是5的話,那么這個conv2d涉及的參數(shù)數(shù)是3*3*3*5+5=140個,輸出大小5*8*8。其中3*3*3代表的是核參數(shù),5代表的是bias數(shù)。
驗證代碼:
from keras.layers import Dense, Conv2D
from keras.models import Sequential
model = Sequential()
conv1 = Conv2D(5, 3, input_shape=(3, 10, 10))
model.add(conv1)
print(model.summary())
_________________________________________________________________
Layer (type)???????????????? Output Shape????????????? Param #??
=========================================================
conv2d_1 (Conv2D)??????????? (None, 5, 8, 8)?????????? 140??????
=========================================================
Total params: 140
Trainable params: 140
Non-trainable params: 0
_________________________________________________________________
?
3D卷積意思是指在多個channel的volume上進(jìn)行convolution操作。比如:5個4*10*10(深度、高度、寬度)大小的volume組成的高維數(shù)組。假設(shè)核大小為2*2*2,stride為1,輸出feature map的數(shù)量為4,那么參數(shù)數(shù)為2*2*2*channels(5)*output feature maps(4)+4=164。輸出大小為4*3*9*9。其中第一個4對應(yīng)的是feature map數(shù),第二個3對應(yīng)的是深度,第三個9對應(yīng)的是高度,第四個9對應(yīng)的是寬度。
驗證代碼:
?
from keras.layers import Conv3D
from keras.models import Sequential
?
model = Sequential()
conv1 = Conv3D(4, 2, input_shape=(5, 4, 10, 10))
model.add(conv1)
?
print(model.summary())
?
_________________________________________________________________
Layer (type)???????????????? Output Shape?????????? ???Param #??
=========================================================
conv3d_1 (Conv3D)??????????? (None, 4, 3, 9, 9)??????? 164??????
=========================================================
Total params: 164
Trainable params: 164
Non-trainable params: 0
_________________________________________________________________
轉(zhuǎn)載于:https://www.cnblogs.com/chuantingSDU/p/8120065.html
總結(jié)
以上是生活随笔為你收集整理的CNN中的卷积操作的参数数计算的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2.1TF模型持久化
- 下一篇: 八.利用springAMQP实现异步消息