首页 > 其他分享 >TensorFlow手写数字识别

TensorFlow手写数字识别

时间:2025-01-19 11:54:40浏览次数:1  
标签:loss val ... 0.4241323 train TensorFlow 手写 识别 accuracy

  In [1]:

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
import pandas as pd
  In [2]:
(x_train_all, y_train_all), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
  In [3]:
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]
  In [4]:
scaler = StandardScaler()
x_train.shape
  Out[4]:
(55000, 28, 28)
  In [5]:
x_train = x_train.astype(np.float32).reshape(-1, 1)
x_train.shape
  Out[5]:
(43120000, 1)
  In [6]:
x_train_scaled = scaler.fit_transform(x_train)
x_train_scaled
  Out[6]:
array([[-0.4241323],
       [-0.4241323],
       [-0.4241323],
       ...,
       [-0.4241323],
       [-0.4241323],
       [-0.4241323]], dtype=float32)
  In [7]:
x_train_scaled = x_train_scaled.reshape(-1, 28, 28)
display(x_train_scaled.shape)
x_train_scaled
   
(55000, 28, 28)
Out[7]:
array([[[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        ...,
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323]],

       [[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        ...,
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323]],

       [[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        ...,
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323]],

       ...,

       [[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        ...,
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323]],

       [[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        ...,
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323]],

       [[-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        ...,
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323],
        [-0.4241323, -0.4241323, -0.4241323, ..., -0.4241323,
         -0.4241323, -0.4241323]]], dtype=float32)
  In [8]:
x_valid_scaled = scaler.fit_transform(x_valid.astype(np.float32).reshape(-1,1)).reshape(-1, 28, 28)
x_test_scaled = scaler.fit_transform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1, 28, 28)
  In [9]:
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=[28, 28]),    # 定义输入数据的格式
    tf.keras.layers.Dense(512, activation='relu', kernel_regularizer='l2'),
    # tf.keras.layers.AlphaDropout(0.5),  也可以取消正则化,使用改进版的dropout(均值和方差不变,归一化性质也不变)
    tf.keras.layers.Dense(256, activation='relu', kernel_regularizer='l2'),
    tf.keras.layers.Dense(128, activation='relu', kernel_regularizer='l2'),
    tf.keras.layers.Dense(10, activation='softmax')
])

# 配置模型
model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
  In [10]:
history = model.fit(x_train_scaled, y_train, epochs=10, validation_data=(x_valid_scaled, y_valid))
   
Epoch 1/10
1719/1719 [==============================] - 5s 3ms/step - loss: 8.6183 - accuracy: 0.8999 - val_loss: 6.0358 - val_accuracy: 0.9376
Epoch 2/10
1719/1719 [==============================] - 5s 3ms/step - loss: 4.4725 - accuracy: 0.9445 - val_loss: 3.2236 - val_accuracy: 0.9510
Epoch 3/10
1719/1719 [==============================] - 5s 3ms/step - loss: 2.4441 - accuracy: 0.9506 - val_loss: 1.8074 - val_accuracy: 0.9582
Epoch 4/10
1719/1719 [==============================] - 5s 3ms/step - loss: 1.4214 - accuracy: 0.9556 - val_loss: 1.0957 - val_accuracy: 0.9600
Epoch 5/10
1719/1719 [==============================] - 5s 3ms/step - loss: 0.9005 - accuracy: 0.9595 - val_loss: 0.7379 - val_accuracy: 0.9586
Epoch 6/10
1719/1719 [==============================] - 5s 3ms/step - loss: 0.6334 - accuracy: 0.9615 - val_loss: 0.5429 - val_accuracy: 0.9670
Epoch 7/10
1719/1719 [==============================] - 5s 3ms/step - loss: 0.4934 - accuracy: 0.9639 - val_loss: 0.4449 - val_accuracy: 0.9680
Epoch 8/10
1719/1719 [==============================] - 5s 3ms/step - loss: 0.4187 - accuracy: 0.9654 - val_loss: 0.3877 - val_accuracy: 0.9688
Epoch 9/10
1719/1719 [==============================] - 5s 3ms/step - loss: 0.3773 - accuracy: 0.9665 - val_loss: 0.3654 - val_accuracy: 0.9682
Epoch 10/10
1719/1719 [==============================] - 5s 3ms/step - loss: 0.3529 - accuracy: 0.9679 - val_loss: 0.3481 - val_accuracy: 0.9684
  In [11]:
history.history
  Out[11]:
{'loss': [8.618253707885742,
  4.4725117683410645,
  2.4440758228302,
  1.4214469194412231,
  0.9005103707313538,
  0.633385956287384,
  0.49344661831855774,
  0.4187164306640625,
  0.3772663474082947,
  0.3529081642627716],
 'accuracy': [0.8999454379081726,
  0.9445454478263855,
  0.9505817890167236,
  0.9556363821029663,
  0.9594908952713013,
  0.961509108543396,
  0.9638727307319641,
  0.9654181599617004,
  0.9665091037750244,
  0.9678727388381958],
 'val_loss': [6.035829067230225,
  3.2235751152038574,
  1.8073712587356567,
  1.0957262516021729,
  0.7379411458969116,
  0.5428724884986877,
  0.4449375569820404,
  0.38767603039741516,
  0.36538875102996826,
  0.34807276725769043],
 'val_accuracy': [0.9376000165939331,
  0.9509999752044678,
  0.9581999778747559,
  0.9599999785423279,
  0.9585999846458435,
  0.9670000076293945,
  0.9679999947547913,
  0.9688000082969666,
  0.9682000279426575,
  0.9684000015258789]}
  In [12]:
