解析lstm(官方文档)源码

源码来自http://deeplearning.net/tutorial/lstm.html

最近了解到lstm,这份官方源码应该是学习了解lstm的最佳范本了,为了防止自己遗忘,写下这篇解析文档。

先大致回顾一下lstm到底是什么,简单的说是一种时间递归网络,解决之前普通rnn梯度爆炸或消失,无法对间隔时间很长的知识记忆的缺点。
http://colah.github.io/posts/2015-08-Understanding-LSTMs/ 一篇很容易理解的lstm的文档。

本文拟从lstm官方文档的代码出发,解析步骤按照程序运行步骤步步解析,anyway,开撸。

if __name__ == '__main__':
    # See function train for all possible parameter and there definition.
    train_lstm(
        max_epochs=100,
        test_size=500,
    )

程序开始,设置了两个量,max_epoches, test_size ,其他参数缺省调用。

转 def train_lstm函数开头一堆缺省调用,每个参数都有解释,应该很好理解。

说一个model_options = locals().copy()

model_options = locals().copy()
print("model options", model_options)

将函数中所有参数爬虫下来,保存为一个词典。

 train, valid, test = load_data(n_words=n_words, valid_portion=0.05,
                                   maxlen=maxlen)

load_data在imdb.py里

def load_data(path="imdb.pkl", n_words=100000, valid_portion=0.1, maxlen=None,
              sort_by_len=True):
   path = get_dataset_file(
        path, "imdb.pkl",
        "http://www.iro.umontreal.ca/~lisa/deep/data/imdb.pkl")

    if path.endswith(".gz"):
        f = gzip.open(path, 'rb')
    else:
        f = open(path, 'rb')

    train_set = pickle.load(f)
    test_set = pickle.load(f)
    f.close()
    if maxlen:
        new_train_set_x = []
        new_train_set_y = []
        for x, y in zip(train_set[0], train_set[1]):
            if len(x) < maxlen:
                new_train_set_x.append(x)
                new_train_set_y.append(y)
        train_set = (new_train_set_x, new_train_set_y)
        del new_train_set_x, new_train_set_y

    # split training set into validation set
    train_set_x, train_set_y = train_set
    n_samples = len(train_set_x)
    sidx = numpy.random.permutation(n_samples)
    n_train = int(numpy.round(n_samples * (1. - valid_portion)))
    valid_set_x = [train_set_x[s] for s in sidx[n_train:]]
    valid_set_y = [train_set_y[s] for s in sidx[n_train:]]
  • 4
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值