Spark_Flink_Hadoop

tensorflow2.0 keras-分类问题

2020-01-30  本文已影响0人  kason_zhang
学习And交流

大纲

应用开发:

简介

什么是keras

那什么又是tensorflow-keras是什么?

TF-keras 和 keras联系

TF-keras 和 keras区别

如何选择? 到底是使用tf.keras 还是keras?

目标函数

分类问题

分类问题的目标函数

此处举例

使用tf-keras 来完成一个分类问题:

先导入包:

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

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

本地环境的包版本如下:

2.0.0-beta1
sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)
matplotlib 3.1.1
numpy 1.17.2
pandas 0.25.1
sklearn 0.21.3
tensorflow 2.0.0-beta1
tensorflow.python.keras.api._v2.keras 2.2.4-tf

keras 里面封装了一些数据集, 可以直接导入使用。

# 导入并显示数据
fashion_mnist = keras.datasets.fashion_mnist
print(fashion_mnist, '\n',type(fashion_mnist))
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()

print(type(x_train_all), type(y_train_all))
# 训练集 在拆分为 训练集和验证集
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)

插曲, 一开始下载数据的时候 直接报错:

URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)

解决方式为:

  1. 设置本机代理 http://127.0.0.1:1087 ,科学上网操作
  2. 关掉ssl验证
import ssl
os.environ['http_proxy'] = 'http://127.0.0.1:1087'
os.environ['https_proxy'] = 'https://127.0.0.1:1087'

ssl._create_default_https_context = ssl._create_unverified_context

显示: 有单张图片显示 以及 多张图片显示

def show_single_img(img_arr):
    plt.imshow(img_arr, cmap= 'binary')
    plt.show()
show_single_img(x_train[0])
image.png

多张图片展示

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)
image.png

开始训练模型:
有两种写法:

  1. relu : y = max(0,x)
  2. softmax: 将向量变为概率分布, x = [x1,x2,x3], y = [e^x1/sum, ex2/sum,ex3/sum], sum = e^x1 + e^x2 + e^x3
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.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, 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'])

执行model.summary()


model summary
# 从summary 可以看到
# [None, 784] 如何 转换成下面的(None, 300)呢?
# [None, 784] * W + b -> [none, 300], W是[784,300], b = [300]
# 所以参数量是 784 * 300 + 300 = 785 * 300 = 235500

开始迭代训练:

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

'''
history = model.fit(x_train_all, y_train_all, epochs=10)
'''
训练结果

查看损失结果以及准确率曲线

historyDf = pd.DataFrame(history.history)
historyDf.head()

historyDf.plot(figsize = (8,5))
plt.grid(True)
plt.gca().set_ylim(0,1)
plt.show()
结果曲线

预测

当训练好模型之后, 即可预测测试集,

test_loss, test_acc = model.evaluate(x_test, y_test)

print('\nTest accuracy:', test_acc)
结果预测

从图上可以看到 我们对10000个样本进行预测, 准确率在86.7%

归一化

x = (x - u)/std u 是均值 std 是方差

tf-keras回调函数

tf-keras的连接参考如下:
https://tensorflow.google.cn/versions/r2.0/api_docs/python/tf/keras/callbacks

# 使用三种回调函数
logdir = './tf_callbacks'
if not os.path.exists(logdir) :
    os.mkdir(logdir)
output_model_file = os.path.join(logdir,'fashion_minist_model.h5')
callbacks = [
    keras.callbacks.TensorBoard(log_dir=logdir),
    keras.callbacks.ModelCheckpoint(filepath=output_model_file,
                                   save_best_only=True),
    keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3),
]


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

运行结束之后, 会生成文件夹


image.png

然后使用tensorboard 命令进行加载该文件夹

./tensorboard --logdir /Users/mingtong/Desktop/tfcallbacks/

点开其中的链接 可以看到Tensorboard中的一些信息


tensorboard
image.png
image.png
上一篇下一篇

猜你喜欢

热点阅读