轻量级前端框架助力开发者提升项目效率与性能
649
2022-11-15
自己实现LSTM和GRU内部的代码
LSTM的 GruCell(): def init_matrix(self, shape): return tf.random_normal(shape, stddev=0.1) def zero_state(self, batch_size, type): return tf.zeros([batch_size,self.hidden_dim],dtype=type) @property def state_size(self): return self.hidden_dim def __init__(self, emb_dim, hidden_dim): self.hidden_dim = hidden_dim self.W1 = tf.get_variable("mask_gru_W1",initializer=self.init_matrix([emb_dim, hidden_dim])) self.U1 = tf.get_variable("mask_gru_U1",initializer=self.init_matrix([hidden_dim, hidden_dim])) self.b1 = tf.get_variable("mask_gru_b1",initializer=self.init_matrix([hidden_dim])) self.W2 = tf.get_variable("mask_gru_W2",initializer=self.init_matrix([emb_dim, hidden_dim])) self.U2 = tf.get_variable("mask_gru_U2",initializer=self.init_matrix([hidden_dim, hidden_dim])) self.b2 = tf.get_variable("mask_gru_b2",initializer=self.init_matrix([hidden_dim])) self.W3 = tf.get_variable("mask_gru_W3",initializer=self.init_matrix([emb_dim, hidden_dim])) self.U3 = tf.get_variable("mask_gru_U3",initializer=self.init_matrix([hidden_dim, hidden_dim])) self.b3 = tf.get_variable("mask_gru_b3",initializer=self.init_matrix([hidden_dim])) def __call__(self, x, hidden_memory_tm1): x_z = tf.matmul(x, self.W1) + self.b1 x_r = tf.matmul(x, self.W2) + self.b2 x_h = tf.matmul(x, self.W3) + self.b3 z = tf.sigmoid(x_z + tf.matmul(hidden_memory_tm1, self.U1)) r = tf.sigmoid(x_r + tf.matmul(hidden_memory_tm1, self.U2)) hh = tf.sigmoid(x_h + tf.matmul(r * hidden_memory_tm1, self.U3)) h = z * hidden_memory_tm1 + (1 - z) * hh return
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~