Keras(一)分类模型实战

38 篇文章 2 订阅
16 篇文章 0 订阅

本文将介绍如下内容:

  • 导入、打印使用的python库的版本信息
  • 从keras.datasets中提取图片数据集-“训练数据”、“验证数据”、“测试数据”
  • 查看单、多张图片
  • 分类模型之模型构建
  • 查看模型的图结构
  • 训练模型
  • 绘图表示模型参数的变化过程
  • 数据的标准化
  • 回调函数

一,导入、打印使用的python库的版本信息

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras

# 打印使用的python库的版本信息
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
    print(module.__name__, module.__version__)
    
#------------------output----------------------
sys.version_info(major=3, minor=8, micro=5, releaselevel='final', serial=0)
matplotlib 3.3.2
numpy 1.19.1
pandas 1.0.3
sklearn 0.23.2
tensorflow 2.2.0
tensorflow.keras 2.3.0-tf

二,从keras.datasets中提取图片数据集-“训练数据”、“验证数据”、“测试数据

# 1,从keras.datasets中提取图片数据集-“训练数据”、“验证数据”、“测试数据”
fashion_mnist = keras.datasets.fashion_mnist
# 提取“训练数据”、“测试数据”
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
# 将“训练数据”中的前5000条数据提取为“验证数据”
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]
# 打印数据集shape维度
print(x_valid.shape, y_valid.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

#------------------output----------------------
(5000, 28, 28) (5000,)
(55000, 28, 28) (55000,)
(10000, 28, 28) (10000,)

三,查看单、多张图片

1,查看单张图片
def show_single_image(img_arr):
    plt.imshow(img_arr, cmap="binary")
    plt.show()

show_single_image(x_train[0])
2,查看多张图片
def show_imgs(n_rows, n_cols, x_data, y_data, class_names):
    assert len(x_data) == len(y_data)
    assert n_rows * n_cols < len(x_data)
    plt.figure(figsize = (n_cols * 1.4, n_rows * 1.6))
    for row in range(n_rows):
        for col in range(n_cols):
            index = n_cols * row + col 
            plt.subplot(n_rows, n_cols, index+1)
            plt.imshow(x_data[index], cmap="binary",interpolation = 'nearest')
            plt.axis('off')
            plt.title(class_names[y_data[index]])
    plt.show()

class_names = ['T-shirt', 'Trouser', 'Pullover', 'Dress','Coat', 'Sandal',
                'Shirt', 'Sneaker','Bag', 'Ankle boot']
show_imgs(3, 5, x_train, y_train, class_names)

四,分类模型之模型构建

"""
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28, 28]))
model.add(keras.layers.Dense(300, activation="relu"))
model.add(keras.layers.Dense(100, activation="relu"))
model.add(keras.layers.Dense(10, activation="softmax"))
"""

model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28, 28]), # 输入层
    keras.layers.Dense(300, activation='relu'), # 全连接层
    keras.layers.Dense(100, activation='relu'), # 全连接层
    keras.layers.Dense(10, activation='softmax')# 输出层
])

# relu: y = max(0, x)
# softmax: 将向量变成概率分布. x = [x1, x2, x3], 
#          y = [e^x1/sum, e^x2/sum, e^x3/sum], sum = e^x1 + e^x2 + e^x3

# reason for sparse: y->index. y->one_hot->[] 
# y 如果是一个向量,那么损失函数为:categorical_crossentropy;
# y 如果是一个数字,那么损失函数为:sparse_categorical_crossentropy;
model.compile(loss="sparse_categorical_crossentropy",# categorical 类别;crossentropy交叉熵
              optimizer = keras.optimizers.SGD(0.001),# 优化方法
              metrics = ["accuracy"])

五,查看模型的图结构

# 查看模型的层级
print(model.layers) 

# 查看模型架构的参数
# [None, 784] * W + b -> [None, 300] W.shape [784, 300], b = [300]
print(model.summary())

# ------------output----------------------------
[<tensorflow.python.keras.layers.core.Flatten object at 0x7f727bc5e670>, 
<tensorflow.python.keras.layers.core.Dense object at 0x7f727b244400>, 
<tensorflow.python.keras.layers.core.Dense object at 0x7f727bc8f4c0>, 
<tensorflow.python.keras.layers.core.Dense object at 0x7f727bc8fc70>]

Model: "sequential_9"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_9 (Flatten)          (None, 784)               0         
_________________________________________________________________
dense_27 (Dense)             (None, 300)               235500    
_________________________________________________________________
dense_28 (Dense)             (None, 100)               30100     
_________________________________________________________________
dense_29 (Dense)             (None, 10)                1010      
=================================================================
Total params: 266,610
Trainable params: 266,610
Non-trainable params: 0
_________________________________________________________________
None

