Tensorflow学习笔记(六)——卷积神经网络

  实现对fashion-minist分类:
  (1)引包

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pandas as pd
import sklearn
import sys
import time
import tensorflow as tf
from tensorflow import keras
import pprint

print(tf.__version__)
print(sys.version_info)
for module in mpl,np,pd,sklearn,tf,keras:
    print(module.__name__,module.__version__)

  (2)数据导入及归一化

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:]

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,1)
x_valid_scaled = scaler.transform(
        x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,28,28,1)
x_test_scaled = scaler.transform(
        x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28,1)

  (3)模型构建

#tf.keras.models.Sequential()

model = keras.models.Sequential()
#卷积层
model.add(keras.layers.Conv2D(filters = 32,kernel_size = 3,
                              padding = 'same',
                              activation = 'relu',
                              input_shape = (28,28,1)))
model.add(keras.layers.Conv2D(filters = 32,kernel_size = 3,
                              padding = 'same',
                              activation = 'relu'))
#池化层
model.add(keras.layers.MaxPool2D(pool_size = 2))
#卷积层
model.add(keras.layers.Conv2D(filters = 64,kernel_size = 3,
                              padding = 'same',
                              activation = 'relu'))
model.add(keras.layers.Conv2D(filters = 64,kernel_size = 3,
                              padding = 'same',
                              activation = 'relu'))
#池化层
model.add(keras.layers.MaxPool2D(pool_size = 2))
#卷积层
model.add(keras.layers.Conv2D(filters = 128,kernel_size = 3,
                              padding = 'same',
                              activation = 'relu'))
model.add(keras.layers.Conv2D(filters = 128,kernel_size = 3,
                              padding = 'same',
                              activation = 'relu'))
#池化层
model.add(keras.layers.MaxPool2D(pool_size = 2))
#全连接层
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(128,activation = 'relu'))
model.add(keras.layers.Dense(10,activation='softmax'))

model.compile(loss = "sparse_categorical_crossentropy",
              optimizer = "adam",
              metrics = ["accuracy"])

  (4)训练及生成tensorboard

# Tensorboard, earlystopping, ModelCheckpoint
# logdir = './callbacks'  这么写在windows存在bug上会报错,linux上不会
logdir = os.path.join("cnn_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),
]
his = model.fit(x_train_scaled,y_train,epochs=10,validation_data=(x_valid_scaled,y_valid),
               callbacks = callbacks)

  (5)训练loss图及预测

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

plot_learning_curves(his)
model.evaluate(x_test_scaled,y_test)
"""
输出:loss,accuracy:[0.29273508065640924, 0.9186]
"""

你可能感兴趣的:(Tensorflow学习笔记(六)——卷积神经网络)