当前位置:网站首页>14. Draw network model structure
14. Draw network model structure
2022-07-08 01:01:00 【booze-J】
article
Draw network structure process
Before running the code, you need to install pydot
and graphviz
install pydot:pip install pydot
install graphviz It's a little bit more complicated , Let's Baidu by ourselves .
The code running platform is jupyter-notebook, Code blocks in the article , According to jupyter-notebook Written in the order of division in , Run article code , Glue directly into jupyter-notebook that will do .
1. Import third-party library
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense,Dropout,Convolution2D,MaxPooling2D,Flatten
from tensorflow.keras.optimizers import Adam
from keras.utils.vis_utils import plot_model
import matplotlib.pyplot as plt
# install pydot and graphviz
2. Data preprocessing
# Load data
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# (60000, 28, 28)
print("x_shape:\n",x_train.shape)
# (60000,) Not yet one-hot code You need to operate by yourself later
print("y_shape:\n",y_train.shape)
# (60000, 28, 28) -> (60000,28,28,1)=( Number of pictures , Picture height , Image width , The number of channels in the picture ) reshape() Middle parameter filling -1 Parameter results can be calculated automatically Divide 255.0 To normalize
# Normalization is critical , It can greatly reduce the amount of calculation
x_train = x_train.reshape(-1,28,28,1)/255.0
x_test = x_test.reshape(-1,28,28,1)/255.0
# in one hot Format
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
3. Build a network model
# Define sequential model
model = Sequential()
# The first convolution layer Note that the first layer should write the size of the input image Later layers can be ignored
# input_shape Input plane
# filters Convolution kernel / Number of filters
# kernel_size Convolution window size
# strides step
# padding padding The way same/valid
# activation Activation function
model.add(Convolution2D(
input_shape=(28,28,1),
filters=32,
kernel_size=5,
strides=1,
padding="same",
activation="relu"
))
# The first pool
model.add(MaxPooling2D(
pool_size=2,
strides=2,
padding="same"
))
# The second pooling layer
model.add(Convolution2D(filters=64,kernel_size=5,strides=1,padding="same",activation="relu"))
# The second pooling layer
model.add(MaxPooling2D(pool_size=2,strides=2,padding="same"))
# Flatten the output of the second pool layer into 1 dimension
model.add(Flatten())
# The first full connection layer
model.add(Dense(units=1024,activation="relu"))
# Dropout Random selection 50% Neurons are trained
model.add(Dropout(0.5))
# The second full connection layer
model.add(Dense(units=10,activation="softmax"))
# # Define optimizer Set the learning rate to 1e-4
# adam = Adam(lr=1e-4)
# # Define optimizer ,loss function, The accuracy of calculation during training
# model.compile(optimizer=adam,loss="categorical_crossentropy",metrics=["accuracy"])
# # Training models
# model.fit(x_train,y_train,batch_size=64,epochs=10)
# # Evaluation model
# loss,accuracy=model.evaluate(x_test,y_test)
# print("test loss:",loss)
# print("test accuracy:",accuracy)
4. Draw the network model structure
# rankdir="TB" Finally, this is what determines the direction T representative TOP B representative BOTTOM TB From top to bottom If you want to go from left to right , modify rankdir="LR" that will do
plot_model(model,to_file="model.png",show_shapes=True,show_layer_names="False",rankdir="TB")
plt.figure(figsize=(10,10))
img = plt.imread("model.png")
plt.imshow(img)
plt.axis("off")
plt.show()
Running results :plot_model(model,to_file="model.png",show_shapes=True,show_layer_names="False",rankdir="TB")
Medium rankdir="TB"
Finally, this is what determines the direction T
representative TOP ,B
representative BOTTOM,TB
From top to bottom , If you want to go from left to right , modify rankdir="LR"
that will do .
边栏推荐
- 完整的模型训练套路
- 【笔记】常见组合滤波电路
- ABAP ALV LVC template
- Which securities company has a low, safe and reliable account opening commission
- 攻防演练中沙盘推演的4个阶段
- Course of causality, taught by Jonas Peters, University of Copenhagen
- C#中string用法
- NVIDIA Jetson测试安装yolox过程记录
- 133. 克隆图
- Langchao Yunxi distributed database tracing (II) -- source code analysis
猜你喜欢
DNS series (I): why does the updated DNS record not take effect?
2022-07-07: the original array is a monotonic array with numbers greater than 0 and less than or equal to K. there may be equal numbers in it, and the overall trend is increasing. However, the number
Langchao Yunxi distributed database tracing (II) -- source code analysis
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
7.正则化应用
Cve-2022-28346: Django SQL injection vulnerability
Class head up rate detection based on face recognition
Codeforces Round #804 (Div. 2)(A~D)
Codeforces Round #804 (Div. 2)(A~D)
What has happened from server to cloud hosting?
随机推荐
Reentrantlock fair lock source code Chapter 0
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
Summary of the third course of weidongshan
[reprint] solve the problem that CONDA installs pytorch too slowly
13. Enregistrement et chargement des modèles
Introduction to paddle - using lenet to realize image classification method II in MNIST
NVIDIA Jetson test installation yolox process record
How is it most convenient to open an account for stock speculation? Is it safe to open an account on your mobile phone
CVE-2022-28346:Django SQL注入漏洞
Fofa attack and defense challenge record
AI zhetianchuan ml novice decision tree
2.非线性回归
New library launched | cnopendata China Time-honored enterprise directory
【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
The whole life cycle of commodity design can be included in the scope of industrial Internet
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
They gathered at the 2022 ecug con just for "China's technological power"
Codeforces Round #804 (Div. 2)(A~D)
Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow