当前位置:网站首页>7.正则化应用
7.正则化应用
2022-07-07 23:12:00 【booze-J】
一、正则化的应用
在6.Dropout应用中的未使用Dropout的代码的网络模型构建中添加正则化。
将6.Dropout应用中的
# 创建模型 输入784个神经元,输出10个神经元
model = Sequential([
# 定义输出是200 输入是784,设置偏置为1,添加softmax激活函数 第一个隐藏层有200个神经元
Dense(units=200,input_dim=784,bias_initializer='one',activation="tanh"),
# 第二个隐藏层有 100个神经元
Dense(units=100,bias_initializer='one',activation="tanh"),
Dense(units=10,bias_initializer='one',activation="softmax")
])
修改为
# 创建模型 输入784个神经元,输出10个神经元
model = Sequential([
# 定义输出是200 输入是784,设置偏置为1,添加softmax激活函数 第一个隐藏层有200个神经元
Dense(units=200,input_dim=784,bias_initializer='one',activation="tanh",kernel_regularizer=l2(0.0003)),
# 第二个隐藏层有 100个神经元
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))
])
使用l2正则化之前需要先导入from keras.regularizers import l2
。
运行结果:
从运行结果可以看出来明显克服了一些过拟合的情况,模型对于数据集不是很复杂,加上正则化的话,它的效果可能就不是很好。
完整代码
代码运行平台为jupyter-notebook,文章中的代码块,也是按照jupyter-notebook中的划分顺序进行书写的,运行文章代码,直接分单元粘入到jupyter-notebook即可。
1.导入第三方库
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.加载数据及数据预处理
# 载入数据
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# (60000, 28, 28)
print("x_shape:\n",x_train.shape)
# (60000,) 还未进行one-hot编码 需要后面自己操作
print("y_shape:\n",y_train.shape)
# (60000, 28, 28) -> (60000,784) reshape()中参数填入-1的话可以自动计算出参数结果 除以255.0是为了归一化
x_train = x_train.reshape(x_train.shape[0],-1)/255.0
x_test = x_test.reshape(x_test.shape[0],-1)/255.0
# 换one hot格式
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
3.训练模型
# 创建模型 输入784个神经元,输出10个神经元
model = Sequential([
# 定义输出是200 输入是784,设置偏置为1,添加softmax激活函数 第一个隐藏层有200个神经元
Dense(units=200,input_dim=784,bias_initializer='one',activation="tanh",kernel_regularizer=l2(0.0003)),
# 第二个隐藏层有 100个神经元
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))
])
# 定义优化器
sgd = SGD(lr=0.2)
# 定义优化器,loss_function,训练过程中计算准确率
model.compile(
optimizer=sgd,
loss="categorical_crossentropy",
metrics=['accuracy']
)
# 训练模型
model.fit(x_train,y_train,batch_size=32,epochs=10)
# 评估模型
# 测试集的loss和准确率
loss,accuracy = model.evaluate(x_test,y_test)
print("\ntest loss",loss)
print("test_accuracy:",accuracy)
# 训练集的loss和准确率
loss,accuracy = model.evaluate(x_train,y_train)
print("\ntrain loss",loss)
print("train_accuracy:",accuracy)
边栏推荐
- 3 years of experience, can't you get 20K for the interview and test post? Such a hole?
- New library online | cnopendata China Star Hotel data
- 丸子官网小程序配置教程来了(附详细步骤)
- Service Mesh介绍,Istio概述
- 新库上线 | CnOpenData中国星级酒店数据
- Which securities company has a low, safe and reliable account opening commission
- My best game based on wechat applet development
- A network composed of three convolution layers completes the image classification task of cifar10 data set
- Interface test advanced interface script use - apipost (pre / post execution script)
- They gathered at the 2022 ecug con just for "China's technological power"
猜你喜欢
Semantic segmentation model base segmentation_ models_ Detailed introduction to pytorch
Cause analysis and solution of too laggy page of [test interview questions]
Langchao Yunxi distributed database tracing (II) -- source code analysis
51与蓝牙模块通讯,51驱动蓝牙APP点灯
1293_ Implementation analysis of xtask resumeall() interface in FreeRTOS
fabulous! How does idea open multiple projects in a single window?
Kubernetes static pod (static POD)
Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
随机推荐
The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log
[OBS] the official configuration is use_ GPU_ Priority effect is true
Qt添加资源文件,为QAction添加图标,建立信号槽函数并实现
Codeforces Round #804 (Div. 2)(A~D)
NVIDIA Jetson测试安装yolox过程记录
fabulous! How does idea open multiple projects in a single window?
13. Enregistrement et chargement des modèles
Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
Binder core API
Experience of autumn recruitment in 22 years
Fofa attack and defense challenge record
Kubernetes Static Pod (静态Pod)
Kubernetes static pod (static POD)
Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
9.卷积神经网络介绍
New library online | information data of Chinese journalists
They gathered at the 2022 ecug con just for "China's technological power"
Service Mesh介绍,Istio概述
Leetcode brush questions
国外众测之密码找回漏洞