Ejemplo n.º 1
0
    def _testDecoderTraining(self, decoder):
        batch_size = 4
        vocab_size = 10
        time_dim = 5
        depth = 6
        inputs = tf.placeholder_with_default(np.random.randn(
            batch_size, time_dim, depth).astype(np.float32),
                                             shape=(None, None, depth))
        # NOTE: max(sequence_length) may be less than time_dim when num_gpus > 1
        sequence_length = [1, 3, 4, 2]
        memory_sequence_length = [3, 7, 5, 4]
        memory_time = max(memory_sequence_length)
        memory = tf.placeholder_with_default(np.random.randn(
            batch_size, memory_time, depth).astype(np.float32),
                                             shape=(None, None, depth))
        outputs, _, _ = decoder.decode(
            inputs,
            sequence_length,
            vocab_size=vocab_size,
            memory=memory,
            memory_sequence_length=memory_sequence_length)
        output_time_dim = tf.shape(outputs)[1]

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
        with self.test_session() as sess:
            output_time_dim_val = sess.run(output_time_dim)
            self.assertEqual(time_dim, output_time_dim_val)
Ejemplo n.º 2
0
    def _testDecoderTraining(self,
                             decoder,
                             initial_state_fn=None,
                             num_sources=1,
                             dtype=tf.float32):
        batch_size = 4
        vocab_size = 10
        time_dim = 5
        depth = 6
        inputs = tf.placeholder_with_default(np.random.randn(
            batch_size, time_dim, depth).astype(dtype.as_numpy_dtype()),
                                             shape=(None, None, depth))
        # NOTE: max(sequence_length) may be less than time_dim when num_gpus > 1
        sequence_length = [1, 3, 4, 2]
        initial_state, memory, memory_sequence_length = _generate_source_context(
            batch_size,
            depth,
            initial_state_fn=initial_state_fn,
            num_sources=num_sources,
            dtype=dtype)
        outputs, _, _, attention = decoder.decode(
            inputs,
            sequence_length,
            vocab_size=vocab_size,
            initial_state=initial_state,
            memory=memory,
            memory_sequence_length=memory_sequence_length,
            return_alignment_history=True)
        self.assertEqual(outputs.dtype, dtype)
        output_time_dim = tf.shape(outputs)[1]
        if decoder.support_alignment_history and num_sources == 1:
            self.assertIsNotNone(attention)
        else:
            self.assertIsNone(attention)

        saver = tf.train.Saver(var_list=tf.global_variables())
        with self.test_session(graph=tf.get_default_graph()) as sess:
            sess.run(tf.global_variables_initializer())
            output_time_dim_val = sess.run(output_time_dim)
            self.assertEqual(time_dim, output_time_dim_val)
            if decoder.support_alignment_history and num_sources == 1:
                attention_val, memory_time = sess.run(
                    [attention, tf.shape(memory)[1]])
                self.assertAllEqual([batch_size, time_dim, memory_time],
                                    attention_val.shape)
            return saver.save(sess,
                              os.path.join(self.get_temp_dir(), "model.ckpt"))
Ejemplo n.º 3
0
    def _testDecoderTraining(self,
                             decoder,
                             support_alignment_history=False,
                             dtype=tf.float32):
        batch_size = 4
        vocab_size = 10
        time_dim = 5
        depth = 6
        inputs = tf.placeholder_with_default(np.random.randn(
            batch_size, time_dim, depth).astype(dtype.as_numpy_dtype()),
                                             shape=(None, None, depth))
        # NOTE: max(sequence_length) may be less than time_dim when num_gpus > 1
        sequence_length = [1, 3, 4, 2]
        memory_sequence_length = [3, 7, 5, 4]
        memory_time = max(memory_sequence_length)
        memory = tf.placeholder_with_default(np.random.randn(
            batch_size, memory_time, depth).astype(dtype.as_numpy_dtype()),
                                             shape=(None, None, depth))
        outputs, _, _, attention = decoder.decode(
            inputs,
            sequence_length,
            vocab_size=vocab_size,
            memory=memory,
            memory_sequence_length=memory_sequence_length,
            return_alignment_history=True)
        self.assertEqual(outputs.dtype, dtype)
        output_time_dim = tf.shape(outputs)[1]
        if support_alignment_history:
            self.assertIsNotNone(attention)
        else:
            self.assertIsNone(attention)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
        with self.test_session() as sess:
            output_time_dim_val = sess.run(output_time_dim)
            self.assertEqual(time_dim, output_time_dim_val)
            if support_alignment_history:
                attention_val = sess.run(attention)
                self.assertAllEqual([batch_size, time_dim, memory_time],
                                    attention_val.shape)