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()
标签:loss,plt,训练,keras,train,fashion,test,数据,history 来源: https://www.cnblogs.com/fengyumeng/p/13976814.html