In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how Keras in python to build CNN network, the content is detailed and easy to understand, easy to operate, and has a certain reference value. I believe you will gain something after reading this article on how to build CNN network by Keras in python. Let's take a look.
The important function of constructing CNN in Keras 1, Conv2D
Conv2D is used to build a convolution layer in CNN, and you need to import it at the library function before using it.
From keras.layers import Conv2D
In actual use, several parameters are needed.
Conv2D (nb_filter = 32, nb_row = 5, nb_col = 5, border_mode = 'same', input_shape = (28 ~ 28 ~ (1)
Among them, nb_filter represents the number of channel output of the convolution layer, and the image becomes thicker and thicker after convolution, which is the thickness of the image after convolution. The combination of nb_row and nb_col is the size of the convolver, here the convolver is the size of (5pm. 5). Border_mode represents the way of padding, and same means that the shape of the image remains unchanged before and after convolution. Input_shape represents the shape entered.
2 、 MaxPooling2D
MaxPooling2D refers to the pooled layer, which needs to be import at the library function before using it.
From keras.layers import MaxPooling2D
In actual use, several parameters are needed.
MaxPooling2D (pool_size = (2jue 2), strides = (2jue 2), border_mode = 'same')
Where pool_size represents the size of the pooler, in this case, the shape of the pooler is (2mem2). Strides is the step size of the pooler, which is 2 in both the X and Y directions. After pooling, the output is 1 / 2 smaller than the input shape. Border_mode represents the way of padding.
3 、 Flatten
Flatten is used to pool the final output of convolution into an one-dimensional vector so that it can be connected to the fully connected layer for calculation. You need to import with import before using it.
From keras.layers import Flatten
In practical use, you can add a layer directly after the last pooled layer.
Model.add (Flatten ()) all code
This is an example of a convolutional neural network that is used to identify handwritten characters. The neural network structure is as follows:
Convolution layer 1-> pooled layer 1-> convolution layer 2-> pooled layer 2-> flatten- > fully connected layer 1-> fully connected layer 2-> fully connected layer 3.
The shape of a single sample is as follows:
(28 ~ 28)-> (28 ~ 28)-> (14 ~ 14 ~ 14 ~ 32)-> (14 ~ 14 ~ 14 ~ 64)-> (7 ~ 7 ~ 7 ~ 64)-> (3136)-> (1024)-> (256)
Import numpy as npfrom keras.models import Sequentialfrom keras.layers import Dense,Activation,Conv2D,MaxPooling2D,Flatten # # full connection layer from keras.datasets import mnistfrom keras.utils import np_utilsfrom keras.optimizers import Adam (XerotestMagic Y train) = mnist.load_data () X_train = X_train.reshape (- 1) X_test = X_test.reshape (- 1) Y_train = np_utils.to_categorical (Y_train) Num_classes= 10) Y_test = np_utils.to_categorical (Yantist numb classes10) model = Sequential () # conv1model.add (Conv2D (nb_filter = 32, nb_row = 5, nb_col = 5, border_mode = 'same', input_shape = (2828 same', input_shape) model.add (Activation ("relu")) # pool1model.add (MaxPooling2D (pool_size = (2Men2)) Strides = (2jue 2), border_mode = 'same') # conv2model.add (Conv2D (nb_filter = 64, nb_row = 5, nb_col = 5, border_mode =' same')) model.add (Activation ("relu")) # pool2model.add (MaxPooling2D (pool_size = (2jue 2), strides = (2jue 2)) Border_mode = 'same') # full connection layer model.add (Flatten ()) model.add (Dense (1024)) model.add (Activation ("relu")) model.add (Dense ("relu")) model.add (Dense (10)) model.add (Activation ("softmax") adam = Adam (lr = 1e-4) # # compilemodel.compile (loss =' categorical_crossentropy',optimizer = adam) Metrics = ['accuracy']) # # tarinprint ("\ ntraining") cost = model.fit (X-ray recording page2) print ("\ nTest") # # acccost,accuracy = model.evaluate (X-ray test) # # W weights b = model.layers [0] .get _ weights () print ("accuracy:", accuracy)
The experimental results are as follows:
Epoch 1Universe 26000000169us/stepaccuracy 60000 [=]-64s 1ms/step-loss: 0.7664-acc: 0.9224Epoch 2hammer260000and60000 [=]-62s 1ms/step-loss: 0.0473-acc: 0.9858Test10000/10000 [=]-2s 169us/stepaccuracy: 0.9856 this is the end of the article on "how to build a CNN network in Keras in python". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to build a CNN network in Keras in python". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.