Попробуйте предсказать с РНН - PullRequest
0 голосов
/ 03 мая 2020
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? Я только начал изучать нейронную сеть, поэтому буду очень признателен, если кто-нибудь сможет ее решить.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...