神经网络_使用tensorflow对mnist手写数字分类

python 复制代码
from pathlib import Path
import requests
import pickle 
import gzip
from matplotlib import pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.utils import to_categorical
import pandas as pd 
%matplotlib inline

1. 数据预处理

python 复制代码
data_path = Path("data/mnist")
data_path.mkdir(parents=True, exist_ok=True)

#是否从网络下载数据文件
source_data_file_from_net = False

#下载数据
#此网址打开较慢,可以使用 https://tianchi.aliyun.com/dataset/165658 阿里云下载
mnist_url = "http://deeplearning.net/data/mnist/"
mnist_zip_name = "mnist.pkl.gz"

if source_data_file_from_net:
    if not (data_path/mnist_zip_name).exists:
        content = requests.get(mnist_url + mnist_zip_name).content
        (data_path / mnist_zip_name).open('wb').write(content)
        ((x_train, y_train),(x_valid, y_valid), _) = pick.load(f, encoding='latin-1')
    else:    
        with gzip.open((data_path / mnist_zip_name).as_posix(), "rb") as data_file:
            ((x_train, y_train),(x_valid, y_valid), _) = pick.load(data_file, encoding='latin-1')

else:
    data_file = open('data/mnist/mnist.pkl', 'rb+')
    ((x_train, y_train),(x_valid, y_valid), _) = pickle.load(data_file, encoding='latin-1')
    
print("x_train shape ", x_train.shape)
print(x_train[:5])
print("y_train shape ", y_train.shape)
print(y_train[:5])
print("第一个数字5展示:")
plt.imshow(x_train[0].reshape((28,28)), cmap='gray')        
复制代码
x_train shape  (50000, 784)
[[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]
y_train shape  (50000,)
[5 0 4 1 9]
第一个数字5展示:





<matplotlib.image.AxesImage at 0x207575478d0>

2.模型实现

2.1模型实现

python 复制代码
#模型 api 参考 https://tensorflow.google.cn/api_docs/python/tf/keras
model = tf.keras.Sequential()
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))

model.compile(optimizer=tf.keras.optimizers.Adam(0.005),
             loss=tf.keras.losses.SparseCategoricalCrossentropy(),
             metrics=[tf.keras.metrics.SparseCategoricalCrossentropy])
model.fit(x_train, y_train, epochs=5, batch_size=64,
         validation_data=(x_valid, y_valid))
