首页 > 解决方案 > 我在 python 中的循环语句时出错

问题描述

我的 While Loop 语句出现错误,显示错误在课堂上

batchsize = 10
train_iter = iterators.SerialIterator(train, batchsize)
max_epoch = 90
studyThreshold = 1
needStudy = True

try:            
    while(train_iter.epoch < max_epoch) and needStudy:
        train_batch = train_iter.next()
        x, t = concat_examples(train_batch)
        print(t)
        y = model(x)
        loss = F.mean_squared_error(y, t)
        model.cleargrads()
        loss.backward()
        optimizer.update()
        if train_iter.is_new_epoch:
            print("epoch", train_iter.epoch, "loss=", loss.data, end=" ")
            loss_X.append(train_iter.epoch)
            loss_Y.append(loss.data)
except Exception as e: # replaced with Exception to handle other errors
    print('except :',type(e)) 

并且输出是异常:

except : <class 'KeyError'>

注意:我正在使用 Pandas 包

我得到的整个错误

except : <class 'KeyError'>
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 33617

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-57-7a80e54a4aea> in <module>
     35     while((train_iter.epoch < max_epoch) and needStudy):
---> 36         train_batch = train_iter.next()
     37         x, t = concat_examples(train_batch)

~\Anaconda3\lib\site-packages\chainer\iterators\serial_iterator.py in __next__(self)
     76 
---> 77         batch = [self.dataset[index] for index in indices]
     78         return batch

~\Anaconda3\lib\site-packages\chainer\iterators\serial_iterator.py in <listcomp>(.0)
     76 
---> 77         batch = [self.dataset[index] for index in indices]
     78         return batch

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2658             except KeyError:
-> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

标签: pythonpandasdataframewhile-loop

解决方案


推荐阅读