本文介绍了如何计算CNN的权数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到用于将图像分为两类的卷积神经网络,我们如何计算权重数量:

How can we compute number of weights considering a convolutional neural network that is used to classify images into two classes :

  • 输入:100x100灰度图像.
  • 第1层:具有60个7x7卷积滤波器的卷积层(步幅= 1,有效填充).
  • 第2层:具有100个5x5卷积滤镜的卷积层(步幅= 1,有效填充).
  • 第3层:最大池化层,将第2层降采样为4倍(例如,从500x500到250x250)
  • 第4层:具有250个单位的致密层
  • 第5层:200个单位的致密层
  • 第6层:单个输出单元

假设每个层中都存在偏差.而且,池化层具有权重(类似于AlexNet)

Assume the existence of biases in each layer. Moreover, pooling layer has a weight (similar to 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()

推荐答案

TL; DR-用于TensorFlow + Keras

使用 Sequential.summary -链接到文档.

用法示例:

from tensorflow.keras.models import *

model = Sequential([
    # Your architecture here
]);

model.summary()

您的体系结构的输出为:

The output for your architecture is:

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
_________________________________________________________________

这是50,828,751个参数.

That's 50,828,751 parameters.

对于具有

  • num_filters 过滤器
  • 过滤器大小为 filter_size * filter_size * num_channels
  • 以及每个过滤器的偏差参数

权数为:(num_filters * filter_size * filter_size * num_channels)+ num_filters

例如:您的神经网络中的第1层具有

E.g.: LAYER 1 in your neural network has

  • 60个过滤器
  • ,过滤器大小为7 * 7 *1.(请注意,通道数(1)来自输入图像.)

其中的权重数为:(60 * 7 * 7 * 1)+ 60 ,即 3000 .

The number of weights in it is: (60 * 7 * 7 * 1) + 60, which is 3000.

对于具有的密集层

  • num_units 个神经元,
  • 上一层中的
  • num_inputs 个神经元,
  • 以及每个神经元的偏差参数

权数为:(num_units * num_inputs)+ num_units

例如神经网络中的第5层具有

E.g. LAYER 5 in your neural network has

  • 200个神经元
  • 及其之前的层-层4-具有250个神经元.

其中的权重数为 200 * 250 ,即 50200 .

这篇关于如何计算CNN的权数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:42
查看更多