单层LSTM网络对MNIST数据集分类

2024-01-07 03:18

本文主要是介绍单层LSTM网络对MNIST数据集分类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

单层LSTM网络对MNIST数据集分类

实验代码:(使用tensorflow框架)

# -*- coding: utf-8 -*-import tensorflow as tf
# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/data/", one_hot=True)n_input = 28 # MNIST data 输入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10  # MNIST 列别 (0-9 ,一共10类)tf.reset_default_graph()# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])x1 = tf.unstack(x, n_steps, 1)#1 BasicLSTMCell
lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x1, dtype=tf.float32)#2 LSTMCell
#lstm_cell = tf.contrib.rnn.LSTMCell(n_hidden, forget_bias=1.0)
#outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x1, dtype=tf.float32)#3 gru
#gru = tf.contrib.rnn.GRUCell(n_hidden)
#outputs = tf.contrib.rnn.static_rnn(gru, x1, dtype=tf.float32)#4 创建动态RNN
#outputs,_  = tf.nn.dynamic_rnn(gru,x,dtype=tf.float32)
#outputs = tf.transpose(outputs, [1, 0, 2])pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)# Evaluate model
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))# 启动session
with tf.Session() as sess:sess.run(tf.global_variables_initializer())step = 1# Keep training until reach max iterationswhile step * batch_size < training_iters:batch_x, batch_y = mnist.train.next_batch(batch_size)# Reshape data to get 28 seq of 28 elementsbatch_x = batch_x.reshape((batch_size, n_steps, n_input))# Run optimization op (backprop)sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})if step % display_step == 0:# 计算批次数据的准确率acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})# Calculate batch lossloss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})print ("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \"{:.6f}".format(loss) + ", Training Accuracy= " + \"{:.5f}".format(acc))step += 1print (" Finished!")# 计算准确率 for 128 mnist test imagestest_len = 128test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))test_label = mnist.test.labels[:test_len]print ("Testing Accuracy:", \sess.run(accuracy, feed_dict={x: test_data, y: test_label}))

