news 2026/1/30 15:20:33

python:keras 实现 mnist数据集手写数字识别

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
python:keras 实现 mnist数据集手写数字识别

where python
D:\Python37\python.exe

cd D:\Python37
pip freeze |grep -i tensorflow
tensorflow==2.11.0
tensorflow-estimator==2.11.0
tensorflow-intel==2.11.0
tensorflow-io-gcs-filesystem==0.31.0

pip freeze |grep -i keras
keras==2.11.0

编写一个 keras 实现 mnist 数据集手写数字识别的示例 keras_mnist_1.py 如下

# -*- coding: utf-8 -*- from tensorflow import keras from keras import layers from keras.datasets import mnist # 在 keras 中加载 MNIST数据集 (train_images, train_labels), (test_images, test_labels) = mnist.load_data() print('mnist train:',train_images.shape, len(train_labels)) print('mnist test :',test_images.shape, len(test_labels)) # 神经网络模型 model = keras.Sequential([ layers.Dense(512, activation="relu"), layers.Dense(10, activation="softmax") ]) # 模型编译 model.compile(optimizer="rmsprop", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) print(model.summary) # 准备图像数据 train_images = train_images.reshape((60000, 28 * 28)) train_X = train_images.astype("float32") / 255 test_images = test_images.reshape((10000, 28 * 28)) test_X = test_images.astype("float32") / 255 # 模型拟合 model.fit(train_X, train_labels, epochs=5, batch_size=128) # 使用模型进行预测 test_digits = test_images[0:10] predictions = model.predict(test_digits) print('predictions[0]:') print(predictions[0].argmax()) print(predictions[0][7]) print('test_labels_0:',test_labels[0]) # 在测试数据上评估模型 test_loss, test_acc = model.evaluate(test_X, test_labels) print(f"test_acc: {test_acc}")

运行 cmd
python keras_mnist_1.py

D:\python> python keras_mnist_1.py mnist train: (60000, 28, 28) 60000 mnist test : (10000, 28, 28) 10000 2026-01-11 09:54:07.683219: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. <bound method Model.summary of <keras.engine.sequential.Sequential object at 0x0000025721819348>> Epoch 1/5 469/469 [==============================] - 12s 15ms/step - loss: 0.2653 - accuracy: 0.9240 Epoch 2/5 469/469 [==============================] - 7s 15ms/step - loss: 0.1081 - accuracy: 0.9680 Epoch 3/5 469/469 [==============================] - 7s 15ms/step - loss: 0.0714 - accuracy: 0.9788 Epoch 4/5 469/469 [==============================] - 7s 15ms/step - loss: 0.0517 - accuracy: 0.9845 Epoch 5/5 469/469 [==============================] - 7s 15ms/step - loss: 0.0381 - accuracy: 0.9887 1/1 [==============================] - 1s 937ms/step predictions[0]: 7 1.0 test_labels_0: 7 313/313 [==============================] - 5s 8ms/step - loss: 0.0675 - accuracy: 0.9801 test_acc: 0.9800999760627747
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/29 18:55:02

AI智能体工业质检应用:快速测试指南

AI智能体工业质检应用&#xff1a;快速测试指南 1. 什么是AI智能体工业质检&#xff1f; 想象一下&#xff0c;工厂里有一位不知疲倦的质检员&#xff0c;它能24小时盯着生产线&#xff0c;用"火眼金睛"发现每一个细微的产品缺陷。这就是AI智能体在工业质检中的应用…

作者头像 李华
网站建设 2026/1/26 2:30:37

AI智能体工单处理实战:云端GPU 1小时部署,成本透明

AI智能体工单处理实战&#xff1a;云端GPU 1小时部署&#xff0c;成本透明 引言&#xff1a;当客服主管遇到AI智能体 作为客服主管&#xff0c;你是否经常面临这样的困境&#xff1a;每天数百个工单需要处理&#xff0c;团队人力有限&#xff0c;高峰期响应延迟&#xff0c;客…

作者头像 李华
网站建设 2026/1/17 19:11:15

AI威胁情报生产线:从采集到分析,云端自动化流水线

AI威胁情报生产线&#xff1a;从采集到分析&#xff0c;云端自动化流水线 引言&#xff1a;当安全团队遇上AI流水线 想象一下&#xff0c;你是一名网络安全分析师&#xff0c;每天要处理成千上万的威胁日志——就像在暴雨中试图用咖啡滤纸接住每一滴雨水。传统的手工处理方式…

作者头像 李华
网站建设 2026/1/26 18:47:24

5个热门AI智能体对比:云端GPU3小时完成选型测试

5个热门AI智能体对比&#xff1a;云端GPU3小时完成选型测试 1. 为什么需要AI智能体选型&#xff1f; 作为技术负责人&#xff0c;当我们需要为项目选择AI智能体框架时&#xff0c;常常面临几个现实问题&#xff1a; 本地开发机性能不足&#xff0c;跑不动大模型购买测试服务…

作者头像 李华
网站建设 2026/1/27 12:39:49

开题报告 “一次过” 秘籍!虎贲等考 AI:30 分钟搞定专业框架,答辩不慌

毕业论文的 “第一关” 从来不是正文写作&#xff0c;而是开题报告。选题被导师否定、研究意义表述模糊、技术路线逻辑混乱、文献综述缺乏深度…… 这些开题路上的 “绊脚石”&#xff0c;让无数学生反复修改却迟迟无法通过&#xff0c;甚至耽误后续论文创作进度。 而虎贲等考…

作者头像 李华
网站建设 2026/1/17 5:51:06

宏智树AI:重塑学术写作新范式,开启智能科研新纪元——让论文写作从“复杂工程”变为“高效创作”

在学术研究的道路上&#xff0c;论文写作是每位学者必经的“最后一公里”。然而&#xff0c;从选题开题到数据整理&#xff0c;从文献综述到查重降重&#xff0c;每一步都充满挑战&#xff1a;灵感枯竭、逻辑混乱、数据处理低效、查重率居高不下……这些问题不仅消耗大量时间&a…

作者头像 李华