【问题标题】:IndexError: list index out of range in TensorFlowIndexError:在 TensorFlow 中列出超出范围的索引
【发布时间】:2019-04-24 02:33:27
【问题描述】:

我收到一个错误,IndexError: list index out of range.Traceback 说

Run id: P0W5X0
Log directory: /tmp/tflearn_logs/
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/Users/xxx/anaconda/xxx/lib/python2.7/site-packages/tflearn/data_flow.py", line 201, in fill_batch_ids_queue
    ids = self.next_batch_ids()
  File "/Users/xxx/anaconda/xxx/lib/python2.7/site-packages/tflearn/data_flow.py", line 215, in next_batch_ids
    batch_start, batch_end = self.batches[self.batch_index]
IndexError: list index out of range

我写了代码,

# coding: utf-8
import tensorflow as tf
import tflearn

from tflearn.layers.core import input_data,dropout,fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression

tf.reset_default_graph()
net = input_data(shape=[None,20000, 4, 42])
net = conv_2d(net, 4, 16, activation='relu')
net = max_pool_2d(net, 1)
net = tflearn.activations.relu(net)
net = dropout(net, 0.5)
net = tflearn.fully_connected(net, 2, activation='softmax')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.5, loss='categorical_crossentropy')

model = tflearn.DNN(net)

model.fit(np.array(trainDataSet).reshape(1,20000, 4, 42), np.array(trainLabel), n_epoch=400, batch_size=32, validation_set=0.1, show_metric=True)


pred = np.array(model.predict(np.array(testDataSet).reshape(1,20000, 4, 42)).argmax(axis=1))

label = np.array(testLabel).argmax(axis=0)
accuracy = np.mean(pred == label, axis=0)

print(accuracy)

我真的不明白为什么会发生这样的错误。我试图重写成

model.fit(np.array(trainDataSet).reshape(1,20000, 4, 42), np.array(trainLabel), n_epoch=400, batch_size=1, validation_set=0.1, show_metric=True) 

因为 bach 导致了这个错误,但是发生了同样的错误。我在这部分重写了另一个数字,但发生了同样的错误。我的代码有什么问题?我应该如何解决这个问题?

【问题讨论】:

  • 告诉我有关训练数据集的信息。它是什么,为什么它会形成这样的形状?从 input_data(shape=[None,20000, 4, 42]) 看来,您期望有一些形状为 20000x4x42 的批次,但您在 model.fit 中提供了 1 个 20000x4x42 的样本。
  • 我有点听不懂你在说什么。np.array(trainDataSet).shape 是 (20000, 4, 42)。

标签: python tensorflow tflearn


【解决方案1】:

我也遇到了同样的问题。我的解决方案是使 n_epoch 的数量等于数据集的行数。例如,我的数组的形状是 461*5,n_epoch 的值是 461。你也可以让这个值比你的行数大一点或小一点。在我的代码中,500 或 400 也很有用。

【讨论】:

    【解决方案2】:

    问题

    如何修复我的列表索引超出范围错误?

    回答

    从您的代码中可以看出,您传递给神经网络的训练和测试集只有 1 个由形状为 20000x4x42 的 reshape(1,20000, 4, 42) 给出的元素。我相信你的意思是拥有 20000 个 4x42 的元素。

    我们用reshape(20000, 4, 42, 1)代替reshape(1,20000, 4, 42)。我们还必须将input_data(shape=[None, 20000, 4, 42]) 更改为input_data(shape=[None, 4, 42, 1])

    如果你这样做,你的代码就可以正常工作。

    工作代码

    # coding: utf-8
    import tensorflow as tf
    import tflearn
    
    from tflearn.layers.core import input_data,dropout,fully_connected
    from tflearn.layers.conv import conv_2d, max_pool_2d
    from tflearn.layers.normalization import local_response_normalization
    from tflearn.layers.estimator import regression
    
    tf.reset_default_graph()
    net = input_data(shape=[None, 4, 42, 1])
    net = conv_2d(net, 4, 16, activation='relu')
    net = max_pool_2d(net, 1)
    net = tflearn.activations.relu(net)
    net = dropout(net, 0.5)
    net = tflearn.fully_connected(net, 2, activation='softmax')
    net = tflearn.regression(net, optimizer='adam', learning_rate=0.5, loss='categorical_crossentropy')
    
    model = tflearn.DNN(net)
    
    model.fit(np.array(trainDataSet).reshape(20000, 4, 42, 1), np.array(trainLabel), n_epoch=400, batch_size=32, validation_set=0.1, show_metric=True)
    
    
    pred = np.array(model.predict(np.array(testDataSet).reshape(20000, 4, 42, 1)).argmax(axis=1))
    
    label = np.array(testLabel).argmax(axis=0)
    accuracy = np.mean(pred == label, axis=0)
    
    print(accuracy)
    

    输出

    要使上述代码正常工作,我们必须包含一些训练和测试数据。 Numpy random 是这样使用的

    import numpy as np
    
    trainDataSet = np.random.rand(20000, 4, 42)
    trainLabel = ( np.random.rand(20000,2) > .5 ) *1.0
    
    testDataSet = np.random.rand(20000, 4, 42)
    testLabel = ( np.random.rand(20000,2) > .5 ) *1.0
    

    这是输出

    Run id: JDSG88
    Log directory: /tmp/tflearn_logs/
    ---------------------------------
    Training samples: 18000
    Validation samples: 2000
    --
    Training Step: 563  | total loss: 12.13387 | time: 5.312s
    | Adam | epoch: 001 | loss: 12.13387 - acc: 0.7138 | val_loss: 11.90437 - val_acc: 0.7400 -- iter: 18000/18000
    --
    Training Step: 1126  | total loss: 11.58909 | time: 5.184s
    | Adam | epoch: 002 | loss: 11.58909 - acc: 0.7496 | val_loss: 11.90437 - val_acc: 0.7400 -- iter: 18000/18000
    --
    Training Step: 1689  | total loss: 11.93482 | time: 5.174s
    | Adam | epoch: 003 | loss: 11.93482 - acc: 0.7357 | val_loss: 11.90437 - val_acc: 0.7400 -- iter: 18000/18000
    --
    ...
    

    【讨论】:

      猜你喜欢
      • 2021-02-11
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多