Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

tensorflow - How to check the number of layers in a neural network in python and when should we increase the layers?

Please add a minimum comment on your thought, so that I can improve my query. Thanks. -)


I am working on the MNIST dataset and write some CNN code. However, I am confused about some of the points with the CNN code. How to know the number of layers in a neural network? With my current understanding, I think this has 6 layers with 4 hidden layers. Is that right? and what if I need to extend to 10 layers? how to do it?

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D

model = Sequential()
model.add(Conv2D(28, kernel_size=(3,3), 
                    input_shape = ...))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dropout(0.2))
model.add(Dense(10, activation=tf.nn.softmax))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While counting the number of layers in a Neural Network we usually only count convolutional layers and fully connected layers. Pooling Layer is taken together with the Convolutional Layer and counted as one layer and Dropout is a regularization technique so it will also not be counted as a separate layer.

For reference, the VGG16 mode is defined as a 16 layer model. Those 16 layers are only the Convolutional Layers and Fully Connected Dense Layers. If you count all the pooling and activation layers it will change to a 41 layer model, which it is not. Reference: VGG16, VGG16 Paper

So as per your code you have 3 layers (1 Convolutional Layer with 28 Neurons, 1 Fully Connected Layer with 128 Neurons and 1 Fully Connected Layer with 10 neurons)

As for making it a 10 layer network you can add more convolutional layers or dense layers before the output layers, but it won't be necessary for the MNIST dataset.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...