Esempio n. 1
0
    def test_set(self):
        state = RecurrentState([5, 10], 3)
        layer1_state = numpy.arange(15, dtype='int64').reshape((1, 3, 5))
        layer2_state = numpy.arange(30, dtype='int64').reshape((1, 3, 10))
        state.set([layer1_state, layer2_state])
        assert_equal(state.get(0), layer1_state)
        assert_equal(state.get(1), layer2_state)

        with self.assertRaises(ValueError):
            state.set([layer2_state, layer1_state])
Esempio n. 2
0
    def test_set(self):
        state = RecurrentState([5, 10], 3)
        layer1_state = numpy.arange(15, dtype='int64').reshape((1, 3, 5))
        layer2_state = numpy.arange(30, dtype='int64').reshape((1, 3, 10))
        state.set([layer1_state, layer2_state])
        assert_equal(state.get(0), layer1_state)
        assert_equal(state.get(1), layer2_state)

        with self.assertRaises(ValueError):
            state.set([layer2_state, layer1_state])
Esempio n. 3
0
    def generate(self, max_length=30, num_sequences=1):
        """Generates a text sequence.

        Calls self.step_function() repeatedly, reading the word output and
        the state output of the hidden layer and passing the hidden layer state
        output to the next time step.

        Generates at most ``max_length`` words, stopping if a sentence break is
        generated.

        :type max_length: int
        :param max_length: maximum number of words in a sequence

        :type num_sequences: int
        :param num_sequences: number of sequences to generate in parallel

        :rtype: list of list of strs
        :returns: list of word sequences
        """

        sos_id = self.vocabulary.word_to_id['<s>']
        sos_class_id = self.vocabulary.word_id_to_class_id[sos_id]
        eos_id = self.vocabulary.word_to_id['</s>']

        word_input = sos_id * \
                     numpy.ones(shape=(1, num_sequences)).astype('int64')
        class_input = sos_class_id * \
                      numpy.ones(shape=(1, num_sequences)).astype('int64')
        result = sos_id * \
                 numpy.ones(shape=(max_length, num_sequences)).astype('int64')
        state = RecurrentState(self.network, num_sequences)

        for time_step in range(1, max_length):
            # The input is the output from the previous step.
            step_result = self.step_function(word_input,
                                             class_input,
                                             *state.get())
            class_ids = step_result[0]
            # The class IDs from the single time step.
            step_class_ids = class_ids[0]
            step_word_ids = numpy.array(
                self.vocabulary.class_ids_to_word_ids(step_class_ids))
            result[time_step] = step_word_ids
            word_input = step_word_ids[numpy.newaxis]
            class_input = class_ids
            state.set(step_result[1:])

        return self.vocabulary.id_to_word[result.transpose()].tolist()
Esempio n. 4
0
    def generate(self, length, num_sequences=1):
        """Generates a text sequence.

        Calls self.step_function() repeatedly, reading the word output and
        the state output of the hidden layer and passing the hidden layer state
        output to the next time step.

        :type length: int
        :param length: number of words (tokens) in each sequence

        :type num_sequences: int
        :param num_sequences: number of sequences to generate in parallel

        :rtype: list of list of strs
        :returns: list of word sequences
        """

        sos_id = self._vocabulary.word_to_id['<s>']
        sos_class_id = self._vocabulary.word_id_to_class_id[sos_id]
        eos_id = self._vocabulary.word_to_id['</s>']

        input_word_ids = sos_id * \
                         numpy.ones(shape=(1, num_sequences)).astype('int64')
        input_class_ids = sos_class_id * \
                          numpy.ones(shape=(1, num_sequences)).astype('int64')
        result = sos_id * \
                 numpy.ones(shape=(length, num_sequences)).astype('int64')
        state = RecurrentState(self._network.recurrent_state_size,
                               num_sequences)

        for time_step in range(1, length):
            # The input is the output from the previous step.
            step_result = self.step_function(input_word_ids,
                                             input_class_ids,
                                             *state.get())
            class_ids = step_result[0]
            # The class IDs from the single time step.
            step_class_ids = class_ids[0]
            step_word_ids = numpy.array(
                self._vocabulary.class_ids_to_word_ids(step_class_ids))
            result[time_step] = step_word_ids
            input_word_ids = step_word_ids[numpy.newaxis]
            input_class_ids = class_ids
            state.set(step_result[1:])

        return self._vocabulary.id_to_word[result.transpose()].tolist()
Esempio n. 5
0
    def generate(self, length, num_sequences=1):
        """Generates a text sequence.

        Calls self.step_function() repeatedly, reading the word output and
        the state output of the hidden layer and passing the hidden layer state
        output to the next time step.

        :type length: int
        :param length: number of words (tokens) in each sequence

        :type num_sequences: int
        :param num_sequences: number of sequences to generate in parallel

        :rtype: list of list of strs
        :returns: list of word sequences
        """

        sos_id = self._vocabulary.word_to_id['<s>']
        sos_class_id = self._vocabulary.word_id_to_class_id[sos_id]
        eos_id = self._vocabulary.word_to_id['</s>']

        input_word_ids = sos_id * \
                         numpy.ones(shape=(1, num_sequences)).astype('int64')
        input_class_ids = sos_class_id * \
                          numpy.ones(shape=(1, num_sequences)).astype('int64')
        result = sos_id * \
                 numpy.ones(shape=(length, num_sequences)).astype('int64')
        state = RecurrentState(self._network.recurrent_state_size,
                               num_sequences)

        for time_step in range(1, length):
            # The input is the output from the previous step.
            step_result = self.step_function(input_word_ids, input_class_ids,
                                             *state.get())
            class_ids = step_result[0]
            # The class IDs from the single time step.
            step_class_ids = class_ids[0]
            step_word_ids = numpy.array(
                self._vocabulary.class_ids_to_word_ids(step_class_ids))
            result[time_step] = step_word_ids
            input_word_ids = step_word_ids[numpy.newaxis]
            input_class_ids = class_ids
            state.set(step_result[1:])

        return self._vocabulary.id_to_word[result.transpose()].tolist()
