Deep Learning Tutorial (翻译) 之 RNN

阅读: 评论:0

Deep Learning Tutorial (翻译) 之 RNN

Deep Learning Tutorial (翻译) 之 RNN

英文原文请参考.html

总结

在本文中,你将学会:

  • 词嵌套
  • RNN架构
  • 上下文窗口

in order to perform Semantic Parsing / Slot-Filling (Spoken Language Understanding)

Code

直接运行实验可以使用这个github部署资源。

任务

Slot-Filling是一个分类任务,给定一个句子,给每个word标记一个label

数据库

这个任务的benchmark是ATIS数据库(Airline Travel Information System)。用IOB表示。下面是例子

 ATIS包含4978/893个句子,56590/9198个词(平均句子长度15)。类别数为128,包含label(NULL)

我们将未知的单词设为<UNK>,数字表示为DIGIT.

我们将官方训练数据分为训练和验证集。我们使用如下指标进行评估:

  • 准确率
  • 召回率
  • F1 score

我们将使用PERL脚本来测量模型的性能。

递归神经网络模型

原始输入编码

a token对应于一个单词,ATIS中的每个token与一个index相连。每个句子是indexes的数组(int32)。(train,valid,test)是indexes数组的列表。定义一个python字典将indexes空间映射到words空间。如下:

这个句子的标签(labels)也类似,如下:

上下文窗口

给定一个句子,例如一个index的数组,和一个窗口大小如1,,我们需要将句子中的每个单词转换成包含这个特定单词的上下文窗口。例如:

其中index为-1对应于PADDING, PADDING是我们插入在句子前面或最后的。下面是例子:

输入是一个句子每个单词的index数组,输出为一个矩阵,每一行对应于句子中一个单词的上下文窗口。

词嵌套

既然我们已经将句子转换成上下文窗口(index的矩阵),我们必须将这些index关联到embeddings(每个单词真正的值向量)

 

x符号变量为矩阵,shape为(句子中的单词数量,嵌入空间的维度*上下文窗口大小(de*cs=50*5))

使用theano函数来完成这个过程:

现在我们得到了上下文窗口词嵌套(每行)的序列(序列长度为句子长度,上例为5)。

Elman RNN

前面,我们将输入处理成序列结构(sequential/temporal structure),row 0表示时间t=0步,row 1表示时间t=1步,等等。

需要学习的E-RNN参数为:

  • 词嵌套(真值矩阵,real-valued matrix)
  • 初始隐藏状态(real-value vector)
  •  two matrices for the linear projection of the input t and the previous hidden layer state t-1
  • (可选)bias 
  • 最上面的softmax分类层

超参数定义了整个架构:

  • 词嵌套的维度
  • 词典大小
  • 隐藏单元数
  • 类别数
  • 随机种子+初始化模型的方式

给出如下代码:

class myRNNSLU(object):def __init__(self, nh, nc, ne, de, cs):''':param nh: 隐藏层维度:param nc: 类别数:param ne: number of word embeddings in the vocabulary,词典中词的个数(eg.1000):param de: 词嵌套的维度(eg.50):param cs: 上下文窗口大小(eg.5):return:'''# 模型参数# emb 嵌套向量 (1000+1, b = theano.shared(name='embeddings',value=0.2 * numpy.random.uniform(-1.0, 1.0,(ne + 1, de)).fig.floatX))# wx 输入层权重 (50*5, nh)self.wx = theano.shared(name='wx',value=0.2 * numpy.random.uniform(-1.0, 1.0,(de * cs, nh)).fig.floatX))# wh 隐藏层权重 (nh, nh)self.wh = theano.shared(name='wh',value=0.2 * numpy.random.uniform(-1.0, 1.0,(nh, nh)).fig.floatX))# w 全连接权重 (nh, nc)self.w = theano.shared(name='w',value=0.2 * numpy.random.uniform(-1.0, 1.0,(nh, nc)).fig.floatX))# bh 隐藏层偏差self.bh = theano.shared(name='bh',value&#s(nh,dtype&#fig.floatX))self.b = theano.shared(name='b',value&#s(nc,dtype&#fig.floatX))self.h0 = theano.shared(name='h0',value&#s(nh,dtype&#fig.floatX))self.params = [b, self.wx, self.wh, self.w,self.bh, self.b, self.h0]# 根据嵌套矩阵构建输入# idxs (一个句子长度,窗口大小5)# x (一个句子长度,50*5) 就是将对应word的index变为50维的词向量# emb[idxs] 维度为(一个句子长度, 窗口大小5,维度50)idxs = T.imatrix()x = b[idxs].reshape((idxs.shape[0], de*cs))y_sentence = T.ivector('y_sentence') # labelsdef recurrence(x_t, h_tm1):# h_t (len, nh)h_t = T.nnet.sigmoid(T.dot(x_t, self.wx)+ T.dot(h_tm1, self.wh) + self.bh)# s_t (len, nc)s_t = T.nnet.softmax(T.dot(h_t, self.w) + self.b)return [h_t, s_t]# n_steps 表示迭代的次数# sequences 需要迭代的序列,会变化# outputs_info 指定初值# s (len, n_steps, nc) 是这样吗?[h, s], _ = theano.scan(fn=recurrence,sequences=x,outputs_info=[self.h0, None],n_steps=x.shape[0])p_y_given_x_sentence = s[:,0,:] # (len, nc) 为什么是0y_pred = T.argmax(p_y_given_x_sentence, axis=1)# 计算梯度,最大化似然函数lr = T.scalar('lr') # 学习率sentence_nll = - T.mean(T.log(p_y_given_x_sentence)[T.arange(x.shape[0]), y_sentence])sentence_gradients = T.grad(sentence_nll, self.params)sentence_updates = OrderedDict((p, p - lr*g)for p, g inzip(self.params, sentence_gradients))# theano functionsself.classify = theano.function(inputs=[idxs], outputs=y_pred)self.sentence_train = theano.function(inputs=[idxs, y_sentence, lr],outputs=sentence_nll,updates=sentence_updates)# 每次更新后 normalize alize = theano.function(inputs=[],updates={b /T.sqrt((b ** 2).sum(axis=1)).dimshuffle(0, 'x')})

 

理解一些基本操作

 

训练

更新

对于随机梯度下降更新,我们将整个句子视为一个mini-batch并且每个句子执行一次更新。

每次迭代/更新后,我们normalize词嵌套来keep them on a unit sphere。

超参数选择

  • learning rate : uniform([0.05,0.01])
  • window size : random value from {3,...,19}
  • number of hidden units : random value from {100,200}
  • embedding dimension : random value from {50,100}

 参考资料:

 1.scan——Theano中循环的实现

 2.Theano2.1.10-基础知识之循环

转载于:.html

本文发布于:2024-01-31 05:43:06,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170665099025969.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:Learning   Deep   RNN   Tutorial
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23