tensorflow2.0学习——tensorflow-keras归一化

更新时间:2023-07-30 14:17:18 阅读: 评论:0

tensorflow2.0学习——tensorflow-keras归⼀化
⼀、归⼀化简介
在对数据进⾏预处理时,经常要⽤到归⼀化⽅法。
在深度学习中,将数据归⼀化到⼀个特定的范围能够在反向传播中获得更好的收敛。如果不进⾏数据标准化,有些特征(值很⼤)将会对损失函数影响更⼤,使得其他值⽐较⼩的特征的重要性降低。因此数据标准化可以使得每个特征的重要性更加均衡。
公式表达为:
⼆、归⼀化实战
在这⾥我们可以将所使⽤的的图像分类的代码,修改为有将数据归⼀化的代码,命名为 ------ tf_keras_classification_model-normalize,工匠
代码如下:
import进必要的模块
import matplotlib as mpl
import matplotlib.pyplot as plt
#%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
print(sys.version_info)
for module in mpl,np,pd,sklearn,tf,keras:
print(module.__name__,module.__version__)
导⼊数据
# 导⼊Keras中的数据
fashion_mnist = keras.datats.fashion_mnist
幻城结局(x_train_all,y_train_all),(x_test,y_test) = fashion_mnist.load_data()
# 将数据集拆分成训练集与验证集
# 将前5000张图⽚作为验证集,后55000张图⽚作为训练集
x_valid,x_train = x_train_all[:5000],x_train_all[5000:]
y_valid,y_train = y_train_all[:5000],y_train_all[5000:]
print(x_valid.shape,y_valid.shape)
print(x_train.shape,y_train.shape)
print(x_test.shape,y_test.shape)
在这⾥我们可以查看下数据集中的最⼤最⼩值
print(np.max(x_train),np.min(x_train))
代码执⾏结果如下:
255 0
即训练集中最⼤值为255,最⼩值为0
接下来我们将数据集中的数据进⾏归⼀化处理
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(
x_train.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
x_valid_scaled = ansform(
x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
x_test_scaled = ansform(
x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
在这⾥ scaler.fit_transform 即是把 x_train 作归⼀化处理的⽅法,但这个⽅法的输⼊参数要求是⼆维的,⽽我们的 x_train 是三维的([None, 28, 28]), 故需先将其转化为⼆维的。
上述 x_train.astype(np.float32).reshape(-1, 1) 的作⽤就是将三维数据转化为⼆维([None, 784])。
但是在将数据转换为⼆维进⾏归⼀化之后,还需在转化回三维,这样才能传⼊模型中进⾏训练。reshape(-1, 28, 28)的作⽤就是如此。
这⾥还需注意的是训练集的归⼀化⽅法为 scaler.fit_transform,验证集和测试集的归⼀化⽅法为ansform。
归⼀化后我们可以再查看下训练集中的最⼤最⼩值:
print(np.max(x_train_scaled),np.min(x_train_scaled))
结果为:
2.0231433 -0.8105136
这样⼀来将数据归⼀化处理就做好了。
推开的拼音
再构建同样的模型进⾏训练
工作精神model = dels.Sequential([
世界探索keras.layers.Flatten(input_shape=[28,28]),
keras.layers.Den(300,activation='relu'),
keras.layers.Den(100,activation='relu'),
keras.layers.Den(10,activation='softmax')
])
价值的定义是什么#配置学习过程
optimizer='sgd',
metrics=['accuracy'])weightlifting
#开启训练
history = model.fit(x_train_scaled,y_train,epochs=10,
validation_data=(x_valid_scaled,y_valid))
训练过程打印如下:
Train on 55000 samples, validate on 5000 samples
Epoch 1/10
55000/55000 [==============================] - 10s 175us/sample - loss: 0.5347 - accuracy: 0.8103 - val_loss: 0.4324 - val_accuracy: 0.8478
Epoch 2/10
55000/55000 [==============================] - 8s 149us/sample - loss: 0.3912 - accuracy: 0.8603 - val_loss: 0.3692 - val_accuracy: 0.8670
Epoch 3/10
55000/55000 [==============================] - 15s 273us/sample - loss: 0.3520 - accuracy: 0.8729 - val_loss: 0.3528 - val_accuracy: 0.8742
Epoch 4/10
55000/55000 [==============================] - 11s 195us/sample - loss: 0.3269 - accuracy: 0.8804 - val_loss: 0.3582 - val_accuracy: 0.8714
Epoch 5/10
55000/55000 [==============================] - 10s 185us/sample - loss: 0.3080 - accuracy: 0.8878 - val_loss: 0.3339 - val_accuracy: 0.8806
Epoch 6/10
55000/55000 [==============================] - 11s 202us/sample - loss: 0.2919 - accuracy: 0.8942 - val_loss: 0.3576 - val_accuracy: 0.8726
Epoch 7/10
55000/55000 [==============================] - 7s 136us/sample - loss: 0.2793 - accuracy: 0.8981 - val_loss: 0.3159 - val_accuracy: 0.8872
Epoch 8/10
55000/55000 [==============================] - 8s 149us/sample - loss: 0.2661 - accuracy: 0.9031 - val_loss: 0.3157 - val_accuracy: 0.8828
Epoch 9/10
55000/55000 [==============================] - 9s 166us/sample - loss: 0.2552 - accuracy: 0.9083 - val_loss: 0.3118 - val_accuracy: 0.8820
Epoch 10/10
55000/55000 [==============================] - 8s 152us/sample - loss: 0.2455 - accuracy: 0.9110 - val_loss: 0.2982 - val_accuracy: 0.8920
可以看出相⽐上⼀节的训练结果,使⽤归⼀化后的训练效果更好。
也可以将训练结果可视化
#变化过程可视化
def plot_learning_curves(history):
pd.DataFrame(history.history).plot(figsize=(8,5))会计分期的意义
plt.show()
plot_learning_curves(history)
打印结果为:
最后可以对测试集进⾏评估
#测试,评估
model.evaluate(x_test_scaled,y_test)输出结果为:
loss: 0.2733 - accuracy: 0.8825
即精度为0.8825

本文发布于:2023-07-30 14:17:18,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1102044.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据   训练   集中
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图