当前位置: 首页 > wzjs >正文

网站开发公司怎么做账网络营销企业网站

网站开发公司怎么做账,网络营销企业网站,iis 网站文件被占用,图片类网站如何做优化系列文章目录 终章 1:Attention的结构 终章 2:带Attention的seq2seq的实现 终章 3:Attention的评价 终章 4:关于Attention的其他话题 终章 5:Attention的应用 目录 系列文章目录 前言 一、编码器的实现 二、解…

系列文章目录

终章 1:Attention的结构

终章 2:带Attention的seq2seq的实现

终章 3:Attention的评价

终章 4:关于Attention的其他话题

终章 5:Attention的应用


目录

系列文章目录

前言

一、编码器的实现

二、解码器的实现

三、seq2seq的实现


前言

上一节实现了Attention层(以及Time Attention层),现在我们使用 这个层来实现“带Attention的seq2seq”。和上一章实现了3个类(Encoder、 Decoder 和 seq2seq)一样,这里我们也分别实现3个类(AttentionEncoder、 AttentionDecoder 和 AttentionSeq2seq)


一、编码器的实现

首先实现AttentionEncoder 类。这个类和上一章实现的Encoder类几乎一样,唯一的区别是,Encoder类的forward()方法仅返回LSTM层的最后的隐藏状态向量,而AttentionEncoder类则返回所有的隐藏状态向量。因此, 这里我们继承上一章的Encoder类进行实现。AttentionEncoder类的实现如下所示

class AttentionEncoder(Encoder):def forward(self, xs):xs = self.embed.forward(xs)hs = self.lstm.forward(xs)return hsdef backward(self, dhs):dout = self.lstm.backward(dhs)dout = self.embed.backward(dout)return dout

二、解码器的实现

接着实现使用了Attention层的解码器。使用了Attention的解码器的层结构如下图所示

从上图中可以看出,和上一章的实现一样,Softmax层(更确切地说,是Time Softmax with Loss层)之前的层都作为解码器。另外,和上一章一样,除了正向传播forward()方法和反向出传播backward()方法之外,还实现了生成新单词序列(字符序列)的generate()方法。这里给出Attention Decoder层的初始化方法和forward()方法等的实现,如下所示

class AttentionDecoder:def __init__(self, vocab_size, wordvec_size, hidden_size):V, D, H = vocab_size, wordvec_size, hidden_sizern = np.random.randnembed_W = (rn(V, D) / 100).astype('f')lstm_Wx = (rn(D, 4 * H) / np.sqrt(D)).astype('f')lstm_Wh = (rn(H, 4 * H) / np.sqrt(H)).astype('f')lstm_b = np.zeros(4 * H).astype('f')affine_W = (rn(2*H, V) / np.sqrt(2*H)).astype('f')affine_b = np.zeros(V).astype('f')self.embed = TimeEmbedding(embed_W)self.lstm = TimeLSTM(lstm_Wx, lstm_Wh, lstm_b, stateful=True)self.attention = TimeAttention()self.affine = TimeAffine(affine_W, affine_b)layers = [self.embed, self.lstm, self.attention, self.affine]self.params, self.grads = [], []for layer in layers:self.params += layer.paramsself.grads += layer.gradsdef forward(self, xs, enc_hs):h = enc_hs[:,-1]self.lstm.set_state(h)out = self.embed.forward(xs)dec_hs = self.lstm.forward(out)c = self.attention.forward(enc_hs, dec_hs)out = np.concatenate((c, dec_hs), axis=2)score = self.affine.forward(out)return scoredef backward(self, dscore):dout = self.affine.backward(dscore)N, T, H2 = dout.shapeH = H2 // 2dc, ddec_hs0 = dout[:,:,:H], dout[:,:,H:]denc_hs, ddec_hs1 = self.attention.backward(dc)ddec_hs = ddec_hs0 + ddec_hs1dout = self.lstm.backward(ddec_hs)dh = self.lstm.dhdenc_hs[:, -1] += dhself.embed.backward(dout)return denc_hsdef generate(self, enc_hs, start_id, sample_size):sampled = []sample_id = start_idh = enc_hs[:, -1]self.lstm.set_state(h)for _ in range(sample_size):x = np.array([sample_id]).reshape((1, 1))out = self.embed.forward(x)dec_hs = self.lstm.forward(out)c = self.attention.forward(enc_hs, dec_hs)out = np.concatenate((c, dec_hs), axis=2)score = self.affine.forward(out)sample_id = np.argmax(score.flatten())sampled.append(sample_id)return sampled

这里的实现除使用了新的Time Attention层之外,和上一章的Decoder类 没有什么太大的不同。需要注意的是,forward()方法中拼接了Time Attention 层的输出和LSTM层的输出。在上面的代码中,使用np.concatenate()方法进行拼接。 这里省略对AttentionDecoder 类的backward() 和 generate() 方法的说明。最后,我们使用AttentionEncoder类和AttentionDecoder 类来实现 AttentionSeq2seq类

三、seq2seq的实现

AttentionSeq2seq 类的实现也和上一章实现的seq2seq几乎一样。区别仅在于,编码器使用AttentionEncoder类,解码器使用AttentionDecoder类。 因此,只要继承上一章的Seq2seq类,并改一下初始化方法,就可以实现 AttentionSeq2seq 类

class AttentionSeq2seq(Seq2seq):def __init__(self, vocab_size, wordvec_size, hidden_size):args = vocab_size, wordvec_size, hidden_sizeself.encoder = AttentionEncoder(*args)self.decoder = AttentionDecoder(*args)self.softmax = TimeSoftmaxWithLoss()self.params = self.encoder.params + self.decoder.paramsself.grads = self.encoder.grads + self.decoder.grads

以上就是带Attention的seq2seq的实现。

http://www.dtcms.com/wzjs/148438.html

相关文章:

  • 哪些做海报比较好的网站流量平台
  • 广州化妆品网站制作定制网站开发公司
  • 做英文网站2014seo引擎优化工具
  • 行业网站联盟宣传方式
  • 英文外贸网站建设短视频seo优化
  • 怎么在wordpress建英文网站百度推广技巧方法
  • 设计素材网站会员怎么买划算四川省人民政府官网
  • c 做asp.net网站北京培训seo哪个好
  • 网站建设div设置圆角线上推广方式
  • 上海景朋建设工程有限公司网站汕头疫情最新消息
  • 郑州网站建设 天强科技代运营网店公司
  • 珊瑚绒毯移动网站建设百度搜索引擎网站
  • 万网 公司网站链接秦洁婷seo博客
  • 武汉建站排名怎么营销自己的产品
  • 做网站要分几部分完成公司网站建设北京
  • 怎么做公众号网站seo好学吗入门怎么学
  • 网站营销优化玉林seo
  • 哪个网站可以做批发新闻最新消息今天
  • 大连九死一疯事件抖音seo教程
  • 像网站分类一样的表格图怎么做网络推广方法有哪几种
  • 小游戏链接点开即玩天津seo诊断技术
  • 域名污染优化关键词排名seo软件
  • 建设网站教程全集无锡网站制作
  • faq wordpressseo如何去做优化
  • 想做找人做网站各大网站提交入口
  • c net做的网站手机端关键词排名优化软件
  • 推荐做任务网站北京seo管理
  • 建设部网站公示网站运营师
  • 网站建设胶州百度seo排名优化费用
  • 海宁市住房与城乡规划建设局网站seo优化外包顾问