pd.DataFrame(history.history).plot(figsize=(8,5))
plt.grid()
plt.gca().set_ylim(0,1)
plt.show()
    No description has been provided for this image   In [13]:
model.evaluate(x_test_scaled, y_test)
   
313/313 [==============================] - 0s 1ms/step - loss: 0.3486 - accuracy: 0.9644
Out[13]:
[0.34860044717788696, 0.9643999934196472]
  In [14]:
model.summary()
   
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 flatten (Flatten)           (None, 784)               0                                                                    
 dense (Dense)               (None, 512)               401920                                                         
 dense_1 (Dense)             (None, 256)               131328                                                       
 dense_2 (Dense)             (None, 128)               32896                                                           
 dense_3 (Dense)             (None, 10)                1290                                                               
=================================================================
Total params: 567434 (2.16 MB)
Trainable params: 567434 (2.16 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
  In [ ]:  

标签:loss,val,...,0.4241323,train,TensorFlow,手写,识别,accuracy
From: https://www.cnblogs.com/funsion/p/18679462

相关文章

  • 让你手写一个reset的文件,你应该怎么写?要考虑哪些方面呢?
    在前端开发中,一个reset文件通常指的是一个CSS重置文件,其目的是消除浏览器默认样式的不一致性,从而提供一个更一致的起点来构建项目的样式。下面是一个简单的示例,展示了如何手写一个CSS重置文件,并考虑了几个方面:/*reset.css*//*1.移除边距*/body,h1,h2,h3,h4,h5......
  • matlab仪表表盘识别程序
    仪表表盘识别程序,基于matlab编写。可识别数字表盘和指针表盘。对于清晰可辨的图片有较高的可靠性文件列表image/digital/28-10.tif,2004178image/digital/28-11.tif,2004178image/digital/28-12.tif,2004178image/digital/28-13.tif,2004178image/digital/28-14.tif......
  • R 实现英文数字验证码识别
    R的强大扩展生态使其也能完成OCR任务,我们将结合magick包(图像处理)和tesseract包(OCR)来实现。安装所需包在R环境中,安装以下R包:R更多内容访问ttocr.com或联系1436423940install.packages("magick")install.packages("tesseract")R实现代码编写以下R代码来完......
  • Perl 实现英文数字验证码识别
    我们将通过结合TesseractOCR和ImageMagick工具来实现图像处理和文本识别。安装依赖确保已安装以下工具和模块:TesseractOCR:用于文本识别。ImageMagick:用于图像处理。Perl模块:Image::Magick和Tesseract::OCR。在命令行中执行以下命令:bash安装TesseractOCRsudo......
  • MATLAB 实现英文数字验证码识别
    MATLAB是一个强大的数值计算和图像处理工具,结合其内置的图像处理工具箱和OCR功能,可以轻松完成验证码识别任务。前置准备安装MATLAB确保你的MATLAB版本支持OCR功能(通常需要安装ComputerVisionToolbox)。如果没有,先通过MATLAB的工具箱管理器安装该工具箱。准备验......
  • 识别可用的卷积神经网络
    1、ResNet(残差网络):ResNet通过引入残差学习框架解决了深度网络训练中的退化问题。它通过添加跳跃连接(skipconnections)来提高网络的深度,从而提高性能。2、DenseNet(密集连接网络):DenseNet通过将每一层与前面所有层连接起来,实现了特征的重用和传递。这种结构可以减少参数数量,提高训练......
  • 深度学习框架大比拼:TensorFlow vs PyTorch,亦菲彦祖的选择
    深度学习框架大比拼:TensorFlowvsPyTorch,亦菲彦祖的选择亲爱的亦菲彦祖,欢迎来到这次的深度学习框架擂台!在我们之前的讨论中,你已经学习了深度学习的核心概念、神经网络的基本原理、卷积神经网络(CNN)和循环神经网络(RNN)等技术。但要想真正落地实践,还需要一个合适、易用且高效的深......
  • 微信小程序上传身份证(身份证识别)
    1、父页面引用组件<viewclass="topic_box"><view>请拍摄本人真实有效的身份证原件和人脸照片</view><view>拍摄时请保证身份证边缘完整,内容清晰可见</view></view><viewclass="upload_box"data-type="idCardPortrait"bind:tap="onC......
  • FunASR - 语音识别模型训练和微调
    文章目录一、关于FunASR核心功能模型仓库最新动态二、安装教程三、快速开始1、可执行命令行2、非实时语音识别2.1SenseVoice2.2Paraformer3、实时语音识别4、语音端点检测(非实时)5、语音端点检测(实时)6、标点恢复7、时间戳预测8、情感识别四、导出ONNX1、从命令行导......
  • Python 自动识别验证码的详细指南
    验证码(CAPTCHA)广泛应用于在线表单和注册页面,旨在防止自动化攻击。尽管验证码设计旨在增加机器识别的难度,但借助光学字符识别(OCR)技术,我们仍然可以实现自动化的验证码识别。在本教程中,我们将使用Python编写一个简单的验证码识别程序,利用TesseractOCR引擎结合图像预处理技术提高......