尝试使用RNN进行预测

时间:2020-05-03 07:26:54

标签: neural-network prediction recurrent-neural-network

import tensorflow as tf
from tensorflow.python.ops import rnn

class my_rnn(tf.keras.layers.Layer):
  def _init_(self, rnn_units, input_dim, output_dim):
    super(my_rnn,self)._init_()

    self.W_xh=self.add_weight([rnn_units, input_dim])
    self.W_hh=self.add_weight([rnn_units, rnn_units])
    self.W_hy=self.add_weight([output_dim, rnn_units])

    self.h= tf.zeros([rnn_units,1]) 

 def call(self,x,y):
    self.h=tf.math.tanh(self.W_hh * self.h+self.W_xh * x) 
    output= self.W_hy*self.h 
    return output 


my_object= my_rnn()
hidden_state=[0,0,0,0,0]
sentence=["the","color","of","Sun","is"]
for word in sentence:
  prediction, hidden_state= my_object(word, hidden_state)

next_word=prediction

现在这是错误:

AttributeError              
<ipython> in <module>()
     24 sentence=["the","color","of","Sun","is"]
     25 for word in sentence:
---> 26   prediction, hidden_state= my_object(word, hidden_state)
     27 next_word=prediction

<ipython> in call(self, x, y)
     15 
     16   def call(self,x,y):
---> 17     self.h=tf.math.tanh(self.W_hh * self.h+self.W_xh * x) 
     18     output= self.W_hy*self.h 
     19     return output 
AttributeError: 'my_rnn' object has no attribute 'W_hh'

我该如何解决? hidden_​​state的通过是否存在任何问题? 我刚刚开始训练有关神经网络的知识,所以如果有人能解决它,将非常感激。

0 个答案:

没有答案