千家信息网

TensorFlow中Softmax逻辑回归如何识别手写数字MNIST数据集

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,今天就跟大家聊聊有关TensorFlow中Softmax逻辑回归如何识别手写数字MNIST数据集,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
千家信息网最后更新 2026年11月01日TensorFlow中Softmax逻辑回归如何识别手写数字MNIST数据集

今天就跟大家聊聊有关TensorFlow中Softmax逻辑回归如何识别手写数字MNIST数据集,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

基于MNIST数据集的逻辑回归模型做十分类任务

没有隐含层的Softmax Regression只能直接从图像的像素点推断是哪个数字,而没有特征抽象的过程。多层神经网络依靠隐含层,则可以组合出高阶特征,比如横线、竖线、圆圈等,之后可以将这些高阶特征或者说组件再组合成数字,就能实现精准的匹配和分类。

import tensorflow as tfimport numpy as npimport input_dataprint('Download and Extract MNIST dataset')mnist = input_data.read_data_sets('data/', one_hot=True) # one_hot=True意思是编码格式为01编码print("tpye of 'mnist' is %s" % (type(mnist)))print("number of train data is %d" % (mnist.train.num_examples))print("number of test data is %d" % (mnist.test.num_examples))trainimg = mnist.train.imagestrainlabel = mnist.train.labelstestimg = mnist.test.imagestestlabel = mnist.test.labelsprint("MNIST loaded")"""print("type of 'trainimg' is %s"    % (type(trainimg)))print("type of 'trainlabel' is %s"  % (type(trainlabel)))print("type of 'testimg' is %s"     % (type(testimg)))print("type of 'testlabel' is %s"   % (type(testlabel)))print("------------------------------------------------")print("shape of 'trainimg' is %s"   % (trainimg.shape,))print("shape of 'trainlabel' is %s" % (trainlabel.shape,))print("shape of 'testimg' is %s"    % (testimg.shape,))print("shape of 'testlabel' is %s"  % (testlabel.shape,))"""x = tf.placeholder(tf.float32, [None, 784])y = tf.placeholder(tf.float32, [None, 10]) # None is for infinitew = tf.Variable(tf.zeros([784, 10])) # 为了方便直接用0初始化,可以高斯初始化b = tf.Variable(tf.zeros([10])) # 10分类的任务,10种label,所以只需要初始化10个bpred = tf.nn.softmax(tf.matmul(x, w) + b) # 前向传播的预测值cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=[1])) # 交叉熵损失函数optm = tf.train.GradientDescentOptimizer(0.01).minimize(cost)corr = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) # tf.equal()对比预测值的索引和真实label的索引是否一样,一样返回True,不一样返回Falseaccr = tf.reduce_mean(tf.cast(corr, tf.float32))init = tf.global_variables_initializer() # 全局参数初始化器training_epochs = 100 # 所有样本迭代100次batch_size = 100 # 每进行一次迭代选择100个样本display_step = 5# SESSIONsess = tf.Session() # 定义一个Sessionsess.run(init) # 在sess里run一下初始化操作# MINI-BATCH LEARNINGfor epoch in range(training_epochs): # 每一个epoch进行循环    avg_cost = 0. # 刚开始损失值定义为0    num_batch = int(mnist.train.num_examples/batch_size)    for i in range(num_batch): # 每一个batch进行选择        batch_xs, batch_ys = mnist.train.next_batch(batch_size) # 通过next_batch()就可以一个一个batch的拿数据,        sess.run(optm, feed_dict={x: batch_xs, y: batch_ys}) # run一下用梯度下降进行求解,通过placeholder把x,y传进来        avg_cost += sess.run(cost, feed_dict={x: batch_xs, y:batch_ys})/num_batch    # DISPLAY    if epoch % display_step == 0: # display_step之前定义为5,这里每5个epoch打印一下        train_acc = sess.run(accr, feed_dict={x: batch_xs, y:batch_ys})        test_acc = sess.run(accr, feed_dict={x: mnist.test.images, y: mnist.test.labels})        print("Epoch: d/d cost: %.9f TRAIN ACCURACY: %.3f TEST ACCURACY: %.3f"              % (epoch, training_epochs, avg_cost, train_acc, test_acc))print("DONE")

迭代100次跑一下模型,最终,在测试集上可以达到92.2%的准确率,虽然还不错,但是还达不到实用的程度。手写数字的识别的主要应用场景是识别银行支票,如果准确率不够高,可能会引起严重的后果。

Epoch: 095/100 loss: 0.283259882 train_acc: 0.940 test_acc: 0.922

插一些知识点,关于tensorflow中一些函数的用法

sess = tf.InteractiveSession()arr = np.array([[31, 23,  4, 24, 27, 34],                [18,  3, 25,  0,  6, 35],                [28, 14, 33, 22, 30,  8],                [13, 30, 21, 19,  7,  9],                [16,  1, 26, 32,  2, 29],                [17, 12,  5, 11, 10, 15]])
在tensorflow中打印要用.eval()tf.rank(arr).eval() # 打印矩阵arr的维度tf.shape(arr).eval() # 打印矩阵arr的大小tf.argmax(arr, 0).eval() # 打印最大值的索引,参数0为按列求索引,1为按行求索引

看完上述内容,你们对TensorFlow中Softmax逻辑回归如何识别手写数字MNIST数据集有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

数字 索引 数据 逻辑 内容 特征 分类 迭代 任务 准确率 函数 参数 损失 样本 模型 知识 矩阵 编码 预测值 高阶 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 手冲咖啡和咖啡机区别 互联网科技图片 互联网科技有限公司图片 互联网巨头科技公司 麻工互联网科技 华中科技大学互联网 奇酷互联网络科技公司 广西互联网科技公司 互联网金融和金融科技 恒生科技互联网指数分析 互联网科技公司需要的职位 科技与互联网哪个更好 互联网软件和科技 乌苏新华互联网科技做什么的 中国互联网公司排名锤子科技 智帮互联网科技公司最近动向 深圳无忧互联网科技 产业互联网科技服务公司 云南互联网科技学费 现代互联网科技公司 上海挚品互联网科技有限公司 广州大一互联网络科技有限公司 互联网科技的英文 迪茉互联网科技 昭通云南众福互联网科技有限公司 知域互联网科技有限公司大骗子 工业互联网等科技实现智能化赋能 互联网数据科技专业 广东工业互联网大赛深圳学泰科技 互联网科技创新创业项目
0