识别mnist数字数据集 最开始根据nndl_exercise5的思路写 题目用tensorflow1写的,结果有一堆bug要调,最后做出来的东西虽然能跑,但是accuracy一直卡0.0987(正好差不多是随机猜一个数的概率) 大胆推测是输入数据时的尺寸大小没调节好,所以训练根本没效果,也就没有过拟合(逃 网上比较流行一开始就降维的写法 连官方都是一开始就摊平 传送门 最后的代码就放在这里吧
import tensorflow as tf
import tensorflow.python.keras.layers
mnist = tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train = x_train.reshape(60000,28,28,1)
x_test = x_test.reshape(10000,28,28,1)
_learning_rate = 1e-4
max_epoch = 2000
x_train = tf.keras.utils.normalize(x_train,axis=1)
x_test = tf.keras.utils.normalize(x_test,axis=1)
model = tf.keras.models.Sequential()
import tensorflow
from tensorflow.python.keras.layers import Conv2D
from tensorflow.python.keras.layers import MaxPool2D
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.layers import Flatten
model.add(Flatten())
model.add(Dense(514,activation='relu'))
model.add(Dense(114,activation='relu'))
model.add(Dense(10,activation='softmax'))
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,epochs=300,validation_data=(x_test,y_test))
_,test_acc = model.evaluate(x_test,y_test,verbose=2)
|