news 2026/2/19 11:24:27

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/2/14 7:21:46

AI智能体音乐生成教程:没显卡也能创作,1小时1块

AI智能体音乐生成教程&#xff1a;没显卡也能创作&#xff0c;1小时1块 1. 为什么选择AI音乐生成&#xff1f; 你是否想过创作自己的音乐&#xff0c;却被专业音频工作站的价格和复杂度劝退&#xff1f;现在&#xff0c;借助AI音乐生成技术&#xff0c;即使没有专业设备和高配…

作者头像 李华
网站建设 2026/2/18 7:18:53

AI安全众测平台:标准化测试环境+灵活计费,降低参与门槛

AI安全众测平台&#xff1a;标准化测试环境灵活计费&#xff0c;降低参与门槛 1. 为什么需要AI安全众测平台&#xff1f; 想象一下&#xff0c;你正在参加一场编程马拉松比赛&#xff0c;但有的选手用的是最新款MacBook Pro&#xff0c;有的却只能使用老旧笔记本——这种硬件…

作者头像 李华
网站建设 2026/2/18 22:21:23

达梦(DM8)对 JSON 与 XML 的使用教程

JSON达梦提供了丰富的JSON处理函数&#xff1a;-- 创建包含JSON字段的表 CREATE TABLE user_info (id INT,profile JSON,settings JSONB );-- 插入JSON数据 INSERT INTO user_info VALUES (1, {"name": "张三", "age": 25}, {"theme":…

作者头像 李华
网站建设 2026/2/18 3:40:14

Redis 可以实现哪些业务功能

Redis 是一款高性能的内存数据库&#xff0c;支持多种数据结构&#xff0c;在实际业务中应用广泛&#xff0c;以下是其主要业务功能&#xff1a;1. 缓存这是 Redis 最核心的用途&#xff0c;能显著提升系统性能&#xff1a;热点数据缓存&#xff1a;缓存数据库查询结果&#xf…

作者头像 李华
网站建设 2026/2/17 0:12:47

AI安全自动化实战:告警聚合+事件溯源,1小时全掌握

AI安全自动化实战&#xff1a;告警聚合事件溯源&#xff0c;1小时全掌握 引言&#xff1a;当运维遇上告警海啸 每天处理上百条SIEM告警&#xff0c;却找不到真正的攻击线索&#xff1f;作为运维工程师&#xff0c;你可能正在经历这样的典型场景&#xff1a;凌晨3点被告警短信…

作者头像 李华
网站建设 2026/2/15 5:31:20

AI异常检测保姆级指南:MacBook也能跑,云端GPU按需付费

AI异常检测保姆级指南&#xff1a;MacBook也能跑&#xff0c;云端GPU按需付费 引言&#xff1a;为什么产品经理需要云端AI异常检测&#xff1f; 作为产品经理&#xff0c;向客户演示行为分析功能是日常工作的重要环节。但当你使用MacBook办公时&#xff0c;可能会遇到这样的困…

作者头像 李华