Loading... 12.5由于jupyter问题重新安装了anaconda 蛋没关系啊,唉俩小时以后,他就装好了 ```python from keras.datasets import mnist (train_images,train_labels),(test_images,test_labels) = mnist.load_data() ``` 第一步,加载mnist数据集 --- ```python from keras import models from keras import layers network = models.Sequential() network.add(layers.Dense(512, activation='relu',input_shape=(28 * 28,))) network.add(layers.Dense(10, activation='softmax')) ``` 第二步,构建网络框架,keras作为最快上手的,基本上一开始直接相信教程中的选择。 该网络层有俩个层,他们是全连接的神经层 relu函数作为一开始介绍到的激活函数 softmax作为一个10个概率值的恒等函数,他的输出就是识别的结果 --- ```python network.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy']) ``` 第三步:编译步骤(相信即可):确定损失函数,优化器等,暂不做具体介绍 --- ```python train_images = train_images.reshape((60000, 28 * 28)) train_images = train_images.astype('float32') / 255 test_images = test_images.reshape((10000, 28 * 28)) test_images = test_images.astype('float32') / 255 ``` 第四步:准备图像数据,对图像数据进行一个预处理 --- ```python from keras.utils import to_categorical train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels) ``` 第五步:准备标签,准备开始拟合! --- ``` network.fit(train_images, train_labels, epochs=5, batch_size=128) ``` 进行拟合 --- 完整代码: ```python from keras.datasets import mnist (train_images,train_labels),(test_images,test_labels) = mnist.load_data() from keras import models from keras import layers network = models.Sequential() network.add(layers.Dense(512, activation='relu',input_shape=(28 * 28,))) network.add(layers.Dense(10, activation='softmax')) network.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy']) train_images = train_images.reshape((60000, 28 * 28)) train_images = train_images.astype('float32') / 255 test_images = test_images.reshape((10000, 28 * 28)) test_images = test_images.astype('float32') / 255 from keras.utils import to_categorical train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels) network.fit(train_images, train_labels, epochs=5, batch_size=128) ``` 执行结果(你可能会不一样) ```python Epoch 1/5 60000/60000 [==============================] - 6s 97us/step - loss: 0.2550 - acc: 0.9263 Epoch 2/5 60000/60000 [==============================] - 7s 116us/step - loss: 0.1043 - acc: 0.9696 Epoch 3/5 60000/60000 [==============================] - 8s 131us/step - loss: 0.0694 - acc: 0.9782 Epoch 4/5 60000/60000 [==============================] - 6s 95us/step - loss: 0.0503 - acc: 0.9853 Epoch 5/5 60000/60000 [==============================] - 6s 97us/step - loss: 0.0374 - acc: 0.9883 ``` Last modification:December 5, 2020 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 如果觉得我的内容对你有用,请随意赞赏