Python Keras model.summary() 结果 - 了解参数数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36946671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keras model.summary() result - Understanding the # of Parameters
提问by user3501476
I have a simple NN model for detecting hand-written digits from a 28x28px image written in python using Keras (Theano backend):
我有一个简单的 NN 模型,用于从使用 Keras(Theano 后端)用 Python 编写的 28x28px 图像中检测手写数字:
model0 = Sequential()
#number of epochs to train for
nb_epoch = 12
#amount of data each iteration in an epoch sees
batch_size = 128
model0.add(Flatten(input_shape=(1, img_rows, img_cols)))
model0.add(Dense(nb_classes))
model0.add(Activation('softmax'))
model0.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
model0.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test, Y_test))
score = model0.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
This runs well and I get ~90% accuracy. I then perform the following command to get a summary of my network's structure by doing print(model0.summary())
. This outputs the following:
这运行良好,我获得了约 90% 的准确率。然后我执行以下命令以通过执行print(model0.summary())
. 这将输出以下内容:
Layer (type) Output Shape Param # Connected to
=====================================================================
flatten_1 (Flatten) (None, 784) 0 flatten_input_1[0][0]
dense_1 (Dense) (None, 10) 7850 flatten_1[0][0]
activation_1 (None, 10) 0 dense_1[0][0]
======================================================================
Total params: 7850
I don't understand how they get to 7850 total params and what that actually means?
我不明白他们是如何达到 7850 个总参数的,这实际上意味着什么?
回答by Marcin Mo?ejko
The number of parameters is 7850 because with every hidden unit you have 784 input weights and one weight of connection with bias. This means that every hidden unit gives you 785 parameters. You have 10 units so it sums up to 7850.
参数的数量是 7850,因为每个隐藏单元都有 784 个输入权重和一个带有偏差的连接权重。这意味着每个隐藏单元为您提供 785 个参数。您有 10 个单位,因此总和为 7850。
The role of this additional bias term is really important. It significantly increases the capacity of your model. You can read details e.g. here Role of Bias in Neural Networks.
这个额外的偏置项的作用非常重要。它显着增加了模型的容量。您可以在此处阅读详细信息,例如在神经网络中偏差的作用。
回答by tauseef_CuriousGuy
I feed a 514 dimensional real-valued input to a Sequential
model in Keras.
My model is constructed in following way :
我将 514 维实值输入提供给Sequential
Keras 中的模型。我的模型是按以下方式构建的:
predictivemodel = Sequential()
predictivemodel.add(Dense(514, input_dim=514, W_regularizer=WeightRegularizer(l1=0.000001,l2=0.000001), init='normal'))
predictivemodel.add(Dense(257, W_regularizer=WeightRegularizer(l1=0.000001,l2=0.000001), init='normal'))
predictivemodel.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
When I print model.summary()
I get following result:
当我打印时,model.summary()
我得到以下结果:
Layer (type) Output Shape Param # Connected to
================================================================
dense_1 (Dense) (None, 514) 264710 dense_input_1[0][0]
________________________________________________________________
activation_1 (None, 514) 0 dense_1[0][0]
________________________________________________________________
dense_2 (Dense) (None, 257) 132355 activation_1[0][0]
================================================================
Total params: 397065
________________________________________________________________
For the dense_1 layer , number of params is 264710. This is obtained as : 514 (input values) * 514 (neurons in the first layer) + 514 (bias values)
对于dense_1 层,参数数为264710。获得如下:514(输入值)* 514(第一层的神经元)+ 514(偏差值)
For dense_2 layer, number of params is 132355. This is obtained as : 514 (input values) * 257 (neurons in the second layer) + 257 (bias values for neurons in the second layer)
对于dense_2 层,参数数为132355。这是获得为:514(输入值)* 257(第二层神经元)+ 257(第二层神经元的偏差值)
回答by DiveIntoML
The "none" in the shape means it does not have a pre-defined number. For example, it can be the batch size you use during training, and you want to make it flexible by not assigning any value to it so that you can change your batch size. The model will infer the shape from the context of the layers.
形状中的“无”意味着它没有预定义的数字。例如,它可以是您在训练期间使用的批量大小,并且您希望通过不为其分配任何值来使其灵活,以便您可以更改批量大小。该模型将从层的上下文推断形状。
To get nodes connected to each layer, you can do the following:
要将节点连接到每一层,您可以执行以下操作:
for layer in model.layers:
print(layer.name, layer.inbound_nodes, layer.outbound_nodes)
回答by Ashiq Imran
For Dense Layers:
对于密集层:
output_size * (input_size + 1) == number_parameters
For Conv Layers:
对于转换层:
output_channels * (input_channels * window_size + 1) == number_parameters
Consider following example,
考虑下面的例子,
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=input_shape),
Conv2D(64, (3, 3), activation='relu'),
Conv2D(128, (3, 3), activation='relu'),
Dense(num_classes, activation='softmax')
])
model.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 222, 222, 32) 896
_________________________________________________________________
conv2d_2 (Conv2D) (None, 220, 220, 64) 18496
_________________________________________________________________
conv2d_3 (Conv2D) (None, 218, 218, 128) 73856
_________________________________________________________________
dense_9 (Dense) (None, 218, 218, 10) 1290
=================================================================
Calculating params,
计算参数,
assert 32 * (3 * (3*3) + 1) == 896
assert 64 * (32 * (3*3) + 1) == 18496
assert 128 * (64 * (3*3) + 1) == 73856
assert num_classes * (128 + 1) == 1290
回答by Ashiq Imran
The easiest way to calculate number of neurons in one layer is: Param value / (number of units * 4)
计算一层神经元数的最简单方法是:Param value / (number of units * 4)
- Number of units is in predictivemodel.add(Dense(514,...)
- Param value is Param in model.summary() function
- 单位数在predictivemodel.add(Dense(514,...)
- 参数值是 model.summary() 函数中的参数
For example in Paul Lo's answer , number of neurons in one layer is 264710 / (514 * 4 ) = 130
例如在Paul Lo的回答中,一层的神经元数量为 264710 / (514 * 4 ) = 130
回答by mari.mts
Number of parameters is the amount of numbers that can be changed in the model. Mathematically this means number of dimensions of your optimization problem. For you as a programmer, each of this parameters is a floating point number, which typically takes 4 bytes of memory, allowing you to predict the size of this model once saved.
参数数量是模型中可以更改的数量。从数学上讲,这意味着优化问题的维数。对于作为程序员的您来说,这些参数中的每一个都是一个浮点数,通常需要 4 字节的内存,让您可以在保存后预测此模型的大小。
This formula for this number is different for each neural network layer type, but for Dense layer it is simple: each neuron has one bias parameter and one weight per input:
N = n_neurons * ( n_inputs + 1)
.
这个数字的公式对于每个神经网络层类型都不同,但对于密集层很简单:每个神经元每个输入有一个偏置参数和一个权重:
N = n_neurons * ( n_inputs + 1)
。