Esempio n. 6
0
    def test_init(self):
        state = RecurrentState([200, 100, 300], 3)
        self.assertEqual(len(state.get()), 3)
        self.assertEqual(state.get(0).shape, (1, 3, 200))
        self.assertEqual(state.get(1).shape, (1, 3, 100))
        self.assertEqual(state.get(2).shape, (1, 3, 300))
        assert_equal(state.get(0), numpy.zeros(shape=(1, 3, 200),
                                               dtype='int64'))
        assert_equal(state.get(1), numpy.zeros(shape=(1, 3, 100),
                                               dtype='int64'))
        assert_equal(state.get(2), numpy.zeros(shape=(1, 3, 300),
                                               dtype='int64'))

        layer1_state = numpy.arange(15, dtype='int64').reshape((1, 3, 5))
        layer2_state = numpy.arange(30, dtype='int64').reshape((1, 3, 10))
        state = RecurrentState([5, 10], 3, [layer1_state, layer2_state])
        assert_equal(state.get(0), layer1_state)
        assert_equal(state.get(1), layer2_state)
Esempio n. 7
0
    def test_init(self):
        state = RecurrentState([200, 100, 300], 3)
        self.assertEqual(len(state.get()), 3)
        self.assertEqual(state.get(0).shape, (1,3,200))
        self.assertEqual(state.get(1).shape, (1,3,100))
        self.assertEqual(state.get(2).shape, (1,3,300))
        assert_equal(state.get(0), numpy.zeros(shape=(1,3,200), dtype='int64'))
        assert_equal(state.get(1), numpy.zeros(shape=(1,3,100), dtype='int64'))
        assert_equal(state.get(2), numpy.zeros(shape=(1,3,300), dtype='int64'))

        layer1_state = numpy.arange(15, dtype='int64').reshape((1, 3, 5))
        layer2_state = numpy.arange(30, dtype='int64').reshape((1, 3, 10))
        state = RecurrentState([5, 10], 3, [layer1_state, layer2_state])
        assert_equal(state.get(0), layer1_state)
        assert_equal(state.get(1), layer2_state)
Esempio n. 8
0
    def generate(self, length, num_sequences=1, seed_sequence=''):
        """Generates a text sequence.

        Calls self.step_function() repeatedly, reading the word output and
        the state output of the hidden layer and passing the hidden layer state
        output to the next time step.

        :type length: int
        :param length: number of words (tokens) in each sequence

        :type num_sequences: int
        :param num_sequences: number of sequences to generate in parallel

        :rtype: list of list of strs
        :returns: list of word sequences
        """
        seed_tokens = seed_sequence.strip().split()
        sos_id = self._vocabulary.word_to_id['<s>']
        sos_class_id = self._vocabulary.word_id_to_class_id[sos_id]

        input_word_ids = sos_id * \
                         numpy.ones(shape=(1, num_sequences)).astype('int64')
        input_class_ids = sos_class_id * \
                          numpy.ones(shape=(1, num_sequences)).astype('int64')
        result = sos_id * \
                 numpy.ones(shape=(len(seed_tokens)+length,
                   num_sequences)).astype('int64')
        state = RecurrentState(self._network.recurrent_state_size,
                               num_sequences)

        #First, possibly compute forward passes with the seed sequence
        for time_step, token in enumerate(seed_tokens, start=1):
            step_result = self.step_function(input_word_ids, input_class_ids,
                                             *state.get())
            token_id = self._vocabulary.word_to_id[token]
            token_class_id = \
                self._vocabulary.word_id_to_class_id[token_id]
            input_word_ids = token_id * \
                             numpy.ones(shape=(1,
                               num_sequences)).astype('int64')
            input_class_ids = token_class_id * \
                              numpy.ones(shape=(1,
                                num_sequences)).astype('int64')
            step_word_ids = input_word_ids
            result[time_step] = step_word_ids
            state.set(step_result[1:])

        #Then sample:
        for time_step in range(
                len(seed_tokens) + 1, length + len(seed_tokens)):
            # the input is the output from the previous step.
            step_result = self.step_function(input_word_ids, input_class_ids,
                                             *state.get())
            class_ids = step_result[0]
            # The class IDs from the single time step.
            step_class_ids = class_ids[0]
            step_word_ids = numpy.array(
                self._vocabulary.class_ids_to_word_ids(step_class_ids))
            result[time_step] = step_word_ids
            input_word_ids = step_word_ids[numpy.newaxis]
            input_class_ids = class_ids
            state.set(step_result[1:])

        return self._vocabulary.id_to_word[result.transpose()].tolist()