IndexError:无法从空序列中选择

问题描述 投票:0回答:1

我正在尝试运行此命令python run.py --mode MLE并得到此错误。我无法找到正确的解决方案。

Traceback (most recent call last):
  File "run.py", line 208, in <module>
    train_MLE()
  File "run.py", line 94, in train_MLE
    encoder_input, decoder_input, weight = model.get_batch(d_valid, i)
  File "C:\Users\Kriti Gupta\Desktop\GitHub_repo\Seq2seq-Chatbot-With-Deep-Reinforcement-Learning\seq2seq_model.py", line 342, in get_batch
    encoder_input, decoder_input = random.choice(data[bucket_id])
  File "C:\Users\Kriti Gupta\AppData\Local\Programs\Python\Python37\lib\random.py", line 261, in choice
    raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence

下面是包含该功能的代码

def get_batch(self, data, bucket_id, rand = True, order = False):
    # data should be [whole_data_length x (source, target)] 
    # decoder_input should contain "GO" symbol and target should contain "EOS" symbol
    encoder_size, decoder_size = self.buckets[bucket_id]
    encoder_inputs, decoder_inputs = [], []
    #print(bucket_id)
    print(random.choice(data[bucket_id]))

    encoder_input, decoder_input = random.choice(data[bucket_id])
    c = 0

    for i in xrange(self.batch_size):
      if rand:
        encoder_input, decoder_input = random.choice(data[bucket_id])
      if order:
        encoder_input, decoder_input = data[bucket_id][i]
        c += 1 

请帮助!

python tensorflow deep-learning artificial-intelligence reinforcement-learning
1个回答
0
投票
random.choice总是在空序列上引发IndexError。我建议检查您传递给函数的数据

get_batch()

您还可以在'get_batch()'方法中添加'if条件',以检查传递的数据是否为空。

参考:Python Bug Tracker

© www.soinside.com 2019 - 2024. All rights reserved.