data_file.close()
复制代码
Epoch 1/5
[1m782/782[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m2s[0m 1ms/step - loss: 0.4966 - sparse_categorical_crossentropy: 0.4966 - val_loss: 0.1806 - val_sparse_categorical_crossentropy: 0.1806
Epoch 2/5
[1m782/782[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m1s[0m 1ms/step - loss: 0.1690 - sparse_categorical_crossentropy: 0.1690 - val_loss: 0.1906 - val_sparse_categorical_crossentropy: 0.1906
Epoch 3/5
[1m782/782[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m1s[0m 997us/step - loss: 0.1287 - sparse_categorical_crossentropy: 0.1287 - val_loss: 0.1682 - val_sparse_categorical_crossentropy: 0.1682
Epoch 4/5
[1m782/782[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m1s[0m 1ms/step - loss: 0.1103 - sparse_categorical_crossentropy: 0.1103 - val_loss: 0.1394 - val_sparse_categorical_crossentropy: 0.1394
Epoch 5/5
[1m782/782[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m1s[0m 1ms/step - loss: 0.1025 - sparse_categorical_crossentropy: 0.1025 - val_loss: 0.1345 - val_sparse_categorical_crossentropy: 0.1345

2.2模型优化

python 复制代码
# 修改损失函数为CategoricalCrossentropy, 报错,还未解决
model = tf.keras.Sequential()
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))

data_file = open('data/mnist/mnist.pkl', 'rb+')
((x_train, y_train),(x_valid, y_valid), _) = pickle.load(data_file, encoding='latin-1')

model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
             loss=tf.losses.CategoricalCrossentropy(),
             metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)
y_valid = tf.keras.utils.to_categorical(y_valid, num_classes=10)
print("x_train.shape ", x_train.shape)
print("y_train.shape ", y_train.shape)
print("x_valid.shape ", x_valid.shape)
print("y_valid.shape ", y_valid.shape)
model.fit(x_train, y_train, epochs=5, batch_size=64,
         validation_data=(x_valid, y_valid))
data_file.close()
复制代码
x_train.shape  (50000, 784)
y_train.shape  (50000, 10)
x_valid.shape  (10000, 784)
y_valid.shape  (10000, 10)
Epoch 1/5



---------------------------------------------------------------------------

InvalidArgumentError                      Traceback (most recent call last)

Cell In[63], line 18
     16 print("x_valid.shape ", x_valid.shape)
     17 print("y_valid.shape ", y_valid.shape)
---> 18 model.fit(x_train, y_train, epochs=5, batch_size=64,
     19          validation_data=(x_valid, y_valid))
     20 data_file.close()


File D:\python\Lib\site-packages\keras\src\utils\traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119     filtered_tb = _process_traceback_frames(e.__traceback__)
    120     # To get the full stack trace, call:
    121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
    123 finally:
    124     del filtered_tb


File D:\python\Lib\site-packages\tensorflow\python\eager\execute.py:53, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     51 try:
     52   ctx.ensure_initialized()
---> 53   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     54                                       inputs, attrs, num_outputs)
     55 except core._NotOkStatusException as e:
     56   if name is not None:


InvalidArgumentError: Graph execution error:

Detected at node Equal defined at (most recent call last):
  File "D:\python\Lib\runpy.py", line 198, in _run_module_as_main

  File "D:\python\Lib\runpy.py", line 88, in _run_code

  File "D:\python\Lib\site-packages\ipykernel_launcher.py", line 18, in <module>

  File "D:\python\Lib\site-packages\traitlets\config\application.py", line 1043, in launch_instance

  File "D:\python\Lib\site-packages\ipykernel\kernelapp.py", line 739, in start

  File "D:\python\Lib\site-packages\tornado\platform\asyncio.py", line 205, in start

  File "D:\python\Lib\asyncio\base_events.py", line 607, in run_forever

  File "D:\python\Lib\asyncio\base_events.py", line 1919, in _run_once

  File "D:\python\Lib\asyncio\events.py", line 80, in _run

  File "D:\python\Lib\site-packages\ipykernel\kernelbase.py", line 545, in dispatch_queue

  File "D:\python\Lib\site-packages\ipykernel\kernelbase.py", line 534, in process_one

  File "D:\python\Lib\site-packages\ipykernel\kernelbase.py", line 437, in dispatch_shell

  File "D:\python\Lib\site-packages\ipykernel\ipkernel.py", line 362, in execute_request

  File "D:\python\Lib\site-packages\ipykernel\kernelbase.py", line 778, in execute_request

  File "D:\python\Lib\site-packages\ipykernel\ipkernel.py", line 449, in do_execute

  File "D:\python\Lib\site-packages\ipykernel\zmqshell.py", line 549, in run_cell

  File "D:\python\Lib\site-packages\IPython\core\interactiveshell.py", line 2945, in run_cell

  File "D:\python\Lib\site-packages\IPython\core\interactiveshell.py", line 3000, in _run_cell

  File "D:\python\Lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner

  File "D:\python\Lib\site-packages\IPython\core\interactiveshell.py", line 3203, in run_cell_async

  File "D:\python\Lib\site-packages\IPython\core\interactiveshell.py", line 3382, in run_ast_nodes

  File "D:\python\Lib\site-packages\IPython\core\interactiveshell.py", line 3442, in run_code

  File "C:\Users\AXZQ\AppData\Local\Temp\ipykernel_8960\1530222387.py", line 18, in <module>

  File "D:\python\Lib\site-packages\keras\src\utils\traceback_utils.py", line 117, in error_handler

  File "D:\python\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 320, in fit

  File "D:\python\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 121, in one_step_on_iterator

  File "D:\python\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 108, in one_step_on_data

  File "D:\python\Lib\site-packages\keras\src\backend\tensorflow\trainer.py", line 77, in train_step

  File "D:\python\Lib\site-packages\keras\src\trainers\trainer.py", line 452, in compute_metrics

  File "D:\python\Lib\site-packages\keras\src\trainers\compile_utils.py", line 330, in update_state

  File "D:\python\Lib\site-packages\keras\src\trainers\compile_utils.py", line 17, in update_state

  File "D:\python\Lib\site-packages\keras\src\metrics\reduction_metrics.py", line 204, in update_state

  File "D:\python\Lib\site-packages\keras\src\metrics\accuracy_metrics.py", line 240, in sparse_categorical_accuracy

  File "D:\python\Lib\site-packages\keras\src\ops\numpy.py", line 2355, in equal

  File "D:\python\Lib\site-packages\keras\src\backend\tensorflow\numpy.py", line 1144, in equal

Incompatible shapes: [64,10] vs. [64]
	 [[{{node Equal}}]] [Op:__inference_one_step_on_iterator_69389]
相关推荐
宝贝儿好12 天前
【LLM】第二章:HuggingFace入门学习
人工智能·深度学习·神经网络·学习·算法·自然语言处理
神仙别闹12 天前
基于C++ 实现 BP 神经网络
开发语言·c++·神经网络
天行健,君子而铎12 天前
自适应分类·高准确率·可视化易用——运营商数据分类分级解决方案
大数据·分类
装不满的克莱因瓶12 天前
了解多标签图像分类方法——从Sigmoid输出到真实世界复杂视觉理解
人工智能·pytorch·python·深度学习·机器学习·分类·数据挖掘
冷小鱼12 天前
TensorFlow 2.21 进阶实战:从训练优化到生产部署的完整指南
人工智能·pytorch·python·tensorflow
我登哥MVP12 天前
NeuSO揭秘:用神经网络玩转图数据库子图查询优化
数据库·人工智能·神经网络·图数据库
木叶子---13 天前
前端打包出错
前端·人工智能·tensorflow
装不满的克莱因瓶13 天前
掌握语义分割经典模型 FCN——从像素分类到端到端分割的奠基之作
人工智能·python·深度学习·算法·机器学习·分类·数据挖掘
DXM052113 天前
第14期|高阶分割模型:Transformer/SegFormer遥感应用
人工智能·python·神经网络·算法·计算机视觉·cnn·ageo
雷工笔记13 天前
MES系列51-人防门行业 MES 质检分类体系
人工智能·分类·数据挖掘