tensorflow2.0学习笔记:简单分类模型实现

tensorflow2.0 实现最简单的分类模型

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

print(tf.__version__)
    2.0.0
#读取数据
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,)
def show_single_image(img_arr):
    plt.imshow(img_arr,cmap="binary")
show_single_image(x_train[0])

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-j6uqVjDy-1588643154442)(output_3_0.png)]

def show_imgs(n_rows,n_cols,x_data,y_data,class_names):
    assert len(x_data) == len(y_data)  #验证:assert--断言,大致相当if - else.
    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']

#显示3行5列
show_imgs(3,5,x_train,y_train,class_names)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dUYrmb9F-1588643154444)(output_4_0.png)]

通过tf.keras.Sequential()来实现分类网络,有以下两种方式:

#1.先定义一个模型,以叠加得形式添加网络层。
'''
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28])) #输入28*28的图像,展平成28*28的1维向量
model.add(keras.layers.Dense(300,activation="relu")) #全连接层,300个神经元,激活函数rule
model.add(keras.layers.Dense(100,activation="relu")) #relu: y = max(0,x)
model.add(keras.layers.Dense(10,activation="softmax")) #输出层为全连接层,10类,激活函数softmax,获得每一类的概率

# soltmax: 将向量变成概率分布. example: x = [x1,x2,x3],y = [e^x1/sum,e^x2/sum,e^x3/sum],sum = e^x1+e^x2+e^x3
'''

#2.以列表形式书写
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")
])

model.compile(loss="sparse_categorical_crossentropy",
             optimizer = "adam",
             metrics = ["accuracy"]) #编译,将精度accuracy保存到metrics
#reason for sparse,y是一个index值,并没有one-hot编码,one-hot直接用categorical_crossentropy.
model.layers #展示模型有多少层
[<tensorflow.python.keras.layers.core.Flatten at 0x20205374108>,
 <tensorflow.python.keras.layers.core.Dense at 0x20205384ac8>,
 <tensorflow.python.keras.layers.core.Dense at 0x20266e66288>,
 <tensorflow.python.keras.layers.core.Dense at 0x20204f68308>]
model.summary() #模型的概况
# 第一层 维数[样本数,28*28] -> (None,784)
# 第二层 维数[样本数,300]:(None,784) * W + b ,W.shape = (784,300),b.shape = (300) -> [None,300] ,参数 784*300+300=235500个
# 第三层 维数[样本数,100]:(None,300) * W + b ,W.shape = (300,100),b.shape = (100) -> [None,100] ,参数 300*100+100 = 30100个
# 第四层 维数[样本数,10]:(None,100) * W + b ,W.shape = (100,10=0),b.shape = (10) -> [None,10] ,参数 100*10+10 = 1010
Model: "sequential"
_________________________________________________________________
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
_________________________________________________________________
history = model.fit(x_train,y_train,epochs=10,
                    validation_data=(x_valid,y_valid))
#epochs:训练次数
Train on 55000 samples, validate on 5000 samples
Epoch 1/10
55000/55000 [==============================] - 6s 115us/sample - loss: 2.2319 - accuracy: 0.7370 - val_loss: 0.5676 - val_accuracy: 0.8054
Epoch 2/10
55000/55000 [==============================] - 5s 100us/sample - loss: 0.5831 - accuracy: 0.7919 - val_loss: 0.5015 - val_accuracy: 0.8246
Epoch 3/10
55000/55000 [==============================] - 5s 96us/sample - loss: 0.5068 - accuracy: 0.8210 - val_loss: 0.4636 - val_accuracy: 0.8402
Epoch 4/10
55000/55000 [==============================] - 5s 100us/sample - loss: 0.4620 - accuracy: 0.8347 - val_loss: 0.4476 - val_accuracy: 0.8474
Epoch 5/10
55000/55000 [==============================] - 5s 98us/sample - loss: 0.4295 - accuracy: 0.8481 - val_loss: 0.4232 - val_accuracy: 0.8602
Epoch 6/10
55000/55000 [==============================] - 5s 99us/sample - loss: 0.4138 - accuracy: 0.8530 - val_loss: 0.4341 - val_accuracy: 0.8534
Epoch 7/10
55000/55000 [==============================] - 5s 97us/sample - loss: 0.3912 - accuracy: 0.8608 - val_loss: 0.3741 - val_accuracy: 0.8668
Epoch 8/10
55000/55000 [==============================] - 5s 99us/sample - loss: 0.3761 - accuracy: 0.8657 - val_loss: 0.3753 - val_accuracy: 0.8702
Epoch 9/10
55000/55000 [==============================] - 5s 99us/sample - loss: 0.3621 - accuracy: 0.8722 - val_loss: 0.3805 - val_accuracy: 0.8684
Epoch 10/10
55000/55000 [==============================] - 6s 103us/sample - loss: 0.3537 - accuracy: 0.8743 - val_loss: 0.3732 - val_accuracy: 0.8714
print(type(history)) 
print((history.history)) # history.history:历史数据,字典数据类型
<class 'tensorflow.python.keras.callbacks.History'>
{'loss': [2.2318869050589476, 0.583110001633384, 0.5067881024490704, 0.46199263807210056, 0.4295014597372575, 0.41379175137173047, 0.3912296513860876, 0.37610344263423573, 0.36209654412269593, 0.35372181126854635], 'accuracy': [0.7369636, 0.7919091, 0.821, 0.8346909, 0.84810907, 0.853, 0.8608, 0.86570907, 0.87218183, 0.8743273], 'val_loss': [0.5675769282341003, 0.5014562381982803, 0.4635880431652069, 0.44759077718257906, 0.4231703567743301, 0.43412879557609557, 0.37405781469345095, 0.37533429304361343, 0.3804672242343426, 0.37321702243089677], 'val_accuracy': [0.8054, 0.8246, 0.8402, 0.8474, 0.8602, 0.8534, 0.8668, 0.8702, 0.8684, 0.8714]}
def plot_learning_curve(history):
    pd.DataFrame(history.history).plot(figsize=(8,5))
    plt.grid(True)
    plt.gca().set_ylim(0,1)
    plt.show()
#DateFrame 数据类型的作图
plot_learning_curve(history)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SQ8xIqrm-1588643154446)(output_10_0.png)]

对图片数据,提高分类准确率,一般应先对数据做一个归一化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值