tf循环神经网络RNN进行mnist手写数字识别2

40 篇文章 0 订阅
34 篇文章 0 订阅
该博客介绍了一个使用TensorFlow实现的带两个隐藏层的LSTM模型,用于手写数字识别任务。通过训练MNIST数据集,模型在测试集上达到了一定的准确率。代码中详细展示了LSTM单元的构建和训练过程。
摘要由CSDN通过智能技术生成

教程地址

带2个隐藏层的LSTM

import tensorflow.compat.v1 as tf
import tensorflow as tf2
tf.disable_v2_behavior()
import numpy as np
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt

# Hyper Parameters
BATCH_SIZE = 128
N_STEPS = 28
N_INPUTS = 28
N_CLASSES=10
N_HIDDEN_UNITS=128

LR = 1e-3               # learning rate
TRAINING_ITERS=3e5

# data
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = (x_train / 255).reshape((-1,N_STEPS,N_INPUTS))
x_test = (x_test / 255).reshape((-1,N_STEPS,N_INPUTS))
y_train = np.eye(10)[y_train]
y_test = np.eye(10)[y_test]


#graph input
x=tf.placeholder(tf.float32,[None,N_STEPS,N_INPUTS])
y=tf.placeholder(tf.float32,[None,N_CLASSES])

W={'in':tf.Variable(tf.random_normal([N_INPUTS,N_HIDDEN_UNITS])),
   'out':tf.Variable(tf.random_normal([N_HIDDEN_UNITS,N_CLASSES]))}
b={'in':tf.Variable(tf.constant(.1,shape=[N_HIDDEN_UNITS,])),
   'out':tf.Variable(tf.constant(.1,shape=[N_CLASSES,]))}

def RNN(X,weights,b):
    #################################################
    #hidden layer for input to cell
    #X(128 batch,28 steps,28 inputs)
    X=tf.reshape(X,(-1,N_INPUTS))

    #128*28,28>>128*28,128
    X_in=tf.matmul(X,weights['in'])+b['in']

    # 128*28,128>>128,28,128
    #X_in作为dynamic_rnn的参数,并不要求最后一个维度等于BasicLSTMCell的num_units参数
    X_in=tf.reshape(X_in,(-1,N_STEPS,N_HIDDEN_UNITS))

    #################################################
    #cell
    #state_is_tupe,指定state的类型为tuple
    #num_units:int类型,LSTM单元中的神经元数量,即输出神经元数量
	#forget_bias:float类型,默认值为1
    lstm_cell=tf.nn.rnn_cell.BasicLSTMCell(N_HIDDEN_UNITS,
    				forget_bias=1,state_is_tuple=True)
    outputs, states = tf.nn.dynamic_rnn(lstm_cell, X_in, dtype=tf.float32)

    #################################################
    #hidden layer for output as the final results
    results=tf.matmul(states[1],weights['out'],)+b['out']

    return results

pred=RNN(x,W,b)
#注意加形参logits和labels,否则会报错或者搞错参数位置
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))
#softmax_cross_entropy_with_logits:将logits转换成概率,再计算交叉熵损失
#cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(tf2.nn.softmax(pred)), reduction_indices=1))

train_op=tf.train.AdamOptimizer(LR).minimize(cost)

correct_pred=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct_pred,tf.float32))

results=[]
with tf.Session() as se:
    se.run(tf.global_variables_initializer())
    step=0
    for step in range(0,int(TRAINING_ITERS),BATCH_SIZE):
        random_index=np.random.choice(x_train.shape[0], BATCH_SIZE, replace=False)
        batch_xs,batch_ys=x_train[random_index],y_train[random_index]
        se.run(train_op,feed_dict={x:batch_xs,y:batch_ys})

        sep_=1000
        if step%sep_==0:
            #教程用feed_dict={x:batch_xs,y:batch_ys}进行测试,感觉有问题,改用测试集进行测试
            # acc = se.run(accuracy, feed_dict={x:batch_xs,y:batch_ys})
            acc = se.run(accuracy, feed_dict={x: x_test, y: y_test})
            print(step,acc)
            results.append(acc)

    plt.plot([sep_ * i for i in range(len(results))], results)
    y_major_locator = plt.MultipleLocator(.1)
    ax = plt.gca()
    ax.yaxis.set_major_locator(y_major_locator)
    plt.ylim(0, 1)
    plt.show()

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值