实验结果:
Iter 1280, Minibatch Loss= 2.098885, Training Accuracy= 0.30469
Iter 2560, Minibatch Loss= 1.772232, Training Accuracy= 0.38281
Iter 3840, Minibatch Loss= 1.404505, Training Accuracy= 0.52344
Iter 5120, Minibatch Loss= 1.321466, Training Accuracy= 0.57031
Iter 6400, Minibatch Loss= 1.020606, Training Accuracy= 0.65625
Iter 7680, Minibatch Loss= 0.767583, Training Accuracy= 0.76562
Iter 8960, Minibatch Loss= 0.945606, Training Accuracy= 0.66406
Iter 10240, Minibatch Loss= 0.643211, Training Accuracy= 0.78906
Iter 11520, Minibatch Loss= 0.737389, Training Accuracy= 0.76562
Iter 12800, Minibatch Loss= 0.589967, Training Accuracy= 0.83594
Iter 14080, Minibatch Loss= 0.432091, Training Accuracy= 0.89062
Iter 15360, Minibatch Loss= 0.375092, Training Accuracy= 0.90625
Iter 16640, Minibatch Loss= 0.509971, Training Accuracy= 0.82031
Iter 17920, Minibatch Loss= 0.431015, Training Accuracy= 0.85156
Iter 19200, Minibatch Loss= 0.420453, Training Accuracy= 0.85156
Iter 20480, Minibatch Loss= 0.338827, Training Accuracy= 0.88281
Iter 21760, Minibatch Loss= 0.427024, Training Accuracy= 0.86719
Iter 23040, Minibatch Loss= 0.419629, Training Accuracy= 0.87500
Iter 24320, Minibatch Loss= 0.343750, Training Accuracy= 0.90625
Iter 25600, Minibatch Loss= 0.232130, Training Accuracy= 0.92188
Iter 26880, Minibatch Loss= 0.491618, Training Accuracy= 0.89062
Iter 28160, Minibatch Loss= 0.226970, Training Accuracy= 0.92188
Iter 29440, Minibatch Loss= 0.287028, Training Accuracy= 0.91406
Iter 30720, Minibatch Loss= 0.348053, Training Accuracy= 0.90625
Iter 32000, Minibatch Loss= 0.232494, Training Accuracy= 0.92969
Iter 33280, Minibatch Loss= 0.294077, Training Accuracy= 0.89062
Iter 34560, Minibatch Loss= 0.269400, Training Accuracy= 0.90625
Iter 35840, Minibatch Loss= 0.257503, Training Accuracy= 0.92969
Iter 37120, Minibatch Loss= 0.176288, Training Accuracy= 0.95312
Iter 38400, Minibatch Loss= 0.263634, Training Accuracy= 0.89844
Iter 39680, Minibatch Loss= 0.350406, Training Accuracy= 0.89062
Iter 40960, Minibatch Loss= 0.175449, Training Accuracy= 0.94531
Iter 42240, Minibatch Loss= 0.311644, Training Accuracy= 0.89844
Iter 43520, Minibatch Loss= 0.202412, Training Accuracy= 0.92188
Iter 44800, Minibatch Loss= 0.238732, Training Accuracy= 0.92188
Iter 46080, Minibatch Loss= 0.262362, Training Accuracy= 0.91406
Iter 47360, Minibatch Loss= 0.277031, Training Accuracy= 0.92188
Iter 48640, Minibatch Loss= 0.167007, Training Accuracy= 0.93750
Iter 49920, Minibatch Loss= 0.208343, Training Accuracy= 0.95312
Iter 51200, Minibatch Loss= 0.237634, Training Accuracy= 0.91406
Iter 52480, Minibatch Loss= 0.133993, Training Accuracy= 0.96094
Iter 53760, Minibatch Loss= 0.255377, Training Accuracy= 0.92188
Iter 55040, Minibatch Loss= 0.204812, Training Accuracy= 0.92969
Iter 56320, Minibatch Loss= 0.183624, Training Accuracy= 0.92969
Iter 57600, Minibatch Loss= 0.131443, Training Accuracy= 0.96094
Iter 58880, Minibatch Loss= 0.096448, Training Accuracy= 0.97656
Iter 60160, Minibatch Loss= 0.163977, Training Accuracy= 0.96875
Iter 61440, Minibatch Loss= 0.185323, Training Accuracy= 0.95312
Iter 62720, Minibatch Loss= 0.107512, Training Accuracy= 0.97656
Iter 64000, Minibatch Loss= 0.174152, Training Accuracy= 0.95312
Iter 65280, Minibatch Loss= 0.173235, Training Accuracy= 0.95312
Iter 66560, Minibatch Loss= 0.115825, Training Accuracy= 0.96875
Iter 67840, Minibatch Loss= 0.190322, Training Accuracy= 0.92969
Iter 69120, Minibatch Loss= 0.073072, Training Accuracy= 0.97656
Iter 70400, Minibatch Loss= 0.161416, Training Accuracy= 0.93750
Iter 71680, Minibatch Loss= 0.148715, Training Accuracy= 0.95312
Iter 72960, Minibatch Loss= 0.174622, Training Accuracy= 0.95312
Iter 74240, Minibatch Loss= 0.100780, Training Accuracy= 0.97656
Iter 75520, Minibatch Loss= 0.177840, Training Accuracy= 0.96094
Iter 76800, Minibatch Loss= 0.119568, Training Accuracy= 0.96094
Iter 78080, Minibatch Loss= 0.116565, Training Accuracy= 0.96094
Iter 79360, Minibatch Loss= 0.124705, Training Accuracy= 0.96094
Iter 80640, Minibatch Loss= 0.068246, Training Accuracy= 0.97656
Iter 81920, Minibatch Loss= 0.152009, Training Accuracy= 0.97656
Iter 83200, Minibatch Loss= 0.150834, Training Accuracy= 0.96094
Iter 84480, Minibatch Loss= 0.082806, Training Accuracy= 0.98438
Iter 85760, Minibatch Loss= 0.239210, Training Accuracy= 0.94531
Iter 87040, Minibatch Loss= 0.194339, Training Accuracy= 0.94531
Iter 88320, Minibatch Loss= 0.141747, Training Accuracy= 0.96094
Iter 89600, Minibatch Loss= 0.110870, Training Accuracy= 0.97656
Iter 90880, Minibatch Loss= 0.066232, Training Accuracy= 0.98438
Iter 92160, Minibatch Loss= 0.085497, Training Accuracy= 0.96875
Iter 93440, Minibatch Loss= 0.141791, Training Accuracy= 0.96094
Iter 94720, Minibatch Loss= 0.143089, Training Accuracy= 0.93750
Iter 96000, Minibatch Loss= 0.234196, Training Accuracy= 0.93750
Iter 97280, Minibatch Loss= 0.143507, Training Accuracy= 0.94531
Iter 98560, Minibatch Loss= 0.069923, Training Accuracy= 0.96875
Iter 99840, Minibatch Loss= 0.079662, Training Accuracy= 0.98438
Finished!
Testing Accuracy: 0.976562


