optim.lr_scheduler.ReduceLROnPlateau给出的错误值无法转换为float而没有溢出:inf

时间:2018-03-04 16:35:16

标签: python machine-learning deep-learning pytorch

我在使用此安装命令时使用pytorch:pip3 install http://download.pytorch.org/whl/cu80/torch-0.3.1-cp35-cp35m-linux_x86_64.whl

我有一个没有问题的模型,但是当我添加学习速率调度程序时,我收到错误

我的日程安排员:

# In init
self.optimizer = optim.Adam(self.model.parameters(), lr=0.01)
self.scheduler = optim.lr_scheduler.ReduceLROnPlateau(
    self.optimizer, 'min', factor=0.1, patience=5, verbose=True
)

# after each epoch
self.scheduler.step(loss)

错误:

...
my_project/.env/lib/python3.5/site-packages/torch/optim/lr_scheduler.py in <lambda>(a, best)
    330         if mode == 'min' and threshold_mode == 'rel':
    331             rel_epsilon = 1. - threshold
--> 332             self.is_better = lambda a, best: a < best * rel_epsilon
    333             self.mode_worse = float('Inf')
    334         elif mode == 'min' and threshold_mode == 'abs':

RuntimeError: value cannot be converted to type float without overflow: inf

Doc:http://pytorch.org/docs/master/optim.html#torch.optim.lr_scheduler.ReduceLROnPlateau 相关主题:https://discuss.pytorch.org/t/value-cannot-be-converted-to-type-double-without-overflow-inf/11752/7

1 个答案:

答案 0 :(得分:1)

我正在使用gpu张量,例如:

Variable(torch.from_numpy(X).type(torch.FloatTensor).cuda(), requires_grad=False)

如果我像这样将它转换为cpu,那么错误就会消失

# after each epoch
self.scheduler.step(loss.cpu().data.numpy())

不过,我想要一个更清洁的解决方案。