tensorflow1.1/构建双向神经网络识别mnist

环境:tensorflow1.1,python3

双向循环神经网络(BRNN)的基本思想是提出每一个训练序列向前和向后分别是两个循环神经网络(RNN),而且这两个都连接着一个输出层。这个结构提供给输出层输入序列中每一个点的完整的过去和未来的上下文信息。
下图展示的是一个沿着时间展开的双向循环神经网络。六个独特的权值在每一个时步被重复的利用,六个权值分别对应:输入到向前和向后隐含层(w1, w3),隐含层到隐含层自己(w2, w5),向前和向后隐含层到输出层(w4, w6)。值得注意的是:向前和向后隐含层之间没有信息流,这保证了展开图是非循环的。
这里写图片描述

BRNN:

#coding:utf-8
import tensorflow as tf
from tensorflow.contrib import rnn
import input_data
"""
python3
tensorflow 1.1
"""
#加载数据
mnist = input_data.read_data_sets('mnist/',one_hot=True)
test_x = mnist.test.images[:2000].reshape(2000,28,28)
test_y = mnist.test.labels[:2000]

#定义参数
learning_rate = 0.01
epochs = 50000
batch_size = 128
n_inputs = 28
n_steps = 28
n_hidden_units = 128
n_classes = 10

xs = tf.placeholder(tf.float32,[None,n_steps,n_inputs]) #输入形状
ys = tf.placeholder(tf.float32,[None,n_classes])

#定义weights和biases
#双向RNN隐藏层神经元x2
weights = {
    "in":tf.Variable(tf.random_normal([n_inputs,2*n_hidden_units])),
    "out":tf.Variable(tf.random_normal([2*n_hidden_units,n_classes]))
}

biases = {
    "in":tf.Variable(tf.constant(0.1,shape = [1,2*n_hidden_units])),
    "out":tf.Variable(tf.constant(0.1,shape = [1,n_classes]))
}

#定义双向RNN
def BiRNN(x, weights, biases):
    #输入数据形状(batch_size,n_steps,n_inputs)---输入到RNN中的数据形状(batch_size,n_inputs)
    #tf.unpack(x,n_step,1)将n_step这个维度消除
    x = tf.unstack(x, n_steps, 1)
    #定义双向RNN的cell,前向cell
    lstm_fw_cell = rnn.BasicLSTMCell(n_hidden_units, forget_bias=1.0)
    # 反向cell
    lstm_bw_cell = rnn.BasicLSTMCell(n_hidden_units, forget_bias=1.0)

    #rnn.static_bidirectional_rnn()返回三个参数,outputs,主线state,分线state
    try:
        outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
                                              dtype=tf.float32)
    except Exception: 
        #老版本rnn.static_bidirectional_rnn()返回一个参数
        outputs = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
                                        dtype=tf.float32)
    #outputs包含所有输出,取最后一个时刻的输出
    return tf.matmul(outputs[-1], weights['out']) + biases['out']

prediction = BiRNN(xs,weights,biases)
#计算cost
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=ys,logits=prediction))
train = tf.train.AdamOptimizer(learning_rate).minimize(cost)

true_pred = tf.equal(tf.argmax(prediction,1),tf.argmax(ys,1))
accuracy = tf.reduce_mean(tf.cast(true_pred,tf.float32))

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for step in range(1000):
        batch_xs,batch_ys = mnist.train.next_batch(batch_size)
        batch_xs = batch_xs.reshape([batch_size,n_steps,n_inputs])
        _,c = sess.run([train,cost],feed_dict={xs:batch_xs,ys:batch_ys})
        if step % 100 == 0:
            acc = sess.run(accuracy,feed_dict={xs:test_x,ys:test_y})
            print('= = = = = = > > > > > >','epoch: ',int(step/100),'train loss : %.4f' %c,'test accuracy: %.3f' %acc)

RNN:

#coding:utf-8
import tensorflow as tf
from tensorflow.contrib import rnn
import input_data
"""
python3
tensorflow 1.1
"""
#加载数据
mnist = input_data.read_data_sets('mnist/',one_hot=True)
test_x = mnist.test.images[:2000].reshape(2000,28,28)
test_y = mnist.test.labels[:2000]

#定义参数
learning_rate = 0.01
epochs = 50000
batch_size = 128
n_inputs = 28
n_steps = 28
n_hidden_units = 128
n_classes = 10

xs = tf.placeholder(tf.float32,[None,n_steps,n_inputs]) #输入形状
ys = tf.placeholder(tf.float32,[None,n_classes])

#定义weights和biases
#RNN隐藏层神经元
weights = {
    "in":tf.Variable(tf.random_normal([n_inputs,n_hidden_units])),
    "out":tf.Variable(tf.random_normal([n_hidden_units,n_classes]))
}

biases = {
    "in":tf.Variable(tf.constant(0.1,shape = [1,n_hidden_units])),
    "out":tf.Variable(tf.constant(0.1,shape = [1,n_classes]))
}


def RNN(x,weights,biases):
    #输入数据形状(batch_size,n_steps,n_inputs)---RNN输入形状(batch_size,n_inputs)
    x = tf.unstack(x,n_steps,1)
    #定义RNN的cell
    lstm_cell = rnn.BasicLSTMCell(n_hidden_units,forget_bias=1.0)
    #返回outputs,主线state,分线state
    outputs,states = rnn.static_rnn(lstm_cell,x,dtype=tf.float32)
    return tf.matmul(outputs[-1],weights['out'])+biases['out']

prediction = RNN(xs,weights,biases)
#计算cost
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=ys,logits=prediction))
train = tf.train.AdamOptimizer(learning_rate).minimize(cost)

true_pred = tf.equal(tf.argmax(prediction,1),tf.argmax(ys,1))
accuracy = tf.reduce_mean(tf.cast(true_pred,tf.float32))

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for step in range(1000):
        batch_xs,batch_ys = mnist.train.next_batch(batch_size)
        batch_xs = batch_xs.reshape([batch_size,n_steps,n_inputs])
        _,c = sess.run([train,cost],feed_dict={xs:batch_xs,ys:batch_ys})
        if step % 100 == 0:
            acc = sess.run(accuracy,feed_dict={xs:test_x,ys:test_y})
            print('= = = = = = > > > > > >','epoch: ',int(step/100),'train loss : %.4f' %c,'test accuracy: %.3f' %acc)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值