• fashion数据集训练


    下载数据集

    fashion数据集总共有7万张28*28像素点的灰度图片和标签,涵盖十个分类:T恤、裤子、套头衫、连衣裙、外套、凉鞋、衬衫、运动鞋、包、靴子。

    其中6万张用于训练,1万张用于测试。

    import tensorflow as tf
    from tensorflow import keras
    from matplotlib import pyplot as plt
    import numpy as np
    from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense,Dropout
    
    fashion = keras.datasets.fashion_mnist
    (x_train, y_train), (x_test, y_test) = fashion.load_data()
    x_train, x_test = x_train / 255.0, x_test / 255.0
    
    x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
    x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)

    搭建网络结构

    model = keras.models.Sequential([
        Conv2D(64, (3, 3), activation='relu'),
        MaxPool2D((2, 2)),
        Conv2D(128, (3, 3), activation='relu'),
        MaxPool2D((2, 2)),
        Conv2D(128, (3, 3), activation='relu'),
        Flatten(),
        Dropout(0.4),
        Dense(128, activation='relu',kernel_regularizer=keras.regularizers.l2()),
        Dropout(0.4),
        Dense(10, activation='softmax')
    ])

    模型编译

    model.compile(optimizer=keras.optimizers.Adam(lr=0.0001), loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=['accuracy'])

    执行训练

    history = model.fit(x_train, y_train, epochs=100, batch_size=32,verbose=1,validation_data=(x_test, y_test),validation_freq=1)

    模型评估

    score = model.evaluate(x_test, y_test, verbose=0, batch_size=32)
    print('测试准确率:{}, 测试loss值: {}'.format(score[1], score[0]))

    可视化acc/loss曲线

    #显示训练集和测试集的acc和loss曲线
    plt.rcParams['font.sans-serif']=['SimHei']
    acc = history.history['accuracy']
    val_acc = history.history['val_accuracy']
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    
    plt.subplot(1, 2, 1)
    plt.plot(acc, label='训练Acc')
    plt.plot(val_acc, label='测试Acc')
    plt.title('Acc曲线')
    plt.legend()
    
    plt.subplot(1, 2, 2)
    plt.plot(loss, label='训练Loss')
    plt.plot(val_loss, label='测试Loss')
    plt.title('Loss曲线')
    plt.legend()
    plt.show()

  • 相关阅读:
    ICO图标的制作、下载
    网页Loading,让页面加载完再显示
    鹏城之行
    The shit live,learn to give up!
    缘分
    一篇网络文章,能否让我真正顿悟???
    李开复给中国学生的第三封信:成功、自信、快乐
    Uncountable missing,missing...
    李开复给中国学生的第五封信:做个积极主动的你
    李开复给中国学生的第四封信:大学应这样过
  • 原文地址:https://www.cnblogs.com/fengyumeng/p/13976814.html
Copyright © 2020-2023  润新知