2.使用tensorflow 2.0_keras 搭建分类模型

2019-06-21  本文已影响0人  李涛AT北京

导入库

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(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
    print(module.__name__, module.__version__)

运行结果

sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
matplotlib 3.0.3
numpy 1.16.2
pandas 0.24.2
sklearn 0.20.3
tensorflow 2.0.0-beta1
tensorflow.python.keras.api._v2.keras 2.2.4-tf

加载数据

fashion_mnist = keras.datasets.fashion_mnist
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
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)

运行结果

(5000, 28, 28) (5000,)
(55000, 28, 28) (55000,)
(10000, 28, 28) (10000,)

标准化数据

为何标准化,在以前的一篇文章中,已经讲过。一般深度学习都要标准化。在实际工程中,是否标准化,可以对比标准化前后的训练集,测试集的结果.
# x = (x - u) / std

from sklearn.preprocessing import StandardScaler

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)

使用tf.keras 搭建模型

# tf.keras.models.Sequential()

"""
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->[] 
model.compile(loss="sparse_categorical_crossentropy",
              optimizer = "sgd",
              metrics = ["accuracy"])

创建 callbacks 及训练模型

# 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-5)
]
history = model.fit(x_train_scaled, y_train, epochs=10,
                    validation_data=(x_valid_scaled, y_valid),
                    callbacks = callbacks)

运行结果

Train on 55000 samples, validate on 5000 samples
Epoch 1/10
55000/55000 [==============================] - 3s 54us/sample - loss: 0.5263 - accuracy: 0.8138 - val_loss: 0.4263 - val_accuracy: 0.8460
Epoch 2/10
55000/55000 [==============================] - 3s 49us/sample - loss: 0.3879 - accuracy: 0.8605 - val_loss: 0.3648 - val_accuracy: 0.8696
Epoch 3/10
55000/55000 [==============================] - 3s 48us/sample - loss: 0.3518 - accuracy: 0.8727 - val_loss: 0.3436 - val_accuracy: 0.8764
Epoch 4/10
55000/55000 [==============================] - 3s 49us/sample - loss: 0.3282 - accuracy: 0.8812 - val_loss: 0.3314 - val_accuracy: 0.8804
Epoch 5/10
55000/55000 [==============================] - 3s 49us/sample - loss: 0.3079 - accuracy: 0.8889 - val_loss: 0.3393 - val_accuracy: 0.8766
Epoch 6/10
55000/55000 [==============================] - 3s 48us/sample - loss: 0.2925 - accuracy: 0.8944 - val_loss: 0.3184 - val_accuracy: 0.8866
Epoch 7/10
55000/55000 [==============================] - 3s 49us/sample - loss: 0.2793 - accuracy: 0.8985 - val_loss: 0.3131 - val_accuracy: 0.8852
Epoch 8/10
55000/55000 [==============================] - 3s 49us/sample - loss: 0.2673 - accuracy: 0.9034 - val_loss: 0.3182 - val_accuracy: 0.8838
Epoch 9/10
55000/55000 [==============================] - 3s 49us/sample - loss: 0.2555 - accuracy: 0.9067 - val_loss: 0.3221 - val_accuracy: 0.8848
Epoch 10/10
55000/55000 [==============================] - 3s 49us/sample - loss: 0.2453 - accuracy: 0.9119 - val_loss: 0.2971 - val_accuracy: 0.8914

展示训练集,验证集的效果

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)

运行结果

2019062112.png

测试集效果

model.evaluate(x_test_scaled, y_test)

运行结果

10000/10000 [==============================] - 0s 26us/sample - loss: 0.3393 - accuracy: 0.8775
[0.33926754564046857, 0.8775]

查看tensorboard训练过程

2019062111.png
上一篇 下一篇

猜你喜欢

热点阅读