Ejemplo n.º 1
0
def test_static_dropout_lstm_cell():
    sess = tf.Session()
    x = np.random.randn(1, 10, 50).astype(np.float32)
    with sess.graph.as_default():
        with tf.variable_scope("DropoutIsOn"):
            rnn_drop_cell = lstm_cell_w_dropout(100,
                                                0.9999999999,
                                                training=True)
            rnn_drop, _ = tf.nn.dynamic_rnn(rnn_drop_cell,
                                            x,
                                            sequence_length=np.array(
                                                [10], dtype=np.int),
                                            dtype=tf.float32)
        with tf.variable_scope("DropoutIsOff"):
            rnn_no_drop_cell = lstm_cell_w_dropout(100,
                                                   0.9999999999,
                                                   training=False)
            rnn_no_drop, _ = tf.nn.dynamic_rnn(rnn_no_drop_cell,
                                               x,
                                               sequence_length=np.array(
                                                   [10], dtype=np.int),
                                               dtype=tf.float32)
    sess.run(tf.global_variables_initializer())
    out_ten = sess.run(rnn_drop)
    assert len(out_ten[np.nonzero(out_ten)].squeeze()) < 20
    out_ten = sess.run(rnn_no_drop)
    assert len(out_ten[np.nonzero(out_ten)].squeeze()) > 20
Ejemplo n.º 2
0
    def pool(self, word_embeddings, dsz, init, **kwargs):
        """LSTM with dropout yielding a final-state as output
        
        :param word_embeddings: The input word embeddings
        :param dsz: The input word embedding depth
        :param init: The tensorflow initializer to use (currently ignored)
        :param kwargs: See below
        
        :Keyword Arguments:
        * *hsz* -- (``int``) The number of hidden units (defaults to `100`)
        * *cmotsz* -- (``int``) An alias for `hsz`
        
        :return: 
        """
        hsz = kwargs.get('rnnsz', kwargs.get('hsz', 100))
        if type(hsz) is list:
            hsz = hsz[0]
        char_rnnfwd = lstm_cell_w_dropout(hsz, self.pkeep)
        rnnout, final_state = tf.nn.dynamic_rnn(char_rnnfwd,
                                                word_embeddings,
                                                dtype=tf.float32,
                                                sequence_length=self.lengths)

        output_state = final_state.h
        combine = tf.reshape(output_state, [-1, hsz])
        return combine
Ejemplo n.º 3
0
def test_static_dropout_lstm_cell():
    with tf.device('/cpu:0'):
        sess = tf.Session()
        x = np.random.randn(1, 10, 50).astype(np.float32)
        with sess.graph.as_default():
            with tf.variable_scope("DropoutIsOn"):
                rnn_drop_cell = lstm_cell_w_dropout(100, 0.9999999999, training=True)
                rnn_drop, _ = tf.nn.dynamic_rnn(rnn_drop_cell, x, sequence_length=np.array([10], dtype=np.int), dtype=tf.float32)
            with tf.variable_scope("DropoutIsOff"):
                rnn_no_drop_cell = lstm_cell_w_dropout(100, 0.9999999999, training=False)
                rnn_no_drop, _ = tf.nn.dynamic_rnn(rnn_no_drop_cell, x, sequence_length=np.array([10], dtype=np.int), dtype=tf.float32)
        sess.run(tf.global_variables_initializer())
        out_ten = sess.run(rnn_drop)
        assert len(out_ten[np.nonzero(out_ten)].squeeze()) < 20
        out_ten = sess.run(rnn_no_drop)
        assert len(out_ten[np.nonzero(out_ten)].squeeze()) > 20
Ejemplo n.º 4
0
def test_placeholder_dropout_lstm_cell():
    sess = tf.Session()
    x = np.random.randn(1, 10, 50).astype(np.float32)
    with sess.graph.as_default():
        train_flag = tf.placeholder_with_default(False, shape=(), name='TEST_TRAIN_FLAG')
        with tf.variable_scope("DropoutMightBeOn"):
            rnn_cell = lstm_cell_w_dropout(100, 0.9999999999, training=train_flag)
            rnn, _ = tf.nn.dynamic_rnn(rnn_cell, x, sequence_length=np.array([10], dtype=np.int), dtype=tf.float32)

    sess.run(tf.global_variables_initializer())
    out_ten = sess.run(rnn, {train_flag: True})
    assert len(out_ten[np.nonzero(out_ten)].squeeze()) < 20
    out_ten = sess.run(rnn)
    assert len(out_ten[np.nonzero(out_ten)].squeeze()) > 20
Ejemplo n.º 5
0
def test_placeholder_dropout_lstm_cell():
    with tf.device('/cpu:0'):
        sess = tf.Session()
        x = np.random.randn(1, 10, 50).astype(np.float32)
        with sess.graph.as_default():
            train_flag = tf.placeholder_with_default(False, shape=(), name='TEST_TRAIN_FLAG')
            with tf.variable_scope("DropoutMightBeOn"):
                rnn_cell = lstm_cell_w_dropout(100, 0.9999999999, training=train_flag)
                rnn, _ = tf.nn.dynamic_rnn(rnn_cell, x, sequence_length=np.array([10], dtype=np.int), dtype=tf.float32)

        sess.run(tf.global_variables_initializer())
        out_ten = sess.run(rnn, {train_flag: True})
        assert len(out_ten[np.nonzero(out_ten)].squeeze()) < 20
        out_ten = sess.run(rnn)
        assert len(out_ten[np.nonzero(out_ten)].squeeze()) > 20