单向LSTM与双向LSTM对比

本文通过对比实验展示了单向和双向LSTM在手写数字识别任务中的性能。使用TensorFlow数据集,单向LSTM在100次训练后准确率趋于稳定,而双向LSTM虽然需要更多训练次数,但100次后准确率显著提高,稳定性更强。结论是双向LSTM在图像识别中表现更优。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个简单的DEMO:实现手写数字图片的识别

单向LSTM

利用的数据集是tensorflow提供的一个手写数字数据集。该数据集是一个包含55000张28*28的数据集。
训练100次
识别准确率还不是很稳定,但是从第17次开始就趋于相对稳定的状态了。

# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow.contrib import rnn

import numpy as np
#import input_data
from tensorflow.examples.tutorials.mnist import input_data #####
mnist = input_data.read_data_sets('MNIST_data', one_hot=True) #####

# configuration
#                        O * W + b -> 10 labels for each image, O[? 28], W[28 10], B[10]
#                       ^ (O: output 28 vec from 28 vec input)
#                       |
#      +-+  +-+       +--+
#      |1|->|2|-> ... |28| time_step_size = 28
#      +-+  +-+       +--+
#       ^    ^    ...  ^
#       |    |         |
# img1:[28] [28]  ... [28]
# img2:[28] [28]  ... [28]
# img3:[28] [28]  ... [28]
# ...
# img128 or img256 (batch_size or test_size 256)
#      each input size = input_vec_size=lstm_size=28

# configuration variables
input_vec_size = lstm_size = 28 # 输入向量的维度
time_step_size = 28 # 循环层长度

batch_size = 128
test_size = 256

def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))


def model(X, W, B, lstm_size):
    # X, input shape: (batch_size, time_step
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值