自己实现LSTM和GRU内部的代码

LSTM的
https://github.com/LantaoYu/SeqGAN/blob/master/generator.py#L157-L190

GRU的

class 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 h,h

你可能感兴趣的:(TensorFlow)