Keras model.summary()结果-了解参数数 [英] Keras model.summary() result - Understanding the # of Parameters

查看:1680
本文介绍了Keras model.summary()结果-了解参数数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的NN模型,用于使用Keras(Theano后端)从以python编写的28x28px图像中检测手写数字:

I have a simple NN model for detecting hand-written digits from a 28x28px image written in python using Keras (Theano backend):

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])

这运行得很好,我的准确率达到了90%.然后,我通过执行print(model0.summary())来执行以下命令,以获取网络结构的摘要.输出以下内容:

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:

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

我不知道它们如何达到7850个总参数,这实际上意味着什么?

I don't understand how they get to 7850 total params and what that actually means?

推荐答案

参数的数量为7850,因为对于每个隐藏的单位,您都有784个输入权重和一个带偏置的连接权重.这意味着每个隐藏的单位都会为您提供785个参数.您有10个单位,所以总计为7850.

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.

这个额外的偏见术语的作用确实很重要.它显着增加了模型的容量.您可以阅读详细信息,例如此处偏见在神经网络中的作用.

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.

这篇关于Keras model.summary()结果-了解参数数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