作家
登录

如何用PyTorch实现递归神经网络?

作者: 来源: 2017-04-26 13:03:04 阅读 我要评论

  1. def tree_lstm(c1, c2, lstm_in): 
  2.     # Takes the memory cell states (c1, c2) of the two children, as 
  3.     # well as the sum of linear transformations of the children’s 
  4.     # hidden states (lstm_in) 
  5.     # That sum of transformed hidden states is broken up into a 
  6.     # candidate output a and four gates (i, f1, f2, and o). 
  7.     a, i, f1, f2, o = lstm_in.chunk(5, 1) 
  8.     c = a.tanh() * i.sigmoid() + f1.sigmoid() * c1 + f2.sigmoid() * c2 
  9.     h = o.sigmoid() * c.tanh() 
  10.     return h, cclass Reduce(nn.Module): 
  11.     def __init__(self, size, tracker_size=None): 
  12.         super(Reduce, self).__init__() 
  13.         self.left = nn.Linear(size, 5 * size
  14.         self.right = nn.Linear(size, 5 * size, bias=False
  15.         if tracker_size is not None: 
  16.             self.track = nn.Linear(tracker_size, 5 * size, bias=False
  17.  
  18.     def forward(self, left_in, right_in, tracking=None): 
  19.         leftright = batch(left_in), batch(right_in) 
  20.         tracking = batch(tracking) 
  21.         lstm_in = self.left(left[0]) 
  22.         lstm_in += self.right(right[0]) 
  23.         if hasattr(self, 'track'): 
  24.             lstm_in += self.track(tracking[0]) 
  25.         return unbatch(tree_lstm(left[1], right[1], lstm_in)) 

      推荐阅读

      未来的人们,将会如何看待我们现在的机器人?

    【义务编辑:wangxueyan TEL:(010)68476606】 >>>详细阅读


    本文标题:如何用PyTorch实现递归神经网络?

    地址:http://www.17bianji.com/lsqh/34965.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)