请修改代码改为训练训练集中的所有然后使用校验集校验所有如果有错就再重复一遍import tensorflow as tfimport matplotlibpyplot as pltimport numpy as npimport timeimport osprint--------------nowtime = timestrftimeY-m-d HMSprintnowtime#指定GPU#ose
import tensorflow as tf import matplotlib.pyplot as plt import numpy as np import time import os
print('--------------') nowtime = time.strftime('%Y-%m-%d %H:%M:%S') print(nowtime)
#指定GPU #os.environ["CUDA_VISIBLE_DEVICES"] = "0" #gpus = tf.config.experimental.list_physical_devices('GPU') #tf.config.experimental.set_memory_growth(gpus[0],True) #初始化 plt.rcParams['font.sans-serif'] = ['SimHei']
#加载数据 mnist = tf.keras.datasets.mnist (train_x,train_y),(test_x,test_y) = mnist.load_data() print('\n train_x:%s, train_y:%s, test_x:%s, test_y:%s'%(train_x.shape,train_y.shape,test_x.shape,test_y.shape))
#数据预处理 #X_train = train_x.reshape((60000,2828)) #Y_train = train_y.reshape((60000,2828)) #后面采用tf.keras.layers.Flatten()改变数组形状 X_train,X_test = tf.cast(train_x/255.0,tf.float32),tf.cast(test_x/255.0,tf.float32) #归一化 y_train,y_test = tf.cast(train_y,tf.int16),tf.cast(test_y,tf.int16)
#建立模型 model = tf.keras.Sequential() model.add(tf.keras.layers.Flatten(input_shape=(28,28))) #添加Flatten层说明输入数据的形状 model.add(tf.keras.layers.Dense(128,activation='relu')) #添加隐含层,为全连接层,128个节点,relu激活函数 model.add(tf.keras.layers.Dense(10,activation='softmax')) #添加输出层,为全连接层,10个节点,softmax激活函数 print('\n',model.summary()) #查看网络结构和参数信息
#配置模型训练方法 #adam算法参数采用keras默认的公开参数,损失函数采用稀疏交叉熵损失函数,准确率采用稀疏分类准确率函数 model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['sparse_categorical_accuracy'])
#训练模型 #批量训练大小为64,迭代5次,测试集比例0.2(48000条训练集数据,12000条测试集数据) print('--------------') nowtime = time.strftime('%Y-%m-%d %H:%M:%S') print('训练前时刻:'+str(nowtime))
history = model.fit(X_train,y_train,batch_size=64,epochs=5,validation_split=0.2)
print('--------------') nowtime = time.strftime('%Y-%m-%d %H:%M:%S') print('训练后时刻:'+str(nowtime)) #评估模型 model.evaluate(X_test,y_test,verbose=2) #每次迭代输出一条记录,来评价该模型是否有比较好的泛化能力
#保存模型参数 #model.save_weights('C:\Users\xuyansong\Desktop\深度学习\python\MNIST\模型参数\mnist_weights.h5') #保存整个模型 model.save('./train/model.h5')
#结果可视化 print(history.history) loss = history.history['loss'] #训练集损失 val_loss = history.history['val_loss'] #测试集损失 acc = history.history['sparse_categorical_accuracy'] #训练集准确率 val_acc = history.history['val_sparse_categorical_accuracy'] #测试集准确率
plt.figure(figsize=(10,3))
plt.subplot(121) plt.plot(loss,color='b',label='train') plt.plot(val_loss,color='r',label='test') plt.ylabel('loss') plt.legend()
plt.subplot(122) plt.plot(acc,color='b',label='train') plt.plot(val_acc,color='r',label='test') plt.ylabel('Accuracy') plt.legend()
#暂停5秒关闭画布,否则画布一直打开的同时,会持续占用GPU内存 #根据需要自行选择 #plt.ion() #打开交互式操作模式 #plt.show() #plt.pause(5) #plt.close()
#使用模型 plt.figure() for i in range(10): num = np.random.randint(1,10000)
plt.subplot(2,5,i+1)
plt.axis('off')
plt.imshow(test_x[num],cmap='gray')
demo = tf.reshape(X_test[num],(1,28,28))
y_pred = np.argmax(model.predict(demo))
plt.title('标签值:'+str(test_y[num])+'\n预测值:'+str(y_pred))
#y_pred = np.argmax(model.predict(X_test[0:5]),axis=1) #print('X_test[0:5]: %s'%(X_test[0:5].shape)) #print('y_pred: %s'%(y_pred))
#plt.ion() #打开交互式操作模式 plt.show() #plt.pause(5) #plt.close(
原文地址: https://www.cveoy.top/t/topic/iguW 著作权归作者所有。请勿转载和采集!