介绍
这期内容当中小编将会给大家带来有关利用numpy怎么实现一个RNN功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
import numpy as np class Rnn (): def 才能;__init__ (input_size,自我,还以为,hidden_size, num_layers,,双向=False): ,,,self.input_size =input_size ,,,self.hidden_size =hidden_size ,,,self.num_layers =num_layers ,,,self.bidirectional =,双向的 def 才能;饲料(自我,,x): ,,,& # 39;& # 39;& # 39; ,,,:param x:, (seq, batch_size,,嵌入) ,,,:返回:,,隐藏 ,,,& # 39;& # 39;& # 39; ,,,#,x.shape [9月,,批处理,,特性) ,,,#,hidden.shape (hidden_size,批处理) ,,,#,Whh0.shape [hidden_size, hidden_size], Wih0.shape (hidden_size,,特性) ,,,#,Whh2.shape [hidden_size, hidden_size], Wih2.size [hidden_size, hidden_size] ,,,out =, [] ,,,x,, hidden =, np.array (x), [np.zeros ((self.hidden_size, x.shape [1])), for 小姐:拷贝范围(self.num_layers)] ,,,Wih =, (np.random.random ((self.hidden_size, self.hidden_size)), for 小姐:拷贝范围(1,self.num_layers)) ,,,Wih.insert (0,, np.random.random ((self.hidden_size, x.shape [2]))) ,,,Whh =, (np.random.random ((self.hidden_size, self.hidden_size)), for 小姐:拷贝范围(self.num_layers)] ,,,time =, x.shape [0] ,,,for 小姐:拷贝范围(时间): ,,,,,隐藏[0],=,np.tanh (np.dot(看法。[0],np.transpose (x(我,,…),(1,0))),+ ,,,,,,,,,,,,,,np.dot (Whh[0],,隐藏[0]) ,,,,,,,,,,,,,,) ,,,,,for 小姐:拷贝范围(1,self.num_layers): ,,,,,,,隐藏[我],=,np.tanh (np.dot(看法。[我],隐藏(张)),+ ,,,,,,,,,,,,,,,,,,np.dot (Whh[我],[我])隐藏 ,,,,,,,,,,,,,,,,,,) ,,,,,out.append(隐藏[self.num_layers-1]) ,,,return np.array(出),np.array(隐藏) def 乙状结肠(x): return 才能;1.0/(1.0,+,1.0/np.exp (x)) if __name__ ==, & # 39; __main__ # 39;: rnn 才能=,Rnn (5 1,,,, 4) 时间=input 才能;np.random.random ((6,, 2,, 1)) ,,,h =, rnn.feed(输入) 打印才能(f # 39; seq  is {input.shape [0]},, batch_size is {input.shape [1]}, & # 39;,, & # 39; out.shape & # 39;,, out.shape,, & # 39;, h.shape & # 39;,, h.shape) #,才能打印(乙状结肠(np.random.random ((2, 3)))) ,# #,才能element-wise 乘法 #,才能打印(np.array ([1, 2]) * np.array ((2, 1)))
上述就是小编为大家分享的利用numpy怎么实现一个RNN功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。