⚠申明: 未经许可,禁止以任何形式转载,若要引用,请标注链接地址。 全文共计3077字,阅读大概需要3分钟
🌈更多学习内容, 欢迎👏关注👀【文末】我的个人微信公众号:不懂开发的程序猿
个人网站:https://jerry-jy.co/
神经网络鸢尾花分类
- 神经网络鸢尾花分类
- 一、任务需求
- 二、任务目标
- 1、掌握神经网络的构建
- 2、掌握神经网络的编译
- 3、掌握神经网络的训练
- 4、掌握神经网络的概要输出
- 三、任务环境
- 1、jupyter开发环境
- 2、python3.6
- 3、tensorflow2.4
- 四、任务实施过程
- 五、任务小结
神经网络鸢尾花分类
一、任务需求
Iris 鸢尾花数据集是一个经典数据集,在统计学习和机器学习领域都经常被用作示例。数据集内包含 3 类共 150 条记录,每类各 50 个数据,每条记录都有 4 项特征:
iris是鸢尾植物,共4个特征:花萼长度、花萼宽度、花瓣长度、花瓣宽度,可以通过这4个特征预测鸢尾花卉属于(iris-setosa, iris-versicolour, iris-virginica)中的哪一品种。数据说明如下:
要求:利用Sequential模型神经网络使用Tensorflow实现鸢尾花分类模型构建与输出
二、任务目标
1、掌握神经网络的构建
2、掌握神经网络的编译
3、掌握神经网络的训练
4、掌握神经网络的概要输出
三、任务环境
1、jupyter开发环境
2、python3.6
3、tensorflow2.4
四、任务实施过程
1、导入鸢尾花分类所用到的模块
# 利用鸢尾花数据集,实现前向传播、反向传播,可视化loss曲线
# 导入所需模块
import tensorflow as tf
from sklearn import datasets
import numpy as np
2、加载鸢尾花的特征和标签
# 导入数据,分别为输入特征和标签
x_data = datasets.load_iris().data
y_data = datasets.load_iris().target
3、将特征数据和标签数据乱序
# 随机打乱数据(因为原始数据是顺序的,顺序不打乱会影响准确率)
# seed: 随机数种子,是一个整数,当设置之后,每次生成的随机数都一样
np.random.seed(116) # 使用相同的seed,保证输入特征和标签一一对应
np.random.shuffle(x_data)
np.random.seed(116)
np.random.shuffle(y_data)
tf.random.set_seed(116)
4、构建全连接的神经网络,输出层为3个神经元,激活函数是softmax。即以概率的方式输出,正则化采用L2。
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
])
5、模型编译,采用的优化器是SGD,学习率是0.1。损失函数交叉熵,评价指标是准确率。
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
6、模型训练,数据的80%作为训练集,20%作为测试集。32条数据作为一个批次。一共500个批次。校验的频率是20个批次。
model.fit(x_data,y_data, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
Epoch 1/500
4/4 [==============================] - 0s 2ms/step - loss: 2.8038 - sparse_categorical_accuracy: 0.3533
Epoch 2/500
4/4 [==============================] - 0s 1ms/step - loss: 1.0218 - sparse_categorical_accuracy: 0.6467
Epoch 3/500
4/4 [==============================] - 0s 1ms/step - loss: 0.9625 - sparse_categorical_accuracy: 0.6250
Epoch 4/500
4/4 [==============================] - 0s 1ms/step - loss: 0.6996 - sparse_categorical_accuracy: 0.6875
Epoch 5/500
4/4 [==============================] - 0s 1ms/step - loss: 1.0346 - sparse_categorical_accuracy: 0.6067
Epoch 6/500
4/4 [==============================] - 0s 1ms/step - loss: 0.7820 - sparse_categorical_accuracy: 0.6165
Epoch 7/500
4/4 [==============================] - 0s 1ms/step - loss: 0.6032 - sparse_categorical_accuracy: 0.7129
Epoch 8/500
4/4 [==============================] - 0s 1ms/step - loss: 0.5836 - sparse_categorical_accuracy: 0.7369
Epoch 9/500
4/4 [==============================] - 0s 3ms/step - loss: 0.5972 - sparse_categorical_accuracy: 0.7352
Epoch 10/500
4/4 [==============================] - 0s 1ms/step - loss: 0.6684 - sparse_categorical_accuracy: 0.6694
Epoch 11/500
4/4 [==============================] - 0s 1ms/step - loss: 0.8877 - sparse_categorical_accuracy: 0.6708
Epoch 12/500
4/4 [==============================] - 0s 1ms/step - loss: 0.5393 - sparse_categorical_accuracy: 0.7542
Epoch 13/500
...
Epoch 499/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3691 - sparse_categorical_accuracy: 0.9306
Epoch 500/500
4/4 [==============================] - 0s 14ms/step - loss: 0.3634 - sparse_categorical_accuracy: 0.9304 - val_loss: 0.3516 - val_sparse_categorical_accuracy: 0.8667
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
<tensorflow.python.keras.callbacks.History at 0x7fe4545077f0>
7、模型概要,打印模型的参数信息。
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_2 (Dense) (None, 3) 15
=================================================================
Total params: 15
Trainable params: 15
Non-trainable params: 0
_________________________________________________________________
8、我们将模型封装成一个类,d1为该类的方法,为全连接的输出层3个神经元,正则化为L2正则化。激活函数为sigmod函数。
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense
class IrisModel(Model):
def __init__(self):
super(IrisModel, self).__init__()
self.d1 = Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
def call(self, x):
y = self.d1(x)
return y
9、模型的构建
model = IrisModel()
10、模型编译,采用SGD优化器,学习率为0.1,损失函数为交叉熵,以概率方式输出。评价指标是准确率。
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
11、模型训练。数据的80%作为训练集,20%作为测试集。32条数据作为一个批次。一共500个批次。校验的频率是20个批次。
model.fit(x_data, y_data, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
Epoch 1/500
4/4 [==============================] - 0s 2ms/step - loss: 6.3509 - sparse_categorical_accuracy: 0.3127
Epoch 2/500
4/4 [==============================] - 0s 1ms/step - loss: 1.0127 - sparse_categorical_accuracy: 0.6658
Epoch 3/500
4/4 [==============================] - 0s 1ms/step - loss: 0.8219 - sparse_categorical_accuracy: 0.6871
Epoch 4/500
4/4 [==============================] - 0s 1ms/step - loss: 0.6942 - sparse_categorical_accuracy: 0.7735
Epoch 5/500
4/4 [==============================] - 0s 1ms/step - loss: 0.8788 - sparse_categorical_accuracy: 0.6363
Epoch 6/500
4/4 [==============================] - 0s 1ms/step - loss: 0.6772 - sparse_categorical_accuracy: 0.7940
Epoch 7/500
4/4 [==============================] - 0s 1ms/step - loss: 0.5831 - sparse_categorical_accuracy: 0.7408
Epoch 8/500
4/4 [==============================] - 0s 1ms/step - loss: 0.5659 - sparse_categorical_accuracy: 0.8356
Epoch 9/500
4/4 [==============================] - 0s 1ms/step - loss: 0.5365 - sparse_categorical_accuracy: 0.7850
Epoch 10/500
4/4 [==============================] - 0s 2ms/step - loss: 0.5922 - sparse_categorical_accuracy: 0.6813
Epoch 11/500
4/4 [==============================] - 0s 1ms/step - loss: 0.8033 - sparse_categorical_accuracy: 0.6656
Epoch 12/500
4/4 [==============================] - 0s 1ms/step - loss: 0.5156 - sparse_categorical_accuracy: 0.7573
Epoch 13/500
...
Epoch 499/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3696 - sparse_categorical_accuracy: 0.9392
Epoch 500/500
4/4 [==============================] - 0s 13ms/step - loss: 0.3643 - sparse_categorical_accuracy: 0.9304 - val_loss: 0.3525 - val_sparse_categorical_accuracy: 0.8667
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
<tensorflow.python.keras.callbacks.History at 0x7fe4544a2e48>
12、模型概要,输出模型的参数。
model.summary()
Model: "iris_model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_3 (Dense) multiple 15
=================================================================
Total params: 15
Trainable params: 15
Non-trainable params: 0
_________________________________________________________________
五、任务小结
本节完成了使用TensorFlow实现鸢尾花分类。在此过程中,我们对鸢尾花数据读取、划分特征数据和标签数据,同时对鸢尾花数据乱序。构建全连接网络,网络模型编译、训练和输出模型信息。同时我们也对网路模型进行了封装,调用封装后的类来完成神经网络的构建、编译、训练和概要输出。通过本节任务需要掌握以下知识:
- 神经网络的构建
- 神经网络的编译
- 神经网络的训练
- 神经网络的概要输出
–end–