keras 全连接手写识别 coursera week2

发布时间:2026/7/28 17:47:57
keras 全连接手写识别  coursera week2
Exercise 2In the course you learned how to do classificaiton using Fashion MNIST, a data set containing items of clothing. There’s another, similar dataset called MNIST which has items of handwriting – the digits 0 through 9.Write an MNIST classifier that trains to 99% accuracy or above, and does it without a fixed number of epochs – i.e. you should stop training once you reach that level of accuracy.Some notes:It should succeed in less than 10 epochs, so it is okay to change epochs to 10, but nothing largerWhen it reaches 99% or greater it should print out the string “Reached 99% accuracy so cancelling training!”If you add any additional variables, make sure you use the same names as the ones used in the classI’ve started the code for you below – how would you finish it?1.全连接手写识别import tensorflow as tf from os import path, getcwd, chdir # DO NOT CHANGE THE LINE BELOW. If you are developing in a local # environment, then grab mnist.npz from the Coursera Jupyter Notebook # and place it inside a local folder and edit the path to that location path f{getcwd()}/../tmp2/mnist.npz# GRADED FUNCTION: train_mnist def train_mnist(): # Please write your code only where you are indicated. # please do not remove # model fitting inline comments. # YOUR CODE SHOULD START HERE **class myCallback(tf.keras.callbacks.Callback): def on_epoch_end(self,epoch,logs{}): if(logs.get(acc)0.6): print(\nReached 60% accuracy so cancelling training!) self.model.stop_trainingTrue** # YOUR CODE SHOULD END HERE mnist tf.keras.datasets.mnist (x_train, y_train),(x_test, y_test) mnist.load_data(pathpath) # YOUR CODE SHOULD START HERE **callbacksmyCallback()** # YOUR CODE SHOULD END HERE model tf.keras.models.Sequential([ # YOUR CODE SHOULD START HERE **tf.keras.layers.Flatten(input_shape(28,28)), tf.keras.layers.Dense(512,activationrelu), tf.keras.layers.Dense(10,activationsoftmax)** # YOUR CODE SHOULD END HERE ]) model.compile(optimizeradam, losssparse_categorical_crossentropy, metrics[accuracy]) # model fitting history model.fit(# YOUR CODE SHOULD START HERE **x_train,y_train,epochs10,callbacks[callbacks]** # YOUR CODE SHOULD END HERE ) # model fitting return history.epoch, history.history[acc][-1]train_mnist()2.卷积手写识别改几处地方就行了首先 卷积要求图片是3维 mnist tf.keras.datasets.mnist (x_train, y_train),(x_test, y_test) mnist.load_data(pathpath) x_train x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1) x_test x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)改模型层 改为卷积 model tf.keras.models.Sequential([ # YOUR CODE SHOULD START HERE # tf.keras.layers.Flatten(input_shape(28,28)), # tf.keras.layers.Dense(512,activationrelu), # tf.keras.layers.Dense(10,activationsoftmax) tf.keras.layers.Conv2D(32,(3,3),activationrelu,input_shape(28,28,1)), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(64,(3,3),activationrelu), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(10,activationsoftmax) # YOUR CODE SHOULD END HERE ])训练的时候加入测试集作为验证 history model.fit(# YOUR CODE SHOULD START HERE x_train,y_train,epochs10,callbacks[callbacks], # YOUR CODE SHOULD END HERE validation_data(x_test, y_test) )如果上面没有validation_data(x_test, y_test)在 model.fit 外面加上 **test_loss model.evaluate(x_test, y_test)** 也是可以验证的损失函数说明如果你的 targets 是 one-hot 编码用 categorical_crossentropyone-hot 编码[0, 0, 1], [1, 0, 0], [0, 1, 0]如果你的 tagets 是 数字编码 用 sparse_categorical_crossentropy数字编码2, 0, 1