コード例 #1
0
ファイル: nlp.py プロジェクト: Kaemer1645/DATA_SCIENCE
 def generate(seed: str = START, max_len: int = 50) -> str:
     rnn1.reset_hidden_state()  # Zresetuj obydwa ukryte stany
     rnn2.reset_hidden_state()
     output = [seed]            # rozpocznij od podstawienia pod output określonej wartości seed
 
     # Kontynuuj, aż trafisz na znak STOP lub do osiągnięcia maksymalnej długości 
     while output[-1] != STOP and len(output) < max_len:
         # Użyj ostatniego znaku na wejściu
         input = vocab.one_hot_encode(output[-1])
 
         # Wygeneruj wyniki, używając modelu
         predicted = model.forward(input)
 
         # Przekonwertuj je na prawdopodobieństwa i pobierz losowy char_id
         probabilities = softmax(predicted)
         next_char_id = sample_from(probabilities)
 
         # Dodaj odpowiedni znak do wartości wyjściowej
         output.append(vocab.get_word(next_char_id))
 
     # usuń znaki START i END i zwróć wygenerowane słowo
     return ''.join(output[1:-1])
コード例 #2
0
    def generate(seed: str = START, max_len: int = 50) -> str:
        rnn1.reset_hidden_state()  # Reset both hidden states.
        rnn2.reset_hidden_state()
        output = [seed]  # Start the output with the specified seed.

        # Keep going until we produce the STOP character or reach the max length
        while output[-1] != STOP and len(output) < max_len:
            # Use the last character as the input
            input = vocab.one_hot_encode(output[-1])

            # Generate scores using the model
            predicted = model.forward(input)

            # Convert them to probabilities and draw a random char_id
            probabilities = softmax(predicted)
            next_char_id = sample_from(probabilities)

            # Add the corresponding char to our output
            output.append(vocab.get_word(next_char_id))

        # Get rid of START and END characters and return the word.
        return ''.join(output[1:-1])
コード例 #3
0
 def generate(seed: str = START, max_len: int = 50) -> str:
     rnn1.reset_hidden_state()  # Reset both hidden states.
     rnn2.reset_hidden_state()
     output = [seed]            # Start the output with the specified seed.
 
     # Keep going until we produce the STOP character or reach the max length
     while output[-1] != STOP and len(output) < max_len:
         # Use the last character as the input
         input = vocab.one_hot_encode(output[-1])
 
         # Generate scores using the model
         predicted = model.forward(input)
 
         # Convert them to probabilities and draw a random char_id
         probabilities = softmax(predicted)
         next_char_id = sample_from(probabilities)
 
         # Add the corresponding char to our output
         output.append(vocab.get_word(next_char_id))
 
     # Get rid of START and END characters and return the word.
     return ''.join(output[1:-1])