Python3 - 列表索引超出范围

时间:2018-01-25 19:12:16

标签: python python-3.x list deep-learning recurrent-neural-network

我正在研究一个字符级的回归神经网络。为了训练网,我从互联网上复制了一个文本语料库。以下是包含错误的代码块:

X = np.zeros((int(len(data)/SEQ_LENGTH), SEQ_LENGTH, VOCAB_SIZE))
y = np.zeros((int(len(data)/SEQ_LENGTH), SEQ_LENGTH, VOCAB_SIZE))
for i in range(0, int(len(data)/SEQ_LENGTH)):
    X_sequence = data[i*SEQ_LENGTH:(i+1)*SEQ_LENGTH]
    X_sequence_ix = [char_to_ix[value] for value in X_sequence]
    input_sequence = np.zeros((SEQ_LENGTH, VOCAB_SIZE))
    for j in range(SEQ_LENGTH):
        input_sequence[j][X_sequence_ix[j]] = 1.
    X[i] = input_sequence

    y_sequence = data[i*SEQ_LENGTH+1:(i+1)*SEQ_LENGTH+1]
    y_sequence_ix = [char_to_ix[value] for value in y_sequence]
    target_sequence = np.zeros((SEQ_LENGTH, VOCAB_SIZE))
    for j in range(SEQ_LENGTH):

        target_sequence[j][y_sequence_ix[j]] = 1       
    y[i] = target_sequence

基本上我所做的就是将字符转换为ASCII等效字符。 y_sequence是字符序列,y_sequence_ix是其对应的ASCII序列。 VOCAB_SIZE变量包含文本语料库中唯一字符的数量。此行中出现错误:

target_sequence[j][y_sequence_ix[j]] = 1  

完整的源代码以及文本语料库:https://github.com/tanmay-edgelord/charRNN

请询问您回答问题所需的任何信息。

修改

调用函数traceback.print_stack()

时的TRACEBACK
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/usr/local/lib/python3.5/dist-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelapp.py", line 478, in start
    self.io_loop.start()
  File "/usr/local/lib/python3.5/dist-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/usr/local/lib/python3.5/dist-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/usr/local/lib/python3.5/dist-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
    handler(stream, idents, msg)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2728, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2856, in run_ast_nodes
    if self.run_code(code, result):
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-15-b47f6c9a5577>", line 2, in <module>
    traceback.print_stack()

1 个答案:

答案 0 :(得分:0)

问题出在y_sequence_ix[j],因为您的j范围函数使用的是SEQ_LENGTH + 1。这是您想要的,因为您预测列表中的next值,但它也会产生一个问题,y_sequence_ixi data的最后一次迭代时,1个样本太短{1}}与SEQ_LENGTH均分。

import string
import random
import numpy as np

ix_to_char = list(string.ascii_lowercase)
data = [random.choice(string.ascii_lowercase) for x in range(1000)]
char_to_ix = {v:i for i,v in enumerate(ix_to_char)}
VOCAB_SIZE  = len(char_to_ix)
SEQ_LENGTH = 5

usable_len = len(data)-1    #Note: don't try to use the last entry in data
X = np.zeros((int(usable_len/SEQ_LENGTH), SEQ_LENGTH, VOCAB_SIZE))
y = np.zeros((int(usable_len/SEQ_LENGTH), SEQ_LENGTH, VOCAB_SIZE))
for i in range(0, int(usable_len/SEQ_LENGTH)):       
    X_sequence = data[i*SEQ_LENGTH:(i+1)*SEQ_LENGTH]
    X_sequence_ix = [char_to_ix[value] for value in X_sequence]

    input_sequence = np.zeros((SEQ_LENGTH, VOCAB_SIZE))
    for j in range(SEQ_LENGTH):
        input_sequence[j][X_sequence_ix[j]] = 1.
    X[i] = input_sequence

    y_sequence = data[i*SEQ_LENGTH+1:(i+1)*SEQ_LENGTH+1]
    y_sequence_ix = [char_to_ix[value] for value in y_sequence]
    target_sequence = np.zeros((SEQ_LENGTH, VOCAB_SIZE))
    for j in range(SEQ_LENGTH):
        target_sequence[j][y_sequence_ix[j]] = 1
    y[i] = target_sequence

除了顶部的数据创建之外,此处修改的唯一内容是在数据创建中使用len(data)-1而不是len(data)。通过截断单个值,您可以确保不会出现index out of range类型错误。这是最简单的事情,但是你也可以将SEQ_LENGTH设置为一个不是len(数据)的偶数倍的值,即.. 6适用于上面的例子。