当前位置:网站首页>7. Regularization application
7. Regularization application
2022-07-08 01:01:00 【booze-J】
One 、 Application of regularization
stay 6.Dropout application Unused in Dropout Add regularization to the network model construction of code .
take 6.Dropout In application
# Creating models Input 784 Neurons , Output 10 Neurons
model = Sequential([
# Define output yes 200 Input is 784, Set offset to 1, add to softmax Activation function The first hidden layer has 200 Neurons
Dense(units=200,input_dim=784,bias_initializer='one',activation="tanh"),
# The second hidden layer has 100 Neurons
Dense(units=100,bias_initializer='one',activation="tanh"),
Dense(units=10,bias_initializer='one',activation="softmax")
])
It is amended as follows
# Creating models Input 784 Neurons , Output 10 Neurons
model = Sequential([
# Define output yes 200 Input is 784, Set offset to 1, add to softmax Activation function The first hidden layer has 200 Neurons
Dense(units=200,input_dim=784,bias_initializer='one',activation="tanh",kernel_regularizer=l2(0.0003)),
# The second hidden layer has 100 Neurons
Dense(units=100,bias_initializer='one',activation="tanh",kernel_regularizer=l2(0.0003)),
Dense(units=10,bias_initializer='one',activation="softmax",kernel_regularizer=l2(0.0003))
])
Use l2 Before regularization, you need to import from keras.regularizers import l2
.
Running results :
It can be seen from the running results that some over fitting conditions have been obviously overcome , The model is not very complex for data sets , With regularization , Its effect may not be very good .
Complete code
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
from tensorflow.keras.optimizers import SGD
from keras.regularizers import l2
2. Loading data and 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,784) reshape() Middle parameter filling -1 Parameter results can be calculated automatically Divide 255.0 To normalize
x_train = x_train.reshape(x_train.shape[0],-1)/255.0
x_test = x_test.reshape(x_test.shape[0],-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. Training models
# Creating models Input 784 Neurons , Output 10 Neurons
model = Sequential([
# Define output yes 200 Input is 784, Set offset to 1, add to softmax Activation function The first hidden layer has 200 Neurons
Dense(units=200,input_dim=784,bias_initializer='one',activation="tanh",kernel_regularizer=l2(0.0003)),
# The second hidden layer has 100 Neurons
Dense(units=100,bias_initializer='one',activation="tanh",kernel_regularizer=l2(0.0003)),
Dense(units=10,bias_initializer='one',activation="softmax",kernel_regularizer=l2(0.0003))
])
# Define optimizer
sgd = SGD(lr=0.2)
# Define optimizer ,loss_function, The accuracy of calculation during training
model.compile(
optimizer=sgd,
loss="categorical_crossentropy",
metrics=['accuracy']
)
# Training models
model.fit(x_train,y_train,batch_size=32,epochs=10)
# Evaluation model
# Test set loss And accuracy
loss,accuracy = model.evaluate(x_test,y_test)
print("\ntest loss",loss)
print("test_accuracy:",accuracy)
# Training set loss And accuracy
loss,accuracy = model.evaluate(x_train,y_train)
print("\ntrain loss",loss)
print("train_accuracy:",accuracy)
边栏推荐
- Marubeni official website applet configuration tutorial is coming (with detailed steps)
- 炒股开户怎么最方便,手机上开户安全吗
- Cve-2022-28346: Django SQL injection vulnerability
- 5g NR system messages
- 完整的模型训练套路
- 14.绘制网络模型结构
- The weight of the product page of the second level classification is low. What if it is not included?
- Codeforces Round #804 (Div. 2)
- Basic types of 100 questions for basic grammar of Niuke
- Lecture 1: the entry node of the link in the linked list
猜你喜欢
Codeforces Round #804 (Div. 2)(A~D)
Letcode43: string multiplication
5g NR system messages
They gathered at the 2022 ecug con just for "China's technological power"
取消select的默认样式的向下箭头和设置select默认字样
Codeforces Round #804 (Div. 2)(A~D)
Malware detection method based on convolutional neural network
Binder core API
Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
Interface test advanced interface script use - apipost (pre / post execution script)
随机推荐
133. 克隆图
完整的模型训练套路
Handwriting a simulated reentrantlock
QT establish signal slots between different classes and transfer parameters
Prediction of the victory or defeat of the League of heroes -- simple KFC Colonel
tourist的NTT模板
y59.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三二)
1.线性回归
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
Malware detection method based on convolutional neural network
8道经典C语言指针笔试题解析
Su embedded training - Day5
DNS series (I): why does the updated DNS record not take effect?
跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
The method of server defense against DDoS, Hangzhou advanced anti DDoS IP section 103.219.39 x
FOFA-攻防挑战记录
基于微信小程序开发的我最在行的小游戏
A network composed of three convolution layers completes the image classification task of cifar10 data set
QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)