国内响应式网站模板,网站开发人员考核,saas云建站平台源码,wordpress翻译了 mo无效希望对你有帮助呀#xff01;#xff01;#x1f49c;#x1f49c; 如有更好理解的思路#xff0c;欢迎大家留言补充 ~ 一起加油叭 #x1f4a6; 欢迎关注、订阅专栏 【深度学习从 0 到 1】谢谢你的支持#xff01; ⭐ 手写数字分类: Keras MNIST 数据集
手写数字分类… 希望对你有帮助呀 如有更好理解的思路欢迎大家留言补充 ~ 一起加油叭 欢迎关注、订阅专栏 【深度学习从 0 到 1】谢谢你的支持 ⭐ 手写数字分类: Keras MNIST 数据集
手写数字分类任务
任务将手写数字的灰度图像28像素×28像素划分到10个类别中0~9 MNIST数据集包含60 000张训练图像和10 000张测试图像由美国国家标准与技术研究院National Institute of Standards and Technology即 MNIST 中的NIST在20世纪80年代收集得到 样本示例如下(hint: 显示数据集的第一个数字的代码plt.imshow(train_images[0], cmapplt.cm.binary)) 步骤一 加载Keras中的MNIST数据集
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) mnist.load_data() # 包括4个Numpy数组# 准备数据
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)步骤二 构建网络架构 (两层全连接层为例)
from keras import models
from keras import layers
network models.Sequential()
network.add(layers.Dense(512, activationrelu, input_shape(28 * 28,)))
network.add(layers.Dense(10, activationsoftmax))步骤三 编译步骤 (optimizer loss metrics)
network.compile(optimizerrmsprop, losscategorical_crossentropy, metrics[accuracy])步骤四训练网络
network.fit(train_images, train_labels, epochs5, batch_size128)步骤五测试网络 test_loss, test_acc network.evaluate(test_images, test_labels) 完整代码参考
from keras.datasets import mnist
from keras import models
from keras import layers (train_images, train_labels), (test_images, test_labels) mnist.load_data() # 包括4个Numpy数组# 准备数据
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 models.Sequential()
network.add(layers.Dense(512, activationrelu, input_shape(28 * 28,)))
network.add(layers.Dense(10, activationsoftmax))# 编译步骤
network.compile(optimizerrmsprop, losscategorical_crossentropy, metrics[accuracy])# 训练网络
network.fit(train_images, train_labels, epochs5, batch_size128)# 测试网络
test_loss, test_acc network.evaluate(test_images, test_labels) print(Loss: {}, Acc: {}.format(test_loss, test_acc))----- 结束后会得到类似如下结果
Epoch 1/5
469/469 [] - 2s 5ms/step - loss: 0.2598 - accuracy: 0.9253
Epoch 2/5
469/469 [] - 2s 5ms/step - loss: 0.1041 - accuracy: 0.9692
Epoch 3/5
469/469 [] - 2s 5ms/step - loss: 0.0684 - accuracy: 0.9795
Epoch 4/5
469/469 [] - 2s 5ms/step - loss: 0.0492 - accuracy: 0.9848
Epoch 5/5
469/469 [] - 2s 5ms/step - loss: 0.0367 - accuracy: 0.9892
313/313 [] - 0s 702us/step - loss: 0.0665 - accuracy: 0.9803
Loss: 0.06652633100748062, Acc: 0.9803000092506409参考书籍Python 深度学习