考虑到用于将图像分为两类的卷积神经网络,我们如何计算权重数量:
假设每个层中都存在偏差。而且,池化层具有权重(类似于AlexNet)
此网络有多少权重?
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Conv2D, MaxPooling2D
model = Sequential()
# Layer 1
model.add(Conv2D(60, (7, 7), input_shape = (100, 100, 1), padding="same", activation="relu"))
# Layer 2
model.add(Conv2D(100, (5, 5), padding="same", activation="relu"))
# Layer 3
model.add(MaxPooling2D(pool_size=(2, 2)))
# Layer 4
model.add(Dense(250))
# Layer 5
model.add(Dense(200))
model.summary()
答案 0 :(得分:3)
使用Sequential.summary
-Link进行文档编制。
用法示例:
from tensorflow.keras.models import *
model = Sequential([
# Your architecture here
]);
model.summary()
您的体系结构的输出为:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 94, 94, 60) 3000
_________________________________________________________________
conv2d_1 (Conv2D) (None, 90, 90, 100) 150100
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 45, 45, 100) 0
_________________________________________________________________
flatten (Flatten) (None, 202500) 0
_________________________________________________________________
dense (Dense) (None, 250) 50625250
_________________________________________________________________
dense_1 (Dense) (None, 200) 50200
_________________________________________________________________
dense_2 (Dense) (None, 1) 201
=================================================================
Total params: 50,828,751
Trainable params: 50,828,751
Non-trainable params: 0
_________________________________________________________________
这是50828751个参数。
对于具有
的2D卷积层num_filters
过滤器,filter_size * filter_size * num_channels
,权数为:(num_filters * filter_size * filter_size * num_channels) + num_filters
例如:您的神经网络中的第1层具有
其中的权重数为:(60 * 7 * 7 * 1) + 60
,即3000
。
对于具有的密集层
num_units
神经元,num_inputs
神经元,权数为:(num_units * num_inputs) + num_units
例如神经网络中的第5层具有
其中的权重数为200 * 250
,即50200
。