Ananas120 / Tacotron2_in_keras

3 stars 1 forks source link

ZoneoutLSTM's inference does not seem to be right. Here is a reference~ Thx for your keras codes #1

Open qq547276542 opened 3 years ago

qq547276542 commented 3 years ago
class ZoneoutLSTMCell(tf.keras.layers.LSTMCell):
    def __init__(self, units, 
                 zoneout_factor_cell=0., 
                 zoneout_factor_output=0., 
                 **kwargs):
        super(ZoneoutLSTMCell, self).__init__(units, **kwargs)
        zm = min(zoneout_factor_output, zoneout_factor_cell)
        zs = max(zoneout_factor_output, zoneout_factor_cell)

        if zm < 0. or zs > 1.:
            raise ValueError('One / both provided zoneoutfacotrs are not in [0, 1]')

        self._zoneout_cell = zoneout_factor_cell
        self._zoneout_outputs = zoneout_factor_output

    def get_config(self):
        config = super(ZoneoutLSTMCell, self).get_config()
        config['zoneout_factor_cell'] = self._zoneout_cell
        config['zoneout_factor_output'] = self._zoneout_outputs
        return config

    def call(self, inputs, state, training=False, **kwargs):
        output, new_state = super(ZoneoutLSTMCell, self).call(inputs, state)
        (prev_c, prev_h) = state
        (new_c, new_h) = new_state

        if training:
            drop_c = K.dropout(new_c - prev_c, self._zoneout_cell)
            drop_h = K.dropout(new_h - prev_h, self._zoneout_outputs)
            c = (1. - self._zoneout_cell) * drop_c + prev_c
            h = (1. - self._zoneout_outputs) * drop_h + prev_h
        else:
            c = (1. - self._zoneout_cell) * new_c + self._zoneout_cell * prev_c
            h = (1. - self._zoneout_outputs) * new_h + self._zoneout_cell * prev_h

        return output, [c, h]
Ananas120 commented 3 years ago

Hello, yes sorry this was an old implementation that I do not maintain but I made a better implementation of Tacotron-2 in tensorflow with really good results but without the ZoneOutLSTM (simple LSTMCell) The tensorflow 2.x uses keras so the code is as simple as with keras (it uses tf.keras) and has more optimization (with tf.function for instance) I am currently improving my tensorflow implementation but you can also look at the TFTTS library which has a proper tensorflow implementation of Tacotron-2 However I did not have any good inference-results with their model but a really impressive teacher-forcing prediction