tensorflow2.0 使用mnist数据集训练模型并测试手写图片识别率

本文展示了如何使用TensorFlow 2.0训练MNIST数据集,创建一个能识别手写数字的模型。虽然在测试集上准确率达到95%,但在实际的手绘数字识别中,效果受限于笔画、位置和尺寸等因素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

话不多说,下面是源码,参考了网上找的资料.模型结构:输入->h1->h2->h3->输出,尽管在mnist测试准确率达到95%,但实际使用画图程序手写10个数字进行识别, 最多只能识别出7个,跟你笔画粗细,数字是否居中,数字大小都有关系

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets

# 加载mnist数据集
(x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()

# 对数据进行预处理
def preprocess(x, y):
    # 将x映射到[0,1]范围内
    x = tf.cast(x, dtype=tf.float32) / 255.
    y = tf.cast(y, dtype=tf.int32)
    return (x, y)

# 构建dataset对象,对数据进行打乱,批处理等操作
train_data = tf.data.Dataset.from_tensor_slices(
    (x_train, y_train)).shuffle(10000).batch(512)
train_data = train_data.map(preprocess)
test_data = tf.data.Dataset.from_tensor_slices(
    (x_test, y_test)).shuffle(1000).batch(512)
test_data = test_data.map(preprocess)

# 建立权重和偏置,学习效率等训练中用到的变量
w1 = tf.Variable(tf.random.truncated_normal([28*28, 256], stddev=0.1))
b1 = tf.Variable(tf.zeros(256))
w2 = tf.Variable(</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值