news 2026/5/25 17:39:03

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/5/20 21:00:58

AI智能侦测开箱即用镜像推荐:0配置部署,1块钱起试用

AI智能侦测开箱即用镜像推荐&#xff1a;0配置部署&#xff0c;1块钱起试用 1. 为什么物业经理需要AI智能侦测&#xff1f; 作为物业经理&#xff0c;你是否经常面临这些困扰&#xff1a;小区公共区域频繁出现违规停车、垃圾乱堆、陌生人闯入等异常行为&#xff0c;但传统监控…

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

5个最火AI智能体推荐:0配置开箱即用,10块钱全试遍

5个最火AI智能体推荐&#xff1a;0配置开箱即用&#xff0c;10块钱全试遍 引言 作为一名数据分析师&#xff0c;你是否经常遇到这样的困扰&#xff1a;老板突然要求评估不同AI智能体的性能表现&#xff0c;但本地环境配置复杂&#xff0c;光是搭环境就要花上大半天&#xff1…

作者头像 李华
网站建设 2026/5/25 16:44:20

AI智能体数据可视化:5分钟生成动态报表,1小时1块

AI智能体数据可视化&#xff1a;5分钟生成动态报表&#xff0c;1小时1块 1. 为什么市场分析师需要AI智能体数据可视化 作为一名市场分析师&#xff0c;你是否经常遇到这些困扰&#xff1a; 每周都要手动更新销售报表&#xff0c;复制粘贴数据到Excel&#xff0c;调整格式就要…

作者头像 李华
网站建设 2026/5/20 18:49:36

学生党福利:AI智能体学习1小时1块随便玩

学生党福利&#xff1a;AI智能体学习1小时1块随便玩 引言&#xff1a;计算机专业学生的AI学习困境 作为一名计算机专业的学生&#xff0c;相信你一定遇到过这样的烦恼&#xff1a;AI课程设计需要跑模型&#xff0c;实验室的电脑总是排不上队&#xff0c;自己的笔记本性能又跟…

作者头像 李华
网站建设 2026/5/22 7:59:56

AI智能体零售分析实战:转化率提升技巧

AI智能体零售分析实战&#xff1a;转化率提升技巧 1. 为什么零售店需要AI智能体 想象一下你经营着一家便利店&#xff0c;每天要面对几百种商品的摆放决策。传统方式可能是凭经验或直觉来调整货架&#xff0c;但这样往往效率低下且容易出错。AI智能体就像一位24小时在线的数据…

作者头像 李华
网站建设 2026/5/25 6:16:39

轻量级办公处理神器,绝了

今天给大家推荐一款好用的轻量级word办公处理软件&#xff0c;无需第三方支持&#xff0c;仅8M大小&#xff0c;有需要的小伙伴可以下载收藏。 DocLite 轻量级Word办公处理软件 软件支持*.doc,*.docx,*.rtf,*.txt,*.htm,*.html 等常见文档格式&#xff0c;无需第三方依赖&…

作者头像 李华