六,训练模型

history = model.fit(x_train, y_train, epochs=10,validation_data=(x_valid, y_valid))
print(type(history))
print(history.history)

# ------------output----------------------------
Epoch 1/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3319 - accuracy: 0.8824 - val_loss: 0.3526 - val_accuracy: 0.8740
Epoch 2/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3285 - accuracy: 0.8847 - val_loss: 0.3505 - val_accuracy: 0.8740
Epoch 3/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3252 - accuracy: 0.8857 - val_loss: 0.3473 - val_accuracy: 0.8768
Epoch 4/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3222 - accuracy: 0.8865 - val_loss: 0.3435 - val_accuracy: 0.8780
Epoch 5/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3187 - accuracy: 0.8875 - val_loss: 0.3462 - val_accuracy: 0.8786
Epoch 6/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.3163 - accuracy: 0.8885 - val_loss: 0.3403 - val_accuracy: 0.8784
Epoch 7/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3134 - accuracy: 0.8896 - val_loss: 0.3405 - val_accuracy: 0.8796
Epoch 8/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3104 - accuracy: 0.8898 - val_loss: 0.3389 - val_accuracy: 0.8796
Epoch 9/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3078 - accuracy: 0.8914 - val_loss: 0.3384 - val_accuracy: 0.8802
Epoch 10/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3055 - accuracy: 0.8923 - val_loss: 0.3346 - val_accuracy: 0.8818
<class 'tensorflow.python.keras.callbacks.History'>
{'loss': [0.3319365084171295, 0.3284938931465149, 0.3252427577972412, 0.32224059104919434, 0.31868669390678406, 0.31627702713012695, 0.3133821487426758, 0.31036657094955444, 0.3077640235424042, 0.30545520782470703], 'accuracy': [0.8824363350868225, 0.8846545219421387, 0.8857272863388062, 0.8864727020263672, 0.8875272870063782, 0.8885454535484314, 0.8896363377571106, 0.8898181915283203, 0.8913999795913696, 0.8923090696334839], 'val_loss': [0.35259583592414856, 0.3505200743675232, 0.34725818037986755, 0.34350329637527466, 0.34617453813552856, 0.3403223752975464, 0.34054407477378845, 0.33890753984451294, 0.33837735652923584, 0.3346402645111084], 'val_accuracy': [0.8740000128746033, 0.8740000128746033, 0.876800000667572, 0.878000020980835, 0.878600001335144, 0.8784000277519226, 0.8795999884605408, 0.8795999884605408, 0.8802000284194946, 0.8817999958992004]}

七,绘图表示模型参数的变化过程

def plot_learning_curves(history):
    pd.DataFrame(history.history).plot(figsize=(8, 5))
    plt.grid(True)
    plt.gca().set_ylim(0, 1)
    plt.show()
plot_learning_curves(history)

八,数据的标准化

print(np.max(x_train), np.min(x_train))
# x = (x - u) / std

from sklearn.preprocessing import StandardScaler
# from sklearn.preprocessing import MinMaxScaler


scaler = StandardScaler()
# x_train: [None, 28, 28] -> [None, 784]
x_train_scaled = scaler.fit_transform(x_train.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)
x_valid_scaled = scaler.transform(x_valid.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)
x_test_scaled = scaler.transform(x_test.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)

print(np.max(x_train_scaled), np.min(x_train_scaled))

x_train = x_train_scaled
x_valid = x_valid_scaled
x_test = x_test_scaled

九,回调函数

在callbacks同级目录中,执行“tensorboard --logdir=callbacks”,会临时在本地搭建服务tensorboard服务。
层级结构如下:
在这里插入图片描述
代码如下:

# Tensorboard, earlystopping, ModelCheckpoint
logdir = './callbacks'
if not os.path.exists(logdir):
    os.mkdir(logdir)
output_model_file = os.path.join(logdir,
                                 "fashion_mnist_model.h5")

callbacks = [
    keras.callbacks.TensorBoard(logdir),
    keras.callbacks.ModelCheckpoint(output_model_file,
                                    save_best_only = True),
    keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3),
]
history = model.fit(x_train_scaled, y_train, epochs=10,
                    validation_data=(x_valid_scaled, y_valid),
                    callbacks = callbacks)

十,总结代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 30 10:07:57 2020

