尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
TensorFlow(2) 使用TF构建多层感知机预测MNIST数据集
import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data import matplotlib.pyplot as plt #读入数据---------------------------------------------------------------------- mnist input_data.read_data_sets(MNIST_data/,one_hotTrue)#label为one-hot-encoding #查看读入的数据格式------------------------------------------------------------- print(train,mnist.train.num_examples, ,validation,mnist.validation.num_examples, ,test,mnist.test.num_examples) print(train images:,mnist.train.images.shape, labels:,mnist.train.labels.shape) #查看label的值 mnist.train.labels[0] #函数将读入的数据图像显示 def plot_image(image): #上面读入的图像是一维的用reshape转成矩阵输出 plt.imshow(image.reshape(28,28),cmapbinary) plt.show() plot_image(mnist.train.images[0]) #函数输出10张图像带标签label和预测值prediction def plot_images_labels_prediction(images, labels, prediction, idx, num10): figplt.gcf() fig.set_size_inches(12,14) if num25: num25 for i in range(0, num): ax plt.subplot(5,5,1i) ax.imshow(np.reshape(images[idx],(28,28)),cmapbinary) title labelstr(np.argmax(labels[idx])) if len(prediction)0: title,predictionstr(prediction[idx]) ax.set_title(title, fontsize10) ax.set_xticks([]);ax.set_yticks([]) idx1 plt.show() plot_images_labels_prediction(mnist.validation.images, mnist.validation.labels,[],0) #构建MLP模型--------------------------------------------------------------------- #自定义layer def layer(output_dim, input_dim, inputs, activationNone): W tf.Variable(tf.random_normal([input_dim, output_dim])) b tf.Variable(tf.random_normal([1, output_dim])) WXb tf.matmul(inputs,W)b if activation is None: outputs WXb else: outputs activation(WXb) return outputs #两个隐藏层h1、h2一个输入x一个输出y_predict x tf.placeholder(float,[None, 784]) h1 layer(output_dim1000,input_dim784,inputsx,activationtf.nn.relu) h2 layer(output_dim1000,input_dim1000,inputsh1,activationtf.nn.relu) y_predict layer(output_dim10,input_dim1000,inputsh2,activationNone) #定义标签值 y_label tf.placeholder(float,[None,10]) #损失函数 loss_function tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logitsy_predict,labelsy_label)) #优化器使loss最小化学习率为0.001 optimizer tf.train.AdamOptimizer(learning_rate0.001).minimize(loss_function) #预测结果的正确性 correct_prediction tf.equal(tf.argmax(y_label,1),tf.argmax(y_predict,1)) #计算精度cast():转换值的类型,reduce_mean():计算平均值 accuracy tf.reduce_mean(tf.cast(correct_prediction,float)) #开始训练--------------------------------------------------------------------- trainEpochs 15 #epoch batchSize 100 totalBatchs int(len(mnist.train.images)/batchSize) #一个周期总的批次 loss_list[];epoch_list[];accuracy_list[] #记录训练过程的loss,epoch,accuracy from time import time startTime time() sess tf.Session() sess.run(tf.global_variables_initializer()) for epoch in range(trainEpochs): for i in range(totalBatchs): batch_x, batch_y mnist.train.next_batch(batchSize) #读取下一个批次的数据循环读取 sess.run(optimizer, feed_dict{x:batch_x,y_label:batch_y}) loss, acc sess.run([loss_function,accuracy],feed_dict\ {x:mnist.validation.images,y_label:mnist.validation.labels}) epoch_list.append(epoch) loss_list.append(loss) accuracy_list.append(acc) print(Train Epoch:, %02d%(epoch1), Loss,\ {:.9f}.format(loss), Accuary,acc) duration time()-startTime print(Train Finished takes:, duration) #训练结果显示-------------------------------------------------------------------- %matplotlib inline fig plt.gcf()#获取当前的figure图 fig.set_size_inches(4,2) plt.plot(epoch_list, loss_list, labelloss) plt.ylabel(loss) plt.xlabel(epoch) plt.legend([loss], locupper right) plt.plot(epoch_list,accuracy_list,labelaccuracy) fig plt.gcf() fig.set_size_inches(4,2) plt.ylim(0.8,1)#设置y轴范围 plt.ylabel(accuracy) plt.xlabel(epoch) plt.legend([accuarcy], locupper right) #评估------------------------------------------------------------------------------ #测试集test准确率 print(Accuracy:, sess.run(accuracy,feed_dict\ {x:mnist.test.images,y_label:mnist.test.labels})) #预测test prediction_result sess.run(tf.argmax(y_predict,1),feed_dict{x:mnist.test.images}) #显示真实值和预测值及图像 plot_images_labels_prediction(mnist.test.images,mnist.test.labels,prediction_result,0)
RELATED

相关推荐

3个维度重塑你的英雄联盟体验:League Akari如何成为你的智能游戏伙伴

3个维度重塑你的英雄联盟体验:League Akari如何成为你的智能游戏伙伴

3个维度重塑你的英雄联盟体验:League Akari如何成为你的智能游戏伙伴 【免费下载链接】League-Toolkit An all-in-one toolkit for LeagueClient. Gathering power 🚀. 项目地址: https://gitcode.com/gh_mirrors/le/League-Toolkit 当英雄联盟的…

📅 2026/9/15 11:52:11
中欧 PHP 开发者大会因多元化争议而取消

中欧 PHP 开发者大会因多元化争议而取消

没有女性的演讲者名单导致提倡多元化的男性演讲者退出会议,最终使得计划于德国举办的中欧 PHP 开发者大会宣布取消。 上周末,原定于 10 月 4 日至 6 日在德国德累斯顿举行的 PHP 会议 PHP Central Europe developer conference (PHP.CE) 因多元化争议宣布…

📅 2026/8/24 14:51:56
SpringBoot在线投稿系统开发与优化实践

SpringBoot在线投稿系统开发与优化实践

1. 项目概述 在线投稿系统是学术期刊、会议和内容平台的核心基础设施,它直接关系到内容生产的效率和质量管控。基于SpringBoot框架开发的投稿系统,能够为编辑部、审稿人和作者提供全流程的数字化解决方案。这个毕业设计项目采用Java技术栈实现&#xff0…

📅 2026/9/17 14:23:02
MORE NEWS

更多资讯

📰

把数千子智能体任务收回,Cursor Projects 的 TaoToken Key 怎么统一

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

Anthropic 红队评测多模型,Key 走 TaoToken 行不行?

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

Keil5开发环境高频问题排查:从安装到调试的完整指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

oh-my-hermes:Hermes 引擎工程化配置与命令行工具实践

刚接触“oh-my-hermes”这个名字时,我第一反应是:这又是一个模仿 oh-my-zsh 的配置管理项目吧。实际深入之后发现,它确实延续了“oh-my-”家族的核心思路——把一套原本需要手动维护的环境配置、初始化和日常操作脚本,收敛成一个开…

📰

ESP32-S3语音唤醒实战:麦克风电路、自定义唤醒词与端侧AI推理

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

新环境切换时,Anthropic API 的 Key 改从 TaoToken 取

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