python summary结果提取_Keras model.summary()结果-了解参数数

在Keras中创建了一个用于手写数字识别的简单神经网络模型,使用Theano后端。模型包括 Flatten、Dense 和 Activation 层,训练后达到约90%的准确率。通过model.summary()获取网络结构,总参数数为7850。参数数量的计算方式是每个隐藏单元有784个输入权重和一个偏置权重,共785,乘以10个隐藏单元即得7850。偏置项在神经网络中起重要作用,增加模型容量。
摘要由CSDN通过智能技术生成

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

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

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值