python - 我在 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()
解决方案
推荐阅读
- python - 在 Django 模型表单中为一个模型创建多个条目
- php - 出现此错误:extraction.map 不是函数
- python - 从旧列表中删除数据并附加到新列表 python
- javascript - Shopify:需要编辑哪个 LIQUID 文件才能在购物车页面中的行项目下方显示一行文本?
- html - (Bootstrap 5) Col 元素不是内联的
- html - Wordpress 如何更改自定义主题部分的颜色
- javascript - Select2 - 搜索功能关键字
- python - 从 iframe 中提取抓取的 Web 内容
- jira - 如何设置自动回复并将问题状态更改为“进行中”作为 Jira 中的第一响应
- django - Django Rest Framework - 将“用户”从 ModelViewSet 传递到序列化程序