RuntimeError:预期设备cpu和dtype浮动,但设备cpu和dtype布尔

问题描述

我使用来自AllenAI的this python脚本将CRF模块(有条件的随机字段)层集成到我的ML系统中...

并执行以下示例以测试CRF模块。

import torch
from allennlp.modules import ConditionalRandomField

num_tags = 2

model = ConditionalRandomField(num_tags)
    
seq_length = 3  # maximum sequence length in a batch
batch_size = 2  # number of samples in the batch
emissions = torch.randn(seq_length,batch_size,num_tags)
tags = torch.tensor([[0.0,1.0],[1.0,[0.0,1.0]],dtype=torch.long)  # (seq_length,batch_size)
model(emissions,tags)

执行后,出现以下错误。

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-3-415939853a93> in <module>
  7 emissions = torch.randn(seq_length,num_tags)
  8 tags = torch.tensor([[0.0,dtype=torch.long)  # 
(seq_length,batch_size)
----> 9 model(emissions,tags)

~/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/modules/module.py in 
__call__(self,*input,**kwargs)
545                     result = (result,)
546                 input = result
--> 547         if torch._C._get_tracing_state():
548             result = self._slow_forward(*input,**kwargs)
549         else:

<ipython-input-2-2d984bd97cf1> in forward(self,inputs,tags,mask)
329             mask = mask.to(torch.bool)
330 
--> 331         log_denominator = self._input_likelihood(inputs,mask)
332         log_numerator = self._joint_likelihood(inputs,mask)
333 

<ipython-input-2-2d984bd97cf1> in _input_likelihood(self,logits,mask)
249             # In valid positions (mask == True) we want to take the logsumexp over the 
current_tag dimension
250             # of `inner`. Otherwise (mask == False) we want to retain the previous 
alpha.
--> 251             alpha = util.logsumexp(inner,1) * mask[i].view(batch_size,1) + alpha * 
(
252                 ~mask[i]
253             ).view(batch_size,1)

RuntimeError: expected device cpu and dtype Float but got device cpu and dtype Bool

我确实知道这是PyTorch版本与allennlp软件包中使用的功能不兼容的问题,但是我不确定它期望的是什么版本的Torch和Torchvision。 这些是我机器上安装的PyTorch的版本。

torch                            1.5.1               
torchvision                      0.4.2
allennlp                         1.0.0               

任何帮助或提示将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...