当前位置: 首页 > news >正文

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
http://www.cnnetsun.cn/news/557523.html

相关文章:

  • 实体行为分析省钱攻略:云端GPU按需付费,比本地省90%成本
  • 制造业零件检测AI方案:缺陷识别准确率98%,成本透明
  • AI侦测模型部署避坑指南:云端镜像免配置,新手指南
  • 达梦(DM8) DDL DML DQL DCL 大全
  • AI威胁检测省钱攻略:按秒计费云端GPU,比买服务器省万元
  • 从理论到实践:AI安全检测完整项目实战,环境一键直达
  • C 层函数调用与概念梳理
  • GTE中文语义相似度服务监控可视化:自定义看板
  • AI智能体异常检测对比:3个模型快速评测
  • GTE中文语义相似度计算详细步骤:构建智能问答系统基础
  • nodejs基于vue 的酒店管理系统设计与实现_v2fvn
  • GTE模型输出解释:理解余弦相似度评分含义
  • 基于 YOLOv8 的电网绝缘子破损与闪络缺陷智能检测系统识别项目 [目标检测完整源码]
  • 导师不会说的7款AI神器:1天生成3万字计算机论文,真实文献揭秘!
  • 中文文本情感分析API开发:StructBERT
  • StructBERT情感分析置信度解读:结果可信度评估
  • DOM XSS漏洞解析:利用innerHTML的location.search注入点
  • GTE中文语义相似度服务实战:社交媒体话题聚类分析
  • Stable Diffusion+智能侦测联动教程:2块钱玩转AI视觉创作
  • GTE中文语义相似度服务性能对比:CPU与GPU版本差异分析
  • GTE中文语义相似度计算快速入门:语义搜索系统构建
  • 深度学习环境配置从入门到放弃?不如直接用云端镜像
  • 3个AI智能体对比评测:云端GPU 2小时搞定,成本不到5块
  • UEBA模型部署避坑指南:云端GPU免环境配置,新用户首小时免费
  • AI智能实体侦测全家桶:12种预训练模型任选,按需付费
  • AI智能体行为分析实战:云端3步搞定,2块钱玩转大数据
  • AI智能体绘画实战:云端GPU 10分钟出图,2块钱玩一下午
  • Mac用户福音:云端跑AI侦测模型,不换电脑也能用
  • 中文情感分析API:安全配置
  • StructBERT情感分析API集成:快速接入现有系统教程