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])
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()
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()
def test_combine_sequences(self): state1 = RecurrentState([5, 10], 1) layer1_state = numpy.arange(5, dtype='int64').reshape(1, 1, 5) layer2_state = numpy.arange(10, 20, dtype='int64').reshape(1, 1, 10) state1.set([layer1_state, layer2_state]) state2 = RecurrentState([5, 10], 1) layer1_state = numpy.arange(100, 105, dtype='int64').reshape(1, 1, 5) layer2_state = numpy.arange(110, 120, dtype='int64').reshape(1, 1, 10) state2.set([layer1_state, layer2_state]) state3 = RecurrentState([5, 10], 2) layer1_state = numpy.arange(200, 210, dtype='int64').reshape(1, 2, 5) layer2_state = numpy.arange(210, 230, dtype='int64').reshape(1, 2, 10) state3.set([layer1_state, layer2_state]) combined_state = RecurrentState.combine_sequences( [state1, state2, state3]) self.assertEqual(combined_state.num_sequences, 4) self.assertEqual(len(combined_state.get()), 2) self.assertEqual(combined_state.get(0).shape, (1, 4, 5)) self.assertEqual(combined_state.get(1).shape, (1, 4, 10)) assert_equal( combined_state.get(0), numpy.asarray([[ list(range(5)), list(range(100, 105)), list(range(200, 205)), list(range(205, 210)) ]], dtype='int64')) assert_equal( combined_state.get(1), numpy.asarray([[ list(range(10, 20)), list(range(110, 120)), list(range(210, 220)), list(range(220, 230)) ]], dtype='int64')) state4 = RecurrentState([5, 11], 2) with self.assertRaises(ValueError): combined_state = RecurrentState.combine_sequences( [state1, state2, state3, state4])
def test_combine_sequences(self): state1 = RecurrentState([5, 10], 1) layer1_state = numpy.arange(5, dtype='int64').reshape(1, 1, 5) layer2_state = numpy.arange(10, 20, dtype='int64').reshape(1, 1, 10) state1.set([layer1_state, layer2_state]) state2 = RecurrentState([5, 10], 1) layer1_state = numpy.arange(100, 105, dtype='int64').reshape(1, 1, 5) layer2_state = numpy.arange(110, 120, dtype='int64').reshape(1, 1, 10) state2.set([layer1_state, layer2_state]) state3 = RecurrentState([5, 10], 2) layer1_state = numpy.arange(200, 210, dtype='int64').reshape(1, 2, 5) layer2_state = numpy.arange(210, 230, dtype='int64').reshape(1, 2, 10) state3.set([layer1_state, layer2_state]) combined_state = RecurrentState.combine_sequences([state1, state2, state3]) self.assertEqual(combined_state.num_sequences, 4) self.assertEqual(len(combined_state.get()), 2) self.assertEqual(combined_state.get(0).shape, (1,4,5)) self.assertEqual(combined_state.get(1).shape, (1,4,10)) assert_equal(combined_state.get(0), numpy.asarray( [[list(range(5)), list(range(100, 105)), list(range(200, 205)), list(range(205, 210))]], dtype='int64')) assert_equal(combined_state.get(1), numpy.asarray( [[list(range(10, 20)), list(range(110, 120)), list(range(210, 220)), list(range(220, 230))]], dtype='int64')) state4 = RecurrentState([5, 11], 2) with self.assertRaises(ValueError): combined_state = RecurrentState.combine_sequences([state1, state2, state3, state4])
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()