01keras分类模型


##分类问题预测的是类别,模型的输出时概论的分布,需要衡量目标类别与当前预测的差距
# 回归问题预测的是值,模型的输出时一个实数值
# 目标函数:对问题的建模(逐步的调整参数)##

import tensorflow as tf
import matplotlib as mpl
import matplotlib.pyplot as plt

import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
from tensorflow import keras
       #实战1.分类模型之 数据的读取与展示 keras——classification

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:]              #前5000张为验证集,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)

def show_single_image(img_arr):
    plt.imshow(img_arr,cmap="binary")
    plt.show()

#show_single_image(x_train[0])

#n_rows:行数,n_cols:列数,x_data:总共显示的图像个数,y_data:是其索引
def show_imgs(n_rows,n_cols,x_data,y_data,class_names):
    #断言,不满足条件就抛出异常
    assert len(x_data) == len(y_data)#验证x的样本数与y的样本数一样
    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 = row * n_cols + 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(2,5,x_train,y_train,class_names)
#tf.keras.models.Sequential()

# 创建一个Sequential的对象,顺序模型,多个网络层的线性堆叠
# 可使用add方法将各层添加到模块中
model = keras.models.Sequential()
# 输入层:Flatten将28*28的图像矩阵展平成为一个一维向量
model.add(keras.layers.Flatten(input_shape = [28,28]))

#------初始化第一种方式-------#
# 全连接层(上层所有单元与下层所有单元都连接):
# 第一层300个单元,第二层100个单元,激活函数为 relu:
# relu: y = max(0, x)
model.add(keras.layers.Dense(300,activation="relu"))
model.add(keras.layers.Dense(100,activation="relu"))
model.add(keras.layers.Dense(10,activation="softmax"))#softmax:将向量变成概率分布

#------初始化第二种方式-------#
'''
model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28,28]),
    keras.layers.Dense(300,activation="relu"),
    keras.layers.Dense(300,activation="relu"),
    keras.layers.Dense(10,activation="softmax")
])
'''

#softmax:x = [x1,x2,x3],sum = e^x1 + e^x2 + e^x3
# y = [e^x1/sum, e^x2/sum, e^x3/sum ]

# 目标函数的构建与求解方法
# 为什么使用sparse? :
# y->是一个数,要用sparse_categorical_crossentropy
# y->是一个向量,直接用categorical_crossentropy
model.compile(loss="sparse_categorical_crossentropy",
              optimizer = "adam",#因为用sgd梯度下降法会导致陷入局部最小值点
              metrics = ['accuracy'])
print(model.layers)
print(model.summary())
#[None,784]*w +b = [None,300] 则:w = [784*300] b = [300]

# 开启训练
# epochs:训练集遍历10次
# validation_data:每个epoch就会用验证集验证
# 会发现loss和accuracy到后面一直不变,因为用sgd梯度下降法会导致陷入局部最小值点
# 因此将loss函数的下降方法改为 adam
history = model.fit(x_train,y_train,epochs = 10,validation_data = (x_valid,y_valid))
print(type(history))
print(history.history)

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)`在这里插入代码片`
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值