参考资料:《深度学习之Tensorflow》李金洪编著

这篇关于单层LSTM网络对MNIST数据集分类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/578550

相关文章

详谈redis跟数据库的数据同步问题

《详谈redis跟数据库的数据同步问题》文章讨论了在Redis和数据库数据一致性问题上的解决方案,主要比较了先更新Redis缓存再更新数据库和先更新数据库再更新Redis缓存两种方案,文章指出,删除R... 目录一、Redis 数据库数据一致性的解决方案1.1、更新Redis缓存、删除Redis缓存的区别二

Redis事务与数据持久化方式

《Redis事务与数据持久化方式》该文档主要介绍了Redis事务和持久化机制,事务通过将多个命令打包执行,而持久化则通过快照(RDB)和追加式文件(AOF)两种方式将内存数据保存到磁盘,以防止数据丢失... 目录一、Redis 事务1.1 事务本质1.2 数据库事务与redis事务1.2.1 数据库事务1.

Oracle Expdp按条件导出指定表数据的方法实例

《OracleExpdp按条件导出指定表数据的方法实例》:本文主要介绍Oracle的expdp数据泵方式导出特定机构和时间范围的数据,并通过parfile文件进行条件限制和配置,文中通过代码介绍... 目录1.场景描述 2.方案分析3.实验验证 3.1 parfile文件3.2 expdp命令导出4.总结

更改docker默认数据目录的方法步骤

《更改docker默认数据目录的方法步骤》本文主要介绍了更改docker默认数据目录的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1.查看docker是否存在并停止该服务2.挂载镜像并安装rsync便于备份3.取消挂载备份和迁

不删数据还能合并磁盘? 让电脑C盘D盘合并并保留数据的技巧

《不删数据还能合并磁盘?让电脑C盘D盘合并并保留数据的技巧》在Windows操作系统中,合并C盘和D盘是一个相对复杂的任务,尤其是当你不希望删除其中的数据时,幸运的是,有几种方法可以实现这一目标且在... 在电脑生产时,制造商常为C盘分配较小的磁盘空间,以确保软件在运行过程中不会出现磁盘空间不足的问题。但在

Java如何接收并解析HL7协议数据

《Java如何接收并解析HL7协议数据》文章主要介绍了HL7协议及其在医疗行业中的应用,详细描述了如何配置环境、接收和解析数据,以及与前端进行交互的实现方法,文章还分享了使用7Edit工具进行调试的经... 目录一、前言二、正文1、环境配置2、数据接收:HL7Monitor3、数据解析:HL7Busines

Mybatis拦截器如何实现数据权限过滤

《Mybatis拦截器如何实现数据权限过滤》本文介绍了MyBatis拦截器的使用,通过实现Interceptor接口对SQL进行处理,实现数据权限过滤功能,通过在本地线程变量中存储数据权限相关信息,并... 目录背景基础知识MyBATis 拦截器介绍代码实战总结背景现在的项目负责人去年年底离职,导致前期规

Redis KEYS查询大批量数据替代方案

《RedisKEYS查询大批量数据替代方案》在使用Redis时,KEYS命令虽然简单直接,但其全表扫描的特性在处理大规模数据时会导致性能问题,甚至可能阻塞Redis服务,本文将介绍SCAN命令、有序... 目录前言KEYS命令问题背景替代方案1.使用 SCAN 命令2. 使用有序集合(Sorted Set)

SpringBoot整合Canal+RabbitMQ监听数据变更详解

《SpringBoot整合Canal+RabbitMQ监听数据变更详解》在现代分布式系统中,实时获取数据库的变更信息是一个常见的需求,本文将介绍SpringBoot如何通过整合Canal和Rabbit... 目录需求步骤环境搭建整合SpringBoot与Canal实现客户端Canal整合RabbitMQSp

MyBatis框架实现一个简单的数据查询操作

《MyBatis框架实现一个简单的数据查询操作》本文介绍了MyBatis框架下进行数据查询操作的详细步骤,括创建实体类、编写SQL标签、配置Mapper、开启驼峰命名映射以及执行SQL语句等,感兴趣的... 基于在前面几章我们已经学习了对MyBATis进行环境配置,并利用SqlSessionFactory核