Sentence Generation: TypeError: 'NoneType' object is not subscriptable

Sentence Generation: TypeError: 'NoneType' object is not subscriptable

我有一个代码,用于在 sentence_start

之后给定一个词来生成句子

遇到错误的代码:

def generate_sentences(model, n, index_to_word, word_to_index):
    for i in range(n):
        sent = None
        while not sent:
            for i in range(len(arr)):
                sent = generate_sentence(arr[i], model, index_to_word, word_to_index)
                # print (arr[i])
                print_sentence(sent, index_to_word)
        print("\n")

这里是被调用的函数:

def generate_sentence(anot, model, index_to_word, word_to_index, min_length=5):
    # We start the sentence with the start token
    new_sentence = [word_to_index[SENTENCE_START_TOKEN], word_to_index[anot]]
    # Repeat until we get an end token
    while not new_sentence[-1] == word_to_index[SENTENCE_END_TOKEN]:
            next_word_probs = model.predict(new_sentence)[-1]
            samples = np.random.multinomial(1, next_word_probs)
            sampled_word = np.argmax(samples)
            new_sentence.append(sampled_word)
            # Seomtimes we get stuck if the sentence becomes too long, e.g. "........" :(
            # And: We don't want sentences with UNKNOWN_TOKEN's
            if len(new_sentence) > 100 or sampled_word == word_to_index[UNKNOWN_TOKEN]:
                return None
    if len(new_sentence) < min_length:
        return None
    return new_sentence

def print_sentence(s, index_to_word):
    sentence_str = [index_to_word[x] for x in s[1:-1]]
    print(" ".join(sentence_str))
    sys.stdout.flush()

这是追溯:

Traceback (most recent call last):

  File "<ipython-input-10-b9a0a1f5bd04>", line 1, in <module>
    runfile('C:/Users/cerdas/Documents/bil/Code_Latihan/rnn-tutorial-gru-lstm-master/train.py', wdir='C:/Users/cerdas/Documents/bil/Code_Latihan/rnn-tutorial-gru-lstm-master')

  File "C:\Users\cerdas\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\cerdas\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/cerdas/Documents/bil/Code_Latihan/rnn-tutorial-gru-lstm-master/train.py", line 53, in <module>
    generate_sentences(model, 20, index_to_word, word_to_index)

  File "C:\Users\cerdas\Documents\bil\Code_Latihan\rnn-tutorial-gru-lstm-master\utils.py", line 190, in generate_sentences
    print_sentence(sent, index_to_word)

  File "C:\Users\cerdas\Documents\bil\Code_Latihan\rnn-tutorial-gru-lstm-master\utils.py", line 179, in print_sentence
    sentence_str = [index_to_word[x] for x in s[1:-1]]

TypeError: 'NoneType' object is not subscriptable

我怀疑是函数引起的错误print_sentence(sent, index_to_word) 我试图编辑缩进以将 print_sentence 函数从循环中排除。

但输出只读取数组的最后一个元素arr

什么错误信息

TypeError: 'NoneType' object is not subscriptable

意思是你的行

sentence_str = [index_to_word[x] for x in s[1:-1]]

正在尝试访问 index_to_word[x]s[1:-1],尽管其中一个似乎是 None

鉴于您没有提供调用 generate_sentences 的信息,我们只能推测是什么原因造成的。

我建议您添加一个 if 子句,以确保您不会尝试打印一个句子,即 None:

print_sentence(sent, index_to_word)

应替换为

if sent is not None:
    print_sentence(sent, index_to_word)