예제 #1
0
 def testLSTMBlockCell(self):
     with self.session(use_gpu=True, graph=tf.Graph()) as sess:
         with tf.variable_scope("root",
                                initializer=tf.constant_initializer(0.5)):
             x = tf.zeros([1, 2])
             m0 = tf.zeros([1, 2])
             m1 = tf.zeros([1, 2])
             m2 = tf.zeros([1, 2])
             m3 = tf.zeros([1, 2])
             g, ((out_m0, out_m1),
                 (out_m2, out_m3)) = rnn_cell.MultiRNNCell(
                     [contrib_rnn.LSTMBlockCell(2) for _ in range(2)],
                     state_is_tuple=True)(x, ((m0, m1), (m2, m3)))
             sess.run([tf.global_variables_initializer()])
             res = sess.run(
                 [g, out_m0, out_m1, out_m2, out_m3], {
                     x.name: np.array([[1., 1.]]),
                     m0.name: 0.1 * np.ones([1, 2]),
                     m1.name: 0.1 * np.ones([1, 2]),
                     m2.name: 0.1 * np.ones([1, 2]),
                     m3.name: 0.1 * np.ones([1, 2])
                 })
             self.assertLen(res, 5)
             self.assertAllClose(res[0], [[0.24024698, 0.24024698]])
             # These numbers are from testBasicLSTMCell and only test c/h.
             self.assertAllClose(res[1], [[0.68967271, 0.68967271]])
             self.assertAllClose(res[2], [[0.44848421, 0.44848421]])
             self.assertAllClose(res[3], [[0.39897051, 0.39897051]])
             self.assertAllClose(res[4], [[0.24024698, 0.24024698]])
예제 #2
0
    def testLSTMBasicToBlockCell(self):
        with self.session(use_gpu=True) as sess:
            x = tf.zeros([1, 2])
            x_values = np.random.randn(1, 2)

            m0_val = 0.1 * np.ones([1, 2])
            m1_val = -0.1 * np.ones([1, 2])
            m2_val = -0.2 * np.ones([1, 2])
            m3_val = 0.2 * np.ones([1, 2])

            initializer = tf.random_uniform_initializer(-0.01,
                                                        0.01,
                                                        seed=19890212)
            with tf.variable_scope("basic", initializer=initializer):
                m0 = tf.zeros([1, 2])
                m1 = tf.zeros([1, 2])
                m2 = tf.zeros([1, 2])
                m3 = tf.zeros([1, 2])
                g, ((out_m0, out_m1),
                    (out_m2, out_m3)) = rnn_cell.MultiRNNCell(
                        [
                            rnn_cell.BasicLSTMCell(2, state_is_tuple=True)
                            for _ in range(2)
                        ],
                        state_is_tuple=True)(x, ((m0, m1), (m2, m3)))
                sess.run([tf.global_variables_initializer()])
                basic_res = sess.run(
                    [g, out_m0, out_m1, out_m2, out_m3], {
                        x.name: x_values,
                        m0.name: m0_val,
                        m1.name: m1_val,
                        m2.name: m2_val,
                        m3.name: m3_val
                    })

            with tf.variable_scope("block", initializer=initializer):
                m0 = tf.zeros([1, 2])
                m1 = tf.zeros([1, 2])
                m2 = tf.zeros([1, 2])
                m3 = tf.zeros([1, 2])
                g, ((out_m0, out_m1),
                    (out_m2, out_m3)) = rnn_cell.MultiRNNCell(
                        [contrib_rnn.LSTMBlockCell(2) for _ in range(2)],
                        state_is_tuple=True)(x, ((m0, m1), (m2, m3)))
                sess.run([tf.global_variables_initializer()])
                block_res = sess.run(
                    [g, out_m0, out_m1, out_m2, out_m3], {
                        x.name: x_values,
                        m0.name: m0_val,
                        m1.name: m1_val,
                        m2.name: m2_val,
                        m3.name: m3_val
                    })

            self.assertEqual(len(basic_res), len(block_res))
            for basic, block in zip(basic_res, block_res):
                self.assertAllClose(basic, block)
예제 #3
0
def rnn_cell(rnn_cell_size, dropout_keep_prob, residual, is_training=True):
    """Builds an LSTMBlockCell based on the given parameters."""
    dropout_keep_prob = dropout_keep_prob if is_training else 1.0
    cells = []
    for i in range(len(rnn_cell_size)):
        cell = contrib_rnn.LSTMBlockCell(rnn_cell_size[i])
        if residual:
            cell = rnn.ResidualWrapper(cell)
            if i == 0 or rnn_cell_size[i] != rnn_cell_size[i - 1]:
                cell = contrib_rnn.InputProjectionWrapper(
                    cell, rnn_cell_size[i])
        cell = rnn.DropoutWrapper(cell, input_keep_prob=dropout_keep_prob)
        cells.append(cell)
    return rnn.MultiRNNCell(cells)
예제 #4
0
    def testCompatibleNames(self):
        with self.session(use_gpu=True, graph=tf.Graph()):
            cell = rnn_cell.LSTMCell(10)
            pcell = rnn_cell.LSTMCell(10, use_peepholes=True)
            inputs = [tf.zeros([4, 5])] * 6
            tf.nn.static_rnn(cell, inputs, dtype=tf.float32, scope="basic")
            tf.nn.static_rnn(pcell, inputs, dtype=tf.float32, scope="peephole")
            basic_names = {
                v.name: v.get_shape()
                for v in tf.trainable_variables()
            }

        with self.session(use_gpu=True, graph=tf.Graph()):
            cell = contrib_rnn.LSTMBlockCell(10)
            pcell = contrib_rnn.LSTMBlockCell(10, use_peephole=True)
            inputs = [tf.zeros([4, 5])] * 6
            tf.nn.static_rnn(cell, inputs, dtype=tf.float32, scope="basic")
            tf.nn.static_rnn(pcell, inputs, dtype=tf.float32, scope="peephole")
            block_names = {
                v.name: v.get_shape()
                for v in tf.trainable_variables()
            }

        self.assertEqual(basic_names, block_names)
예제 #5
0
    def testNoneDimsWithDynamicRNN(self):
        with self.session(use_gpu=True, graph=tf.Graph()) as sess:
            batch_size = 4
            num_steps = 5
            input_dim = 6
            cell_size = 7

            cell = contrib_rnn.LSTMBlockCell(cell_size)
            x = tf.placeholder(tf.float32, shape=(None, None, input_dim))

            output, _ = tf.nn.dynamic_rnn(cell,
                                          x,
                                          time_major=True,
                                          dtype=tf.float32)
            sess.run(tf.global_variables_initializer())
            feed = {}
            feed[x] = np.random.randn(num_steps, batch_size, input_dim)
            sess.run(output, feed)