@author: nijiahui
"""
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras

# 打印使用的python库的版本信息
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
    print(module.__name__, module.__version__)

# 1,从keras.datasets中提取图片数据集-“训练数据”、“验证数据”、“测试数据”
fashion_mnist = keras.datasets.fashion_mnist
# 提取“训练数据”、“测试数据”
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
# 将“训练数据”中的前5000条数据提取为“验证数据”
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]
# 打印数据集shape维度
print(x_train.shape, y_train.shape)
print(x_valid.shape, y_valid.shape)
print(x_test.shape, y_test.shape)

# 8,数据标准化
print(np.max(x_train), np.min(x_train))
# x = (x - u) / std

from sklearn.preprocessing import StandardScaler
# from sklearn.preprocessing import MinMaxScaler

scaler = StandardScaler()
# x_train: [None, 28, 28] -> [None, 784]
x_train_scaled = scaler.fit_transform(x_train.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)
x_valid_scaled = scaler.transform(x_valid.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)
x_test_scaled = scaler.transform(x_test.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)

print(np.max(x_train_scaled), np.min(x_train_scaled))

x_train = x_train_scaled
x_valid = x_valid_scaled
x_test = x_test_scaled


# # 2,查看单张图片
# def show_single_image(img_arr):
#     plt.imshow(img_arr, cmap="binary")
#     plt.show()

# show_single_image(x_train[0])

# # 3,查看多张图片
# def show_imgs(n_rows, n_cols, x_data, y_data, class_names):
#     assert len(x_data) == len(y_data)
#     assert n_rows * n_cols < len(x_data)
#     plt.figure(figsize = (n_cols * 1.4, n_rows * 1.6))
#     for row in range(n_rows):
#         for col in range(n_cols):
#             index = n_cols * row + col 
#             plt.subplot(n_rows, n_cols, index+1)
#             plt.imshow(x_data[index], cmap="binary",interpolation = 'nearest')
#             plt.axis('off')
#             plt.title(class_names[y_data[index]])
#     plt.show()

# class_names = ['T-shirt', 'Trouser', 'Pullover', 'Dress','Coat', 'Sandal',
#                 'Shirt', 'Sneaker','Bag', 'Ankle boot']
# show_imgs(3, 5, x_train, y_train, class_names)

# 4,分类模型之模型构建
"""
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28, 28]))
model.add(keras.layers.Dense(300, activation="relu"))
model.add(keras.layers.Dense(100, activation="relu"))
model.add(keras.layers.Dense(10, activation="softmax"))
"""

model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28, 28]), # 输入层
    keras.layers.Dense(300, activation='relu'), # 全连接层
    keras.layers.Dense(100, activation='relu'), # 全连接层
    keras.layers.Dense(10, activation='softmax')# 输出层
])

# relu: y = max(0, x)
# softmax: 将向量变成概率分布. x = [x1, x2, x3], 
#          y = [e^x1/sum, e^x2/sum, e^x3/sum], sum = e^x1 + e^x2 + e^x3

# reason for sparse: y->index. y->one_hot->[] 
# y 如果是一个向量,那么损失函数为:categorical_crossentropy;
# y 如果是一个数字,那么损失函数为:sparse_categorical_crossentropy;
model.compile(loss="sparse_categorical_crossentropy",# categorical 类别;crossentropy交叉熵
              optimizer = keras.optimizers.SGD(0.001),# 优化方法
              metrics = ["accuracy"])


# # 5,查看模型的图结构
# # 查看模型的层级
# print(model.layers) 

# # 查看模型架构的参数
# # [None, 784] * W + b -> [None, 300] W.shape [784, 300], b = [300]
# print(model.summary()) 

# # 6,训练模型
# history = model.fit(x_train, y_train, epochs=10,validation_data=(x_valid, y_valid))
# print(type(history))
# print(history.history)


# 9,回调函数
# Tensorboard, earlystopping, ModelCheckpoint
logdir = './callbacks'
if not os.path.exists(logdir):
    os.mkdir(logdir)
output_model_file = os.path.join(logdir,"fashion_mnist_model.h5")

callbacks = [
    keras.callbacks.TensorBoard(logdir),# TensorBoard-终端输入“tensorboard --logdir=callbacks”查看图结构
    keras.callbacks.ModelCheckpoint(output_model_file,save_best_only = True),# 保存最好的模型结果
    keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3),# 当连续“patience”次增益小于“min_delta”时提前结束
]
history = model.fit(x_train, y_train, epochs=40,
                    validation_data=(x_valid, y_valid),
                    callbacks = callbacks)


# 7,绘图表示模型参数的变化过程
def plot_learning_curves(history):
    pd.DataFrame(history.history).plot(figsize=(8, 5))
    plt.grid(True)
    plt.gca().set_ylim(0, 1)
    plt.show()

plot_learning_curves(history)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值