1.tensorflow2.0-keras分类模型(初步实现->使用归一化)(fashion-mnist数据集)

环境:
tensorflow2.0
keras

初步实现

1.导入库

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

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

3.显示数据集中的一张图片

def show_single_image(img_arr):
    plt.imshow(img_arr,cmap="binary")
    plt.show()
    
show_single_image(x_train[0])

1.tensorflow2.0-keras分类模型(初步实现->使用归一化)(fashion-mnist数据集)_第1张图片
4.创建keras模型,主要用sequential
可以在官网中查看Sequential模块
tf2.0->keras->sequential

#tf.keras.models.Sequential()
model = keras.models.Sequential()
#add input layer , dense layer, dense layer , output layer
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"))
#relu : y=max(0,x)
#softmax: 概率分布

#reason for sparse: y->index.    y->one_hot->[]
model.compile(loss="sparse_categorical_crossentropy",
              optimizer="sgd",
              metrics = ["accuracy"])

5.查看模型的层数和结构

#查看模型的层数
model.layers
output:[,
 		,
 		,
 		]

6.查看每一层参数情况

model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten (Flatten)            (None, 784)               0         
_________________________________________________________________
dense (Dense)                (None, 300)               235500    
_________________________________________________________________
dense_1 (Dense)              (None, 100)               30100     
_________________________________________________________________
dense_2 (Dense)              (None, 10)                1010      
=================================================================
Total params: 266,610
Trainable params: 266,610
Non-trainable params: 0
_________________________________________________________________

7.开始训练

#train
history = model.fit(x_train,y_train,epochs=10,
                    validation_data = (x_valid,y_valid))

8。查看训练时的参数变化

history.history

9.画训练迭代次数与训练的精度的折线图

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

plot_learning_curve(history)

实现归一化

归一化主要是将x_trian,x_valid,x_test进行处理,然后将处理后的数据放入model中训练。归一化的方法可见 知识点:归一化与批归一化

在上述的步骤2后添加以下代码:

#normalize  x=(x-u)/std, 均值为0,方差为1

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 = 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)

#除了这种方式,还有min-max normalization: x*=(x-x_min)/(x_max-x_min)

改变步骤7中的参数

#training and validation
history = model.fit(x_train_scaled,y_train,epochs=10,
                    validation_data = (x_valid_scaled,y_valid))

步骤7后添加测试部分代码

#test
model.evaluate(x_test_scaled,y_test)

完整的代码可参见myhub

你可能感兴趣的:(tensorflow)