news 2026/1/20 22:57:06

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/20 18:16:38

实体行为分析省钱攻略:云端GPU按需付费,比本地省90%成本

实体行为分析省钱攻略&#xff1a;云端GPU按需付费&#xff0c;比本地省90%成本 1. 为什么金融风控需要云端GPU方案 金融风控工程师每周只需要跑2次模型分析实体行为&#xff0c;如果购买2万多的显卡&#xff0c;设备利用率可能不到5%。这就像为了偶尔喝咖啡买一台商用咖啡机…

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

制造业零件检测AI方案:缺陷识别准确率98%,成本透明

制造业零件检测AI方案&#xff1a;缺陷识别准确率98%&#xff0c;成本透明 1. 为什么小型加工厂需要AI质检&#xff1f; 最近越来越多的客户在验收订单时&#xff0c;要求供应商提供AI质检报告。传统人工检测方式存在几个痛点&#xff1a; 老师傅培养周期长&#xff0c;新手…

作者头像 李华
网站建设 2026/1/20 22:51:12

AI侦测模型部署避坑指南:云端镜像免配置,新手指南

AI侦测模型部署避坑指南&#xff1a;云端镜像免配置&#xff0c;新手指南 引言&#xff1a;为什么你需要这篇指南 如果你是一名刚转行学习AI的小白&#xff0c;可能已经遇到过这样的困境&#xff1a;看了一堆YOLOv8的教程&#xff0c;却被Docker、CUDA、环境配置这些专业术语…

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

达梦(DM8) DDL DML DQL DCL 大全

一、DDL&#xff08;数据定义语言&#xff09; 1. 数据库操作 -- 创建数据库 CREATE DATABASE 数据库名 DATAFILE 文件路径 SIZE 大小;-- 修改数据库 ALTER DATABASE MOUNT; ALTER DATABASE OPEN; ALTER DATABASE ADD LOGFILE 文件路径 SIZE 大小;-- 删除数据库 DROP DATABAS…

作者头像 李华
网站建设 2026/1/16 9:15:04

AI威胁检测省钱攻略:按秒计费云端GPU,比买服务器省万元

AI威胁检测省钱攻略&#xff1a;按秒计费云端GPU&#xff0c;比买服务器省万元 1. 为什么创业公司需要AI威胁检测&#xff1f; 对于创业公司而言&#xff0c;网络安全是生死攸关的问题。想象一下&#xff0c;你的公司就像一个小型便利店&#xff0c;虽然规模不大&#xff0c;…

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

从理论到实践:AI安全检测完整项目实战,环境一键直达

从理论到实践&#xff1a;AI安全检测完整项目实战&#xff0c;环境一键直达 引言&#xff1a;为什么AI安全检测如此重要&#xff1f; 在数字化时代&#xff0c;网络安全威胁日益复杂&#xff0c;传统的基于规则的防御系统已经难以应对。想象一下&#xff0c;你家的防盗门只能…

作者头像 李华